From 8ddc905e0bcb2b184833c8a762a3354dfe008a1d Mon Sep 17 00:00:00 2001 From: solsen Date: Fri, 19 Jan 2018 23:39:52 -0500 Subject: [PATCH] Improved looping; simplify assign --- README.md | 10 +++++----- src/index.js | 24 +++++++++--------------- src/integrations/preact.js | 2 +- src/integrations/react.js | 2 +- src/util.js | 18 ++++++++---------- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index f6a97dd..49c5ff5 100644 --- a/README.md +++ b/README.md @@ -69,10 +69,10 @@ You can find the library on `window.unistore`. import createStore from 'unistore' import { Provider, connect } from 'unistore/preact' -let store = createStore({ count: 0 }) +const store = createStore({ count: 0 }) // If actions is a function, it gets passed the store: -let actions = store => ({ +const actions = store => ({ // Actions can just return a state update: increment(state) { return { count: state.count+1 } @@ -90,7 +90,7 @@ let actions = store => ({ // Async actions can be pure async/promise functions: async getStuff(state) { - let res = await fetch('/foo.json') + const res = await fetch('/foo.json') return { stuff: await res.json() } }, @@ -126,8 +126,8 @@ Make sure to have [Redux devtools extension](https://github.com/zalmoxisus/redux import createStore from 'unistore' import devtools from 'unistore/devtools' -let initialState = { count: 0 }; -let store = process.env.NODE_ENV === 'production' ? createStore(initialState) : devtools(createStore(initialState)); +const initialState = { count: 0 }; +const store = process.env.NODE_ENV === 'production' ? createStore(initialState) : devtools(createStore(initialState)); // ... ``` diff --git a/src/index.js b/src/index.js index 2e50188..e4fba2b 100644 --- a/src/index.js +++ b/src/index.js @@ -15,22 +15,14 @@ export default function createStore(state) { state = state || {}; function unsubscribe(listener) { - let out = []; - for (let i=0; i -1) listeners.splice(i, 1); } function setState(update, overwrite, action) { - state = overwrite ? update : assign(assign({}, state), update); - let currentListeners = listeners; - for (let i=0; i 0) listeners[i](state, action); } /** An observable state container, returned from {@link createStore} @@ -52,8 +44,10 @@ export default function createStore(state) { // Note: perf tests verifying this implementation: https://esbench.com/bench/5a295e6299634800a0349500 return function() { - let args = [state]; - for (let i=0; i 0) args.shift(arguments[i]); + args.shift(state); let ret = action.apply(this, args); if (ret!=null) { if (ret.then) ret.then(apply); diff --git a/src/integrations/preact.js b/src/integrations/preact.js index de8ac75..becd1b1 100644 --- a/src/integrations/preact.js +++ b/src/integrations/preact.js @@ -40,7 +40,7 @@ export function connect(mapStateToProps, actions) { this.componentWillUnmount = () => { store.unsubscribe(update); }; - this.render = props => h(Child, assign(assign(assign({}, boundActions), props), state)); + this.render = props => h(Child, assign({}, boundActions, props, state)); } return (Wrapper.prototype = new Component()).constructor = Wrapper; }; diff --git a/src/integrations/react.js b/src/integrations/react.js index b499a00..1a1ca4a 100644 --- a/src/integrations/react.js +++ b/src/integrations/react.js @@ -46,7 +46,7 @@ export function connect(mapStateToProps, actions) { this.componentWillUnmount = () => { store.unsubscribe(update); }; - this.render = () => createElement(Child, assign(assign(assign({}, boundActions), this.props), state)); + this.render = () => createElement(Child, assign({}, boundActions, this.props, state)); } Wrapper.contextTypes = CONTEXT_TYPES; return (Wrapper.prototype = Object.create(Component)).constructor = Wrapper; diff --git a/src/util.js b/src/util.js index 7050423..d05f95e 100644 --- a/src/util.js +++ b/src/util.js @@ -2,9 +2,7 @@ export function mapActions(actions, store) { if (typeof actions==='function') actions = actions(store); let mapped = {}; - for (let i in actions) { - mapped[i] = store.action(actions[i]); - } + for (let i in actions) mapped[i] = store.action(actions[i]); return mapped; } @@ -14,16 +12,16 @@ export function select(properties) { if (typeof properties==='string') properties = properties.split(/\s*,\s*/); return state => { let selected = {}; - for (let i=0; i 0) selected[properties[i]] = state[properties[i]]; return selected; }; } -// Lighter Object.assign stand-in -export function assign(obj, props) { - for (let i in props) obj[i] = props[i]; - return obj; +// Lighter Object.assign clone +export function assign(_) { + let i = arguments.length; + while (--i > 0) for (let j in arguments[i]) arguments[0][j] = arguments[i][j]; + return arguments[0]; }