-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstore.js
180 lines (163 loc) · 4.94 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { storeChannel, dispatcherChannel } from "./bus";
import { entries } from "./utils";
import dispatcher from "./dispatcher";
import _ from "lodash";
import { mixin } from "./mixins";
export const stores = {};
function buildActionList( handlers ) {
const actionList = [];
for ( const [ key, handler ] of entries( handlers ) ) {
actionList.push( {
actionType: key,
waitFor: handler.waitFor || []
} );
}
return actionList;
}
function ensureStoreOptions( options, handlers, store ) {
const namespace = ( options && options.namespace ) || store.namespace;
if ( namespace in stores ) {
throw new Error( `The store namespace "${ namespace }" already exists.` );
}
if ( !namespace ) {
throw new Error( "A lux store must have a namespace value provided" );
}
if ( !handlers || !Object.keys( handlers ).length ) {
throw new Error( "A lux store must have action handler methods provided" );
}
}
function getHandlerObject( key, listeners ) {
return {
waitFor: [],
handler() {
let changed = 0;
const args = Array.from( arguments );
listeners[ key ].forEach( function( listener ) {
changed += ( listener.apply( this, args ) === false ? 0 : 1 );
}.bind( this ) );
return changed > 0;
}
};
}
function updateWaitFor( source, handlerObject ) {
if ( source.waitFor ) {
source.waitFor.forEach( function( dep ) {
if ( handlerObject.waitFor.indexOf( dep ) === -1 ) {
handlerObject.waitFor.push( dep );
}
} );
}
}
function addListeners( listeners, key, handler ) {
listeners[ key ] = listeners[ key ] || [];
listeners[ key ].push( handler.handler || handler );
}
function processStoreArgs( ...options ) {
const listeners = {};
const handlers = {};
const state = {};
const otherOpts = {};
options.forEach( function( o ) {
let opt;
if ( o ) {
opt = _.clone( o );
_.merge( state, opt.state );
if ( opt.handlers ) {
Object.keys( opt.handlers ).forEach( function( key ) {
const handler = opt.handlers[ key ];
// set up the actual handler method that will be executed
// as the store handles a dispatched action
handlers[ key ] = handlers[ key ] || getHandlerObject( key, listeners );
// ensure that the handler definition has a list of all stores
// being waited upon
updateWaitFor( handler, handlers[ key ] );
// Add the original handler method(s) to the listeners queue
addListeners( listeners, key, handler );
} );
}
delete opt.handlers;
delete opt.state;
_.merge( otherOpts, opt );
}
} );
return [ state, handlers, otherOpts ];
}
export class Store {
constructor( ...opt ) {
let [ state, handlers, options ] = processStoreArgs( ...opt ); // eslint-disable-line prefer-const
ensureStoreOptions( options, handlers, this );
const namespace = options.namespace;
Object.assign( this, options );
stores[ namespace ] = this;
let inDispatch = false;
this.hasChanged = false;
this.getState = function() {
return state;
};
this.setState = function( newState ) {
if ( !inDispatch ) {
throw new Error( "setState can only be called during a dispatch cycle from a store action handler." );
}
state = Object.assign( state, newState );
};
this.replaceState = function( newState ) {
if ( !inDispatch ) {
throw new Error( "replaceState can only be called during a dispatch cycle from a store action handler." );
}
// we preserve the underlying state ref, but clear it
Object.keys( state ).forEach( function( key ) {
delete state[ key ];
} );
state = Object.assign( state, newState );
};
this.flush = function flush() {
inDispatch = false;
if ( this.hasChanged ) {
this.hasChanged = false;
storeChannel.publish( `${ this.namespace }.changed` );
}
};
mixin( this, mixin.actionListener( {
context: this,
channel: dispatcherChannel,
topic: `${ namespace }.handle.*`,
handlers,
handlerFn: function( data ) {
if ( handlers.hasOwnProperty( data.actionType ) ) {
inDispatch = true;
const res = handlers[ data.actionType ].handler.apply( this, data.actionArgs.concat( data.deps ) );
this.hasChanged = !( res === false );
dispatcherChannel.publish(
`${ this.namespace }.handled.${ data.actionType }`,
{ hasChanged: this.hasChanged, namespace: this.namespace }
);
}
}.bind( this )
} ) );
this.__subscription = {
notify: dispatcherChannel.subscribe( "notify", () => this.flush() )
.constraint( () => inDispatch )
};
dispatcher.registerStore(
{
namespace,
actions: buildActionList( handlers )
}
);
}
// Need to build in behavior to remove this store
// from the dispatcher's actionMap as well!
dispose() {
/*eslint-disable */
for ( let [ k, subscription ] of entries( this.__subscription ) ) {
subscription.unsubscribe();
}
/* eslint-enable */
delete stores[ this.namespace ];
dispatcher.removeStore( this.namespace );
this.luxCleanup();
}
}
export function removeStore( namespace ) {
stores[ namespace ].dispose();
}