-
Notifications
You must be signed in to change notification settings - Fork 177
Debounce of the digests triggered by store modification #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b7ae00f
creating a configurable debounce mode
9d2508b
creating a configurable debounce mode
4a58d12
Merge branch 'master' of github.com:jtassin/ng-redux
4154813
useless .idea
fbcaac1
.idea in gitignore
c281dad
add docuementation for debouncing mode
d00388e
invert middlewares and resolvedEnhancers to handle correclty thunk
93e1cbb
add lodash.debounce for debounce config
4372bb5
Doc for debounce
91c8ff6
use of logasth.debounce instead of home made timeout
b4d699c
add unit tests for debounce mode
9cce848
wording
ec4bfe2
update README
f0453a7
debounce - little trick to avoid a random "Scope.eval is not a functi…
5324db8
Typo issue mawwait => maxwait
jtassin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -6,3 +6,4 @@ es | |
coverage | ||
*.tgz | ||
examples/**/dist | ||
.idea |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); | ||
}); | ||
}); | ||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we do a default debounce of 1ms, would it be possible for us to batch updates "per tick" essentially? I'm thinking of how you might queue several actions at the same time and instead of digesting each one immediately, we'd let it all run on the next tick/frame/whatever.
I'm not sure if $evalAsync does this already or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "per tick" should be already covered by the
$evalAsync
.I think that 1ms won't make a real performance gain.
Here we set it to
{wait: 100, maxWait: 500}
for a gain of ~50% of digests.