Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -66,7 +65,7 @@ export default function createStore(worker) {
// send({ type: '@@STATE', overwrite, update, action });
}
let currentListeners = listeners;
for (let i=0; i<currentListeners.length; i++) currentListeners[i](state, action);
for (let i=0; i<currentListeners.length; i++) currentListeners[i](state, action, update, params);
}

function send(opts) {
Expand All @@ -81,7 +80,7 @@ export default function createStore(worker) {
sendQueue.length = 0;
}
}

function unsubscribe(listener) {
let out = [];
for (let i=0; i<listeners.length; i++) {
Expand All @@ -102,7 +101,7 @@ export default function createStore(worker) {
if (typeof action==='string') action = { type: action, params: params.filter(notEvent) };
if (action && !action.type) {
// console.warn('Action running on main thread: ', actionCreator.name);
setState(action, false, actionCreator.name, true);
setState(action, false, actionCreator.name, true, params);
}
else {
// console.log('ACTION', action.type, ...(action.params || [action.payload]));
Expand Down
7 changes: 3 additions & 4 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import createStore from 'unistore';
import { diff, applyUpdate } from './util';
import { applyUpdate } from './util';


/** The other half of stockroom, which runs inside a Web Worker.
Expand Down Expand Up @@ -78,12 +78,11 @@ export default function createWorkerStore(initialState) {
}
if (typeof addEventListener==='function') addEventListener('message', handleMessage);

store.subscribe( (state, action) => {
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 => {
Expand Down
20 changes: 20 additions & 0 deletions test/backstore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down