Skip to content

Commit

Permalink
feat(Controller): flush async state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Dec 3, 2016
1 parent d81c2c4 commit 720009f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/cerebral/src/Controller.js
Expand Up @@ -82,6 +82,10 @@ class Controller extends EventEmitter {
flush (force) {
const changes = this.model.flush()

if (!force && !Object.keys(changes).length) {
return
}

this.updateComputeds(changes, force)
this.updateComponents(changes, force)
this.emit('flush', changes, Boolean(force))
Expand Down
22 changes: 20 additions & 2 deletions packages/cerebral/src/Controller.test.js
Expand Up @@ -154,7 +154,11 @@ describe('Controller', () => {
]
}
})
controller.on('flush', () => flushCount++)
const originFlush = controller.flush
controller.flush = function (...args) {
flushCount++
originFlush.apply(this, args)
}
controller.runTree.once('end', () => {
assert.equal(flushCount, 3)
done()
Expand All @@ -171,6 +175,20 @@ describe('Controller', () => {
}
}
})
assert.deepEqual(controller.model.flush(), {})
assert.deepEqual(controller.model.changedPaths, [])
})
it('should flush async mutations', (done) => {
const controller = new Controller({
signals: {
test: [
({state}) => setTimeout(() => state.set('foo', 'bar'))
]
}
})
controller.on('flush', (changes) => {
assert.deepEqual(changes, {foo: true})
done()
})
controller.getSignal('test')()
})
})
3 changes: 2 additions & 1 deletion packages/cerebral/src/providers/Recorder.js
Expand Up @@ -77,7 +77,7 @@ export default function RecorderProvider (options = {}) {
if (event.type === 'mutation') {
mutate(event)
} else if (event.type === 'flush') {
controller.flush(currentEventIndex === 0)
controller.flush()
}

lastEventTimestamp = event.timestamp
Expand Down Expand Up @@ -140,6 +140,7 @@ export default function RecorderProvider (options = {}) {
originalRunSignal.apply(controller, args)
}
}
controller.flush(true)
runNextEvent()
},
record (options = {}) {
Expand Down
15 changes: 5 additions & 10 deletions packages/cerebral/src/providers/Recorder.test.js
Expand Up @@ -39,7 +39,7 @@ describe('Recorder', () => {
assert.equal(recording.initialState[0].value, JSON.stringify({
foo: 'bar'
}))
assert.equal(recording.events.length, 4)
assert.equal(recording.events.length, 3)
}]
},
providers: [RecorderProvider({
Expand Down Expand Up @@ -170,7 +170,6 @@ describe('Recorder', () => {
initialState: ['foo']
})],
update: [({state}) => state.set('foo', 'bar2')],
update2: [({state}) => state.set('foo', 'bar3')],
stop: [({recorder}) => recorder.stop()],
play: [({recorder}) => recorder.play({
allowedSignals: ['stop']
Expand All @@ -183,24 +182,20 @@ describe('Recorder', () => {
controller.getSignal('record')()
controller.getSignal('update')()
controller.getSignal('stop')()
controller.getSignal('play')()
controller.once('flush', (changes) => {
assert.deepEqual(changes, {})
})
timeout.tick()
timeout.tick()
controller.once('flush', (changes) => {
assert.deepEqual(changes, {
foo: true
})
})
controller.getSignal('play')()
timeout.tick()
controller.once('flush', (changes) => {
assert.deepEqual(changes, {})
assert.deepEqual(changes, {
foo: true
})
controller.getSignal('stop')()
done()
})
timeout.tick()
})
it('should be able to pause and continue playback', () => {
const timeout = timeoutMock()
Expand Down
6 changes: 6 additions & 0 deletions packages/cerebral/src/providers/State.js
Expand Up @@ -18,6 +18,7 @@ function StateProviderFactory () {

function createProvider (context) {
const model = context.controller.model
let asyncTimeout = null

return methods.reduce((currentStateContext, methodKey) => {
if (methodKey === 'compute') {
Expand All @@ -26,6 +27,11 @@ function StateProviderFactory () {
currentStateContext[methodKey] = (...args) => {
const path = ensurePath(args.shift())

if (methodKey !== 'get') {
clearTimeout(asyncTimeout)
asyncTimeout = setTimeout(() => context.controller.flush())
}

return model[methodKey].apply(model, [path].concat(args))
}
}
Expand Down

0 comments on commit 720009f

Please sign in to comment.