Skip to content

Commit

Permalink
Issue mozilla#1307: Add experiments lastFetched state, new selector f…
Browse files Browse the repository at this point in the history
…unctions
  • Loading branch information
lmorchard committed Sep 6, 2016
1 parent 239c313 commit b053050
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
11 changes: 7 additions & 4 deletions frontend/src/app-new/actions/experiments.js
Expand Up @@ -10,9 +10,12 @@ export default createActions({
return fetch(countsUrl);
})
.then(response => response.json())
.then(counts => experiments.map(experiment => ({
installation_count: counts[experiment.addon_id],
...experiment
})));
.then(counts => ({
lastFetched: Date.now(),
experiments: experiments.map(experiment => ({
...experiment,
installation_count: counts[experiment.addon_id]
}))
}));
}
});
2 changes: 1 addition & 1 deletion frontend/src/app-new/index.js
Expand Up @@ -31,7 +31,7 @@ import AppRouter from './lib/router';
es6Promise.polyfill();

const initialState = {
experiments: [],
experiments: { experiments: [], lastFetched: null },
browser: { isFirefox: false },
addon: { hasAddon: false, installed: [] }
};
Expand Down
22 changes: 20 additions & 2 deletions frontend/src/app-new/lib/addon.js
Expand Up @@ -4,7 +4,7 @@ import cookies from 'js-cookie';
import { sendToGA } from './utils';
import addonActions from '../actions/addon';

let installStateWatcherTimer = null;
import { getExperiments, getExperimentsLastFetched } from '../reducers/experiments';

export function installAddon(eventCategory) {
const downloadUrl = '/static/addon/addon.xpi';
Expand All @@ -20,13 +20,15 @@ export function installAddon(eventCategory) {
export function setupAddonConnection(store) {
store.dispatch(addonActions.setInstalled([]));
watchForAddonInstallStateChange(store);
watchForExperimentChanges(store);
window.addEventListener('from-addon-to-web',
evt => messageReceived(store, evt));
sendMessage('sync-installed');
}

// Periodically check window.navigator.testpilotAddon and keep store in sync
export function watchForAddonInstallStateChange(store) {
let installStateWatcherTimer = null;
function watchForAddonInstallStateChange(store) {
store.dispatch(addonActions.setHasAddon(window.navigator.testpilotAddon));

if (installStateWatcherTimer) {
Expand All @@ -43,6 +45,22 @@ export function watchForAddonInstallStateChange(store) {
}, 1000);
}

/*
let currentExperimentsFetched = null;
function watchForExperimentChanges(store) {
store.subscribe(() => {
const { experiments } = store.getState();
const previousExperimentsFetched = currentExperimentsFetched;
currentExperimentsFetched = getExperimentsLastFetched(experiments);
console.log('experiment changes', previousExperimentsFetched);
if (previousExperimentsFetched !== currentExperimentsFetched) {
sendMessage('loaded', getExperiments(experiments));
}
});
}
*/

export function sendMessage(type, data) {
console.log('TO ADDON', type, data);
document.documentElement.dispatchEvent(new CustomEvent('from-web-to-addon', {
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/app-new/reducers/experiments.js
@@ -1,7 +1,14 @@
import { handleActions } from 'redux-actions';

const experimentsFetch = (state, { payload: experiments }) => experiments;
const experimentsFetch = (state, { payload: { lastFetched, experiments } }) =>
({ ...state, lastFetched, experiments });

export default handleActions({
experimentsFetch
}, []);
}, {
experiments: [],
lastFetched: null
});

export const getExperiments = (state) => state.experiments;
export const getExperimentsLastFetched = (state) => state.lastFetched;
4 changes: 3 additions & 1 deletion frontend/src/app-new/views/ExperimentPage.js
Expand Up @@ -15,11 +15,13 @@ import ExperimentDisableDialog from '../components/ExperimentDisableDialog';
import ExperimentTourDialog from '../components/ExperimentTourDialog';
import MainInstallButton from '../components/MainInstallButton';

import { getExperiments } from '../reducers/experiments';

const CHANGE_HEADER_ON = 105;

export default connect(
state => ({
experiments: state.experiments,
experiments: getExperiments(state.experiments),
isFirefox: state.browser.isFirefox,
hasAddon: state.addon.hasAddon,
installed: state.addon.installed
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/app-new/views/ExperimentsListPage.js
Expand Up @@ -7,9 +7,11 @@ import Footer from '../components/Footer';
import ExperimentRowCard from '../components/ExperimentRowCard';
import EmailDialog from '../components/EmailDialog';

import { getExperiments } from '../reducers/experiments';

export default connect(
state => ({
experiments: state.experiments,
experiments: getExperiments(state.experiments),
isFirefox: state.browser.isFirefox,
hasAddon: state.addon.hasAddon,
installed: state.addon.installed
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/app-new/views/LandingPage.js
Expand Up @@ -5,9 +5,11 @@ import Footer from '../components/Footer';
import ExperimentRowCard from '../components/ExperimentRowCard';
import MainInstallButton from '../components/MainInstallButton';

import { getExperiments } from '../reducers/experiments';

export default connect(
state => ({
experiments: state.experiments,
experiments: getExperiments(state.experiments),
isFirefox: state.browser.isFirefox,
hasAddon: state.addon.hasAddon,
installed: state.addon.installed
Expand Down

0 comments on commit b053050

Please sign in to comment.