Comparing version
@@ -1,55 +0,1 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.chronicle = global.chronicle || {}))); | ||
}(this, (function (exports) { | ||
var thunk = function thunk(args) { | ||
return function (store) { | ||
return function (next) { | ||
return function (action) { | ||
return typeof action === 'function' ? action(store.dispatch, store.getState, args) : next(action); | ||
}; | ||
}; | ||
}; | ||
}; | ||
var index = (function (reducer) { | ||
var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : reducer(reducer._, {}); | ||
var subscribers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var store = function store(s) { | ||
return function (subscriber) { | ||
return subscribers.unshift(subscriber), function (i) { | ||
return subscribers.splice(i = subscribers.indexOf(subscriber), !!~i); | ||
}; | ||
}(s(store)); | ||
}; | ||
store.getState = function () { | ||
return state; | ||
}; | ||
store.dispatch = function (action) { | ||
return function (next) { | ||
return subscribers.length === 0 ? function (action) { | ||
return next(action); | ||
} : subscribers.reduce(function (a, b) { | ||
return function (next) { | ||
return a(b(next)); | ||
}; | ||
})(next); | ||
}(function (action) { | ||
return state = reducer(state, action); | ||
})(action); | ||
}; | ||
return store; | ||
}); | ||
exports.thunk = thunk; | ||
exports['default'] = index; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
Object.defineProperty(exports,"__esModule",{value:!0});var rxjs_BehaviorSubject=require("rxjs/BehaviorSubject"),rxjs_Subject=require("rxjs/Subject"),rxjs_observable_merge=require("rxjs/observable/merge"),rxjs_operator_scan=require("rxjs/operator/scan"),rxjs_operator_startWith=require("rxjs/operator/startWith");exports.combineEpics=function(){var e=[].slice.call(arguments);return function(){var r=[].slice.call(arguments);return rxjs_observable_merge.merge.apply(null,e.map(function(e){return e.apply(null,r)}))}},exports.createStore=function(e,r,t,s){void 0===r&&(r=e(e._,{})),void 0===s&&(s={});var a=new rxjs_BehaviorSubject.BehaviorSubject,o=new rxjs_Subject.Subject;rxjs_operator_scan.scan.call(rxjs_operator_startWith.startWith.call(o,r),e).subscribe(a);var c=function(){return a.getValue()},n=function(e){o.next(e)};return"function"==typeof t&&("dependencies"in s?t(o,{getState:c},s.dependencies).subscribe(n):t(o,{getState:c}).subscribe(n)),{getState:c,dispatch:n}}; |
{ | ||
"name": "chronicle", | ||
"version": "1.0.0", | ||
"version": "2.0.0-alpha.1", | ||
"description": "Tiny observable & high-performance state management", | ||
"license": "MIT", | ||
"author": "Robin van der Vleuten <robin@webstronauts.co>", | ||
"author": "Robin van der Vleuten <robin@webstronauts.co> (robinvdvleuten.nl)", | ||
"main": "dist/chronicle.js", | ||
"jsnext:main": "src/chronicle.js", | ||
"browser": "dist/chronicle.umd.js", | ||
"jsnext:main": "src/index.js", | ||
"keywords": [ | ||
@@ -17,10 +18,9 @@ "flux", | ||
"src", | ||
"dist/*.js" | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "npm-run-all --silent transpile minify", | ||
"transpile": "rollup -c", | ||
"minify": "uglifyjs dist/chronicle.js -c collapse_vars,evaluate,unsafe,loops=false,keep_fargs=false,pure_getters,unused,dead_code -m -o dist/chronicle.min.js -p relative", | ||
"build": "mkdir -p dist && npm run -s build:cjs && npm run -s build:umd", | ||
"build:cjs": "rollup -c -i $npm_package_jsnext_main -f cjs --no-strict | uglifyjs -cm -o $npm_package_main", | ||
"build:umd": "rollup -c -i $npm_package_jsnext_main -f umd -n $npm_package_name --no-strict | uglifyjs -cm -o $npm_package_browser", | ||
"test": "jest --env=jsdom", | ||
"test:perf": "node ./test/perf", | ||
"format": "prettier --single-quote --trailing-comma es5 --write '{src,test,build}/**/*.js'", | ||
@@ -36,6 +36,3 @@ "precommit": "lint-staged --verbose", | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/robinvdvleuten/chronicle.git" | ||
}, | ||
"repository": "robinvdvleuten/chronicle", | ||
"bugs": { | ||
@@ -48,3 +45,2 @@ "url": "https://github.com/robinvdvleuten/chronicle/issues" | ||
"babel-preset-env": "^1.4.0", | ||
"benchmark": "^2.1.4", | ||
"husky": "^0.13.3", | ||
@@ -55,9 +51,10 @@ "jest": "^20.0.1", | ||
"prettier": "^1.3.1", | ||
"redux": "^3.7.0", | ||
"redux-saga": "^0.15.3", | ||
"redux-thunk": "^2.2.0", | ||
"rollup": "^0.41.6", | ||
"rollup-plugin-babel": "^2.7.1", | ||
"rollup-plugin-buble": "^0.17.0", | ||
"rxjs": "^5.5.2", | ||
"uglify-js": "^3.0.3" | ||
}, | ||
"peerDependencies": { | ||
"rxjs": "^5.5.0" | ||
} | ||
} |
# chronicle | ||
Tiny observable & high-performance state management. | ||
Tiny observable & high-performance state management with RxJS as first-class citizen. | ||
[](https://www.npmjs.com/package/chronicle) | ||
[](https://travis-ci.org/robinvdvleuten/chronicle) | ||
@@ -16,48 +17,31 @@ | ||
```js | ||
import chronicle, { thunk } from 'chronicle'; | ||
import { createStore } from 'chronicle'; | ||
const ACTIONS = { | ||
INCREMENT: state => ({ counter: state.counter + 1 }), | ||
DECREMENT: state => ({ counter: state.counter - 1 }), | ||
}; | ||
const counter = (state = 0, action) => { | ||
switch (action.type) { | ||
case 'INCREMENT': | ||
return state + 1 | ||
case 'DECREMENT': | ||
return state - 1 | ||
default: | ||
return state | ||
} | ||
} | ||
const store = chronicle( | ||
(state = { counter: 0 }, action) => | ||
action && ACTIONS[action.type] | ||
? ACTIONS[action.type](state, action) | ||
: state | ||
); | ||
const epic = (action$, store) => | ||
action$ | ||
.filter(action => action.type === 'DECREMENT') | ||
.map(() => ({ type: 'INCREMENT' })); | ||
store(thunk()); | ||
const store = createStore(counter, null, epic); | ||
const unsubscribe = store(({ dispatch }) => next => action => { | ||
console.log(action); | ||
return next(action); | ||
}); | ||
store.dispatch({ type: 'DECREMENT' }); | ||
store.dispatch(dispatch => dispatch({ type: 'INCREMENT' })); | ||
unsubscribe(); | ||
store.dispatch({ type: 'INCREMENT' }); | ||
store.dispatch({ type: 'INCREMENT' }); | ||
// Will output 1 instead of -1 | ||
console.log(store.getState()); | ||
``` | ||
## Performance | ||
You can run the performance test through `npm run test:perf`; | ||
```bash | ||
chronicle x 892,393 ops/sec ±4.99% (66 runs sampled) | ||
chronicle with thunk x 332,470 ops/sec ±3.61% (49 runs sampled) | ||
redux x 557,308 ops/sec ±1.63% (89 runs sampled) | ||
redux with thunk x 288,050 ops/sec ±1.25% (90 runs sampled) | ||
Fastest is chronicle | ||
``` | ||
## Credits | ||
Thanks Forbes Lindesay for donating the `chronicle` npm name. | ||
Thanks to [Redux Observable](https://redux-observable.js.org/) for the initial inspiration and [Forbes Lindesay](https://github.com/ForbesLindesay) for donating the `chronicle` npm name. | ||
@@ -64,0 +48,0 @@ ## License |
@@ -1,25 +0,50 @@ | ||
export const thunk = args => store => next => action => | ||
typeof action === 'function' | ||
? action(store.dispatch, store.getState, args) | ||
: next(action); | ||
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | ||
import { Subject } from 'rxjs/Subject'; | ||
import { merge } from 'rxjs/observable/merge'; | ||
import { scan } from 'rxjs/operator/scan'; | ||
import { startWith } from 'rxjs/operator/startWith'; | ||
export default (reducer, state = reducer(reducer._, {}), subscribers = []) => { | ||
const store = s => | ||
(subscriber => | ||
(subscribers.unshift(subscriber), i => | ||
subscribers.splice((i = subscribers.indexOf(subscriber)), !!~i)))( | ||
s(store) | ||
exports.combineEpics = function() { | ||
const epics = [].slice.call(arguments); | ||
return function() { | ||
const args = [].slice.call(arguments); | ||
return merge.apply( | ||
null, | ||
epics.map(function(epic) { | ||
return epic.apply(null, args); | ||
}) | ||
); | ||
}; | ||
}; | ||
store.getState = () => state; | ||
exports.createStore = function( | ||
reducer, | ||
state = reducer(reducer._, {}), | ||
rootEpic, | ||
options = {} | ||
) { | ||
const store$ = new BehaviorSubject(); | ||
const action$ = new Subject(); | ||
store.dispatch = action => | ||
(next => | ||
subscribers.length === 0 | ||
? action => next(action) | ||
: subscribers.reduce((a, b) => next => a(b(next)))(next))( | ||
action => (state = reducer(state, action)) | ||
)(action); | ||
scan.call(startWith.call(action$, state), reducer).subscribe(store$); | ||
return store; | ||
const getState = function() { | ||
return store$.getValue(); | ||
}; | ||
const dispatch = function(action) { | ||
action$.next(action); | ||
}; | ||
if (typeof rootEpic === 'function') { | ||
'dependencies' in options | ||
? rootEpic(action$, { getState }, options.dependencies).subscribe( | ||
dispatch | ||
) | ||
: rootEpic(action$, { getState }).subscribe(dispatch); | ||
} | ||
return { getState, dispatch }; | ||
}; |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the 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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
11
-21.43%0
-100%7287
-1.74%1
Infinity%41
-38.81%3
Infinity%50
-24.24%1
Infinity%