From c73e0c7103b10425c19dd98d1164d799a21175cb Mon Sep 17 00:00:00 2001 From: superKalo Date: Sat, 14 Oct 2017 18:01:25 +0300 Subject: [PATCH 1/3] Add: fire a callback each time fresh data is fetched --- src/index.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index b287bf9..87104bd 100644 --- a/src/index.js +++ b/src/index.js @@ -395,13 +395,13 @@ class SuperRepo { * @param {Number} _interval - the interval, in milliseconds * @return {Void} */ - _initSyncInterval(_interval) { + _initSyncInterval(_interval, _cb) { // Do not initiate intervals which are quicker then a second, // otherwise, this might be a big network (performance) overhead. const interval = _interval < 1000 ? 1000: _interval; return setInterval( - () => this._requestFreshData(), interval + () => this._requestFreshData().then(_cb), interval ); } @@ -412,7 +412,7 @@ class SuperRepo { * * @return {Void} */ - initSyncer() { + initSyncer(_cb = () => {}) { const { outOfDateAfter } = this.config; return new Promise(_resolve => { @@ -427,13 +427,14 @@ class SuperRepo { const diff = new Date().valueOf() - _res.lastFetched; let remainingTime = outOfDateAfter - diff; - this.syncInterval = this._initSyncInterval(remainingTime); + this.syncInterval = + this._initSyncInterval(remainingTime, _cb); setTimeout( () => { this.destroySyncer(); this.syncInterval = - this._initSyncInterval(outOfDateAfter); + this._initSyncInterval(outOfDateAfter, _cb); }, remainingTime < 1000 ? 1000 : remainingTime); _resolve(); @@ -441,8 +442,9 @@ class SuperRepo { this._requestFreshData() .then(_response => { this.syncInterval = - this._initSyncInterval(outOfDateAfter); + this._initSyncInterval(outOfDateAfter, _cb); + _cb(); _resolve(); }); } From 5f35249089823946c4ab2389a42f12895b624fec Mon Sep 17 00:00:00 2001 From: superKalo Date: Sat, 14 Oct 2017 18:02:44 +0300 Subject: [PATCH 2/3] Add: init syncer cb test --- test/data-sync.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/data-sync.js b/test/data-sync.js index 9b0e035..1178758 100644 --- a/test/data-sync.js +++ b/test/data-sync.js @@ -264,4 +264,21 @@ describe('Data Sync', () => { expect(networkRequestsCount).to.equal(13); }).then(done, done); }); + + it('Should fire a success callback as soon as the background data sync process gets fresh data.', done => { + const callback = sinon.spy(); + + repository.initSyncer(callback).then( () => { + expect(callback.callCount).to.equal(1); + + clock.tick(TIMEFRAME - 1); + expect(callback.callCount).to.equal(1); + + clock.tick(1); + expect(callback.callCount).to.equal(2); + + clock.tick(TIMEFRAME); + expect(callback.callCount).to.equal(3); + }).then(done, done); + }); }); From e5d8f3b275f5837e58ab3fc7b9c6d549e971fc89 Mon Sep 17 00:00:00 2001 From: superKalo Date: Sat, 14 Oct 2017 19:00:46 +0300 Subject: [PATCH 3/3] Add: test syncer success cb --- test/data-sync.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/data-sync.js b/test/data-sync.js index 1178758..20cdeb3 100644 --- a/test/data-sync.js +++ b/test/data-sync.js @@ -265,9 +265,11 @@ describe('Data Sync', () => { }).then(done, done); }); - it('Should fire a success callback as soon as the background data sync process gets fresh data.', done => { + it('Should fire a success callback as soon as the background data sync process gets fresh data. Use case: there is no data.', done => { const callback = sinon.spy(); + expect(callback.callCount).to.equal(0); + repository.initSyncer(callback).then( () => { expect(callback.callCount).to.equal(1); @@ -281,4 +283,29 @@ describe('Data Sync', () => { expect(callback.callCount).to.equal(3); }).then(done, done); }); + + it('Should fire a success callback as soon as the background data sync process gets fresh data. Use case: there is data.', done => { + const callback = sinon.spy(); + + repository.getData().then(() => { + expect(callback.callCount).to.equal(0); + + repository.initSyncer(callback).then( () => { + expect(callback.callCount).to.equal(0); + + clock.tick(TIMEFRAME - 1); + expect(callback.callCount).to.equal(0); + + clock.tick(1); + expect(callback.callCount).to.equal(1); + + clock.tick(TIMEFRAME); + expect(callback.callCount).to.equal(2); + + clock.tick(10 * TIMEFRAME); + expect(callback.callCount).to.equal(12); + }).then(done, done); + + }); + }); });