From bc22314a18a6697a7ce1ea86882e2d2f7a19569c Mon Sep 17 00:00:00 2001 From: Patrick Borowy Date: Tue, 6 Aug 2019 03:41:48 +0200 Subject: [PATCH 1/2] subscribers now receive update & params --- src/index.js | 13 ++++++------- src/worker.js | 7 +++---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index d06bdec..2c98023 100644 --- a/src/index.js +++ b/src/index.js @@ -20,7 +20,7 @@ export default function createStore(worker) { state = {}, sendQueue = [], initialized = false; - + function handleMessage({ data }) { if (typeof data!=='object') {} else if ('pop' in data) { @@ -38,14 +38,13 @@ export default function createStore(worker) { function process(data) { let { type, overwrite, update, action, initial, partial } = data; - if (type==='@@STATE') { if (partial===true) { update = applyUpdate(state, update); overwrite = true; } - setState(update, overwrite===true, action, false); + setState(update, overwrite===true, action, false, data.params); if (initial) { initialized = true; @@ -57,7 +56,7 @@ export default function createStore(worker) { worker.addEventListener('message', handleMessage); - function setState(update, overwrite, action, replicate) { + function setState(update, overwrite, action, replicate, params) { let oldState = state; state = assign(overwrite ? {} : assign({}, state), update); if (replicate) { @@ -66,7 +65,7 @@ export default function createStore(worker) { // send({ type: '@@STATE', overwrite, update, action }); } let currentListeners = listeners; - for (let i=0; i { + store.subscribe( (state, action, update, params) => { if (lock===true) return; - let update = diff(state, currentState); // console.log('sub: ', { action, lock, update, state, currentState }); currentState = state; - send({ type: '@@STATE', update, action: action && action.name, partial: true }); + send({ type: '@@STATE', update, action: action && action.name, partial: true, params }); }); store.registerActions = newActions => { From adbec9f0a8513e10166e7540700142fa5557f1c9 Mon Sep 17 00:00:00 2001 From: Patrick Borowy Date: Tue, 6 Aug 2019 18:51:34 +0200 Subject: [PATCH 2/2] added a naive batch test --- test/backstore.test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/backstore.test.js b/test/backstore.test.js index 25c4a66..e1febd2 100644 --- a/test/backstore.test.js +++ b/test/backstore.test.js @@ -105,6 +105,26 @@ describe('createStore()', () => { expect(store.getState(), 'main thread state').to.have.property('count', 2); }); + it('should batch actions', async () => { + postMessage.reset(); + + let increment = store.action('increment'); + increment({ x: 'y' }); + increment({ x: 'y' }); + + await sleep(10); + + expect(postMessage).to.have.been.calledOnce.and.calledWith([ + { type: '@@ACTION', action: { type: 'increment', params: [{ x: 'y' }] } }, + { type: '@@ACTION', action: { type: 'increment', params: [{ x: 'y' }] } } + ]); + + let state = await worker.getState(); + + expect(state, 'worker state').to.have.property('count', 4); + expect(store.getState(), 'main thread state').to.have.property('count', 4); + }); + it('should invoke inline action on main thread', async () => { postMessage.reset();