Skip to content

Commit

Permalink
fix(matching contexts): auto refresh matching contexts from backend e…
Browse files Browse the repository at this point in the history
…very x minutes

fix #178
  • Loading branch information
bmenant committed Oct 18, 2018
1 parent 53a89c7 commit 82b37ce
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/app/background/actions/kraftBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,14 @@ export function refreshMatchingContextsFromBackend(criteria, editors) {
fetchMatchingContexts(criteria, editors).then(json => dispatch(receivedMatchingContexts(json)));
};
}

export function refreshMatchingContextsEvery(milliseconds) {
function recurse(dispatch) {
setTimeout(() => {
dispatch({ type: REFRESH_MATCHING_CONTEXTS });
recurse(dispatch);
}, milliseconds);
}

return recurse;
}
10 changes: 10 additions & 0 deletions src/app/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import prepareDraftPreview from '../lmem/draft-preview/main.js';

import {
dispatchInitialStateFromBackend,
refreshMatchingContextsEvery,
} from './actions/kraftBackend';
import updateDraftRecommendations from './actions/updateDraftRecommendations';

Expand Down Expand Up @@ -117,5 +118,14 @@ configureStore(store => {

store.dispatch(dispatchInitialStateFromBackend()); // store initialization from the kraft server

const refreshInterval = Number(process.env.REFRESH_MC_INTERVAL);
if (refreshInterval > 0) {
console.info(`Matching contexts will be refreshed every ${refreshInterval / 1000 / 60} minutes.`);
store.dispatch(refreshMatchingContextsEvery(refreshInterval));
}
else console.warn(
'Matching contexts auto-refresh disabled:',
'assuming "process.env.REFRESH_MC_INTERVAL" is deliberately not defined.');

}, true);

30 changes: 29 additions & 1 deletion test/app/actions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import chai from 'chai';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';

import { Set as ImmutableSet } from 'immutable';

import {
receivedMatchingContexts,
receivedCriteria,
receivedEditors
receivedEditors,
refreshMatchingContextsEvery,
} from '../../src/app/background/actions/kraftBackend';

import {
Expand All @@ -15,6 +18,8 @@ import {
} from '../../src/app/background/actions/tabs';

const expect = chai.expect;
chai.use(sinonChai);


describe('background actions', function () {

Expand Down Expand Up @@ -73,4 +78,27 @@ describe('background actions', function () {
expect(action.recommendation).to.equal(recommendation);
});

describe('auto refresh matching contexts', () => {
before(() => {
this.clock = sinon.useFakeTimers();
});

it('every x minutes', () => {
const recursiveFn = refreshMatchingContextsEvery(10000);
const dispatch = sinon.fake();

recursiveFn(dispatch);
expect(dispatch).to.not.have.been.called;

this.clock.next();
this.clock.next();
expect(dispatch).to.have.been.calledTwice;
expect(dispatch.lastCall.lastArg.type).to.be.a('string').of.length.above(5);
});

after(() => {
this.clock.restore();
});
});

});
1 change: 1 addition & 0 deletions webpack/chromium.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default baseConfig({
LMEM_BACKEND_ORIGIN: '"https://recommendations.lmem.net"',
LMEM_SCRIPTS_ORIGIN: "'.'",
UNINSTALL_ORIGIN: "'https://www.lmem.net/desinstallation'",
REFRESH_MC_INTERVAL: '10*60*1000',
ONBOARDING_ORIGIN: '"https://bienvenue.lmem.net?extensionInstalled"',
HEAP_APPID: '"3705584166"', // production
}
Expand Down
1 change: 1 addition & 0 deletions webpack/dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default baseConfig({
NODE_ENV: '"development"',
LMEM_BACKEND_ORIGIN: '"https://recommendations.lmem.net"',
LMEM_SCRIPTS_ORIGIN: "'.'", // Use local build
// REFRESH_MC_INTERVAL: '1*60*1000', // Uncomment to enable auto-refresh
}
}
});
1 change: 1 addition & 0 deletions webpack/firefox.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default baseConfig({
LMEM_BACKEND_ORIGIN: '"https://recommendations.lmem.net"',
LMEM_SCRIPTS_ORIGIN: "'.'", // Use local build
ONBOARDING_ORIGIN: '"https://bienvenue.lmem.net?extensionInstalled"',
REFRESH_MC_INTERVAL: '10*60*1000',
//HEAP_APPID: '"3705584166"', // No analytics with Firefox
}
},
Expand Down
1 change: 1 addition & 0 deletions webpack/staging.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default baseConfig({
LMEM_SCRIPTS_ORIGIN: "'.'",
UNINSTALL_ORIGIN: "'https://www.lmem.net/desinstallation'",
HEAP_APPID: '"234457910"', // testing
REFRESH_MC_INTERVAL: '5*60*1000',
}
}
});

0 comments on commit 82b37ce

Please sign in to comment.