Comparing version 0.0.2 to 1.0.0
86
index.js
@@ -1,84 +0,2 @@ | ||
let states = [{}]; | ||
Object.defineProperty(states, 'last', { | ||
get () { | ||
return this[this.length - 1]; | ||
} | ||
}); | ||
module.exports = class Store { | ||
constructor () { | ||
Object.assign(this, { | ||
queue: Promise.resolve(), | ||
reducers: [], | ||
listeners: [], | ||
middlewares: [] | ||
}); | ||
} | ||
get state () { | ||
states.push(Object.assign({}, states.last)); | ||
return states.last; | ||
} | ||
use (middleware) { | ||
this.middlewares.push(middleware); | ||
} | ||
listen (types, listener) { | ||
this.listeners.push(Object.assign(listener, { | ||
types: prepareTypes(types) | ||
})); | ||
} | ||
attach (types, reducer) { | ||
this.reducers.push(Object.assign(reducer, { | ||
types: prepareTypes(types) | ||
})); | ||
} | ||
dispatch (type, action = {}) { | ||
let chain = Promise.resolve(action) | ||
.then(alter.bind(this)) | ||
.catch(err => err) | ||
.then(reduce(type, this.reducers, this.state)); | ||
notify.call(this, type, chain); | ||
} | ||
}; | ||
function alter (action) { | ||
let alter_chain = Promise.resolve(action); | ||
for (let middleware of this.middlewares) { | ||
alter_chain = alter_chain.then(action => middleware(action)); | ||
} | ||
return alter_chain; | ||
} | ||
function reduce (type, reducers, state) { | ||
return function (action) { | ||
let reducer_chain = Promise.resolve(); | ||
for (let reducer of reducers) { | ||
if (reducer.types.includes(type)) { | ||
reducer_chain = reducer_chain.then(reducer(state, action)); | ||
} | ||
} | ||
reducer_chain = reducer_chain.then(() => action); | ||
return reducer_chain; | ||
}; | ||
} | ||
function notify (type, chain) { | ||
this.queue | ||
.then(() => chain) | ||
.then(action => { | ||
for (let listener of this.listeners) { | ||
if (listener.types.includes(type)) { | ||
listener(this.state, action); | ||
} | ||
} | ||
}); | ||
} | ||
function prepareTypes (types) { | ||
if (typeof types == 'string') { | ||
types = [types]; | ||
} | ||
else if (!Array.isArray(types)) { | ||
throw new Error ('types must be an array or a string'); | ||
} | ||
return types; | ||
} | ||
require('es6-promise').polyfill(); | ||
module.exports = require('./src/store'); |
{ | ||
"name": "delux", | ||
"version": "0.0.2", | ||
"description": "Simplified combinition of Flux Redux", | ||
"version": "1.0.0", | ||
"description": "Beautiful, light and simple state manager inspired by Redux", | ||
"main": "index.js", | ||
@@ -11,3 +11,5 @@ "scripts": { | ||
"license": "MIT", | ||
"dependencies": {} | ||
"dependencies": { | ||
"es6-promise": "" | ||
} | ||
} |
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10528
9
161
2
161
1
2
+ Addedes6-promise@
+ Addedes6-promise@4.2.8(transitive)