Skip to content

Commit 4ba58b0

Browse files
committed
Improved looping; simplify assign
1 parent 365211c commit 4ba58b0

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ You can find the library on `window.unistore`.
6969
import createStore from 'unistore'
7070
import { Provider, connect } from 'unistore/preact'
7171

72-
let store = createStore({ count: 0 })
72+
const store = createStore({ count: 0 })
7373

7474
// If actions is a function, it gets passed the store:
75-
let actions = store => ({
75+
const actions = store => ({
7676
// Actions can just return a state update:
7777
increment(state) {
7878
return { count: state.count+1 }
@@ -90,7 +90,7 @@ let actions = store => ({
9090

9191
// Async actions can be pure async/promise functions:
9292
async getStuff(state) {
93-
let res = await fetch('/foo.json')
93+
const res = await fetch('/foo.json')
9494
return { stuff: await res.json() }
9595
},
9696

@@ -126,8 +126,8 @@ Make sure to have [Redux devtools extension](https://github.com/zalmoxisus/redux
126126
import createStore from 'unistore'
127127
import devtools from 'unistore/devtools'
128128

129-
let initialState = { count: 0 };
130-
let store = process.env.NODE_ENV === 'production' ? createStore(initialState) : devtools(createStore(initialState));
129+
const initialState = { count: 0 };
130+
const store = process.env.NODE_ENV === 'production' ? createStore(initialState) : devtools(createStore(initialState));
131131

132132
// ...
133133
```

src/index.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,14 @@ export default function createStore(state) {
1515
state = state || {};
1616

1717
function unsubscribe(listener) {
18-
let out = [];
19-
for (let i=0; i<listeners.length; i++) {
20-
if (listeners[i]===listener) {
21-
listener = null;
22-
}
23-
else {
24-
out.push(listeners[i]);
25-
}
26-
}
27-
listeners = out;
18+
let i = listeners.indexOf(listener);
19+
if (i > -1) listeners.splice(i, 1);
2820
}
2921

3022
function setState(update, overwrite, action) {
31-
state = overwrite ? update : assign(assign({}, state), update);
32-
let currentListeners = listeners;
33-
for (let i=0; i<currentListeners.length; i++) currentListeners[i](state, action);
23+
state = overwrite ? update : assign({}, state, update);
24+
let i = listeners.length;
25+
while (i-- > 0) listeners[i](state, action);
3426
}
3527

3628
/** An observable state container, returned from {@link createStore}
@@ -52,8 +44,10 @@ export default function createStore(state) {
5244

5345
// Note: perf tests verifying this implementation: https://esbench.com/bench/5a295e6299634800a0349500
5446
return function() {
55-
let args = [state];
56-
for (let i=0; i<arguments.length; i++) args.push(arguments[i]);
47+
let args = [];
48+
let i = arguments.length;
49+
while (i-- > 0) args.shift(arguments[i]);
50+
args.shift(state);
5751
let ret = action.apply(this, args);
5852
if (ret!=null) {
5953
if (ret.then) ret.then(apply);

src/integrations/preact.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function connect(mapStateToProps, actions) {
4040
this.componentWillUnmount = () => {
4141
store.unsubscribe(update);
4242
};
43-
this.render = props => h(Child, assign(assign(assign({}, boundActions), props), state));
43+
this.render = props => h(Child, assign({}, boundActions, props, state));
4444
}
4545
return (Wrapper.prototype = new Component()).constructor = Wrapper;
4646
};

src/integrations/react.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function connect(mapStateToProps, actions) {
4646
this.componentWillUnmount = () => {
4747
store.unsubscribe(update);
4848
};
49-
this.render = () => createElement(Child, assign(assign(assign({}, boundActions), this.props), state));
49+
this.render = () => createElement(Child, assign({}, boundActions, this.props, state));
5050
}
5151
Wrapper.contextTypes = CONTEXT_TYPES;
5252
return (Wrapper.prototype = Object.create(Component)).constructor = Wrapper;

src/util.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
export function mapActions(actions, store) {
33
if (typeof actions==='function') actions = actions(store);
44
let mapped = {};
5-
for (let i in actions) {
6-
mapped[i] = store.action(actions[i]);
7-
}
5+
for (let i in actions) mapped[i] = store.action(actions[i]);
86
return mapped;
97
}
108

@@ -14,16 +12,18 @@ export function select(properties) {
1412
if (typeof properties==='string') properties = properties.split(/\s*,\s*/);
1513
return state => {
1614
let selected = {};
17-
for (let i=0; i<properties.length; i++) {
18-
selected[properties[i]] = state[properties[i]];
19-
}
15+
let i = properties.length;
16+
while (i-- > 0) selected[properties[i]] = state[properties[i]];
2017
return selected;
2118
};
2219
}
2320

2421

25-
// Lighter Object.assign stand-in
26-
export function assign(obj, props) {
27-
for (let i in props) obj[i] = props[i];
28-
return obj;
22+
// Lighter Object.assign clone
23+
export function assign(_) {
24+
for (let i = 1; i<arguments.length; i++)
25+
for (let j in arguments[i])
26+
arguments[0][j] = arguments[i][j];
27+
28+
return arguments[0];
2929
}

0 commit comments

Comments
 (0)