Permalink
Browse files

Size & performance optimizations

  • Loading branch information...
developit committed Dec 7, 2017
1 parent dfb4c9f commit f860e9911954228bfdcf397576e33d776ca34121
Showing with 24 additions and 18 deletions.
  1. +1 −1 README.md
  2. +2 −4 rollup.config.js
  3. +21 −13 unistore.js
View
@@ -6,7 +6,7 @@
# unistore
> A tiny ~700b centralized state container with component bindings for [Preact].
> A tiny 650b centralized state container with component bindings for [Preact].
- **Small** footprint compliments Preact nicely
- **Familiar** names and ideas from Redux-like libraries
View
@@ -18,14 +18,12 @@ export default {
name: pkg.amdName || pkg.name,
file: (format==='es' && pkg.module) || (format==='umd' && pkg['umd:main']) || pkg.main
},
external: ['preact'].concat(Object.keys(pkg.dependencies)),
external: ['preact'],
globals: {
preact: 'preact',
'preact-context-provider': 'Provider'
preact: 'preact'
},
plugins: [
buble({
objectAssign: 'Object.assign',
jsx: 'h'
}),
es3(),
View
@@ -66,7 +66,9 @@ export function createStore(state) {
* export class Foo { render({ foo, bar }) { } }
*/
export function connect(mapStateToProps, actions) {
if (typeof mapStateToProps!=='function') mapStateToProps = select(mapStateToProps || []);
if (typeof mapStateToProps!=='function') {
mapStateToProps = select(mapStateToProps || []);
}
return Child => {
function Wrapper(props, { store }) {
let state = mapStateToProps(store ? store.getState() : {}, props);
@@ -92,8 +94,9 @@ export function connect(mapStateToProps, actions) {
/** Provider exposes a store (passed as `props.store`) into context.
*
* Generally, an entire application is wrapped in a single `<Provider>` at the root.
* @constructor
* @class
* @extends Component
* @param {Object} props
* @param {Store} props.store A {Store} instance to expose via context.
@@ -107,30 +110,35 @@ Provider.prototype.render = function(props) {
};
// Bind an object/factory of actions to the store and wrap them.
function mapActions(actions, store) {
let mapped = {};
if (typeof actions==='function') actions = actions(store);
let mapped = {};
for (let i in actions) {
mapped[i] = function() {
let args = [store.getState()];
for (let i=0; i<arguments.length; i++) args.push(arguments[i]);
let ret = actions[i].apply(store, args);
if (ret!=null) {
if (ret.then) ret.then(store.setState);
else store.setState(ret);
}
};
mapped[i] = createAction(store, actions[i]);
}
return mapped;
}
// Bind a single action to the store and sequester its return value.
function createAction(store, action) {
let args = [store.getState()];
for (let i=0; i<arguments.length; i++) args.push(arguments[i]);
let ret = action.apply(store, args);
if (ret!=null) {
if (ret.then) ret.then(store.setState);
else store.setState(ret);
}
}
// select('foo,bar') creates a function of the form: ({ foo, bar }) => ({ foo, bar })
function select(properties) {
if (typeof properties==='string') properties = properties.split(',');
return state => {
let selected = {};
for (let i=0; i<properties.length; i++) {
for (let i=properties.length; i--; ) {
selected[properties[i]] = state[properties[i]];
}
return selected;

0 comments on commit f860e99

Please sign in to comment.