-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from jtassin/master
Debounce of the digests triggered by store modification
- Loading branch information
Showing
6 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ umd | |
coverage | ||
*.tgz | ||
examples/**/dist | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
export default function digestMiddleware($rootScope) { | ||
import debounce from 'lodash.debounce'; | ||
|
||
export default function digestMiddleware($rootScope, debounceConfig) { | ||
let debouncedFunction = (expr) => { | ||
$rootScope.$evalAsync(expr); | ||
}; | ||
if(debounceConfig && debounceConfig.wait && debounceConfig.wait > 0) { | ||
debouncedFunction = debounce(debouncedFunction, debounceConfig.wait, { maxWait: debounceConfig.maxWait }); | ||
} | ||
return store => next => action => { | ||
const res = next(action); | ||
$rootScope.$evalAsync(res); | ||
return res; | ||
const res = next(action); | ||
debouncedFunction(res); | ||
return res; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import expect from 'expect'; | ||
import sinon from 'sinon'; | ||
import digestMiddleware from '../../src/components/digestMiddleware'; | ||
|
||
|
||
describe('digestMiddleware', () => { | ||
|
||
it('Should debounce the $evalAsync function if debounce is enabled', (done) => { | ||
const $evalAsync = sinon.spy(); | ||
const $rootScope = { | ||
$evalAsync, | ||
}; | ||
const firstAction = 1; | ||
const secondAction = 2; | ||
const debounceConfig = { | ||
wait: 10, | ||
}; | ||
const next = sinon.spy((action) => (action)); | ||
const middleware = digestMiddleware($rootScope, debounceConfig); | ||
middleware()(next)(firstAction); | ||
setTimeout(() => { | ||
middleware()(next)(secondAction); | ||
}, 1); | ||
setTimeout(() => { | ||
expect($evalAsync.calledOnce).toBe(true); | ||
expect(next.calledTwice).toBe(true); | ||
expect(next.firstCall.calledWithExactly(firstAction)).toBe(true); | ||
expect(next.secondCall.calledWithExactly(secondAction)).toBe(true); | ||
expect($evalAsync.firstCall.calledWithExactly(secondAction)).toBe(true); | ||
done(); | ||
}, debounceConfig.wait + 10); | ||
|
||
}); | ||
|
||
it('Should not debounce the $evalAsync function if debounce is disabled', () => { | ||
const disabledDebounceConfigs = [ | ||
null, | ||
undefined, | ||
{}, | ||
{ wait: 0 }, | ||
]; | ||
disabledDebounceConfigs.forEach(() => { | ||
const $evalAsync = sinon.spy(); | ||
const $rootScope = { | ||
$evalAsync, | ||
}; | ||
const firstAction = 1; | ||
const secondAction = 2; | ||
const debounceConfig = {}; | ||
|
||
const next = sinon.spy((action) => (action)); | ||
const middleware = digestMiddleware($rootScope, debounceConfig); | ||
middleware()(next)(firstAction); | ||
middleware()(next)(secondAction); | ||
expect($evalAsync.calledTwice).toBe(true); | ||
expect(next.calledTwice).toBe(true); | ||
expect(next.firstCall.calledWithExactly(firstAction)).toBe(true); | ||
expect(next.secondCall.calledWithExactly(secondAction)).toBe(true); | ||
expect($evalAsync.firstCall.calledWithExactly(firstAction)).toBe(true); | ||
expect($evalAsync.secondCall.calledWithExactly(secondAction)).toBe(true); | ||
}); | ||
}); | ||
|
||
}); |