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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"bundlesize": [
{
"path": "full/preact.js",
"maxSize": "760b"
"maxSize": "785b"
},
{
"path": "dist/unistore.js",
Expand Down
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export default function createStore(state) {
listeners = out;
}

function setState(update, overwrite, action) {
function setState(update, overwrite, action, params) {
state = overwrite ? update : assign(assign({}, state), update);
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);
}

/**
Expand All @@ -49,8 +49,11 @@ export default function createStore(state) {
* @returns {Function} boundAction()
*/
action(action) {
function apply(result) {
setState(result, false, action);
function apply(result, args) {
if (args && args.length === 2)
setState(result, false, action, args[1]);
else
setState(result, false, action);
}

// Note: perf tests verifying this implementation: https://esbench.com/bench/5a295e6299634800a0349500
Expand All @@ -59,8 +62,8 @@ export default function createStore(state) {
for (let i=0; i<arguments.length; i++) args.push(arguments[i]);
let ret = action.apply(this, args);
if (ret!=null) {
if (ret.then) return ret.then(apply);
return apply(ret);
if (ret.then) return ret.then(apply, args);
return apply(ret, args);
}
};
},
Expand Down
6 changes: 3 additions & 3 deletions test/unistore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ describe('createStore()', () => {
expect(rval).toBeInstanceOf(Function);

store.setState({ a: 'b' });
expect(sub1).toBeCalledWith(store.getState(), action);
expect(sub1).toBeCalledWith(store.getState(), action, { a: 'b' }, undefined);

store.subscribe(sub2);
store.setState({ c: 'd' });

expect(sub1).toHaveBeenCalledTimes(2);
expect(sub1).toHaveBeenLastCalledWith(store.getState(), action);
expect(sub2).toBeCalledWith(store.getState(), action);
expect(sub1).toHaveBeenLastCalledWith(store.getState(), action, { c: 'd' }, undefined);
expect(sub2).toBeCalledWith(store.getState(), action, { c: 'd' }, undefined);
});

it('should unsubscribe', () => {
Expand Down