businessman
Advanced tools
Comparing version 1.4.2 to 1.4.3
@@ -1,5 +0,5 @@ | ||
module.exports = function (config) { | ||
config.set({ | ||
module.exports = function ( config ) { | ||
config.set( { | ||
basePath: '../', | ||
frameworks: ['mocha'], | ||
frameworks: [ 'mocha' ], | ||
plugins: [ | ||
@@ -22,6 +22,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: `src/${name}.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,34 +0,36 @@ | ||
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 multiEntry = require( 'rollup-plugin-multi-entry' ) | ||
rollup.rollup({ | ||
entry: 'test/businessman.js', | ||
rollup.rollup( { | ||
entry: 'test/businessman*.js', | ||
plugins: [ | ||
npm({jsnext: true}), | ||
npm( { jsnext: true } ), | ||
buble(), | ||
commonjs() | ||
commonjs(), | ||
multiEntry() | ||
] | ||
}) | ||
.then(bundle => { | ||
bundle.write({format: 'iife', 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 ) | ||
} ) |
@@ -130,40 +130,40 @@ define(['exports'], function (exports) { 'use strict'; | ||
var o = new function () { | ||
observable(this); | ||
observable( this ); | ||
}(); | ||
var trigger = function (data) { | ||
o.trigger(data.type, data.payload, data.applied); | ||
var trigger = function ( data ) { | ||
o.trigger( data.type, data.payload, data.applied ); | ||
}; | ||
var on = function (type, cb) { | ||
o.on(type, cb); | ||
var on = function ( type, cb ) { | ||
o.on( type, cb ); | ||
}; | ||
var off = function (type, cb) { | ||
if (cb) { | ||
o.off(type, cb); | ||
} else { | ||
o.off(type); | ||
var off = function ( type, cb ) { | ||
if ( cb ) { | ||
o.off( type, cb ); | ||
} else { | ||
o.off( type ); | ||
} | ||
}; | ||
var pack = function (type, payload, applied) { | ||
var pack = function ( type, payload, applied ) { | ||
if ( type === void 0 ) type = ''; | ||
if ( payload === void 0 ) payload = {}; | ||
if (applied) { | ||
return {type: type, payload: payload, applied: applied} | ||
if ( applied ) { | ||
return { type: type, payload: payload, applied: applied } | ||
} | ||
return {type: type, payload: payload} | ||
return { type: type, payload: payload } | ||
}; | ||
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 ]; | ||
} | ||
@@ -181,8 +181,8 @@ } | ||
var mutations = {}; | ||
mutations[GET_STATE] = function (state) { return state; }; | ||
mutations[ GET_STATE ] = function (state) { return state; }; | ||
var actions = {}; | ||
actions[GET_STATE] = function (commit) { return commit(GET_STATE); }; | ||
actions[ GET_STATE ] = function (commit) { return commit( GET_STATE ); }; | ||
var Store = function Store(opt) { | ||
var Store = function Store ( opt ) { | ||
var state = opt.state; | ||
@@ -201,9 +201,9 @@ var mutations$$1 = opt.mutations; if ( mutations$$1 === void 0 ) mutations$$1 = {}; | ||
state = newState; | ||
postMessage(pack(type, state, store.appliedMutation)); | ||
postMessage( pack( type, state, store.appliedMutation ) ); | ||
} | ||
}; | ||
mutations$$1 = assign(mutations$$1, mutations); | ||
actions$$1 = assign(actions$$1, actions); | ||
mutations$$1 = assign( mutations$$1, mutations ); | ||
actions$$1 = assign( actions$$1, actions ); | ||
Object.defineProperties(this, { | ||
Object.defineProperties( this, { | ||
type: { | ||
@@ -226,3 +226,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, | ||
@@ -232,3 +232,3 @@ writable: false | ||
commit: { | ||
value: function (type, payload) { return commit.call(store, _state, type, payload); }, | ||
value: function ( type, payload ) { return commit.call( store, _state, type, payload ); }, | ||
configurable: false, | ||
@@ -241,12 +241,12 @@ writable: false | ||
} | ||
}); | ||
} ); | ||
}; | ||
Store.prototype.commit = function commit (state, type, payload) { | ||
Store.prototype.commit = function commit ( state, type, payload ) { | ||
this.appliedMutation = type; | ||
state.set(this.mutations[type](state.get(), payload)); | ||
state.set( this.mutations[ type ]( state.get(), payload ) ); | ||
}; | ||
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 ); | ||
}; | ||
@@ -265,20 +265,20 @@ | ||
var data = e.data; | ||
if (data.length > 2) { | ||
stores[data[0]].dispatch(data[1], data[2]); | ||
} else if (data.length > 1) { | ||
managers[data[0]](stores, data[1]); | ||
if ( data.length > 2 ) { | ||
stores[ data[ 0 ] ].dispatch( data[ 1 ], data[ 2 ] ); | ||
} else if ( data.length > 1 ) { | ||
managers[ data[ 0 ] ]( stores, data[ 1 ] ); | ||
} | ||
}; | ||
postMessage(pack(INIT, {stores: forClient.stores, managers: forClient.managers})); | ||
postMessage( pack( INIT, { stores: forClient.stores, managers: forClient.managers } ) ); | ||
}, | ||
registerStore: function (config) { | ||
var store = new Store(config); | ||
var store = new Store( config ); | ||
var type = store.type; | ||
var actions = store.actions; | ||
if (!(type in stores)) { | ||
stores[type] = store; | ||
forClient.stores.push({ | ||
if ( !( type in stores ) ) { | ||
stores[ type ] = store; | ||
forClient.stores.push( { | ||
type: type, | ||
actions: Object.keys(actions) | ||
}); | ||
actions: Object.keys( actions ) | ||
} ); | ||
} | ||
@@ -289,7 +289,7 @@ }, | ||
var handler = config.handler; | ||
if (!(type in managers)) { | ||
managers[type] = handler; | ||
forClient.managers.push({ | ||
if ( !( type in managers ) ) { | ||
managers[ type ] = handler; | ||
forClient.managers.push( { | ||
type: type | ||
}); | ||
} ); | ||
} | ||
@@ -299,49 +299,49 @@ } | ||
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 (message) { return trigger(message.data); }; | ||
worker = new Worker( path ); | ||
worker.onmessage = function (message) { return trigger( message.data ); }; | ||
return worker | ||
} catch (err) { | ||
console.error('Error in install', err); | ||
} catch ( err ) { | ||
console.error( 'Error in install', err ); | ||
} | ||
}; | ||
var dispatch$2 = function (storeType, actionType, payload, worker) { | ||
worker.postMessage([storeType, actionType, payload]); | ||
var dispatch$2 = function ( storeType, actionType, payload, worker ) { | ||
worker.postMessage( [ storeType, actionType, payload ] ); | ||
}; | ||
var _operate = function (managerType, payload, worker) { | ||
worker.postMessage([managerType, payload]); | ||
var _operate = function ( managerType, payload, worker ) { | ||
worker.postMessage( [ managerType, payload ] ); | ||
}; | ||
var subscribe$1 = function (type, cb) { | ||
on(type, cb); | ||
var subscribe$1 = function ( type, cb ) { | ||
on( type, cb ); | ||
}; | ||
var unsubscribe$1 = function (type, cb) { | ||
off(type, cb); | ||
var unsubscribe$1 = function ( type, cb ) { | ||
off( type, cb ); | ||
}; | ||
var _getState = function (storeType, worker) { | ||
return new Promise(function (resolve, reject) { | ||
var subscriber = function (state, applied) { | ||
if (applied !== GET_STATE) { | ||
var _getState = function ( storeType, worker ) { | ||
return new Promise( function ( resolve, reject ) { | ||
var subscriber = function ( state, applied ) { | ||
if ( applied !== GET_STATE ) { | ||
return | ||
} | ||
unsubscribe$1(storeType, subscriber); | ||
resolve(state); | ||
unsubscribe$1( storeType, subscriber ); | ||
resolve( state ); | ||
}; | ||
subscribe$1(storeType, subscriber); | ||
subscribe$1( storeType, subscriber ); | ||
try { | ||
dispatch$2(storeType, GET_STATE, '', worker); | ||
} catch (err) { | ||
reject(err); | ||
unsubscribe$1(storeType, subscriber); | ||
dispatch$2( storeType, GET_STATE, '', worker ); | ||
} catch ( err ) { | ||
reject( err ); | ||
unsubscribe$1( storeType, subscriber ); | ||
} | ||
}) | ||
} ) | ||
}; | ||
@@ -352,30 +352,30 @@ | ||
var install = function (path) { | ||
businessmanWoker = _install(path, businessmanWoker); | ||
businessmanWoker = _install( path, businessmanWoker ); | ||
}; | ||
var dispatch$1 = function (storeType, actionType, payload) { return dispatch$2(storeType, actionType, payload, businessmanWoker); }; | ||
var operate = function (managerType, payload) { return _operate(managerType, payload, businessmanWoker); }; | ||
var subscribe = function (type, cb) { return subscribe$1(type, cb); }; | ||
var unsubscribe = function (type, cb) { return unsubscribe$1(type, cb); }; | ||
var getState = function (storeType) { return _getState(storeType, businessmanWoker); }; | ||
var dispatch$1 = function ( storeType, actionType, payload ) { return dispatch$2( storeType, actionType, payload, businessmanWoker ); }; | ||
var operate = function ( managerType, payload ) { return _operate( managerType, payload, businessmanWoker ); }; | ||
var subscribe = function ( type, cb ) { return subscribe$1( type, cb ); }; | ||
var unsubscribe = function ( type, cb ) { return unsubscribe$1( type, cb ); }; | ||
var getState = function (storeType) { return _getState( storeType, businessmanWoker ); }; | ||
subscribe(INIT, function (data) { | ||
subscribe( INIT, function (data) { | ||
var stores = {}; | ||
var managers = {}; | ||
try { | ||
data.stores.map(function (store) { | ||
stores[store.type] = { | ||
dispatch: function (actionType, payload) { return dispatch$1(store.type, actionType, payload); }, | ||
subscribe: function (cb) { return subscribe(store.type, cb); }, | ||
unsubscribe: function (cb) { return unsubscribe(store.type, cb); }, | ||
getState: function () { return getState(store.type); } | ||
data.stores.map( function (store) { | ||
stores[ store.type ] = { | ||
dispatch: function ( actionType, payload ) { return dispatch$1( store.type, actionType, payload ); }, | ||
subscribe: function (cb) { return subscribe( store.type, cb ); }, | ||
unsubscribe: function (cb) { return unsubscribe( store.type, cb ); }, | ||
getState: function () { return getState( store.type ); } | ||
}; | ||
return store | ||
}); | ||
trigger(pack(CREATE_CLIENT_STORE, stores)); | ||
} ); | ||
trigger( pack( CREATE_CLIENT_STORE, stores ) ); | ||
managers = data.managers; | ||
trigger(pack(CREATE_CLIENT_MANAGER, managers)); | ||
} catch (err) { | ||
console.error('Error in creating client store or client manager', err); | ||
trigger( pack( CREATE_CLIENT_MANAGER, managers ) ); | ||
} catch ( err ) { | ||
console.error( 'Error in creating client store or client manager', err ); | ||
} | ||
}); | ||
} ); | ||
@@ -382,0 +382,0 @@ exports.install = install; |
@@ -128,40 +128,40 @@ var observable = function(el) { | ||
var o = new function () { | ||
observable(this); | ||
observable( this ); | ||
}(); | ||
var trigger = function (data) { | ||
o.trigger(data.type, data.payload, data.applied); | ||
var trigger = function ( data ) { | ||
o.trigger( data.type, data.payload, data.applied ); | ||
}; | ||
var on = function (type, cb) { | ||
o.on(type, cb); | ||
var on = function ( type, cb ) { | ||
o.on( type, cb ); | ||
}; | ||
var off = function (type, cb) { | ||
if (cb) { | ||
o.off(type, cb); | ||
} else { | ||
o.off(type); | ||
var off = function ( type, cb ) { | ||
if ( cb ) { | ||
o.off( type, cb ); | ||
} else { | ||
o.off( type ); | ||
} | ||
}; | ||
var pack = function (type, payload, applied) { | ||
var pack = function ( type, payload, applied ) { | ||
if ( type === void 0 ) type = ''; | ||
if ( payload === void 0 ) payload = {}; | ||
if (applied) { | ||
return {type: type, payload: payload, applied: applied} | ||
if ( applied ) { | ||
return { type: type, payload: payload, applied: applied } | ||
} | ||
return {type: type, payload: payload} | ||
return { type: type, payload: payload } | ||
}; | ||
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 ]; | ||
} | ||
@@ -179,8 +179,8 @@ } | ||
var mutations = {}; | ||
mutations[GET_STATE] = function (state) { return state; }; | ||
mutations[ GET_STATE ] = function (state) { return state; }; | ||
var actions = {}; | ||
actions[GET_STATE] = function (commit) { return commit(GET_STATE); }; | ||
actions[ GET_STATE ] = function (commit) { return commit( GET_STATE ); }; | ||
var Store = function Store(opt) { | ||
var Store = function Store ( opt ) { | ||
var state = opt.state; | ||
@@ -199,9 +199,9 @@ var mutations$$1 = opt.mutations; if ( mutations$$1 === void 0 ) mutations$$1 = {}; | ||
state = newState; | ||
postMessage(pack(type, state, store.appliedMutation)); | ||
postMessage( pack( type, state, store.appliedMutation ) ); | ||
} | ||
}; | ||
mutations$$1 = assign(mutations$$1, mutations); | ||
actions$$1 = assign(actions$$1, actions); | ||
mutations$$1 = assign( mutations$$1, mutations ); | ||
actions$$1 = assign( actions$$1, actions ); | ||
Object.defineProperties(this, { | ||
Object.defineProperties( this, { | ||
type: { | ||
@@ -224,3 +224,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, | ||
@@ -230,3 +230,3 @@ writable: false | ||
commit: { | ||
value: function (type, payload) { return commit.call(store, _state, type, payload); }, | ||
value: function ( type, payload ) { return commit.call( store, _state, type, payload ); }, | ||
configurable: false, | ||
@@ -239,12 +239,12 @@ writable: false | ||
} | ||
}); | ||
} ); | ||
}; | ||
Store.prototype.commit = function commit (state, type, payload) { | ||
Store.prototype.commit = function commit ( state, type, payload ) { | ||
this.appliedMutation = type; | ||
state.set(this.mutations[type](state.get(), payload)); | ||
state.set( this.mutations[ type ]( state.get(), payload ) ); | ||
}; | ||
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 ); | ||
}; | ||
@@ -263,20 +263,20 @@ | ||
var data = e.data; | ||
if (data.length > 2) { | ||
stores[data[0]].dispatch(data[1], data[2]); | ||
} else if (data.length > 1) { | ||
managers[data[0]](stores, data[1]); | ||
if ( data.length > 2 ) { | ||
stores[ data[ 0 ] ].dispatch( data[ 1 ], data[ 2 ] ); | ||
} else if ( data.length > 1 ) { | ||
managers[ data[ 0 ] ]( stores, data[ 1 ] ); | ||
} | ||
}; | ||
postMessage(pack(INIT, {stores: forClient.stores, managers: forClient.managers})); | ||
postMessage( pack( INIT, { stores: forClient.stores, managers: forClient.managers } ) ); | ||
}, | ||
registerStore: function (config) { | ||
var store = new Store(config); | ||
var store = new Store( config ); | ||
var type = store.type; | ||
var actions = store.actions; | ||
if (!(type in stores)) { | ||
stores[type] = store; | ||
forClient.stores.push({ | ||
if ( !( type in stores ) ) { | ||
stores[ type ] = store; | ||
forClient.stores.push( { | ||
type: type, | ||
actions: Object.keys(actions) | ||
}); | ||
actions: Object.keys( actions ) | ||
} ); | ||
} | ||
@@ -287,7 +287,7 @@ }, | ||
var handler = config.handler; | ||
if (!(type in managers)) { | ||
managers[type] = handler; | ||
forClient.managers.push({ | ||
if ( !( type in managers ) ) { | ||
managers[ type ] = handler; | ||
forClient.managers.push( { | ||
type: type | ||
}); | ||
} ); | ||
} | ||
@@ -297,49 +297,49 @@ } | ||
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 (message) { return trigger(message.data); }; | ||
worker = new Worker( path ); | ||
worker.onmessage = function (message) { return trigger( message.data ); }; | ||
return worker | ||
} catch (err) { | ||
console.error('Error in install', err); | ||
} catch ( err ) { | ||
console.error( 'Error in install', err ); | ||
} | ||
}; | ||
var dispatch$2 = function (storeType, actionType, payload, worker) { | ||
worker.postMessage([storeType, actionType, payload]); | ||
var dispatch$2 = function ( storeType, actionType, payload, worker ) { | ||
worker.postMessage( [ storeType, actionType, payload ] ); | ||
}; | ||
var _operate = function (managerType, payload, worker) { | ||
worker.postMessage([managerType, payload]); | ||
var _operate = function ( managerType, payload, worker ) { | ||
worker.postMessage( [ managerType, payload ] ); | ||
}; | ||
var subscribe$1 = function (type, cb) { | ||
on(type, cb); | ||
var subscribe$1 = function ( type, cb ) { | ||
on( type, cb ); | ||
}; | ||
var unsubscribe$1 = function (type, cb) { | ||
off(type, cb); | ||
var unsubscribe$1 = function ( type, cb ) { | ||
off( type, cb ); | ||
}; | ||
var _getState = function (storeType, worker) { | ||
return new Promise(function (resolve, reject) { | ||
var subscriber = function (state, applied) { | ||
if (applied !== GET_STATE) { | ||
var _getState = function ( storeType, worker ) { | ||
return new Promise( function ( resolve, reject ) { | ||
var subscriber = function ( state, applied ) { | ||
if ( applied !== GET_STATE ) { | ||
return | ||
} | ||
unsubscribe$1(storeType, subscriber); | ||
resolve(state); | ||
unsubscribe$1( storeType, subscriber ); | ||
resolve( state ); | ||
}; | ||
subscribe$1(storeType, subscriber); | ||
subscribe$1( storeType, subscriber ); | ||
try { | ||
dispatch$2(storeType, GET_STATE, '', worker); | ||
} catch (err) { | ||
reject(err); | ||
unsubscribe$1(storeType, subscriber); | ||
dispatch$2( storeType, GET_STATE, '', worker ); | ||
} catch ( err ) { | ||
reject( err ); | ||
unsubscribe$1( storeType, subscriber ); | ||
} | ||
}) | ||
} ) | ||
}; | ||
@@ -350,31 +350,31 @@ | ||
var install = function (path) { | ||
businessmanWoker = _install(path, businessmanWoker); | ||
businessmanWoker = _install( path, businessmanWoker ); | ||
}; | ||
var dispatch$1 = function (storeType, actionType, payload) { return dispatch$2(storeType, actionType, payload, businessmanWoker); }; | ||
var operate = function (managerType, payload) { return _operate(managerType, payload, businessmanWoker); }; | ||
var subscribe = function (type, cb) { return subscribe$1(type, cb); }; | ||
var unsubscribe = function (type, cb) { return unsubscribe$1(type, cb); }; | ||
var getState = function (storeType) { return _getState(storeType, businessmanWoker); }; | ||
var dispatch$1 = function ( storeType, actionType, payload ) { return dispatch$2( storeType, actionType, payload, businessmanWoker ); }; | ||
var operate = function ( managerType, payload ) { return _operate( managerType, payload, businessmanWoker ); }; | ||
var subscribe = function ( type, cb ) { return subscribe$1( type, cb ); }; | ||
var unsubscribe = function ( type, cb ) { return unsubscribe$1( type, cb ); }; | ||
var getState = function (storeType) { return _getState( storeType, businessmanWoker ); }; | ||
subscribe(INIT, function (data) { | ||
subscribe( INIT, function (data) { | ||
var stores = {}; | ||
var managers = {}; | ||
try { | ||
data.stores.map(function (store) { | ||
stores[store.type] = { | ||
dispatch: function (actionType, payload) { return dispatch$1(store.type, actionType, payload); }, | ||
subscribe: function (cb) { return subscribe(store.type, cb); }, | ||
unsubscribe: function (cb) { return unsubscribe(store.type, cb); }, | ||
getState: function () { return getState(store.type); } | ||
data.stores.map( function (store) { | ||
stores[ store.type ] = { | ||
dispatch: function ( actionType, payload ) { return dispatch$1( store.type, actionType, payload ); }, | ||
subscribe: function (cb) { return subscribe( store.type, cb ); }, | ||
unsubscribe: function (cb) { return unsubscribe( store.type, cb ); }, | ||
getState: function () { return getState( store.type ); } | ||
}; | ||
return store | ||
}); | ||
trigger(pack(CREATE_CLIENT_STORE, stores)); | ||
} ); | ||
trigger( pack( CREATE_CLIENT_STORE, stores ) ); | ||
managers = data.managers; | ||
trigger(pack(CREATE_CLIENT_MANAGER, managers)); | ||
} catch (err) { | ||
console.error('Error in creating client store or client manager', err); | ||
trigger( pack( CREATE_CLIENT_MANAGER, managers ) ); | ||
} catch ( err ) { | ||
console.error( 'Error in creating client store or client manager', err ); | ||
} | ||
}); | ||
} ); | ||
export { install, dispatch$1 as dispatch, operate, subscribe, unsubscribe, getState, worker$1 as worker }; |
@@ -134,40 +134,40 @@ (function (global, factory) { | ||
var o = new function () { | ||
observable(this); | ||
observable( this ); | ||
}(); | ||
var trigger = function (data) { | ||
o.trigger(data.type, data.payload, data.applied); | ||
var trigger = function ( data ) { | ||
o.trigger( data.type, data.payload, data.applied ); | ||
}; | ||
var on = function (type, cb) { | ||
o.on(type, cb); | ||
var on = function ( type, cb ) { | ||
o.on( type, cb ); | ||
}; | ||
var off = function (type, cb) { | ||
if (cb) { | ||
o.off(type, cb); | ||
} else { | ||
o.off(type); | ||
var off = function ( type, cb ) { | ||
if ( cb ) { | ||
o.off( type, cb ); | ||
} else { | ||
o.off( type ); | ||
} | ||
}; | ||
var pack = function (type, payload, applied) { | ||
var pack = function ( type, payload, applied ) { | ||
if ( type === void 0 ) type = ''; | ||
if ( payload === void 0 ) payload = {}; | ||
if (applied) { | ||
return {type: type, payload: payload, applied: applied} | ||
if ( applied ) { | ||
return { type: type, payload: payload, applied: applied } | ||
} | ||
return {type: type, payload: payload} | ||
return { type: type, payload: payload } | ||
}; | ||
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 ]; | ||
} | ||
@@ -185,8 +185,8 @@ } | ||
var mutations = {}; | ||
mutations[GET_STATE] = function (state) { return state; }; | ||
mutations[ GET_STATE ] = function (state) { return state; }; | ||
var actions = {}; | ||
actions[GET_STATE] = function (commit) { return commit(GET_STATE); }; | ||
actions[ GET_STATE ] = function (commit) { return commit( GET_STATE ); }; | ||
var Store = function Store(opt) { | ||
var Store = function Store ( opt ) { | ||
var state = opt.state; | ||
@@ -205,9 +205,9 @@ var mutations$$1 = opt.mutations; if ( mutations$$1 === void 0 ) mutations$$1 = {}; | ||
state = newState; | ||
postMessage(pack(type, state, store.appliedMutation)); | ||
postMessage( pack( type, state, store.appliedMutation ) ); | ||
} | ||
}; | ||
mutations$$1 = assign(mutations$$1, mutations); | ||
actions$$1 = assign(actions$$1, actions); | ||
mutations$$1 = assign( mutations$$1, mutations ); | ||
actions$$1 = assign( actions$$1, actions ); | ||
Object.defineProperties(this, { | ||
Object.defineProperties( this, { | ||
type: { | ||
@@ -230,3 +230,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, | ||
@@ -236,3 +236,3 @@ writable: false | ||
commit: { | ||
value: function (type, payload) { return commit.call(store, _state, type, payload); }, | ||
value: function ( type, payload ) { return commit.call( store, _state, type, payload ); }, | ||
configurable: false, | ||
@@ -245,12 +245,12 @@ writable: false | ||
} | ||
}); | ||
} ); | ||
}; | ||
Store.prototype.commit = function commit (state, type, payload) { | ||
Store.prototype.commit = function commit ( state, type, payload ) { | ||
this.appliedMutation = type; | ||
state.set(this.mutations[type](state.get(), payload)); | ||
state.set( this.mutations[ type ]( state.get(), payload ) ); | ||
}; | ||
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 ); | ||
}; | ||
@@ -269,20 +269,20 @@ | ||
var data = e.data; | ||
if (data.length > 2) { | ||
stores[data[0]].dispatch(data[1], data[2]); | ||
} else if (data.length > 1) { | ||
managers[data[0]](stores, data[1]); | ||
if ( data.length > 2 ) { | ||
stores[ data[ 0 ] ].dispatch( data[ 1 ], data[ 2 ] ); | ||
} else if ( data.length > 1 ) { | ||
managers[ data[ 0 ] ]( stores, data[ 1 ] ); | ||
} | ||
}; | ||
postMessage(pack(INIT, {stores: forClient.stores, managers: forClient.managers})); | ||
postMessage( pack( INIT, { stores: forClient.stores, managers: forClient.managers } ) ); | ||
}, | ||
registerStore: function (config) { | ||
var store = new Store(config); | ||
var store = new Store( config ); | ||
var type = store.type; | ||
var actions = store.actions; | ||
if (!(type in stores)) { | ||
stores[type] = store; | ||
forClient.stores.push({ | ||
if ( !( type in stores ) ) { | ||
stores[ type ] = store; | ||
forClient.stores.push( { | ||
type: type, | ||
actions: Object.keys(actions) | ||
}); | ||
actions: Object.keys( actions ) | ||
} ); | ||
} | ||
@@ -293,7 +293,7 @@ }, | ||
var handler = config.handler; | ||
if (!(type in managers)) { | ||
managers[type] = handler; | ||
forClient.managers.push({ | ||
if ( !( type in managers ) ) { | ||
managers[ type ] = handler; | ||
forClient.managers.push( { | ||
type: type | ||
}); | ||
} ); | ||
} | ||
@@ -303,49 +303,49 @@ } | ||
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 (message) { return trigger(message.data); }; | ||
worker = new Worker( path ); | ||
worker.onmessage = function (message) { return trigger( message.data ); }; | ||
return worker | ||
} catch (err) { | ||
console.error('Error in install', err); | ||
} catch ( err ) { | ||
console.error( 'Error in install', err ); | ||
} | ||
}; | ||
var dispatch$2 = function (storeType, actionType, payload, worker) { | ||
worker.postMessage([storeType, actionType, payload]); | ||
var dispatch$2 = function ( storeType, actionType, payload, worker ) { | ||
worker.postMessage( [ storeType, actionType, payload ] ); | ||
}; | ||
var _operate = function (managerType, payload, worker) { | ||
worker.postMessage([managerType, payload]); | ||
var _operate = function ( managerType, payload, worker ) { | ||
worker.postMessage( [ managerType, payload ] ); | ||
}; | ||
var subscribe$1 = function (type, cb) { | ||
on(type, cb); | ||
var subscribe$1 = function ( type, cb ) { | ||
on( type, cb ); | ||
}; | ||
var unsubscribe$1 = function (type, cb) { | ||
off(type, cb); | ||
var unsubscribe$1 = function ( type, cb ) { | ||
off( type, cb ); | ||
}; | ||
var _getState = function (storeType, worker) { | ||
return new Promise(function (resolve, reject) { | ||
var subscriber = function (state, applied) { | ||
if (applied !== GET_STATE) { | ||
var _getState = function ( storeType, worker ) { | ||
return new Promise( function ( resolve, reject ) { | ||
var subscriber = function ( state, applied ) { | ||
if ( applied !== GET_STATE ) { | ||
return | ||
} | ||
unsubscribe$1(storeType, subscriber); | ||
resolve(state); | ||
unsubscribe$1( storeType, subscriber ); | ||
resolve( state ); | ||
}; | ||
subscribe$1(storeType, subscriber); | ||
subscribe$1( storeType, subscriber ); | ||
try { | ||
dispatch$2(storeType, GET_STATE, '', worker); | ||
} catch (err) { | ||
reject(err); | ||
unsubscribe$1(storeType, subscriber); | ||
dispatch$2( storeType, GET_STATE, '', worker ); | ||
} catch ( err ) { | ||
reject( err ); | ||
unsubscribe$1( storeType, subscriber ); | ||
} | ||
}) | ||
} ) | ||
}; | ||
@@ -356,30 +356,30 @@ | ||
var install = function (path) { | ||
businessmanWoker = _install(path, businessmanWoker); | ||
businessmanWoker = _install( path, businessmanWoker ); | ||
}; | ||
var dispatch$1 = function (storeType, actionType, payload) { return dispatch$2(storeType, actionType, payload, businessmanWoker); }; | ||
var operate = function (managerType, payload) { return _operate(managerType, payload, businessmanWoker); }; | ||
var subscribe = function (type, cb) { return subscribe$1(type, cb); }; | ||
var unsubscribe = function (type, cb) { return unsubscribe$1(type, cb); }; | ||
var getState = function (storeType) { return _getState(storeType, businessmanWoker); }; | ||
var dispatch$1 = function ( storeType, actionType, payload ) { return dispatch$2( storeType, actionType, payload, businessmanWoker ); }; | ||
var operate = function ( managerType, payload ) { return _operate( managerType, payload, businessmanWoker ); }; | ||
var subscribe = function ( type, cb ) { return subscribe$1( type, cb ); }; | ||
var unsubscribe = function ( type, cb ) { return unsubscribe$1( type, cb ); }; | ||
var getState = function (storeType) { return _getState( storeType, businessmanWoker ); }; | ||
subscribe(INIT, function (data) { | ||
subscribe( INIT, function (data) { | ||
var stores = {}; | ||
var managers = {}; | ||
try { | ||
data.stores.map(function (store) { | ||
stores[store.type] = { | ||
dispatch: function (actionType, payload) { return dispatch$1(store.type, actionType, payload); }, | ||
subscribe: function (cb) { return subscribe(store.type, cb); }, | ||
unsubscribe: function (cb) { return unsubscribe(store.type, cb); }, | ||
getState: function () { return getState(store.type); } | ||
data.stores.map( function (store) { | ||
stores[ store.type ] = { | ||
dispatch: function ( actionType, payload ) { return dispatch$1( store.type, actionType, payload ); }, | ||
subscribe: function (cb) { return subscribe( store.type, cb ); }, | ||
unsubscribe: function (cb) { return unsubscribe( store.type, cb ); }, | ||
getState: function () { return getState( store.type ); } | ||
}; | ||
return store | ||
}); | ||
trigger(pack(CREATE_CLIENT_STORE, stores)); | ||
} ); | ||
trigger( pack( CREATE_CLIENT_STORE, stores ) ); | ||
managers = data.managers; | ||
trigger(pack(CREATE_CLIENT_MANAGER, managers)); | ||
} catch (err) { | ||
console.error('Error in creating client store or client manager', err); | ||
trigger( pack( CREATE_CLIENT_MANAGER, managers ) ); | ||
} catch ( err ) { | ||
console.error( 'Error in creating client store or client manager', err ); | ||
} | ||
}); | ||
} ); | ||
@@ -386,0 +386,0 @@ exports.install = install; |
{ | ||
"name": "businessman", | ||
"version": "1.4.2", | ||
"version": "1.4.3", | ||
"description": "Multi-thread State Management by the Worker API", | ||
@@ -37,2 +37,3 @@ "main": "dist/businessman.js", | ||
"rollup-plugin-commonjs": "^6.0.1", | ||
"rollup-plugin-multi-entry": "^2.0.1", | ||
"rollup-plugin-node-resolve": "^2.0.0", | ||
@@ -45,13 +46,42 @@ "xo": "^0.17.1" | ||
"xo": { | ||
"globals": [ | ||
"describe", | ||
"it", | ||
"expect", | ||
"before" | ||
], | ||
"envs": [ | ||
"worker" | ||
], | ||
"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": [ | ||
{ | ||
"files": "test/*.js", | ||
"globals": [ | ||
"describe", | ||
"it", | ||
"expect", | ||
"before", | ||
"beforeEach" | ||
] | ||
} | ||
], | ||
"semicolon": false | ||
} | ||
} |
@@ -10,2 +10,3 @@ const INIT = 'INIT' | ||
CREATE_CLIENT_MANAGER, | ||
GET_STATE} | ||
GET_STATE | ||
} |
@@ -8,4 +8,4 @@ import worker from './worker' | ||
import _getState from './get-state' | ||
import {trigger, pack} from './util' | ||
import {INIT, CREATE_CLIENT_STORE, CREATE_CLIENT_MANAGER} from './behavior-types' | ||
import { trigger, pack } from './util' | ||
import { INIT, CREATE_CLIENT_STORE, CREATE_CLIENT_MANAGER } from './behavior-types' | ||
@@ -15,30 +15,30 @@ let businessmanWoker = null | ||
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 => _getState(storeType, 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 => _getState( storeType, businessmanWoker ) | ||
subscribe(INIT, data => { | ||
subscribe( INIT, data => { | ||
let stores = {} | ||
let managers = {} | ||
try { | ||
data.stores.map(store => { | ||
stores[store.type] = { | ||
dispatch: (actionType, payload) => dispatch(store.type, actionType, payload), | ||
subscribe: cb => subscribe(store.type, cb), | ||
unsubscribe: cb => unsubscribe(store.type, cb), | ||
getState: () => getState(store.type) | ||
data.stores.map( store => { | ||
stores[ store.type ] = { | ||
dispatch: ( actionType, payload ) => dispatch( store.type, actionType, payload ), | ||
subscribe: cb => subscribe( store.type, cb ), | ||
unsubscribe: cb => unsubscribe( store.type, cb ), | ||
getState: () => getState( store.type ) | ||
} | ||
return store | ||
}) | ||
trigger(pack(CREATE_CLIENT_STORE, stores)) | ||
} ) | ||
trigger( pack( CREATE_CLIENT_STORE, stores ) ) | ||
managers = data.managers | ||
trigger(pack(CREATE_CLIENT_MANAGER, managers)) | ||
} catch (err) { | ||
console.error('Error in creating client store or client manager', err) | ||
trigger( pack( CREATE_CLIENT_MANAGER, managers ) ) | ||
} catch ( err ) { | ||
console.error( 'Error in creating client store or client manager', err ) | ||
} | ||
}) | ||
} ) | ||
@@ -45,0 +45,0 @@ export { |
@@ -1,3 +0,3 @@ | ||
export default (storeType, actionType, payload, worker) => { | ||
worker.postMessage([storeType, actionType, payload]) | ||
export default ( storeType, actionType, payload, worker ) => { | ||
worker.postMessage( [ storeType, actionType, payload ] ) | ||
} |
import dispatch from './dispatch' | ||
import subscribe from './subscribe' | ||
import unsubscribe from './unsubscribe' | ||
import {GET_STATE} from './behavior-types' | ||
import { GET_STATE } from './behavior-types' | ||
export default (storeType, worker) => { | ||
return new Promise((resolve, reject) => { | ||
let subscriber = (state, applied) => { | ||
if (applied !== GET_STATE) { | ||
export default ( storeType, worker ) => { | ||
return new Promise( ( resolve, reject ) => { | ||
let subscriber = ( state, applied ) => { | ||
if ( applied !== GET_STATE ) { | ||
return | ||
} | ||
unsubscribe(storeType, subscriber) | ||
resolve(state) | ||
unsubscribe( storeType, subscriber ) | ||
resolve( state ) | ||
} | ||
subscribe(storeType, subscriber) | ||
subscribe( storeType, subscriber ) | ||
try { | ||
dispatch(storeType, GET_STATE, '', worker) | ||
} catch (err) { | ||
reject(err) | ||
unsubscribe(storeType, subscriber) | ||
dispatch( storeType, GET_STATE, '', worker ) | ||
} catch ( err ) { | ||
reject( err ) | ||
unsubscribe( storeType, subscriber ) | ||
} | ||
}) | ||
} ) | ||
} |
@@ -1,11 +0,11 @@ | ||
import {trigger} from './util' | ||
import { trigger } from './util' | ||
export default (path, worker) => { | ||
export default ( path, worker ) => { | ||
try { | ||
worker = new Worker(path) | ||
worker.onmessage = message => trigger(message.data) | ||
worker = new Worker( path ) | ||
worker.onmessage = message => trigger( message.data ) | ||
return worker | ||
} catch (err) { | ||
console.error('Error in install', err) | ||
} catch ( err ) { | ||
console.error( 'Error in install', err ) | ||
} | ||
} |
@@ -1,3 +0,3 @@ | ||
export default (managerType, payload, worker) => { | ||
worker.postMessage([managerType, payload]) | ||
export default ( managerType, payload, worker ) => { | ||
worker.postMessage( [ managerType, payload ] ) | ||
} |
@@ -1,7 +0,7 @@ | ||
import {GET_STATE} from '../behavior-types' | ||
import { GET_STATE } from '../behavior-types' | ||
const actions = { | ||
[GET_STATE]: commit => commit(GET_STATE) | ||
[ GET_STATE ]: commit => commit( GET_STATE ) | ||
} | ||
export default actions |
@@ -1,7 +0,7 @@ | ||
import {GET_STATE} from '../behavior-types' | ||
import { GET_STATE } from '../behavior-types' | ||
const mutations = { | ||
[GET_STATE]: state => state | ||
[ GET_STATE ]: state => state | ||
} | ||
export default mutations |
@@ -1,2 +0,2 @@ | ||
import {pack, assign} from '../util' | ||
import { pack, assign } from '../util' | ||
import builtInMutations from './mutations' | ||
@@ -6,3 +6,3 @@ import builtInActions from './actions' | ||
class Store { | ||
constructor(opt) { | ||
constructor ( opt ) { | ||
let { | ||
@@ -17,3 +17,3 @@ state, | ||
const store = this | ||
const {dispatch, commit} = this | ||
const { dispatch, commit } = this | ||
@@ -24,9 +24,9 @@ let _state = { | ||
state = newState | ||
postMessage(pack(type, state, store.appliedMutation)) | ||
postMessage( pack( type, state, store.appliedMutation ) ) | ||
} | ||
} | ||
mutations = assign(mutations, builtInMutations) | ||
actions = assign(actions, builtInActions) | ||
mutations = assign( mutations, builtInMutations ) | ||
actions = assign( actions, builtInActions ) | ||
Object.defineProperties(this, { | ||
Object.defineProperties( this, { | ||
type: { | ||
@@ -49,3 +49,3 @@ value: type, | ||
dispatch: { | ||
value: (type, payload) => dispatch.call(store, type, payload), | ||
value: ( type, payload ) => dispatch.call( store, type, payload ), | ||
configurable: false, | ||
@@ -55,3 +55,3 @@ writable: false | ||
commit: { | ||
value: (type, payload) => commit.call(store, _state, type, payload), | ||
value: ( type, payload ) => commit.call( store, _state, type, payload ), | ||
configurable: false, | ||
@@ -64,12 +64,12 @@ writable: false | ||
} | ||
}) | ||
} ) | ||
} | ||
commit(state, type, payload) { | ||
commit ( state, type, payload ) { | ||
this.appliedMutation = type | ||
state.set(this.mutations[type](state.get(), payload)) | ||
state.set( this.mutations[ type ]( state.get(), payload ) ) | ||
} | ||
dispatch(type, payload) { | ||
this.actions[type](this.commit, payload) | ||
dispatch ( type, payload ) { | ||
this.actions[ type ]( this.commit, payload ) | ||
} | ||
@@ -76,0 +76,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 observable from 'riot-observable' | ||
const o = new function () { | ||
observable(this) | ||
observable( this ) | ||
}() | ||
export const trigger = function (data) { | ||
o.trigger(data.type, data.payload, data.applied) | ||
export const trigger = function ( data ) { | ||
o.trigger( data.type, data.payload, data.applied ) | ||
} | ||
export const on = function (type, cb) { | ||
o.on(type, cb) | ||
export const on = function ( type, cb ) { | ||
o.on( type, cb ) | ||
} | ||
export const off = function (type, cb) { | ||
if (cb) { | ||
o.off(type, cb) | ||
} else { | ||
o.off(type) | ||
export const off = function ( type, cb ) { | ||
if ( cb ) { | ||
o.off( type, cb ) | ||
} else { | ||
o.off( type ) | ||
} | ||
} | ||
export const pack = function (type = '', payload = {}, applied) { | ||
if (applied) { | ||
return {type: type, payload: payload, applied: applied} | ||
export const pack = function ( type = '', payload = {}, applied ) { | ||
if ( applied ) { | ||
return { type: type, payload: payload, applied: applied } | ||
} | ||
return {type: type, payload: payload} | ||
return { type: type, payload: payload } | ||
} | ||
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 ] | ||
} | ||
@@ -40,0 +40,0 @@ } |
import Store from './store/store' | ||
import {pack} from './util' | ||
import {INIT} from './behavior-types' | ||
import { pack } from './util' | ||
import { INIT } from './behavior-types' | ||
@@ -16,12 +16,12 @@ let stores = {} | ||
const data = e.data | ||
if (data.length > 2) { | ||
stores[data[0]].dispatch(data[1], data[2]) | ||
} else if (data.length > 1) { | ||
managers[data[0]](stores, data[1]) | ||
if ( data.length > 2 ) { | ||
stores[ data[ 0 ] ].dispatch( data[ 1 ], data[ 2 ] ) | ||
} else if ( data.length > 1 ) { | ||
managers[ data[ 0 ] ]( stores, data[ 1 ] ) | ||
} | ||
} | ||
postMessage(pack(INIT, {stores: forClient.stores, managers: forClient.managers})) | ||
postMessage( pack( INIT, { stores: forClient.stores, managers: forClient.managers } ) ) | ||
}, | ||
registerStore: config => { | ||
const store = new Store(config) | ||
const store = new Store( config ) | ||
const { | ||
@@ -31,8 +31,8 @@ type, | ||
} = store | ||
if (!(type in stores)) { | ||
stores[type] = store | ||
forClient.stores.push({ | ||
if ( !( type in stores ) ) { | ||
stores[ type ] = store | ||
forClient.stores.push( { | ||
type: type, | ||
actions: Object.keys(actions) | ||
}) | ||
actions: Object.keys( actions ) | ||
} ) | ||
} | ||
@@ -45,7 +45,7 @@ }, | ||
} = config | ||
if (!(type in managers)) { | ||
managers[type] = handler | ||
forClient.managers.push({ | ||
if ( !( type in managers ) ) { | ||
managers[ type ] = handler | ||
forClient.managers.push( { | ||
type: type | ||
}) | ||
} ) | ||
} | ||
@@ -55,2 +55,2 @@ } | ||
export default Object.freeze(worker) | ||
export default Object.freeze( worker ) |
@@ -1,44 +0,44 @@ | ||
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 ) | ||
} | ||
} | ||
}) | ||
} ) | ||
worker.registerStore({ | ||
worker.registerStore( { | ||
type: 'message', | ||
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() |
Sorry, the diff of this file is not supported yet
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
126346
28
1564
12