businessman
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -5,2 +5,3 @@ define(['exports'], function (exports) { 'use strict'; | ||
var CREATE_CLIENT_STORE = 'CREATE_CLIENT_STORE'; | ||
var CREATE_CLIENT_MANAGER = 'CREATE_CLIENT_MANAGER'; | ||
var GET_STATE = 'GET_STATE'; | ||
@@ -242,3 +243,7 @@ | ||
var stores = {}; | ||
var forFront = []; | ||
var managers = {}; | ||
var forClient = { | ||
stores: [], | ||
managers: [] | ||
}; | ||
@@ -248,19 +253,29 @@ var worker = { | ||
onmessage = function (e) { | ||
var storeType = e.data[ 0 ], | ||
actionType = e.data[ 1 ], | ||
payload = e.data[ 2 ]; | ||
stores[ storeType ].dispatch( actionType, payload ); | ||
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 ] ); } | ||
}; | ||
postMessage( pack( INIT, { stores: forFront } ) ); | ||
postMessage( pack( INIT, { stores: forClient.stores, managers: forClient.managers } ) ); | ||
}, | ||
registerStore: function (config) { | ||
var store = new Store( config ), | ||
type = store.type; | ||
var store = new Store( config ); | ||
var type = store.type; | ||
var actions = store.actions; | ||
if ( ! ( type in stores ) ) { | ||
stores[ type ] = store; | ||
forFront.push( { | ||
forClient.stores.push( { | ||
type: type, | ||
actions: Object.keys( store.actions ) | ||
actions: Object.keys( actions ) | ||
} ); | ||
} | ||
}, | ||
registerManager: function ( config ) { | ||
var type = config.type; | ||
var handler = config.handler; | ||
if ( ! ( type in managers ) ) { | ||
managers[ type ] = handler; | ||
forClient.managers.push( { | ||
type: type | ||
} ); | ||
} | ||
} | ||
@@ -285,2 +300,6 @@ }; | ||
var _manager = function ( managerType, payload, worker ) { | ||
worker.postMessage( [ managerType, payload ] ); | ||
}; | ||
var subscribe$1 = function ( type, cb ) { | ||
@@ -321,2 +340,3 @@ on( type, cb ); | ||
var dispatch$1 = function ( storeType, actionType, payload ) { return dispatch$2( storeType, actionType, payload, businessmanWoker ); }; | ||
var manager = function ( managerType, payload ) { return _manager( managerType, payload, businessmanWoker ); }; | ||
var subscribe = function ( type, cb ) { return subscribe$1( type, cb ); }; | ||
@@ -327,3 +347,4 @@ var unsubscribe = function ( type, cb ) { return unsubscribe$1( type, cb ); }; | ||
subscribe( INIT, function ( data ) { | ||
var stores = {}; | ||
var stores = {}, | ||
managers = {}; | ||
try { | ||
@@ -339,4 +360,6 @@ data.stores.map( function ( store ) { | ||
trigger( pack( CREATE_CLIENT_STORE, stores ) ); | ||
managers = data.managers; | ||
trigger( pack( CREATE_CLIENT_MANAGER, managers ) ); | ||
} catch ( e ) { | ||
console.error( 'Error in creating client store', e ); | ||
console.error( 'Error in creating client store or client manager', e ); | ||
} | ||
@@ -347,2 +370,3 @@ } ); | ||
exports.dispatch = dispatch$1; | ||
exports.manager = manager; | ||
exports.subscribe = subscribe; | ||
@@ -349,0 +373,0 @@ exports.unsubscribe = unsubscribe; |
var INIT = 'INIT'; | ||
var CREATE_CLIENT_STORE = 'CREATE_CLIENT_STORE'; | ||
var CREATE_CLIENT_MANAGER = 'CREATE_CLIENT_MANAGER'; | ||
var GET_STATE = 'GET_STATE'; | ||
@@ -239,3 +240,7 @@ | ||
var stores = {}; | ||
var forFront = []; | ||
var managers = {}; | ||
var forClient = { | ||
stores: [], | ||
managers: [] | ||
}; | ||
@@ -245,19 +250,29 @@ var worker = { | ||
onmessage = function (e) { | ||
var storeType = e.data[ 0 ], | ||
actionType = e.data[ 1 ], | ||
payload = e.data[ 2 ]; | ||
stores[ storeType ].dispatch( actionType, payload ); | ||
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 ] ); } | ||
}; | ||
postMessage( pack( INIT, { stores: forFront } ) ); | ||
postMessage( pack( INIT, { stores: forClient.stores, managers: forClient.managers } ) ); | ||
}, | ||
registerStore: function (config) { | ||
var store = new Store( config ), | ||
type = store.type; | ||
var store = new Store( config ); | ||
var type = store.type; | ||
var actions = store.actions; | ||
if ( ! ( type in stores ) ) { | ||
stores[ type ] = store; | ||
forFront.push( { | ||
forClient.stores.push( { | ||
type: type, | ||
actions: Object.keys( store.actions ) | ||
actions: Object.keys( actions ) | ||
} ); | ||
} | ||
}, | ||
registerManager: function ( config ) { | ||
var type = config.type; | ||
var handler = config.handler; | ||
if ( ! ( type in managers ) ) { | ||
managers[ type ] = handler; | ||
forClient.managers.push( { | ||
type: type | ||
} ); | ||
} | ||
} | ||
@@ -282,2 +297,6 @@ }; | ||
var _manager = function ( managerType, payload, worker ) { | ||
worker.postMessage( [ managerType, payload ] ); | ||
}; | ||
var subscribe$1 = function ( type, cb ) { | ||
@@ -318,2 +337,3 @@ on( type, cb ); | ||
var dispatch$1 = function ( storeType, actionType, payload ) { return dispatch$2( storeType, actionType, payload, businessmanWoker ); }; | ||
var manager = function ( managerType, payload ) { return _manager( managerType, payload, businessmanWoker ); }; | ||
var subscribe = function ( type, cb ) { return subscribe$1( type, cb ); }; | ||
@@ -324,3 +344,4 @@ var unsubscribe = function ( type, cb ) { return unsubscribe$1( type, cb ); }; | ||
subscribe( INIT, function ( data ) { | ||
var stores = {}; | ||
var stores = {}, | ||
managers = {}; | ||
try { | ||
@@ -336,7 +357,9 @@ data.stores.map( function ( store ) { | ||
trigger( pack( CREATE_CLIENT_STORE, stores ) ); | ||
managers = data.managers; | ||
trigger( pack( CREATE_CLIENT_MANAGER, managers ) ); | ||
} catch ( e ) { | ||
console.error( 'Error in creating client store', e ); | ||
console.error( 'Error in creating client store or client manager', e ); | ||
} | ||
} ); | ||
export { install, dispatch$1 as dispatch, subscribe, unsubscribe, getState, worker$1 as worker }; | ||
export { install, dispatch$1 as dispatch, manager, subscribe, unsubscribe, getState, worker$1 as worker }; |
@@ -9,2 +9,3 @@ (function (global, factory) { | ||
var CREATE_CLIENT_STORE = 'CREATE_CLIENT_STORE'; | ||
var CREATE_CLIENT_MANAGER = 'CREATE_CLIENT_MANAGER'; | ||
var GET_STATE = 'GET_STATE'; | ||
@@ -246,3 +247,7 @@ | ||
var stores = {}; | ||
var forFront = []; | ||
var managers = {}; | ||
var forClient = { | ||
stores: [], | ||
managers: [] | ||
}; | ||
@@ -252,19 +257,29 @@ var worker = { | ||
onmessage = function (e) { | ||
var storeType = e.data[ 0 ], | ||
actionType = e.data[ 1 ], | ||
payload = e.data[ 2 ]; | ||
stores[ storeType ].dispatch( actionType, payload ); | ||
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 ] ); } | ||
}; | ||
postMessage( pack( INIT, { stores: forFront } ) ); | ||
postMessage( pack( INIT, { stores: forClient.stores, managers: forClient.managers } ) ); | ||
}, | ||
registerStore: function (config) { | ||
var store = new Store( config ), | ||
type = store.type; | ||
var store = new Store( config ); | ||
var type = store.type; | ||
var actions = store.actions; | ||
if ( ! ( type in stores ) ) { | ||
stores[ type ] = store; | ||
forFront.push( { | ||
forClient.stores.push( { | ||
type: type, | ||
actions: Object.keys( store.actions ) | ||
actions: Object.keys( actions ) | ||
} ); | ||
} | ||
}, | ||
registerManager: function ( config ) { | ||
var type = config.type; | ||
var handler = config.handler; | ||
if ( ! ( type in managers ) ) { | ||
managers[ type ] = handler; | ||
forClient.managers.push( { | ||
type: type | ||
} ); | ||
} | ||
} | ||
@@ -289,2 +304,6 @@ }; | ||
var _manager = function ( managerType, payload, worker ) { | ||
worker.postMessage( [ managerType, payload ] ); | ||
}; | ||
var subscribe$1 = function ( type, cb ) { | ||
@@ -325,2 +344,3 @@ on( type, cb ); | ||
var dispatch$1 = function ( storeType, actionType, payload ) { return dispatch$2( storeType, actionType, payload, businessmanWoker ); }; | ||
var manager = function ( managerType, payload ) { return _manager( managerType, payload, businessmanWoker ); }; | ||
var subscribe = function ( type, cb ) { return subscribe$1( type, cb ); }; | ||
@@ -331,3 +351,4 @@ var unsubscribe = function ( type, cb ) { return unsubscribe$1( type, cb ); }; | ||
subscribe( INIT, function ( data ) { | ||
var stores = {}; | ||
var stores = {}, | ||
managers = {}; | ||
try { | ||
@@ -343,4 +364,6 @@ data.stores.map( function ( store ) { | ||
trigger( pack( CREATE_CLIENT_STORE, stores ) ); | ||
managers = data.managers; | ||
trigger( pack( CREATE_CLIENT_MANAGER, managers ) ); | ||
} catch ( e ) { | ||
console.error( 'Error in creating client store', e ); | ||
console.error( 'Error in creating client store or client manager', e ); | ||
} | ||
@@ -351,2 +374,3 @@ } ); | ||
exports.dispatch = dispatch$1; | ||
exports.manager = manager; | ||
exports.subscribe = subscribe; | ||
@@ -353,0 +377,0 @@ exports.unsubscribe = unsubscribe; |
{ | ||
"name": "businessman", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Multi-thread State Management by the Worker API", | ||
@@ -5,0 +5,0 @@ "main": "dist/businessman.js", |
@@ -77,2 +77,30 @@ Multi-thread State Management by the Worker API. | ||
## Create Manager | ||
Mutation and action belong to one store. | ||
If you want to dispatch to multiple stores at the same time, you can use the manager. | ||
The manager has registered it using `worker.registerManager()`. | ||
``` | ||
import { worker } from 'businessman' | ||
worker.registerManager( { | ||
type: 'countUpMessage', | ||
handler: ( stores, num = 1 ) => { | ||
stores.counter.dispatch( 'increment', num ) | ||
stores.message.dispatch( 'update', `${num} has been added to the counter` ) | ||
} | ||
} ) | ||
``` | ||
Call `manager()` with manager type and payload specified. | ||
``` | ||
import { manager } from 'businessman' | ||
manager( 'countUpMessage', 1 ) | ||
``` | ||
## Start worker | ||
@@ -79,0 +107,0 @@ |
const INIT = 'INIT' | ||
const CREATE_CLIENT_STORE = 'CREATE_CLIENT_STORE' | ||
const CREATE_CLIENT_MANAGER = 'CREATE_CLIENT_MANAGER' | ||
const GET_STATE = 'GET_STATE' | ||
export { INIT, CREATE_CLIENT_STORE, GET_STATE } | ||
export { | ||
INIT, | ||
CREATE_CLIENT_STORE, | ||
CREATE_CLIENT_MANAGER, | ||
GET_STATE } |
import worker from './worker' | ||
import _install from './install' | ||
import _dispatch from './dispatch' | ||
import _manager from './manager' | ||
import _subscribe from './subscribe' | ||
@@ -8,3 +9,3 @@ import _unsubscribe from './unsubscribe' | ||
import { trigger, pack } from './util' | ||
import { INIT, CREATE_CLIENT_STORE } from './behaviorTypes' | ||
import { INIT, CREATE_CLIENT_STORE, CREATE_CLIENT_MANAGER } from './behaviorTypes' | ||
@@ -17,2 +18,3 @@ let businessmanWoker = null | ||
dispatch = ( storeType, actionType, payload ) => _dispatch( storeType, actionType, payload, businessmanWoker ), | ||
manager = ( managerType, payload ) => _manager( managerType, payload, businessmanWoker ), | ||
subscribe = ( type, cb ) => _subscribe( type, cb ), | ||
@@ -23,3 +25,4 @@ unsubscribe = ( type, cb ) => _unsubscribe( type, cb ), | ||
subscribe( INIT, ( data ) => { | ||
let stores = {} | ||
let stores = {}, | ||
managers = {} | ||
try { | ||
@@ -35,4 +38,6 @@ data.stores.map( ( store ) => { | ||
trigger( pack( CREATE_CLIENT_STORE, stores ) ) | ||
managers = data.managers | ||
trigger( pack( CREATE_CLIENT_MANAGER, managers ) ) | ||
} catch ( e ) { | ||
console.error( 'Error in creating client store', e ) | ||
console.error( 'Error in creating client store or client manager', e ) | ||
} | ||
@@ -44,2 +49,3 @@ } ) | ||
dispatch, | ||
manager, | ||
subscribe, | ||
@@ -46,0 +52,0 @@ unsubscribe, |
@@ -6,3 +6,7 @@ import Store from './store/Store' | ||
let stores = {}, | ||
forFront = [] | ||
managers = {}, | ||
forClient = { | ||
stores: [], | ||
managers: [] | ||
} | ||
@@ -12,19 +16,33 @@ const worker = { | ||
onmessage = e => { | ||
let storeType = e.data[ 0 ], | ||
actionType = e.data[ 1 ], | ||
payload = e.data[ 2 ] | ||
stores[ storeType ].dispatch( actionType, payload ) | ||
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 ] ) | ||
} | ||
postMessage( pack( INIT, { stores: forFront } ) ) | ||
postMessage( pack( INIT, { stores: forClient.stores, managers: forClient.managers } ) ) | ||
}, | ||
registerStore: config => { | ||
let store = new Store( config ), | ||
type = store.type | ||
const store = new Store( config ), | ||
{ | ||
type, | ||
actions | ||
} = store | ||
if ( ! ( type in stores ) ) { | ||
stores[ type ] = store | ||
forFront.push( { | ||
forClient.stores.push( { | ||
type: type, | ||
actions: Object.keys( store.actions ) | ||
actions: Object.keys( actions ) | ||
} ) | ||
} | ||
}, | ||
registerManager: ( config ) => { | ||
const { | ||
type, | ||
handler | ||
} = config | ||
if ( ! ( type in managers ) ) { | ||
managers[ type ] = handler | ||
forClient.managers.push( { | ||
type: type | ||
} ) | ||
} | ||
} | ||
@@ -31,0 +49,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { install, dispatch, subscribe, unsubscribe, getState } from '../src/businessman' | ||
import { install, dispatch, manager, subscribe, unsubscribe, getState } from '../src/businessman' | ||
@@ -111,2 +111,14 @@ describe( 'businessman specs', function () { | ||
it( 'Execute action crossed to multiple stores by manager', function ( done ) { | ||
manager( 'countUpMessage', 1 ) | ||
stores.counter.subscribe( ( state ) => { | ||
expect( state ).to.be( 123456789 + 1 ) | ||
} ) | ||
stores.message.unsubscribe() | ||
stores.message.subscribe( ( state ) => { | ||
expect( state ).to.be( '1 has been added to the counter' ) | ||
done() | ||
} ) | ||
} ) | ||
} ) |
@@ -39,2 +39,10 @@ import { worker } from '../src/businessman' | ||
worker.registerManager( { | ||
type: 'countUpMessage', | ||
handler: ( stores, num = 1 ) => { | ||
stores.counter.dispatch( 'increment', num ) | ||
stores.message.dispatch( 'update', `${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
130303
28
1510
218