businessman
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -1,5 +0,5 @@ | ||
module.exports = function ( config ) { | ||
config.set( { | ||
module.exports = function (config) { | ||
config.set({ | ||
basePath: '../', | ||
frameworks: [ 'mocha' ], | ||
frameworks: ['mocha'], | ||
plugins: [ | ||
@@ -26,6 +26,6 @@ 'karma-mocha', | ||
}, | ||
browsers: [ 'PhantomJS' ], | ||
reporters: [ 'mocha' ], | ||
browsers: ['PhantomJS'], | ||
reporters: ['mocha'], | ||
singleRun: true | ||
} ) | ||
}) | ||
} |
@@ -1,27 +0,27 @@ | ||
const rollup = require( 'rollup' ) | ||
const npm = require( 'rollup-plugin-node-resolve' ) | ||
const buble = require( 'rollup-plugin-buble' ) | ||
const commonjs = require( 'rollup-plugin-commonjs' ) | ||
const rollup = require('rollup') | ||
const npm = require('rollup-plugin-node-resolve') | ||
const buble = require('rollup-plugin-buble') | ||
const commonjs = require('rollup-plugin-commonjs') | ||
const name = 'businessman' | ||
rollup.rollup( { | ||
rollup.rollup({ | ||
entry: 'index.js', | ||
plugins: [ | ||
npm( { jsnext: true } ), | ||
npm({jsnext: true}), | ||
buble(), | ||
commonjs() | ||
] | ||
} ) | ||
.then( bundle => { | ||
bundle.write( { format: 'es', dest: `dist/${name}.es.js` } ) | ||
bundle.write( { format: 'amd', dest: `dist/${name}.amd.js` } ) | ||
bundle.write( { | ||
}) | ||
.then(bundle => { | ||
bundle.write({format: 'es', dest: `dist/${name}.es.js`}) | ||
bundle.write({format: 'amd', dest: `dist/${name}.amd.js`}) | ||
bundle.write({ | ||
format: 'umd', | ||
dest: `dist/${name}.js`, | ||
moduleName: name | ||
} ) | ||
} ) | ||
.catch( err => { | ||
console.error( err ) | ||
} ) | ||
}) | ||
}) | ||
.catch(err => { | ||
console.error(err) | ||
}) |
@@ -1,11 +0,11 @@ | ||
const rollup = require( 'rollup' ) | ||
const npm = require( 'rollup-plugin-node-resolve' ) | ||
const buble = require( 'rollup-plugin-buble' ) | ||
const commonjs = require( 'rollup-plugin-commonjs' ) | ||
const multiEntry = require( 'rollup-plugin-multi-entry' ) | ||
const rollup = require('rollup') | ||
const npm = require('rollup-plugin-node-resolve') | ||
const buble = require('rollup-plugin-buble') | ||
const commonjs = require('rollup-plugin-commonjs') | ||
const multiEntry = require('rollup-plugin-multi-entry') | ||
rollup.rollup( { | ||
rollup.rollup({ | ||
entry: 'test/client/*.js', | ||
plugins: [ | ||
npm( { jsnext: true } ), | ||
npm({jsnext: true}), | ||
buble(), | ||
@@ -15,23 +15,23 @@ commonjs(), | ||
] | ||
} ) | ||
.then( bundle => { | ||
bundle.write( { format: 'iife', moduleName: 'businessman', dest: 'dist/test-businessman.js' } ) | ||
} ) | ||
.catch( err => { | ||
console.error( err ) | ||
} ) | ||
}) | ||
.then(bundle => { | ||
bundle.write({format: 'iife', moduleName: 'businessman', dest: 'dist/test-businessman.js'}) | ||
}) | ||
.catch(err => { | ||
console.error(err) | ||
}) | ||
rollup.rollup( { | ||
rollup.rollup({ | ||
entry: 'test/worker.js', | ||
plugins: [ | ||
npm( { jsnext: true } ), | ||
npm({jsnext: true}), | ||
buble(), | ||
commonjs() | ||
] | ||
} ) | ||
.then( bundle => { | ||
bundle.write( { format: 'iife', dest: 'dist/test-worker.js' } ) | ||
} ) | ||
.catch( err => { | ||
console.error( err ) | ||
} ) | ||
}) | ||
.then(bundle => { | ||
bundle.write({format: 'iife', dest: 'dist/test-worker.js'}) | ||
}) | ||
.catch(err => { | ||
console.error(err) | ||
}) |
@@ -7,32 +7,32 @@ define(['exports'], function (exports) { 'use strict'; | ||
register: function (name) { | ||
callbacks[ name ] = {}; | ||
callbacks[name] = {}; | ||
}, | ||
on: function ( name, type, cb ) { | ||
var list = callbacks[ name ]; | ||
if ( type in list ) { | ||
list[ type ].push( cb ); | ||
on: function (name, type, cb) { | ||
var list = callbacks[name]; | ||
if (type in list) { | ||
list[type].push(cb); | ||
} else { | ||
list[ type ] = [ cb ]; | ||
list[type] = [cb]; | ||
} | ||
}, | ||
off: function ( name, type, cb ) { | ||
var list = callbacks[ name ]; | ||
if ( cb ) { | ||
var i = list[ type ].indexOf( cb ); | ||
if ( i ) { | ||
list[ type ].splice( i, 1 ); | ||
off: function (name, type, cb) { | ||
var list = callbacks[name]; | ||
if (cb) { | ||
var i = list[type].indexOf(cb); | ||
if (i) { | ||
list[type].splice(i, 1); | ||
} | ||
} else { | ||
list[ type ] = []; | ||
list[type] = []; | ||
} | ||
}, | ||
trigger: function ( name, type ) { | ||
trigger: function (name, type) { | ||
var args = [], len = arguments.length - 2; | ||
while ( len-- > 0 ) args[ len ] = arguments[ len + 2 ]; | ||
var list = callbacks[ name ]; | ||
var cbs = list[ type ]; | ||
if ( cbs ) { | ||
for ( var i = 0; i < cbs.length; i++ ) { | ||
cbs[ i ].apply( cbs, args ); | ||
var list = callbacks[name]; | ||
var cbs = list[type]; | ||
if (cbs) { | ||
for (var i = 0; i < cbs.length; i++) { | ||
cbs[i].apply(cbs, args); | ||
} | ||
@@ -47,29 +47,29 @@ } | ||
observable.register( GETTER ); | ||
observable.register( CLIENT ); | ||
observable.register( ALLSTATE ); | ||
observable.register(GETTER); | ||
observable.register(CLIENT); | ||
observable.register(ALLSTATE); | ||
var trigger = function ( data, obs ) { | ||
var trigger = function (data, obs) { | ||
if ( obs === void 0 ) obs = CLIENT; | ||
observable.trigger( obs, data.type, data.payload, data.mutation, data.getter ); | ||
observable.trigger(obs, data.type, data.payload, data.mutation, data.getter); | ||
}; | ||
var on = function ( type, cb, obs ) { | ||
var on = function (type, cb, obs) { | ||
if ( obs === void 0 ) obs = CLIENT; | ||
observable.on( obs, type, cb ); | ||
observable.on(obs, type, cb); | ||
}; | ||
var off = function ( type, cb, obs ) { | ||
var off = function (type, cb, obs) { | ||
if ( obs === void 0 ) obs = CLIENT; | ||
if ( cb ) { | ||
observable.off( obs, type, cb ); | ||
if (cb) { | ||
observable.off(obs, type, cb); | ||
} else { | ||
observable.off( obs, type ); | ||
observable.off(obs, type); | ||
} | ||
}; | ||
var pack = function ( options ) { | ||
var pack = function (options) { | ||
var type = options.type; if ( type === void 0 ) type = null; | ||
@@ -80,14 +80,14 @@ var payload = options.payload; if ( payload === void 0 ) payload = null; | ||
var allState = options.allState; if ( allState === void 0 ) allState = null; | ||
return { type: type, payload: payload, mutation: mutation, getter: getter, allState: allState } | ||
return {type: type, payload: payload, mutation: mutation, getter: getter, allState: allState} | ||
}; | ||
var assign = function ( target, sources ) { | ||
var assign = function (target, sources) { | ||
try { | ||
return Object.assign( target, sources ) | ||
} catch ( err ) { | ||
var keys = Object.keys( sources ); | ||
for ( var i = 0; i < keys.length; i++ ) { | ||
var key = keys[ i ]; | ||
if ( !( key in target ) ) { | ||
target[ key ] = sources[ key ]; | ||
return Object.assign(target, sources) | ||
} catch (err) { | ||
var keys = Object.keys(sources); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (!(key in target)) { | ||
target[key] = sources[key]; | ||
} | ||
@@ -107,3 +107,3 @@ } | ||
var Store = function Store ( opt ) { | ||
var Store = function Store(opt) { | ||
var state = opt.state; | ||
@@ -122,12 +122,12 @@ var mutations$$1 = opt.mutations; if ( mutations$$1 === void 0 ) mutations$$1 = {}; | ||
get: function () { return state; }, | ||
set: function ( newState, mutationType, provide ) { | ||
set: function (newState, mutationType, provide) { | ||
state = newState; | ||
postMessage( ( provide ? pack( { type: type, payload: state, mutation: mutationType } ) : pack( { type: type, mutation: mutationType } ) ) ); | ||
postMessage((provide ? pack({type: type, payload: state, mutation: mutationType}) : pack({type: type, mutation: mutationType}))); | ||
} | ||
}; | ||
getters$$1 = assign( getters$$1, getters ); | ||
mutations$$1 = assign( mutations$$1, mutations ); | ||
actions$$1 = assign( actions$$1, actions ); | ||
getters$$1 = assign(getters$$1, getters); | ||
mutations$$1 = assign(mutations$$1, mutations); | ||
actions$$1 = assign(actions$$1, actions); | ||
Object.defineProperties( this, { | ||
Object.defineProperties(this, { | ||
type: { | ||
@@ -155,3 +155,3 @@ value: type, | ||
dispatch: { | ||
value: function ( type, payload ) { return dispatch.call( store, type, payload ); }, | ||
value: function (type, payload) { return dispatch.call(store, type, payload); }, | ||
configurable: false, | ||
@@ -161,6 +161,6 @@ writable: false | ||
commit: { | ||
value: function ( type, payload, provide ) { | ||
value: function (type, payload, provide) { | ||
if ( provide === void 0 ) provide = true; | ||
return commit.call( store, _state, type, payload, provide ); | ||
return commit.call(store, _state, type, payload, provide); | ||
}, | ||
@@ -171,30 +171,30 @@ configurable: false, | ||
getState: { | ||
value: function ( type, payload ) { return getState.call( store, _state, type, payload ); }, | ||
value: function (type, payload) { return getState.call(store, _state, type, payload); }, | ||
configurable: false, | ||
writable: false | ||
} | ||
} ); | ||
}); | ||
}; | ||
Store.prototype.getState = function getState ( state, type, payload ) { | ||
Store.prototype.getState = function getState (state, type, payload) { | ||
if ( type === void 0 ) type = 'default'; | ||
var get = this.getters[ type ]( state.get(), payload, this.getters ); | ||
var get = this.getters[type](state.get(), payload, this.getters); | ||
return get | ||
}; | ||
Store.prototype.commit = function commit ( state, type, payload, provide ) { | ||
state.set( this.mutations[ type ]( state.get(), payload ), type, provide ); | ||
Store.prototype.commit = function commit (state, type, payload, provide) { | ||
state.set(this.mutations[type](state.get(), payload), type, provide); | ||
}; | ||
Store.prototype.dispatch = function dispatch ( type, payload ) { | ||
this.actions[ type ]( this.commit, payload ); | ||
Store.prototype.dispatch = function dispatch (type, payload) { | ||
this.actions[type](this.commit, payload); | ||
}; | ||
var getState$2 = function ( stores, data ) { | ||
var getState$2 = function (stores, data) { | ||
var store = data[0]; | ||
var type = data[1]; | ||
var payload = data[2]; | ||
var get = stores[ store ].getState( type, payload ); | ||
postMessage( pack( { type: store, payload: get, getter: type } ) ); | ||
var get = stores[store].getState(type, payload); | ||
postMessage(pack({type: store, payload: get, getter: type})); | ||
}; | ||
@@ -209,7 +209,7 @@ | ||
var state = {}; | ||
var key = Object.keys( stores ); | ||
for ( var i = 0; i < key.length; i++ ) { | ||
state[ key[ i ] ] = stores[ key[ i ] ].getState(); | ||
var key = Object.keys(stores); | ||
for (var i = 0; i < key.length; i++) { | ||
state[key[i]] = stores[key[i]].getState(); | ||
} | ||
postMessage( pack( { type: GET_ALL_STATE, payload: state, allState: true } ) ); | ||
postMessage(pack({type: GET_ALL_STATE, payload: state, allState: true})); | ||
return state | ||
@@ -227,16 +227,16 @@ }; | ||
var data = e.data; | ||
var c = data[ 0 ]; | ||
var c = data[0]; | ||
data.shift(); | ||
switch ( c ) { | ||
switch (c) { | ||
case DISPATCH: | ||
stores[ data[ 0 ] ].dispatch( data[ 1 ], data[ 2 ] ); | ||
stores[data[0]].dispatch(data[1], data[2]); | ||
break | ||
case OPERATE: | ||
managers[ data[ 0 ] ]( stores, data[ 1 ] ); | ||
managers[data[0]](stores, data[1]); | ||
break | ||
case GET_STATE: | ||
getState$2( stores, data ); | ||
getState$2(stores, data); | ||
break | ||
case GET_ALL_STATE: | ||
getAllState$1( stores ); | ||
getAllState$1(stores); | ||
break | ||
@@ -247,9 +247,9 @@ default: | ||
}; | ||
postMessage( pack( { type: INIT, payload: { stores: Object.keys( stores ), managers: Object.keys( managers ) } } ) ); | ||
postMessage(pack({type: INIT, payload: {stores: Object.keys(stores), managers: Object.keys(managers)}})); | ||
}, | ||
registerStore: function (config) { | ||
var store = new Store( config ); | ||
var store = new Store(config); | ||
var type = store.type; | ||
if ( !( type in stores ) ) { | ||
stores[ type ] = store; | ||
if (!(type in stores)) { | ||
stores[type] = store; | ||
} | ||
@@ -260,4 +260,4 @@ }, | ||
var handler = config.handler; | ||
if ( !( type in managers ) ) { | ||
managers[ type ] = handler; | ||
if (!(type in managers)) { | ||
managers[type] = handler; | ||
} | ||
@@ -267,69 +267,69 @@ } | ||
var worker$1 = Object.freeze( worker ); | ||
var worker$1 = Object.freeze(worker); | ||
var _install = function ( path, worker ) { | ||
var _install = function (path, worker) { | ||
try { | ||
worker = new Worker( path ); | ||
worker.onmessage = function (m) { return trigger( m.data, ( m.data.allState ? ALLSTATE : m.data.getter ? GETTER : CLIENT ) ); }; | ||
worker = new Worker(path); | ||
worker.onmessage = function (m) { return trigger(m.data, (m.data.allState ? ALLSTATE : m.data.getter ? GETTER : CLIENT)); }; | ||
return worker | ||
} catch ( err ) { | ||
console.error( 'Error in install', err ); | ||
} catch (err) { | ||
console.error('Error in install', err); | ||
} | ||
}; | ||
var _dispatch = function ( storeType, actionType, payload, worker ) { | ||
worker.postMessage( [ DISPATCH, storeType, actionType, payload ] ); | ||
var _dispatch = function (storeType, actionType, payload, worker) { | ||
worker.postMessage([DISPATCH, storeType, actionType, payload]); | ||
}; | ||
var _operate = function ( managerType, payload, worker ) { | ||
worker.postMessage( [ OPERATE, managerType, payload ] ); | ||
var _operate = function (managerType, payload, worker) { | ||
worker.postMessage([OPERATE, managerType, payload]); | ||
}; | ||
var _subscribe = function ( type, cb ) { | ||
on( type, cb ); | ||
var _subscribe = function (type, cb) { | ||
on(type, cb); | ||
}; | ||
var _unsubscribe = function ( type, cb ) { | ||
off( type, cb ); | ||
var _unsubscribe = function (type, cb) { | ||
off(type, cb); | ||
}; | ||
var _getState = function ( storeType, getter, options, worker ) { | ||
var _getState = function (storeType, getter, options, worker) { | ||
if ( getter === void 0 ) getter = 'default'; | ||
return new Promise( function ( resolve, reject ) { | ||
var subscriber = function ( state, m, got ) { | ||
if ( got !== getter ) { | ||
return new Promise(function (resolve, reject) { | ||
var subscriber = function (state, m, got) { | ||
if (got !== getter) { | ||
return | ||
} | ||
off( storeType, subscriber, GETTER ); | ||
resolve( state ); | ||
off(storeType, subscriber, GETTER); | ||
resolve(state); | ||
}; | ||
on( storeType, subscriber, GETTER ); | ||
on(storeType, subscriber, GETTER); | ||
try { | ||
worker.postMessage( [ GET_STATE, storeType, getter, options ] ); | ||
} catch ( err ) { | ||
off( storeType, subscriber, GETTER ); | ||
reject( err ); | ||
worker.postMessage([GET_STATE, storeType, getter, options]); | ||
} catch (err) { | ||
off(storeType, subscriber, GETTER); | ||
reject(err); | ||
} | ||
} ) | ||
}) | ||
}; | ||
var _getAllState = function (worker) { | ||
return new Promise( function ( resolve, reject ) { | ||
return new Promise(function (resolve, reject) { | ||
var subscriber = function (state) { | ||
off( GET_ALL_STATE, subscriber, ALLSTATE ); | ||
resolve( state ); | ||
off(GET_ALL_STATE, subscriber, ALLSTATE); | ||
resolve(state); | ||
}; | ||
on( GET_ALL_STATE, subscriber, ALLSTATE ); | ||
on(GET_ALL_STATE, subscriber, ALLSTATE); | ||
try { | ||
worker.postMessage( [ GET_ALL_STATE ] ); | ||
} catch ( err ) { | ||
off( GET_ALL_STATE, subscriber, ALLSTATE ); | ||
reject( err ); | ||
worker.postMessage([GET_ALL_STATE]); | ||
} catch (err) { | ||
off(GET_ALL_STATE, subscriber, ALLSTATE); | ||
reject(err); | ||
} | ||
} ) | ||
}) | ||
}; | ||
@@ -340,10 +340,10 @@ | ||
var install = function (path) { | ||
businessmanWoker = _install( path, businessmanWoker ); | ||
businessmanWoker = _install(path, businessmanWoker); | ||
}; | ||
var dispatch$1 = function ( storeType, actionType, payload ) { return _dispatch( storeType, actionType, payload, businessmanWoker ); }; | ||
var operate = function ( managerType, payload ) { return _operate( managerType, payload, businessmanWoker ); }; | ||
var subscribe = function ( type, cb ) { return _subscribe( type, cb ); }; | ||
var unsubscribe = function ( type, cb ) { return _unsubscribe( type, cb ); }; | ||
var getState$1 = function ( storeType, getter, options ) { return _getState( storeType, getter, options, businessmanWoker ); }; | ||
var getAllState = function () { return _getAllState( businessmanWoker ); }; | ||
var dispatch$1 = function (storeType, actionType, payload) { return _dispatch(storeType, actionType, payload, businessmanWoker); }; | ||
var operate = function (managerType, payload) { return _operate(managerType, payload, businessmanWoker); }; | ||
var subscribe = function (type, cb) { return _subscribe(type, cb); }; | ||
var unsubscribe = function (type, cb) { return _unsubscribe(type, cb); }; | ||
var getState$1 = function (storeType, getter, options) { return _getState(storeType, getter, options, businessmanWoker); }; | ||
var getAllState = function () { return _getAllState(businessmanWoker); }; | ||
@@ -350,0 +350,0 @@ exports.install = install; |
@@ -5,32 +5,32 @@ var callbacks = {}; | ||
register: function (name) { | ||
callbacks[ name ] = {}; | ||
callbacks[name] = {}; | ||
}, | ||
on: function ( name, type, cb ) { | ||
var list = callbacks[ name ]; | ||
if ( type in list ) { | ||
list[ type ].push( cb ); | ||
on: function (name, type, cb) { | ||
var list = callbacks[name]; | ||
if (type in list) { | ||
list[type].push(cb); | ||
} else { | ||
list[ type ] = [ cb ]; | ||
list[type] = [cb]; | ||
} | ||
}, | ||
off: function ( name, type, cb ) { | ||
var list = callbacks[ name ]; | ||
if ( cb ) { | ||
var i = list[ type ].indexOf( cb ); | ||
if ( i ) { | ||
list[ type ].splice( i, 1 ); | ||
off: function (name, type, cb) { | ||
var list = callbacks[name]; | ||
if (cb) { | ||
var i = list[type].indexOf(cb); | ||
if (i) { | ||
list[type].splice(i, 1); | ||
} | ||
} else { | ||
list[ type ] = []; | ||
list[type] = []; | ||
} | ||
}, | ||
trigger: function ( name, type ) { | ||
trigger: function (name, type) { | ||
var args = [], len = arguments.length - 2; | ||
while ( len-- > 0 ) args[ len ] = arguments[ len + 2 ]; | ||
var list = callbacks[ name ]; | ||
var cbs = list[ type ]; | ||
if ( cbs ) { | ||
for ( var i = 0; i < cbs.length; i++ ) { | ||
cbs[ i ].apply( cbs, args ); | ||
var list = callbacks[name]; | ||
var cbs = list[type]; | ||
if (cbs) { | ||
for (var i = 0; i < cbs.length; i++) { | ||
cbs[i].apply(cbs, args); | ||
} | ||
@@ -45,29 +45,29 @@ } | ||
observable.register( GETTER ); | ||
observable.register( CLIENT ); | ||
observable.register( ALLSTATE ); | ||
observable.register(GETTER); | ||
observable.register(CLIENT); | ||
observable.register(ALLSTATE); | ||
var trigger = function ( data, obs ) { | ||
var trigger = function (data, obs) { | ||
if ( obs === void 0 ) obs = CLIENT; | ||
observable.trigger( obs, data.type, data.payload, data.mutation, data.getter ); | ||
observable.trigger(obs, data.type, data.payload, data.mutation, data.getter); | ||
}; | ||
var on = function ( type, cb, obs ) { | ||
var on = function (type, cb, obs) { | ||
if ( obs === void 0 ) obs = CLIENT; | ||
observable.on( obs, type, cb ); | ||
observable.on(obs, type, cb); | ||
}; | ||
var off = function ( type, cb, obs ) { | ||
var off = function (type, cb, obs) { | ||
if ( obs === void 0 ) obs = CLIENT; | ||
if ( cb ) { | ||
observable.off( obs, type, cb ); | ||
if (cb) { | ||
observable.off(obs, type, cb); | ||
} else { | ||
observable.off( obs, type ); | ||
observable.off(obs, type); | ||
} | ||
}; | ||
var pack = function ( options ) { | ||
var pack = function (options) { | ||
var type = options.type; if ( type === void 0 ) type = null; | ||
@@ -78,14 +78,14 @@ var payload = options.payload; if ( payload === void 0 ) payload = null; | ||
var allState = options.allState; if ( allState === void 0 ) allState = null; | ||
return { type: type, payload: payload, mutation: mutation, getter: getter, allState: allState } | ||
return {type: type, payload: payload, mutation: mutation, getter: getter, allState: allState} | ||
}; | ||
var assign = function ( target, sources ) { | ||
var assign = function (target, sources) { | ||
try { | ||
return Object.assign( target, sources ) | ||
} catch ( err ) { | ||
var keys = Object.keys( sources ); | ||
for ( var i = 0; i < keys.length; i++ ) { | ||
var key = keys[ i ]; | ||
if ( !( key in target ) ) { | ||
target[ key ] = sources[ key ]; | ||
return Object.assign(target, sources) | ||
} catch (err) { | ||
var keys = Object.keys(sources); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (!(key in target)) { | ||
target[key] = sources[key]; | ||
} | ||
@@ -105,3 +105,3 @@ } | ||
var Store = function Store ( opt ) { | ||
var Store = function Store(opt) { | ||
var state = opt.state; | ||
@@ -120,12 +120,12 @@ var mutations$$1 = opt.mutations; if ( mutations$$1 === void 0 ) mutations$$1 = {}; | ||
get: function () { return state; }, | ||
set: function ( newState, mutationType, provide ) { | ||
set: function (newState, mutationType, provide) { | ||
state = newState; | ||
postMessage( ( provide ? pack( { type: type, payload: state, mutation: mutationType } ) : pack( { type: type, mutation: mutationType } ) ) ); | ||
postMessage((provide ? pack({type: type, payload: state, mutation: mutationType}) : pack({type: type, mutation: mutationType}))); | ||
} | ||
}; | ||
getters$$1 = assign( getters$$1, getters ); | ||
mutations$$1 = assign( mutations$$1, mutations ); | ||
actions$$1 = assign( actions$$1, actions ); | ||
getters$$1 = assign(getters$$1, getters); | ||
mutations$$1 = assign(mutations$$1, mutations); | ||
actions$$1 = assign(actions$$1, actions); | ||
Object.defineProperties( this, { | ||
Object.defineProperties(this, { | ||
type: { | ||
@@ -153,3 +153,3 @@ value: type, | ||
dispatch: { | ||
value: function ( type, payload ) { return dispatch.call( store, type, payload ); }, | ||
value: function (type, payload) { return dispatch.call(store, type, payload); }, | ||
configurable: false, | ||
@@ -159,6 +159,6 @@ writable: false | ||
commit: { | ||
value: function ( type, payload, provide ) { | ||
value: function (type, payload, provide) { | ||
if ( provide === void 0 ) provide = true; | ||
return commit.call( store, _state, type, payload, provide ); | ||
return commit.call(store, _state, type, payload, provide); | ||
}, | ||
@@ -169,30 +169,30 @@ configurable: false, | ||
getState: { | ||
value: function ( type, payload ) { return getState.call( store, _state, type, payload ); }, | ||
value: function (type, payload) { return getState.call(store, _state, type, payload); }, | ||
configurable: false, | ||
writable: false | ||
} | ||
} ); | ||
}); | ||
}; | ||
Store.prototype.getState = function getState ( state, type, payload ) { | ||
Store.prototype.getState = function getState (state, type, payload) { | ||
if ( type === void 0 ) type = 'default'; | ||
var get = this.getters[ type ]( state.get(), payload, this.getters ); | ||
var get = this.getters[type](state.get(), payload, this.getters); | ||
return get | ||
}; | ||
Store.prototype.commit = function commit ( state, type, payload, provide ) { | ||
state.set( this.mutations[ type ]( state.get(), payload ), type, provide ); | ||
Store.prototype.commit = function commit (state, type, payload, provide) { | ||
state.set(this.mutations[type](state.get(), payload), type, provide); | ||
}; | ||
Store.prototype.dispatch = function dispatch ( type, payload ) { | ||
this.actions[ type ]( this.commit, payload ); | ||
Store.prototype.dispatch = function dispatch (type, payload) { | ||
this.actions[type](this.commit, payload); | ||
}; | ||
var getState$2 = function ( stores, data ) { | ||
var getState$2 = function (stores, data) { | ||
var store = data[0]; | ||
var type = data[1]; | ||
var payload = data[2]; | ||
var get = stores[ store ].getState( type, payload ); | ||
postMessage( pack( { type: store, payload: get, getter: type } ) ); | ||
var get = stores[store].getState(type, payload); | ||
postMessage(pack({type: store, payload: get, getter: type})); | ||
}; | ||
@@ -207,7 +207,7 @@ | ||
var state = {}; | ||
var key = Object.keys( stores ); | ||
for ( var i = 0; i < key.length; i++ ) { | ||
state[ key[ i ] ] = stores[ key[ i ] ].getState(); | ||
var key = Object.keys(stores); | ||
for (var i = 0; i < key.length; i++) { | ||
state[key[i]] = stores[key[i]].getState(); | ||
} | ||
postMessage( pack( { type: GET_ALL_STATE, payload: state, allState: true } ) ); | ||
postMessage(pack({type: GET_ALL_STATE, payload: state, allState: true})); | ||
return state | ||
@@ -225,16 +225,16 @@ }; | ||
var data = e.data; | ||
var c = data[ 0 ]; | ||
var c = data[0]; | ||
data.shift(); | ||
switch ( c ) { | ||
switch (c) { | ||
case DISPATCH: | ||
stores[ data[ 0 ] ].dispatch( data[ 1 ], data[ 2 ] ); | ||
stores[data[0]].dispatch(data[1], data[2]); | ||
break | ||
case OPERATE: | ||
managers[ data[ 0 ] ]( stores, data[ 1 ] ); | ||
managers[data[0]](stores, data[1]); | ||
break | ||
case GET_STATE: | ||
getState$2( stores, data ); | ||
getState$2(stores, data); | ||
break | ||
case GET_ALL_STATE: | ||
getAllState$1( stores ); | ||
getAllState$1(stores); | ||
break | ||
@@ -245,9 +245,9 @@ default: | ||
}; | ||
postMessage( pack( { type: INIT, payload: { stores: Object.keys( stores ), managers: Object.keys( managers ) } } ) ); | ||
postMessage(pack({type: INIT, payload: {stores: Object.keys(stores), managers: Object.keys(managers)}})); | ||
}, | ||
registerStore: function (config) { | ||
var store = new Store( config ); | ||
var store = new Store(config); | ||
var type = store.type; | ||
if ( !( type in stores ) ) { | ||
stores[ type ] = store; | ||
if (!(type in stores)) { | ||
stores[type] = store; | ||
} | ||
@@ -258,4 +258,4 @@ }, | ||
var handler = config.handler; | ||
if ( !( type in managers ) ) { | ||
managers[ type ] = handler; | ||
if (!(type in managers)) { | ||
managers[type] = handler; | ||
} | ||
@@ -265,69 +265,69 @@ } | ||
var worker$1 = Object.freeze( worker ); | ||
var worker$1 = Object.freeze(worker); | ||
var _install = function ( path, worker ) { | ||
var _install = function (path, worker) { | ||
try { | ||
worker = new Worker( path ); | ||
worker.onmessage = function (m) { return trigger( m.data, ( m.data.allState ? ALLSTATE : m.data.getter ? GETTER : CLIENT ) ); }; | ||
worker = new Worker(path); | ||
worker.onmessage = function (m) { return trigger(m.data, (m.data.allState ? ALLSTATE : m.data.getter ? GETTER : CLIENT)); }; | ||
return worker | ||
} catch ( err ) { | ||
console.error( 'Error in install', err ); | ||
} catch (err) { | ||
console.error('Error in install', err); | ||
} | ||
}; | ||
var _dispatch = function ( storeType, actionType, payload, worker ) { | ||
worker.postMessage( [ DISPATCH, storeType, actionType, payload ] ); | ||
var _dispatch = function (storeType, actionType, payload, worker) { | ||
worker.postMessage([DISPATCH, storeType, actionType, payload]); | ||
}; | ||
var _operate = function ( managerType, payload, worker ) { | ||
worker.postMessage( [ OPERATE, managerType, payload ] ); | ||
var _operate = function (managerType, payload, worker) { | ||
worker.postMessage([OPERATE, managerType, payload]); | ||
}; | ||
var _subscribe = function ( type, cb ) { | ||
on( type, cb ); | ||
var _subscribe = function (type, cb) { | ||
on(type, cb); | ||
}; | ||
var _unsubscribe = function ( type, cb ) { | ||
off( type, cb ); | ||
var _unsubscribe = function (type, cb) { | ||
off(type, cb); | ||
}; | ||
var _getState = function ( storeType, getter, options, worker ) { | ||
var _getState = function (storeType, getter, options, worker) { | ||
if ( getter === void 0 ) getter = 'default'; | ||
return new Promise( function ( resolve, reject ) { | ||
var subscriber = function ( state, m, got ) { | ||
if ( got !== getter ) { | ||
return new Promise(function (resolve, reject) { | ||
var subscriber = function (state, m, got) { | ||
if (got !== getter) { | ||
return | ||
} | ||
off( storeType, subscriber, GETTER ); | ||
resolve( state ); | ||
off(storeType, subscriber, GETTER); | ||
resolve(state); | ||
}; | ||
on( storeType, subscriber, GETTER ); | ||
on(storeType, subscriber, GETTER); | ||
try { | ||
worker.postMessage( [ GET_STATE, storeType, getter, options ] ); | ||
} catch ( err ) { | ||
off( storeType, subscriber, GETTER ); | ||
reject( err ); | ||
worker.postMessage([GET_STATE, storeType, getter, options]); | ||
} catch (err) { | ||
off(storeType, subscriber, GETTER); | ||
reject(err); | ||
} | ||
} ) | ||
}) | ||
}; | ||
var _getAllState = function (worker) { | ||
return new Promise( function ( resolve, reject ) { | ||
return new Promise(function (resolve, reject) { | ||
var subscriber = function (state) { | ||
off( GET_ALL_STATE, subscriber, ALLSTATE ); | ||
resolve( state ); | ||
off(GET_ALL_STATE, subscriber, ALLSTATE); | ||
resolve(state); | ||
}; | ||
on( GET_ALL_STATE, subscriber, ALLSTATE ); | ||
on(GET_ALL_STATE, subscriber, ALLSTATE); | ||
try { | ||
worker.postMessage( [ GET_ALL_STATE ] ); | ||
} catch ( err ) { | ||
off( GET_ALL_STATE, subscriber, ALLSTATE ); | ||
reject( err ); | ||
worker.postMessage([GET_ALL_STATE]); | ||
} catch (err) { | ||
off(GET_ALL_STATE, subscriber, ALLSTATE); | ||
reject(err); | ||
} | ||
} ) | ||
}) | ||
}; | ||
@@ -338,11 +338,11 @@ | ||
var install = function (path) { | ||
businessmanWoker = _install( path, businessmanWoker ); | ||
businessmanWoker = _install(path, businessmanWoker); | ||
}; | ||
var dispatch$1 = function ( storeType, actionType, payload ) { return _dispatch( storeType, actionType, payload, businessmanWoker ); }; | ||
var operate = function ( managerType, payload ) { return _operate( managerType, payload, businessmanWoker ); }; | ||
var subscribe = function ( type, cb ) { return _subscribe( type, cb ); }; | ||
var unsubscribe = function ( type, cb ) { return _unsubscribe( type, cb ); }; | ||
var getState$1 = function ( storeType, getter, options ) { return _getState( storeType, getter, options, businessmanWoker ); }; | ||
var getAllState = function () { return _getAllState( businessmanWoker ); }; | ||
var dispatch$1 = function (storeType, actionType, payload) { return _dispatch(storeType, actionType, payload, businessmanWoker); }; | ||
var operate = function (managerType, payload) { return _operate(managerType, payload, businessmanWoker); }; | ||
var subscribe = function (type, cb) { return _subscribe(type, cb); }; | ||
var unsubscribe = function (type, cb) { return _unsubscribe(type, cb); }; | ||
var getState$1 = function (storeType, getter, options) { return _getState(storeType, getter, options, businessmanWoker); }; | ||
var getAllState = function () { return _getAllState(businessmanWoker); }; | ||
export { install, dispatch$1 as dispatch, operate, subscribe, unsubscribe, getState$1 as getState, getAllState, worker$1 as worker }; |
@@ -11,32 +11,32 @@ (function (global, factory) { | ||
register: function (name) { | ||
callbacks[ name ] = {}; | ||
callbacks[name] = {}; | ||
}, | ||
on: function ( name, type, cb ) { | ||
var list = callbacks[ name ]; | ||
if ( type in list ) { | ||
list[ type ].push( cb ); | ||
on: function (name, type, cb) { | ||
var list = callbacks[name]; | ||
if (type in list) { | ||
list[type].push(cb); | ||
} else { | ||
list[ type ] = [ cb ]; | ||
list[type] = [cb]; | ||
} | ||
}, | ||
off: function ( name, type, cb ) { | ||
var list = callbacks[ name ]; | ||
if ( cb ) { | ||
var i = list[ type ].indexOf( cb ); | ||
if ( i ) { | ||
list[ type ].splice( i, 1 ); | ||
off: function (name, type, cb) { | ||
var list = callbacks[name]; | ||
if (cb) { | ||
var i = list[type].indexOf(cb); | ||
if (i) { | ||
list[type].splice(i, 1); | ||
} | ||
} else { | ||
list[ type ] = []; | ||
list[type] = []; | ||
} | ||
}, | ||
trigger: function ( name, type ) { | ||
trigger: function (name, type) { | ||
var args = [], len = arguments.length - 2; | ||
while ( len-- > 0 ) args[ len ] = arguments[ len + 2 ]; | ||
var list = callbacks[ name ]; | ||
var cbs = list[ type ]; | ||
if ( cbs ) { | ||
for ( var i = 0; i < cbs.length; i++ ) { | ||
cbs[ i ].apply( cbs, args ); | ||
var list = callbacks[name]; | ||
var cbs = list[type]; | ||
if (cbs) { | ||
for (var i = 0; i < cbs.length; i++) { | ||
cbs[i].apply(cbs, args); | ||
} | ||
@@ -51,29 +51,29 @@ } | ||
observable.register( GETTER ); | ||
observable.register( CLIENT ); | ||
observable.register( ALLSTATE ); | ||
observable.register(GETTER); | ||
observable.register(CLIENT); | ||
observable.register(ALLSTATE); | ||
var trigger = function ( data, obs ) { | ||
var trigger = function (data, obs) { | ||
if ( obs === void 0 ) obs = CLIENT; | ||
observable.trigger( obs, data.type, data.payload, data.mutation, data.getter ); | ||
observable.trigger(obs, data.type, data.payload, data.mutation, data.getter); | ||
}; | ||
var on = function ( type, cb, obs ) { | ||
var on = function (type, cb, obs) { | ||
if ( obs === void 0 ) obs = CLIENT; | ||
observable.on( obs, type, cb ); | ||
observable.on(obs, type, cb); | ||
}; | ||
var off = function ( type, cb, obs ) { | ||
var off = function (type, cb, obs) { | ||
if ( obs === void 0 ) obs = CLIENT; | ||
if ( cb ) { | ||
observable.off( obs, type, cb ); | ||
if (cb) { | ||
observable.off(obs, type, cb); | ||
} else { | ||
observable.off( obs, type ); | ||
observable.off(obs, type); | ||
} | ||
}; | ||
var pack = function ( options ) { | ||
var pack = function (options) { | ||
var type = options.type; if ( type === void 0 ) type = null; | ||
@@ -84,14 +84,14 @@ var payload = options.payload; if ( payload === void 0 ) payload = null; | ||
var allState = options.allState; if ( allState === void 0 ) allState = null; | ||
return { type: type, payload: payload, mutation: mutation, getter: getter, allState: allState } | ||
return {type: type, payload: payload, mutation: mutation, getter: getter, allState: allState} | ||
}; | ||
var assign = function ( target, sources ) { | ||
var assign = function (target, sources) { | ||
try { | ||
return Object.assign( target, sources ) | ||
} catch ( err ) { | ||
var keys = Object.keys( sources ); | ||
for ( var i = 0; i < keys.length; i++ ) { | ||
var key = keys[ i ]; | ||
if ( !( key in target ) ) { | ||
target[ key ] = sources[ key ]; | ||
return Object.assign(target, sources) | ||
} catch (err) { | ||
var keys = Object.keys(sources); | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (!(key in target)) { | ||
target[key] = sources[key]; | ||
} | ||
@@ -111,3 +111,3 @@ } | ||
var Store = function Store ( opt ) { | ||
var Store = function Store(opt) { | ||
var state = opt.state; | ||
@@ -126,12 +126,12 @@ var mutations$$1 = opt.mutations; if ( mutations$$1 === void 0 ) mutations$$1 = {}; | ||
get: function () { return state; }, | ||
set: function ( newState, mutationType, provide ) { | ||
set: function (newState, mutationType, provide) { | ||
state = newState; | ||
postMessage( ( provide ? pack( { type: type, payload: state, mutation: mutationType } ) : pack( { type: type, mutation: mutationType } ) ) ); | ||
postMessage((provide ? pack({type: type, payload: state, mutation: mutationType}) : pack({type: type, mutation: mutationType}))); | ||
} | ||
}; | ||
getters$$1 = assign( getters$$1, getters ); | ||
mutations$$1 = assign( mutations$$1, mutations ); | ||
actions$$1 = assign( actions$$1, actions ); | ||
getters$$1 = assign(getters$$1, getters); | ||
mutations$$1 = assign(mutations$$1, mutations); | ||
actions$$1 = assign(actions$$1, actions); | ||
Object.defineProperties( this, { | ||
Object.defineProperties(this, { | ||
type: { | ||
@@ -159,3 +159,3 @@ value: type, | ||
dispatch: { | ||
value: function ( type, payload ) { return dispatch.call( store, type, payload ); }, | ||
value: function (type, payload) { return dispatch.call(store, type, payload); }, | ||
configurable: false, | ||
@@ -165,6 +165,6 @@ writable: false | ||
commit: { | ||
value: function ( type, payload, provide ) { | ||
value: function (type, payload, provide) { | ||
if ( provide === void 0 ) provide = true; | ||
return commit.call( store, _state, type, payload, provide ); | ||
return commit.call(store, _state, type, payload, provide); | ||
}, | ||
@@ -175,30 +175,30 @@ configurable: false, | ||
getState: { | ||
value: function ( type, payload ) { return getState.call( store, _state, type, payload ); }, | ||
value: function (type, payload) { return getState.call(store, _state, type, payload); }, | ||
configurable: false, | ||
writable: false | ||
} | ||
} ); | ||
}); | ||
}; | ||
Store.prototype.getState = function getState ( state, type, payload ) { | ||
Store.prototype.getState = function getState (state, type, payload) { | ||
if ( type === void 0 ) type = 'default'; | ||
var get = this.getters[ type ]( state.get(), payload, this.getters ); | ||
var get = this.getters[type](state.get(), payload, this.getters); | ||
return get | ||
}; | ||
Store.prototype.commit = function commit ( state, type, payload, provide ) { | ||
state.set( this.mutations[ type ]( state.get(), payload ), type, provide ); | ||
Store.prototype.commit = function commit (state, type, payload, provide) { | ||
state.set(this.mutations[type](state.get(), payload), type, provide); | ||
}; | ||
Store.prototype.dispatch = function dispatch ( type, payload ) { | ||
this.actions[ type ]( this.commit, payload ); | ||
Store.prototype.dispatch = function dispatch (type, payload) { | ||
this.actions[type](this.commit, payload); | ||
}; | ||
var getState$2 = function ( stores, data ) { | ||
var getState$2 = function (stores, data) { | ||
var store = data[0]; | ||
var type = data[1]; | ||
var payload = data[2]; | ||
var get = stores[ store ].getState( type, payload ); | ||
postMessage( pack( { type: store, payload: get, getter: type } ) ); | ||
var get = stores[store].getState(type, payload); | ||
postMessage(pack({type: store, payload: get, getter: type})); | ||
}; | ||
@@ -213,7 +213,7 @@ | ||
var state = {}; | ||
var key = Object.keys( stores ); | ||
for ( var i = 0; i < key.length; i++ ) { | ||
state[ key[ i ] ] = stores[ key[ i ] ].getState(); | ||
var key = Object.keys(stores); | ||
for (var i = 0; i < key.length; i++) { | ||
state[key[i]] = stores[key[i]].getState(); | ||
} | ||
postMessage( pack( { type: GET_ALL_STATE, payload: state, allState: true } ) ); | ||
postMessage(pack({type: GET_ALL_STATE, payload: state, allState: true})); | ||
return state | ||
@@ -231,16 +231,16 @@ }; | ||
var data = e.data; | ||
var c = data[ 0 ]; | ||
var c = data[0]; | ||
data.shift(); | ||
switch ( c ) { | ||
switch (c) { | ||
case DISPATCH: | ||
stores[ data[ 0 ] ].dispatch( data[ 1 ], data[ 2 ] ); | ||
stores[data[0]].dispatch(data[1], data[2]); | ||
break | ||
case OPERATE: | ||
managers[ data[ 0 ] ]( stores, data[ 1 ] ); | ||
managers[data[0]](stores, data[1]); | ||
break | ||
case GET_STATE: | ||
getState$2( stores, data ); | ||
getState$2(stores, data); | ||
break | ||
case GET_ALL_STATE: | ||
getAllState$1( stores ); | ||
getAllState$1(stores); | ||
break | ||
@@ -251,9 +251,9 @@ default: | ||
}; | ||
postMessage( pack( { type: INIT, payload: { stores: Object.keys( stores ), managers: Object.keys( managers ) } } ) ); | ||
postMessage(pack({type: INIT, payload: {stores: Object.keys(stores), managers: Object.keys(managers)}})); | ||
}, | ||
registerStore: function (config) { | ||
var store = new Store( config ); | ||
var store = new Store(config); | ||
var type = store.type; | ||
if ( !( type in stores ) ) { | ||
stores[ type ] = store; | ||
if (!(type in stores)) { | ||
stores[type] = store; | ||
} | ||
@@ -264,4 +264,4 @@ }, | ||
var handler = config.handler; | ||
if ( !( type in managers ) ) { | ||
managers[ type ] = handler; | ||
if (!(type in managers)) { | ||
managers[type] = handler; | ||
} | ||
@@ -271,69 +271,69 @@ } | ||
var worker$1 = Object.freeze( worker ); | ||
var worker$1 = Object.freeze(worker); | ||
var _install = function ( path, worker ) { | ||
var _install = function (path, worker) { | ||
try { | ||
worker = new Worker( path ); | ||
worker.onmessage = function (m) { return trigger( m.data, ( m.data.allState ? ALLSTATE : m.data.getter ? GETTER : CLIENT ) ); }; | ||
worker = new Worker(path); | ||
worker.onmessage = function (m) { return trigger(m.data, (m.data.allState ? ALLSTATE : m.data.getter ? GETTER : CLIENT)); }; | ||
return worker | ||
} catch ( err ) { | ||
console.error( 'Error in install', err ); | ||
} catch (err) { | ||
console.error('Error in install', err); | ||
} | ||
}; | ||
var _dispatch = function ( storeType, actionType, payload, worker ) { | ||
worker.postMessage( [ DISPATCH, storeType, actionType, payload ] ); | ||
var _dispatch = function (storeType, actionType, payload, worker) { | ||
worker.postMessage([DISPATCH, storeType, actionType, payload]); | ||
}; | ||
var _operate = function ( managerType, payload, worker ) { | ||
worker.postMessage( [ OPERATE, managerType, payload ] ); | ||
var _operate = function (managerType, payload, worker) { | ||
worker.postMessage([OPERATE, managerType, payload]); | ||
}; | ||
var _subscribe = function ( type, cb ) { | ||
on( type, cb ); | ||
var _subscribe = function (type, cb) { | ||
on(type, cb); | ||
}; | ||
var _unsubscribe = function ( type, cb ) { | ||
off( type, cb ); | ||
var _unsubscribe = function (type, cb) { | ||
off(type, cb); | ||
}; | ||
var _getState = function ( storeType, getter, options, worker ) { | ||
var _getState = function (storeType, getter, options, worker) { | ||
if ( getter === void 0 ) getter = 'default'; | ||
return new Promise( function ( resolve, reject ) { | ||
var subscriber = function ( state, m, got ) { | ||
if ( got !== getter ) { | ||
return new Promise(function (resolve, reject) { | ||
var subscriber = function (state, m, got) { | ||
if (got !== getter) { | ||
return | ||
} | ||
off( storeType, subscriber, GETTER ); | ||
resolve( state ); | ||
off(storeType, subscriber, GETTER); | ||
resolve(state); | ||
}; | ||
on( storeType, subscriber, GETTER ); | ||
on(storeType, subscriber, GETTER); | ||
try { | ||
worker.postMessage( [ GET_STATE, storeType, getter, options ] ); | ||
} catch ( err ) { | ||
off( storeType, subscriber, GETTER ); | ||
reject( err ); | ||
worker.postMessage([GET_STATE, storeType, getter, options]); | ||
} catch (err) { | ||
off(storeType, subscriber, GETTER); | ||
reject(err); | ||
} | ||
} ) | ||
}) | ||
}; | ||
var _getAllState = function (worker) { | ||
return new Promise( function ( resolve, reject ) { | ||
return new Promise(function (resolve, reject) { | ||
var subscriber = function (state) { | ||
off( GET_ALL_STATE, subscriber, ALLSTATE ); | ||
resolve( state ); | ||
off(GET_ALL_STATE, subscriber, ALLSTATE); | ||
resolve(state); | ||
}; | ||
on( GET_ALL_STATE, subscriber, ALLSTATE ); | ||
on(GET_ALL_STATE, subscriber, ALLSTATE); | ||
try { | ||
worker.postMessage( [ GET_ALL_STATE ] ); | ||
} catch ( err ) { | ||
off( GET_ALL_STATE, subscriber, ALLSTATE ); | ||
reject( err ); | ||
worker.postMessage([GET_ALL_STATE]); | ||
} catch (err) { | ||
off(GET_ALL_STATE, subscriber, ALLSTATE); | ||
reject(err); | ||
} | ||
} ) | ||
}) | ||
}; | ||
@@ -344,10 +344,10 @@ | ||
var install = function (path) { | ||
businessmanWoker = _install( path, businessmanWoker ); | ||
businessmanWoker = _install(path, businessmanWoker); | ||
}; | ||
var dispatch$1 = function ( storeType, actionType, payload ) { return _dispatch( storeType, actionType, payload, businessmanWoker ); }; | ||
var operate = function ( managerType, payload ) { return _operate( managerType, payload, businessmanWoker ); }; | ||
var subscribe = function ( type, cb ) { return _subscribe( type, cb ); }; | ||
var unsubscribe = function ( type, cb ) { return _unsubscribe( type, cb ); }; | ||
var getState$1 = function ( storeType, getter, options ) { return _getState( storeType, getter, options, businessmanWoker ); }; | ||
var getAllState = function () { return _getAllState( businessmanWoker ); }; | ||
var dispatch$1 = function (storeType, actionType, payload) { return _dispatch(storeType, actionType, payload, businessmanWoker); }; | ||
var operate = function (managerType, payload) { return _operate(managerType, payload, businessmanWoker); }; | ||
var subscribe = function (type, cb) { return _subscribe(type, cb); }; | ||
var unsubscribe = function (type, cb) { return _unsubscribe(type, cb); }; | ||
var getState$1 = function (storeType, getter, options) { return _getState(storeType, getter, options, businessmanWoker); }; | ||
var getAllState = function () { return _getAllState(businessmanWoker); }; | ||
@@ -354,0 +354,0 @@ exports.install = install; |
{ | ||
"name": "businessman", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Powerful, Secure, Multi-threaded Flux Patterns.", | ||
@@ -12,3 +12,4 @@ "main": "dist/businessman.js", | ||
"build": "node config/rollup.build.js", | ||
"prebuild": "xo" | ||
"prebuild": "xo", | ||
"lint": "xo --fix" | ||
}, | ||
@@ -46,25 +47,2 @@ "repository": { | ||
], | ||
"rules": { | ||
"array-bracket-spacing": [ | ||
2, | ||
"always" | ||
], | ||
"object-curly-spacing": [ | ||
2, | ||
"always" | ||
], | ||
"computed-property-spacing": [ | ||
2, | ||
"always" | ||
], | ||
"space-before-function-paren": [ | ||
2, | ||
"always" | ||
], | ||
"space-in-parens": [ | ||
2, | ||
"always" | ||
], | ||
"space-before-blocks": 2 | ||
}, | ||
"overrides": [ | ||
@@ -71,0 +49,0 @@ { |
@@ -13,10 +13,10 @@ import worker from './worker' | ||
const install = path => { | ||
businessmanWoker = _install( path, businessmanWoker ) | ||
businessmanWoker = _install(path, businessmanWoker) | ||
} | ||
const dispatch = ( storeType, actionType, payload ) => _dispatch( storeType, actionType, payload, businessmanWoker ) | ||
const operate = ( managerType, payload ) => _operate( managerType, payload, businessmanWoker ) | ||
const subscribe = ( type, cb ) => _subscribe( type, cb ) | ||
const unsubscribe = ( type, cb ) => _unsubscribe( type, cb ) | ||
const getState = ( storeType, getter, options ) => _getState( storeType, getter, options, businessmanWoker ) | ||
const getAllState = () => _getAllState( businessmanWoker ) | ||
const dispatch = (storeType, actionType, payload) => _dispatch(storeType, actionType, payload, businessmanWoker) | ||
const operate = (managerType, payload) => _operate(managerType, payload, businessmanWoker) | ||
const subscribe = (type, cb) => _subscribe(type, cb) | ||
const unsubscribe = (type, cb) => _unsubscribe(type, cb) | ||
const getState = (storeType, getter, options) => _getState(storeType, getter, options, businessmanWoker) | ||
const getAllState = () => _getAllState(businessmanWoker) | ||
@@ -23,0 +23,0 @@ export { |
@@ -1,5 +0,5 @@ | ||
import { DISPATCH } from './types/api' | ||
import {DISPATCH} from './types/api' | ||
export default ( storeType, actionType, payload, worker ) => { | ||
worker.postMessage( [ DISPATCH, storeType, actionType, payload ] ) | ||
export default (storeType, actionType, payload, worker) => { | ||
worker.postMessage([DISPATCH, storeType, actionType, payload]) | ||
} |
@@ -1,21 +0,21 @@ | ||
import { on, off } from './util' | ||
import { ALLSTATE } from './types/observer' | ||
import { GET_ALL_STATE } from './types/api' | ||
import {on, off} from './util' | ||
import {ALLSTATE} from './types/observer' | ||
import {GET_ALL_STATE} from './types/api' | ||
export default worker => { | ||
return new Promise( ( resolve, reject ) => { | ||
return new Promise((resolve, reject) => { | ||
const subscriber = state => { | ||
off( GET_ALL_STATE, subscriber, ALLSTATE ) | ||
resolve( state ) | ||
off(GET_ALL_STATE, subscriber, ALLSTATE) | ||
resolve(state) | ||
} | ||
on( GET_ALL_STATE, subscriber, ALLSTATE ) | ||
on(GET_ALL_STATE, subscriber, ALLSTATE) | ||
try { | ||
worker.postMessage( [ GET_ALL_STATE ] ) | ||
} catch ( err ) { | ||
off( GET_ALL_STATE, subscriber, ALLSTATE ) | ||
reject( err ) | ||
worker.postMessage([GET_ALL_STATE]) | ||
} catch (err) { | ||
off(GET_ALL_STATE, subscriber, ALLSTATE) | ||
reject(err) | ||
} | ||
} ) | ||
}) | ||
} |
@@ -1,24 +0,24 @@ | ||
import { on, off } from './util' | ||
import { GETTER } from './types/observer' | ||
import { GET_STATE } from './types/api' | ||
import {on, off} from './util' | ||
import {GETTER} from './types/observer' | ||
import {GET_STATE} from './types/api' | ||
export default ( storeType, getter = 'default', options, worker ) => { | ||
return new Promise( ( resolve, reject ) => { | ||
let subscriber = ( state, m, got ) => { | ||
if ( got !== getter ) { | ||
export default (storeType, getter = 'default', options, worker) => { | ||
return new Promise((resolve, reject) => { | ||
let subscriber = (state, m, got) => { | ||
if (got !== getter) { | ||
return | ||
} | ||
off( storeType, subscriber, GETTER ) | ||
resolve( state ) | ||
off(storeType, subscriber, GETTER) | ||
resolve(state) | ||
} | ||
on( storeType, subscriber, GETTER ) | ||
on(storeType, subscriber, GETTER) | ||
try { | ||
worker.postMessage( [ GET_STATE, storeType, getter, options ] ) | ||
} catch ( err ) { | ||
off( storeType, subscriber, GETTER ) | ||
reject( err ) | ||
worker.postMessage([GET_STATE, storeType, getter, options]) | ||
} catch (err) { | ||
off(storeType, subscriber, GETTER) | ||
reject(err) | ||
} | ||
} ) | ||
}) | ||
} |
@@ -1,12 +0,12 @@ | ||
import { trigger } from './util' | ||
import { GETTER, CLIENT, ALLSTATE } from './types/observer' | ||
import {trigger} from './util' | ||
import {GETTER, CLIENT, ALLSTATE} from './types/observer' | ||
export default ( path, worker ) => { | ||
export default (path, worker) => { | ||
try { | ||
worker = new Worker( path ) | ||
worker.onmessage = m => trigger( m.data, ( m.data.allState ? ALLSTATE : m.data.getter ? GETTER : CLIENT ) ) | ||
worker = new Worker(path) | ||
worker.onmessage = m => trigger(m.data, (m.data.allState ? ALLSTATE : m.data.getter ? GETTER : CLIENT)) | ||
return worker | ||
} catch ( err ) { | ||
console.error( 'Error in install', err ) | ||
} catch (err) { | ||
console.error('Error in install', err) | ||
} | ||
} |
@@ -5,29 +5,29 @@ let callbacks = {} | ||
register: name => { | ||
callbacks[ name ] = {} | ||
callbacks[name] = {} | ||
}, | ||
on: ( name, type, cb ) => { | ||
const list = callbacks[ name ] | ||
if ( type in list ) { | ||
list[ type ].push( cb ) | ||
on: (name, type, cb) => { | ||
const list = callbacks[name] | ||
if (type in list) { | ||
list[type].push(cb) | ||
} else { | ||
list[ type ] = [ cb ] | ||
list[type] = [cb] | ||
} | ||
}, | ||
off: ( name, type, cb ) => { | ||
const list = callbacks[ name ] | ||
if ( cb ) { | ||
const i = list[ type ].indexOf( cb ) | ||
if ( i ) { | ||
list[ type ].splice( i, 1 ) | ||
off: (name, type, cb) => { | ||
const list = callbacks[name] | ||
if (cb) { | ||
const i = list[type].indexOf(cb) | ||
if (i) { | ||
list[type].splice(i, 1) | ||
} | ||
} else { | ||
list[ type ] = [] | ||
list[type] = [] | ||
} | ||
}, | ||
trigger: ( name, type, ...args ) => { | ||
const list = callbacks[ name ] | ||
const cbs = list[ type ] | ||
if ( cbs ) { | ||
for ( let i = 0; i < cbs.length; i++ ) { | ||
cbs[ i ]( ...args ) | ||
trigger: (name, type, ...args) => { | ||
const list = callbacks[name] | ||
const cbs = list[type] | ||
if (cbs) { | ||
for (let i = 0; i < cbs.length; i++) { | ||
cbs[i](...args) | ||
} | ||
@@ -34,0 +34,0 @@ } |
@@ -1,5 +0,5 @@ | ||
import { OPERATE } from './types/api' | ||
import {OPERATE} from './types/api' | ||
export default ( managerType, payload, worker ) => { | ||
worker.postMessage( [ OPERATE, managerType, payload ] ) | ||
export default (managerType, payload, worker) => { | ||
worker.postMessage([OPERATE, managerType, payload]) | ||
} |
@@ -1,2 +0,2 @@ | ||
import { pack, assign } from '../util' | ||
import {pack, assign} from '../util' | ||
import builtInGetters from './getters' | ||
@@ -7,3 +7,3 @@ import builtInMutations from './mutations' | ||
class Store { | ||
constructor ( opt ) { | ||
constructor(opt) { | ||
let { | ||
@@ -19,16 +19,16 @@ state, | ||
const store = this | ||
const { dispatch, commit, getState } = this | ||
const {dispatch, commit, getState} = this | ||
let _state = { | ||
get: () => state, | ||
set: ( newState, mutationType, provide ) => { | ||
set: (newState, mutationType, provide) => { | ||
state = newState | ||
postMessage( ( provide ? pack( { type: type, payload: state, mutation: mutationType } ) : pack( { type: type, mutation: mutationType } ) ) ) | ||
postMessage((provide ? pack({type: type, payload: state, mutation: mutationType}) : pack({type: type, mutation: mutationType}))) | ||
} | ||
} | ||
getters = assign( getters, builtInGetters ) | ||
mutations = assign( mutations, builtInMutations ) | ||
actions = assign( actions, builtInActions ) | ||
getters = assign(getters, builtInGetters) | ||
mutations = assign(mutations, builtInMutations) | ||
actions = assign(actions, builtInActions) | ||
Object.defineProperties( this, { | ||
Object.defineProperties(this, { | ||
type: { | ||
@@ -56,3 +56,3 @@ value: type, | ||
dispatch: { | ||
value: ( type, payload ) => dispatch.call( store, type, payload ), | ||
value: (type, payload) => dispatch.call(store, type, payload), | ||
configurable: false, | ||
@@ -62,3 +62,3 @@ writable: false | ||
commit: { | ||
value: ( type, payload, provide = true ) => commit.call( store, _state, type, payload, provide ), | ||
value: (type, payload, provide = true) => commit.call(store, _state, type, payload, provide), | ||
configurable: false, | ||
@@ -68,20 +68,20 @@ writable: false | ||
getState: { | ||
value: ( type, payload ) => getState.call( store, _state, type, payload ), | ||
value: (type, payload) => getState.call(store, _state, type, payload), | ||
configurable: false, | ||
writable: false | ||
} | ||
} ) | ||
}) | ||
} | ||
getState ( state, type = 'default', payload ) { | ||
const get = this.getters[ type ]( state.get(), payload, this.getters ) | ||
getState(state, type = 'default', payload) { | ||
const get = this.getters[type](state.get(), payload, this.getters) | ||
return get | ||
} | ||
commit ( state, type, payload, provide ) { | ||
state.set( this.mutations[ type ]( state.get(), payload ), type, provide ) | ||
commit(state, type, payload, provide) { | ||
state.set(this.mutations[type](state.get(), payload), type, provide) | ||
} | ||
dispatch ( type, payload ) { | ||
this.actions[ type ]( this.commit, payload ) | ||
dispatch(type, payload) { | ||
this.actions[type](this.commit, payload) | ||
} | ||
@@ -88,0 +88,0 @@ |
@@ -1,5 +0,5 @@ | ||
import { on } from './util' | ||
import {on} from './util' | ||
export default ( type, cb ) => { | ||
on( type, cb ) | ||
export default (type, cb) => { | ||
on(type, cb) | ||
} |
@@ -1,5 +0,5 @@ | ||
import { off } from './util' | ||
import {off} from './util' | ||
export default ( type, cb ) => { | ||
off( type, cb ) | ||
export default (type, cb) => { | ||
off(type, cb) | ||
} |
import observer from './observer' | ||
import { GETTER, CLIENT, ALLSTATE } from './types/observer' | ||
import {GETTER, CLIENT, ALLSTATE} from './types/observer' | ||
observer.register( GETTER ) | ||
observer.register( CLIENT ) | ||
observer.register( ALLSTATE ) | ||
observer.register(GETTER) | ||
observer.register(CLIENT) | ||
observer.register(ALLSTATE) | ||
export const trigger = function ( data, obs = CLIENT ) { | ||
observer.trigger( obs, data.type, data.payload, data.mutation, data.getter ) | ||
export const trigger = function (data, obs = CLIENT) { | ||
observer.trigger(obs, data.type, data.payload, data.mutation, data.getter) | ||
} | ||
export const on = function ( type, cb, obs = CLIENT ) { | ||
observer.on( obs, type, cb ) | ||
export const on = function (type, cb, obs = CLIENT) { | ||
observer.on(obs, type, cb) | ||
} | ||
export const off = function ( type, cb, obs = CLIENT ) { | ||
if ( cb ) { | ||
observer.off( obs, type, cb ) | ||
export const off = function (type, cb, obs = CLIENT) { | ||
if (cb) { | ||
observer.off(obs, type, cb) | ||
} else { | ||
observer.off( obs, type ) | ||
observer.off(obs, type) | ||
} | ||
} | ||
export const pack = function ( options ) { | ||
export const pack = function (options) { | ||
const { | ||
@@ -32,14 +32,14 @@ type = null, | ||
} = options | ||
return { type: type, payload: payload, mutation: mutation, getter: getter, allState: allState } | ||
return {type: type, payload: payload, mutation: mutation, getter: getter, allState: allState} | ||
} | ||
export const assign = function ( target, sources ) { | ||
export const assign = function (target, sources) { | ||
try { | ||
return Object.assign( target, sources ) | ||
} catch ( err ) { | ||
let keys = Object.keys( sources ) | ||
for ( let i = 0; i < keys.length; i++ ) { | ||
let key = keys[ i ] | ||
if ( !( key in target ) ) { | ||
target[ key ] = sources[ key ] | ||
return Object.assign(target, sources) | ||
} catch (err) { | ||
let keys = Object.keys(sources) | ||
for (let i = 0; i < keys.length; i++) { | ||
let key = keys[i] | ||
if (!(key in target)) { | ||
target[key] = sources[key] | ||
} | ||
@@ -46,0 +46,0 @@ } |
@@ -1,12 +0,12 @@ | ||
import { pack } from './util' | ||
import { GET_ALL_STATE } from './types/api' | ||
import {pack} from './util' | ||
import {GET_ALL_STATE} from './types/api' | ||
export default stores => { | ||
const state = {} | ||
const key = Object.keys( stores ) | ||
for ( let i = 0; i < key.length; i++ ) { | ||
state[ key[ i ] ] = stores[ key[ i ] ].getState() | ||
const key = Object.keys(stores) | ||
for (let i = 0; i < key.length; i++) { | ||
state[key[i]] = stores[key[i]].getState() | ||
} | ||
postMessage( pack( { type: GET_ALL_STATE, payload: state, allState: true } ) ) | ||
postMessage(pack({type: GET_ALL_STATE, payload: state, allState: true})) | ||
return state | ||
} |
@@ -1,4 +0,4 @@ | ||
import { pack } from './util' | ||
import {pack} from './util' | ||
export default ( stores, data ) => { | ||
export default (stores, data) => { | ||
const [ | ||
@@ -9,4 +9,4 @@ store, | ||
] = data | ||
const get = stores[ store ].getState( type, payload ) | ||
postMessage( pack( { type: store, payload: get, getter: type } ) ) | ||
const get = stores[store].getState(type, payload) | ||
postMessage(pack({type: store, payload: get, getter: type})) | ||
} |
import Store from './store/store' | ||
import getState from './worker-get-state' | ||
import getAllState from './worker-get-all-state' | ||
import { pack } from './util' | ||
import {pack} from './util' | ||
import INIT from './types/built-in' | ||
import { DISPATCH, OPERATE, GET_STATE, GET_ALL_STATE } from './types/api' | ||
import {DISPATCH, OPERATE, GET_STATE, GET_ALL_STATE} from './types/api' | ||
@@ -15,16 +15,16 @@ let stores = {} | ||
const data = e.data | ||
const c = data[ 0 ] | ||
const c = data[0] | ||
data.shift() | ||
switch ( c ) { | ||
switch (c) { | ||
case DISPATCH: | ||
stores[ data[ 0 ] ].dispatch( data[ 1 ], data[ 2 ] ) | ||
stores[data[0]].dispatch(data[1], data[2]) | ||
break | ||
case OPERATE: | ||
managers[ data[ 0 ] ]( stores, data[ 1 ] ) | ||
managers[data[0]](stores, data[1]) | ||
break | ||
case GET_STATE: | ||
getState( stores, data ) | ||
getState(stores, data) | ||
break | ||
case GET_ALL_STATE: | ||
getAllState( stores ) | ||
getAllState(stores) | ||
break | ||
@@ -35,11 +35,11 @@ default: | ||
} | ||
postMessage( pack( { type: INIT, payload: { stores: Object.keys( stores ), managers: Object.keys( managers ) } } ) ) | ||
postMessage(pack({type: INIT, payload: {stores: Object.keys(stores), managers: Object.keys(managers)}})) | ||
}, | ||
registerStore: config => { | ||
const store = new Store( config ) | ||
const store = new Store(config) | ||
const { | ||
type | ||
} = store | ||
if ( !( type in stores ) ) { | ||
stores[ type ] = store | ||
if (!(type in stores)) { | ||
stores[type] = store | ||
} | ||
@@ -52,4 +52,4 @@ }, | ||
} = config | ||
if ( !( type in managers ) ) { | ||
managers[ type ] = handler | ||
if (!(type in managers)) { | ||
managers[type] = handler | ||
} | ||
@@ -59,2 +59,2 @@ } | ||
export default Object.freeze( worker ) | ||
export default Object.freeze(worker) |
@@ -1,19 +0,19 @@ | ||
import { install, dispatch, operate, subscribe, unsubscribe, getState, getAllState } from '../../index' | ||
import { time, timeEnd, timeAverage, reset } from '../time' | ||
import {install, dispatch, operate, subscribe, unsubscribe, getState, getAllState} from '../../index' | ||
import {time, timeEnd, timeAverage, reset} from '../time' | ||
describe( 'Businessman Specs', () => { | ||
describe('Businessman Specs', () => { | ||
let initDatas | ||
let initialize = false | ||
before( () => { | ||
before(() => { | ||
reset() | ||
} ) | ||
}) | ||
beforeEach( done => { | ||
beforeEach(done => { | ||
let i = 0 | ||
const counterSubscriber = state => { | ||
console.log( 'counter', timeEnd( 'counter' ) ) | ||
console.log('counter', timeEnd('counter')) | ||
i++ | ||
unsubscribe( 'counter' ) | ||
if ( state === 0 && i === 2 ) { | ||
unsubscribe('counter') | ||
if (state === 0 && i === 2) { | ||
done() | ||
@@ -23,75 +23,75 @@ } | ||
const messageSubscriber = state => { | ||
console.log( 'message', timeEnd( 'message' ) ) | ||
console.log('message', timeEnd('message')) | ||
i++ | ||
unsubscribe( 'message' ) | ||
if ( state === '' && i === 2 ) { | ||
unsubscribe('message') | ||
if (state === '' && i === 2) { | ||
done() | ||
} | ||
} | ||
if ( initialize ) { | ||
subscribe( 'counter', counterSubscriber ) | ||
time( 'counter' ) | ||
dispatch( 'counter', 'set', 0 ) | ||
subscribe( 'message', messageSubscriber ) | ||
time( 'message' ) | ||
dispatch( 'message', 'set', '' ) | ||
if (initialize) { | ||
subscribe('counter', counterSubscriber) | ||
time('counter') | ||
dispatch('counter', 'set', 0) | ||
subscribe('message', messageSubscriber) | ||
time('message') | ||
dispatch('message', 'set', '') | ||
} else { | ||
done() | ||
} | ||
} ) | ||
}) | ||
afterEach( () => { | ||
if ( initialize ) { | ||
unsubscribe( 'counter' ) | ||
unsubscribe( 'message' ) | ||
afterEach(() => { | ||
if (initialize) { | ||
unsubscribe('counter') | ||
unsubscribe('message') | ||
} | ||
} ) | ||
}) | ||
after( () => { | ||
console.log( 'Average', timeAverage() ) | ||
} ) | ||
after(() => { | ||
console.log('Average', timeAverage()) | ||
}) | ||
it( 'Install Worker', done => { | ||
subscribe( 'INIT', data => { | ||
it('Install Worker', done => { | ||
subscribe('INIT', data => { | ||
initDatas = data | ||
initialize = true | ||
expect( data ).to.be.ok() | ||
expect(data).to.be.ok() | ||
done() | ||
} ) | ||
install( '/dist/test-worker.js' ) | ||
} ) | ||
}) | ||
install('/dist/test-worker.js') | ||
}) | ||
it( 'INIT data has store and managers', () => { | ||
expect( initDatas ).to.be.ok() | ||
expect( initDatas.stores[ 0 ] ).to.be( 'counter' ) | ||
expect( initDatas.stores[ 1 ] ).to.be( 'message' ) | ||
expect( initDatas.stores[ 2 ] ).not.to.be.ok() | ||
expect( initDatas.managers[ 0 ] ).to.be( 'countUpMessage' ) | ||
expect( initDatas.managers[ 1 ] ).not.to.be.ok() | ||
} ) | ||
it('INIT data has store and managers', () => { | ||
expect(initDatas).to.be.ok() | ||
expect(initDatas.stores[0]).to.be('counter') | ||
expect(initDatas.stores[1]).to.be('message') | ||
expect(initDatas.stores[2]).not.to.be.ok() | ||
expect(initDatas.managers[0]).to.be('countUpMessage') | ||
expect(initDatas.managers[1]).not.to.be.ok() | ||
}) | ||
it( 'Dispatch and subscribe', done => { | ||
dispatch( 'counter', 'increment', 1 ) | ||
subscribe( 'counter', ( state, applied ) => { | ||
expect( state ).to.be( 1 ) | ||
expect( applied ).to.be( 'increment' ) | ||
} ) | ||
dispatch( 'message', 'set', 'This is a test' ) | ||
subscribe( 'message', ( state, applied ) => { | ||
expect( state ).to.be( 'This is a test' ) | ||
expect( applied ).to.be( 'set' ) | ||
it('Dispatch and subscribe', done => { | ||
dispatch('counter', 'increment', 1) | ||
subscribe('counter', (state, applied) => { | ||
expect(state).to.be(1) | ||
expect(applied).to.be('increment') | ||
}) | ||
dispatch('message', 'set', 'This is a test') | ||
subscribe('message', (state, applied) => { | ||
expect(state).to.be('This is a test') | ||
expect(applied).to.be('set') | ||
done() | ||
} ) | ||
} ) | ||
}) | ||
}) | ||
it( 'If the third argument of commit is false it will not provide state', done => { | ||
dispatch( 'counter', 'silentSet', 1 ) | ||
subscribe( 'counter', ( state, applied ) => { | ||
expect( state ).to.be( null ) | ||
expect( applied ).to.be( 'set' ) | ||
it('If the third argument of commit is false it will not provide state', done => { | ||
dispatch('counter', 'silentSet', 1) | ||
subscribe('counter', (state, applied) => { | ||
expect(state).to.be(null) | ||
expect(applied).to.be('set') | ||
done() | ||
} ) | ||
} ) | ||
}) | ||
}) | ||
it( 'Unsubscribe', done => { | ||
it('Unsubscribe', done => { | ||
let i = 0 | ||
@@ -101,62 +101,62 @@ let counterSubscriber = () => { | ||
} | ||
subscribe( 'counter', counterSubscriber ) | ||
dispatch( 'counter', 'increment' ) | ||
setTimeout( () => { | ||
unsubscribe( 'counter', counterSubscriber ) | ||
dispatch( 'counter', 'increment' ) | ||
expect( i ).to.be( 1 ) | ||
subscribe('counter', counterSubscriber) | ||
dispatch('counter', 'increment') | ||
setTimeout(() => { | ||
unsubscribe('counter', counterSubscriber) | ||
dispatch('counter', 'increment') | ||
expect(i).to.be(1) | ||
done() | ||
}, 500 ) | ||
} ) | ||
}, 500) | ||
}) | ||
it( 'Get store state', done => { | ||
dispatch( 'counter', 'set', 123456 ) | ||
getState( 'counter' ).then( state => { | ||
expect( state ).to.be( 123456 ) | ||
it('Get store state', done => { | ||
dispatch('counter', 'set', 123456) | ||
getState('counter').then(state => { | ||
expect(state).to.be(123456) | ||
done() | ||
} ) | ||
} ) | ||
}) | ||
}) | ||
it( 'Get store state by getters', done => { | ||
dispatch( 'message', 'set', 'This is a test' ) | ||
getState( 'message', 'wordCount' ).then( state => { | ||
expect( state ).to.be( 14 ) | ||
it('Get store state by getters', done => { | ||
dispatch('message', 'set', 'This is a test') | ||
getState('message', 'wordCount').then(state => { | ||
expect(state).to.be(14) | ||
done() | ||
} ) | ||
} ) | ||
}) | ||
}) | ||
it( 'Execute action crossed to multiple stores by manager', done => { | ||
operate( 'countUpMessage', 1 ) | ||
subscribe( 'counter', state => { | ||
expect( state ).to.be( 1 ) | ||
} ) | ||
unsubscribe( 'message' ) | ||
subscribe( 'message', state => { | ||
expect( state ).to.be( '1 has been added to the counter' ) | ||
it('Execute action crossed to multiple stores by manager', done => { | ||
operate('countUpMessage', 1) | ||
subscribe('counter', state => { | ||
expect(state).to.be(1) | ||
}) | ||
unsubscribe('message') | ||
subscribe('message', state => { | ||
expect(state).to.be('1 has been added to the counter') | ||
done() | ||
} ) | ||
} ) | ||
}) | ||
}) | ||
it( 'Get state of all stores with getAllState', done => { | ||
it('Get state of all stores with getAllState', done => { | ||
const states = {} | ||
const test = () => { | ||
getAllState().then( state => { | ||
expect( state.counter ).to.equal( states.counter ) | ||
expect( state.message ).to.equal( states.message ) | ||
getAllState().then(state => { | ||
expect(state.counter).to.equal(states.counter) | ||
expect(state.message).to.equal(states.message) | ||
done() | ||
} ) | ||
}) | ||
} | ||
getState( 'counter' ).then( state => { | ||
getState('counter').then(state => { | ||
states.counter = state | ||
if ( 'message' in states ) { | ||
if ('message' in states) { | ||
test() | ||
} | ||
} ) | ||
getState( 'message' ).then( state => { | ||
}) | ||
getState('message').then(state => { | ||
states.message = state | ||
if ( 'counter' in states ) { | ||
if ('counter' in states) { | ||
test() | ||
} | ||
} ) | ||
} ) | ||
} ) | ||
}) | ||
}) | ||
}) |
const getTime = () => performance.now() | ||
const floor = ( n, d ) => Math.floor( n * d ) / d | ||
const floor = (n, d) => Math.floor(n * d) / d | ||
@@ -8,14 +8,14 @@ let t = {} | ||
export const time = name => { | ||
t[ name ] = getTime() | ||
t[name] = getTime() | ||
} | ||
export const timeEnd = name => { | ||
const ms = getTime() - t[ name ] | ||
record.push( ms ) | ||
return floor( ms, 1000 ) + 'ms' | ||
const ms = getTime() - t[name] | ||
record.push(ms) | ||
return floor(ms, 1000) + 'ms' | ||
} | ||
export const timeAverage = () => { | ||
const sum = record.reduce( ( p, c ) => p + c ) | ||
return floor( sum / record.length, 1000 ) + 'ms' | ||
const sum = record.reduce((p, c) => p + c) | ||
return floor(sum / record.length, 1000) + 'ms' | ||
} | ||
@@ -22,0 +22,0 @@ |
@@ -1,27 +0,27 @@ | ||
import { worker } from '../src/businessman' | ||
import {worker} from '../src/businessman' | ||
worker.registerStore( { | ||
worker.registerStore({ | ||
type: 'counter', | ||
state: 0, | ||
mutations: { | ||
increment: ( state, num ) => { | ||
increment: (state, num) => { | ||
let n = state += num | ||
return n | ||
}, | ||
set: ( state, num ) => num | ||
set: (state, num) => num | ||
}, | ||
actions: { | ||
increment: ( commit, num = 1 ) => { | ||
commit( 'increment', num ) | ||
increment: (commit, num = 1) => { | ||
commit('increment', num) | ||
}, | ||
set: ( commit, num = 0 ) => { | ||
commit( 'set', num ) | ||
set: (commit, num = 0) => { | ||
commit('set', num) | ||
}, | ||
silentSet: ( commit, num = 0 ) => { | ||
commit( 'set', num, false ) | ||
silentSet: (commit, num = 0) => { | ||
commit('set', num, false) | ||
} | ||
} | ||
} ) | ||
}) | ||
worker.registerStore( { | ||
worker.registerStore({ | ||
type: 'message', | ||
@@ -33,19 +33,19 @@ state: '', | ||
mutations: { | ||
set: ( state, mes ) => mes | ||
set: (state, mes) => mes | ||
}, | ||
actions: { | ||
set: ( commit, mes = '' ) => { | ||
commit( 'set', mes ) | ||
set: (commit, mes = '') => { | ||
commit('set', mes) | ||
} | ||
} | ||
} ) | ||
}) | ||
worker.registerManager( { | ||
worker.registerManager({ | ||
type: 'countUpMessage', | ||
handler: ( stores, num = 1 ) => { | ||
stores.counter.dispatch( 'increment', num ) | ||
stores.message.dispatch( 'set', `${num} has been added to the counter` ) | ||
handler: (stores, num = 1) => { | ||
stores.counter.dispatch('increment', num) | ||
stores.message.dispatch('set', `${num} has been added to the counter`) | ||
} | ||
} ) | ||
}) | ||
worker.start() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
129354