-
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
Changes from 14 commits
b7ae00f
9d2508b
4a58d12
4154813
fbcaac1
c281dad
d00388e
93e1cbb
4372bb5
91c8ff6
b4d699c
9cce848
ec4bfe2
f0453a7
5324db8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ es | |
coverage | ||
*.tgz | ||
examples/**/dist | ||
.idea |
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; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,13 @@ export default function ngReduxProvider() { | |
_initialState = initialState || {}; | ||
}; | ||
|
||
this.config = { | ||
debounce: { | ||
wait: undefined, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. The "per tick" should be already covered by the I think that 1ms won't make a real performance gain. Here we set it to |
||
maxWait: undefined, | ||
}, | ||
}; | ||
|
||
this.$get = ($injector) => { | ||
const resolveMiddleware = middleware => isString(middleware) | ||
? $injector.get(middleware) | ||
|
@@ -71,13 +78,13 @@ export default function ngReduxProvider() { | |
} | ||
|
||
// digestMiddleware needs to be the last one. | ||
resolvedMiddleware.push(digestMiddleware($injector.get('$rootScope'))); | ||
resolvedMiddleware.push(digestMiddleware($injector.get('$rootScope'), this.config.debounce)); | ||
|
||
// combine middleware into a store enhancer. | ||
const middlewares = applyMiddleware(...resolvedMiddleware); | ||
|
||
// compose enhancers with middleware and create store. | ||
const store = createStore(_reducer, _initialState, compose(...resolvedStoreEnhancer, middlewares)); | ||
const store = createStore(_reducer, _initialState, compose(middlewares, ...resolvedStoreEnhancer)); | ||
|
||
return assign({}, store, { connect: Connector(store) }); | ||
}; | ||
|
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); | ||
}); | ||
}); | ||
|
||
}); |
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.
Looks like there's a typo here, I assume this is intended to be
maxWait
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.
You're right, thx for review.
It's corrected.