Skip to content

Commit b963778

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

File tree

6 files changed

+40
-46
lines changed

6 files changed

+40
-46
lines changed

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88

99
> A tiny ~650b centralized state container with component bindings for [Preact] & [React].
1010
11-
- **Small** footprint complements Preact nicely
12-
- **Familiar** names and ideas from Redux-like libraries
13-
- **Useful** data selectors to extract properties from state
14-
- **Portable** actions can be moved into a common place and imported
15-
- **Functional** actions are just reducers
16-
- **NEW**: seamlessly run Unistore in a worker via [Stockroom](https://github.com/developit/stockroom)
11+
- **Small** footprint complements Preact nicely
12+
- **Familiar** names and ideas from Redux-like libraries
13+
- **Useful** data selectors to extract properties from state
14+
- **Portable** actions can be moved into a common place and imported
15+
- **Functional** actions are just reducers
16+
- **NEW**: seamlessly run Unistore in a worker via [Stockroom](https://github.com/developit/stockroom)
1717

1818
## Table of Contents
1919

20-
- [Install](#install)
21-
- [Usage](#usage)
22-
- [Examples](#examples)
23-
- [API](#api)
24-
- [License](#license)
20+
- [Install](#install)
21+
- [Usage](#usage)
22+
- [Examples](#examples)
23+
- [API](#api)
24+
- [License](#license)
2525

2626
## Install
2727

@@ -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
```
@@ -150,4 +150,5 @@ If not, just open a [new clear and descriptive issue](../../issues/new).
150150
[MIT License](https://oss.ninja/mit/developit) © [Jason Miller](https://jasonformat.com)
151151

152152
[preact]: https://github.com/developit/preact
153+
153154
[react]: https://github.com/facebook/react

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
"scripts": {
1111
"build": "npm-run-all --silent -p build:main build:integrations build:combined -s size docs",
1212
"build:main": "microbundle",
13-
"build:integrations": "microbundle src/integrations/*.js -o x.js -f cjs",
14-
"build:combined": "microbundle src/combined/*.js -o full/x.js",
13+
"build:integrations": "microbundle \"src/integrations/*.js\" -o x.js -f cjs",
14+
"build:combined": "microbundle \"src/combined/*.js\" -o full/x.js",
1515
"size": "strip-json-comments --no-whitespace dist/unistore.js | gzip-size && bundlesize",
1616
"docs": "documentation readme unistore.js -q --section API && npm run -s fixreadme",
1717
"fixreadme": "node -e 'var fs=require(\"fs\");fs.writeFileSync(\"README.md\", fs.readFileSync(\"README.md\", \"utf8\").replace(/^- /gm, \"- \"))'",
1818
"test": "eslint src && npm run build && jest",
19+
"test:jest": "jest",
1920
"prepare": "npm t",
2021
"release": "npm t && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
2122
},
@@ -89,7 +90,7 @@
8990
"eslint-config-developit": "^1.1.1",
9091
"gzip-size-cli": "^2.1.0",
9192
"jest": "^21.2.1",
92-
"microbundle": "^0.2.4",
93+
"microbundle": "^0.3.1",
9394
"npm-run-all": "^4.1.2",
9495
"preact": "^8.2.6",
9596
"strip-json-comments-cli": "^1.0.1"

src/index.js

Lines changed: 7 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,8 @@ 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 = [state], l = arguments.length;
48+
for (let i = 0; i<l; i++) args.push(arguments[i]);
5749
let ret = action.apply(this, args);
5850
if (ret!=null) {
5951
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)