dispatchable-store
Advanced tools
Comparing version
@@ -24,2 +24,6 @@ 'use strict'; | ||
}; | ||
// 0, '', false, null, undefined => true | ||
var isEmpty = function isEmpty(maybeStr) { | ||
return !maybeStr; | ||
}; | ||
@@ -46,3 +50,6 @@ /** | ||
return function (callback) { | ||
_this.on('change', function (_ref) { | ||
if (!isFunc(callback)) { | ||
throw new TypeError('Expected a Function'); | ||
} | ||
var subscriber = function subscriber(_ref) { | ||
var action = _ref.action, | ||
@@ -55,3 +62,7 @@ prev = _ref.prev, | ||
if (target !== prevTarget) callback(action, prevTarget, target); | ||
}); | ||
}; | ||
if (!isEmpty(callback.name)) { | ||
_this.subscribers[callback.name] = subscriber; | ||
} | ||
_this.on('change', subscriber); | ||
}; | ||
@@ -71,2 +82,3 @@ }; | ||
_this.chains = {}; | ||
_this.subscribers = {}; | ||
return _this; | ||
@@ -171,5 +183,28 @@ } | ||
/** | ||
* @desc stop subscribe to callback function. | ||
* @param {Function} subscribed function | ||
* @example | ||
* | ||
* function callback(action, prev, state) { | ||
* console.log(state) | ||
* } | ||
* | ||
* store.subscribe(state => state)(callback) | ||
* | ||
* store.unsubscribe(callback) | ||
*/ | ||
}, { | ||
key: 'unsubscribe', | ||
value: function unsubscribe(func) { | ||
if (!isFunc(func)) return; | ||
if (isEmpty(this.subscribers[func.name])) return; | ||
this.off('change', this.subscribers[func.name]); | ||
} | ||
/** | ||
* @function | ||
* @name subscribe | ||
* @memberof DispatchableStore | ||
* @instance | ||
* @desc call the callback function if changed the state. | ||
@@ -190,2 +225,3 @@ * but, doesn't called it,if state is not modified. | ||
* @memberof DispatchableStore | ||
* @instance | ||
* @desc update state of self | ||
@@ -192,0 +228,0 @@ * @private |
{ | ||
"name": "dispatchable-store", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"main": "lib/index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"prebuild": "npm run fix", | ||
"build": "npm-run-all --parallel build:*", | ||
@@ -12,2 +13,3 @@ "build:lib": "BABEL_ENV=production babel src --out-dir lib", | ||
"fix": "prettier --write src/*.js", | ||
"test": "npm-run-all --parallel test:*", | ||
"test:ava": "nyc ava test/*.js", | ||
@@ -14,0 +16,0 @@ "test:lint": "eslint src/*.js" |
@@ -7,2 +7,4 @@ const { EventEmitter2 } = require('eventemitter2') | ||
const isObj = maybeObj => typeof maybeObj == 'object' | ||
// 0, '', false, null, undefined => true | ||
const isEmpty = maybeStr => !maybeStr | ||
@@ -24,2 +26,3 @@ /** | ||
this.chains = {} | ||
this.subscribers = {} | ||
} | ||
@@ -104,5 +107,25 @@ | ||
/** | ||
* @desc stop subscribe to callback function. | ||
* @param {Function} subscribed function | ||
* @example | ||
* | ||
* function callback(action, prev, state) { | ||
* console.log(state) | ||
* } | ||
* | ||
* store.subscribe(state => state)(callback) | ||
* | ||
* store.unsubscribe(callback) | ||
*/ | ||
unsubscribe(func) { | ||
if (!isFunc(func)) return | ||
if (isEmpty(this.subscribers[func.name])) return | ||
this.off('change', this.subscribers[func.name]) | ||
} | ||
/** | ||
* @function | ||
* @name subscribe | ||
* @memberof DispatchableStore | ||
* @instance | ||
* @desc call the callback function if changed the state. | ||
@@ -118,7 +141,14 @@ * but, doesn't called it,if state is not modified. | ||
subscribe = stateMapper => callback => { | ||
this.on('change', ({ action, prev, state }) => { | ||
if (!isFunc(callback)) { | ||
throw new TypeError('Expected a Function') | ||
} | ||
const subscriber = ({ action, prev, state }) => { | ||
const target = stateMapper(state) | ||
const prevTarget = stateMapper(prev) | ||
if (target !== prevTarget) callback(action, prevTarget, target) | ||
}) | ||
} | ||
if (!isEmpty(callback.name)) { | ||
this.subscribers[callback.name] = subscriber | ||
} | ||
this.on('change', subscriber) | ||
}; | ||
@@ -130,2 +160,3 @@ | ||
* @memberof DispatchableStore | ||
* @instance | ||
* @desc update state of self | ||
@@ -132,0 +163,0 @@ * @private |
@@ -8,3 +8,3 @@ import test from 'ava' | ||
B: 'B', | ||
C: 'C', | ||
C: 'C' | ||
} | ||
@@ -15,3 +15,3 @@ | ||
foo: 0, | ||
bar: 0, | ||
bar: 0 | ||
}) | ||
@@ -21,3 +21,3 @@ t.context.store.register({ | ||
[actions.B]: (prevState, payload) => Object.assign({}, prevState, { ...payload }), | ||
[actions.C]: (prevState, payload) => prevState, | ||
[actions.C]: (prevState, payload) => prevState | ||
}) | ||
@@ -51,2 +51,22 @@ }) | ||
test('unsubscribe', t => { | ||
const store = t.context.store | ||
const callback = (_1, _2, state) => t.is(state, 1) | ||
store.subscribe(state => state.foo)(callback) | ||
store.dispatch({ | ||
type: actions.A, | ||
payload: { | ||
foo: 1 | ||
} | ||
}) | ||
store.unsubscribe(callback) | ||
store.dispatch({ | ||
type: actions.A, | ||
payload: { | ||
foo: 2 | ||
} | ||
}) | ||
}) | ||
test('chain case', t => { | ||
@@ -60,3 +80,3 @@ const store = t.context.store | ||
t.is(action['@@chained'], false) | ||
break; | ||
break | ||
case actions.B: | ||
@@ -66,3 +86,3 @@ t.is(state.foo, 1) | ||
t.is(action['@@chained'], true) | ||
break; | ||
break | ||
} | ||
@@ -69,0 +89,0 @@ }) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
4542869
47.64%121
42.35%3709
71%