Skip to content

Commit

Permalink
Improved looping; simplify assign
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy4groovy committed Jan 20, 2018
1 parent 365211c commit b963778
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 46 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

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

## Table of Contents

- [Install](#install)
- [Usage](#usage)
- [Examples](#examples)
- [API](#api)
- [License](#license)
- [Install](#install)
- [Usage](#usage)
- [Examples](#examples)
- [API](#api)
- [License](#license)

## Install

Expand Down Expand Up @@ -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 }
Expand All @@ -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() }
},

Expand Down Expand Up @@ -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));

// ...
```
Expand All @@ -150,4 +150,5 @@ If not, just open a [new clear and descriptive issue](../../issues/new).
[MIT License](https://oss.ninja/mit/developit) © [Jason Miller](https://jasonformat.com)

[preact]: https://github.com/developit/preact

[react]: https://github.com/facebook/react
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
"scripts": {
"build": "npm-run-all --silent -p build:main build:integrations build:combined -s size docs",
"build:main": "microbundle",
"build:integrations": "microbundle src/integrations/*.js -o x.js -f cjs",
"build:combined": "microbundle src/combined/*.js -o full/x.js",
"build:integrations": "microbundle \"src/integrations/*.js\" -o x.js -f cjs",
"build:combined": "microbundle \"src/combined/*.js\" -o full/x.js",
"size": "strip-json-comments --no-whitespace dist/unistore.js | gzip-size && bundlesize",
"docs": "documentation readme unistore.js -q --section API && npm run -s fixreadme",
"fixreadme": "node -e 'var fs=require(\"fs\");fs.writeFileSync(\"README.md\", fs.readFileSync(\"README.md\", \"utf8\").replace(/^- /gm, \"- \"))'",
"test": "eslint src && npm run build && jest",
"test:jest": "jest",
"prepare": "npm t",
"release": "npm t && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
},
Expand Down Expand Up @@ -89,7 +90,7 @@
"eslint-config-developit": "^1.1.1",
"gzip-size-cli": "^2.1.0",
"jest": "^21.2.1",
"microbundle": "^0.2.4",
"microbundle": "^0.3.1",
"npm-run-all": "^4.1.2",
"preact": "^8.2.6",
"strip-json-comments-cli": "^1.0.1"
Expand Down
22 changes: 7 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@ export default function createStore(state) {
state = state || {};

function unsubscribe(listener) {
let out = [];
for (let i=0; i<listeners.length; i++) {
if (listeners[i]===listener) {
listener = null;
}
else {
out.push(listeners[i]);
}
}
listeners = out;
let i = listeners.indexOf(listener);
if (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<currentListeners.length; i++) currentListeners[i](state, action);
state = overwrite ? update : assign({}, state, update);
let i = listeners.length;
while (i-- > 0) listeners[i](state, action);
}

/** An observable state container, returned from {@link createStore}
Expand All @@ -52,8 +44,8 @@ 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<arguments.length; i++) args.push(arguments[i]);
let args = [state], l = arguments.length;
for (let i = 0; i<l; i++) args.push(arguments[i]);
let ret = action.apply(this, args);
if (ret!=null) {
if (ret.then) ret.then(apply);
Expand Down
2 changes: 1 addition & 1 deletion src/integrations/preact.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion src/integrations/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -14,16 +12,18 @@ export function select(properties) {
if (typeof properties==='string') properties = properties.split(/\s*,\s*/);
return state => {
let selected = {};
for (let i=0; i<properties.length; i++) {
selected[properties[i]] = state[properties[i]];
}
let i = properties.length;
while (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(_) {
for (let i = 1; i<arguments.length; i++)
for (let j in arguments[i])
arguments[0][j] = arguments[i][j];

return arguments[0];
}

0 comments on commit b963778

Please sign in to comment.