Skip to content
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Creates a new store, which is a tiny evented state container.
**Parameters**

- `state` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional initial state (optional, default `{}`)
- `mutations` (optional, default `false`)

**Examples**

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"bundlesize": [
{
"path": "full/preact.js",
"maxSize": "760b"
"maxSize": "819b"
},
{
"path": "dist/unistore.js",
"maxSize": "400b"
"maxSize": "417b"
},
{
"path": "preact.js",
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { assign } from './util';
* store.setState({ a: 'b' }); // logs { a: 'b' }
* store.setState({ c: 'd' }); // logs { a: 'b', c: 'd' }
*/
export default function createStore(state) {
export default function createStore(state, mutations=false) {
let listeners = [];
state = state || {};

Expand All @@ -34,6 +34,11 @@ export default function createStore(state) {
for (let i=0; i<currentListeners.length; i++) currentListeners[i](state, action);
}

function mutate(action, overwrite) {
const key = Object.keys(action)[0];
setState(mutations[key](state, action[key]), overwrite, action);
}

/**
* An observable state container, returned from {@link createStore}
* @name store
Expand All @@ -50,7 +55,10 @@ export default function createStore(state) {
*/
action(action) {
function apply(result) {
setState(result, false, action);
if (!mutations)
setState(result, false, action);
else
mutate(result, false);
}

// Note: perf tests verifying this implementation: https://esbench.com/bench/5a295e6299634800a0349500
Expand All @@ -72,6 +80,8 @@ export default function createStore(state) {
*/
setState,

mutate,

/**
* Register a listener function to be called whenever state is changed. Returns an `unsubscribe()` function.
* @param {Function} listener A function to call when state changes. Gets passed the new state.
Expand Down
23 changes: 22 additions & 1 deletion test/unistore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ describe('createStore()', () => {
expect(sub2).toBeCalledWith(store.getState(), action);
});

it('should invoke subscriptions using mutations', () => {
let store = createStore({ foo: 0 }, {
setFoo: (state, {v}) => ({ foo: v }),
})

let sub = jest.fn();
let rval = store.subscribe(sub);

store.mutate({ setFoo: { v: 1 } });
expect(sub).toBeCalledWith(store.getState(), { setFoo: { v: 1 } })
});

it('should unsubscribe', () => {
let store = createStore();

Expand Down Expand Up @@ -93,5 +105,14 @@ describe('createStore()', () => {
expect(sub1).not.toBeCalled();
expect(sub2).not.toBeCalled();
expect(sub3).not.toBeCalled();
});
});

it('should allow mutations', () => {
let store = createStore({ foo: 0 }, {
setFoo: (state, {v}) => ({ foo: v }),
})

store.mutate({ setFoo: { v: 1 } })
expect(store.getState()).toMatchObject({ foo: 1 })
});
});