@@ -23,9 +23,7 @@ function evaluateResult(result, reactorState) {
23
23
* @param {Object<String, Store> } stores
24
24
* @return {ReactorState }
25
25
*/
26
- exports . registerStores = function ( reactorState , stores ) {
27
- const debug = reactorState . get ( 'debug' )
28
-
26
+ export function registerStores ( reactorState , stores ) {
29
27
return reactorState . withMutations ( ( reactorState ) => {
30
28
each ( stores , ( store , id ) => {
31
29
if ( reactorState . getIn ( [ 'stores' , id ] ) ) {
@@ -36,7 +34,7 @@ exports.registerStores = function(reactorState, stores) {
36
34
37
35
const initialState = store . getInitialState ( )
38
36
39
- if ( debug && ! isImmutableValue ( initialState ) ) {
37
+ if ( ! getOption ( reactorState , 'allowNonImmutableStores' ) && ! isImmutableValue ( initialState ) ) {
40
38
throw new Error ( 'Store getInitialState() must return an immutable value, did you forget to call toImmutable' )
41
39
}
42
40
@@ -56,15 +54,12 @@ exports.registerStores = function(reactorState, stores) {
56
54
* @param {* } payload
57
55
* @return {ReactorState }
58
56
*/
59
- exports . dispatch = function ( reactorState , actionType , payload ) {
57
+ export function dispatch ( reactorState , actionType , payload ) {
60
58
const currState = reactorState . get ( 'state' )
61
- const debug = reactorState . get ( 'debug' )
62
59
let dirtyStores = reactorState . get ( 'dirtyStores' )
63
60
64
61
const nextState = currState . withMutations ( state => {
65
- if ( debug ) {
66
- logging . dispatchStart ( actionType , payload )
67
- }
62
+ logging . dispatchStart ( reactorState , actionType , payload )
68
63
69
64
// let each store handle the message
70
65
reactorState . get ( 'stores' ) . forEach ( ( store , id ) => {
@@ -75,13 +70,13 @@ exports.dispatch = function(reactorState, actionType, payload) {
75
70
newState = store . handle ( currState , actionType , payload )
76
71
} catch ( e ) {
77
72
// ensure console.group is properly closed
78
- logging . dispatchError ( e . message )
73
+ logging . dispatchError ( reactorState , e . message )
79
74
throw e
80
75
}
81
76
82
- if ( debug && newState === undefined ) {
77
+ if ( ! getOption ( reactorState , 'allowUndefinedDispatch' ) && newState === undefined ) {
83
78
const errorMsg = 'Store handler must return a value, did you forget a return statement'
84
- logging . dispatchError ( errorMsg )
79
+ logging . dispatchError ( reactorState , errorMsg )
85
80
throw new Error ( errorMsg )
86
81
}
87
82
@@ -91,15 +86,9 @@ exports.dispatch = function(reactorState, actionType, payload) {
91
86
// if the store state changed add store to list of dirty stores
92
87
dirtyStores = dirtyStores . add ( id )
93
88
}
94
-
95
- if ( debug ) {
96
- logging . storeHandled ( id , currState , newState )
97
- }
98
89
} )
99
90
100
- if ( debug ) {
101
- logging . dispatchEnd ( state )
102
- }
91
+ logging . dispatchEnd ( reactorState , state , dirtyStores )
103
92
} )
104
93
105
94
const nextReactorState = reactorState
@@ -115,7 +104,7 @@ exports.dispatch = function(reactorState, actionType, payload) {
115
104
* @param {Immutable.Map } state
116
105
* @return {ReactorState }
117
106
*/
118
- exports . loadState = function ( reactorState , state ) {
107
+ export function loadState ( reactorState , state ) {
119
108
let dirtyStores = [ ]
120
109
const stateToLoad = toImmutable ( { } ) . withMutations ( stateToLoad => {
121
110
each ( state , ( serializedStoreState , storeId ) => {
@@ -154,7 +143,7 @@ exports.loadState = function(reactorState, state) {
154
143
* @param {function } handler
155
144
* @return {ObserveResult }
156
145
*/
157
- exports . addObserver = function ( observerState , getter , handler ) {
146
+ export function addObserver ( observerState , getter , handler ) {
158
147
// use the passed in getter as the key so we can rely on a byreference call for unobserve
159
148
const getterKey = getter
160
149
if ( isKeyPath ( getter ) ) {
@@ -180,7 +169,7 @@ exports.addObserver = function(observerState, getter, handler) {
180
169
storeDeps . forEach ( storeId => {
181
170
let path = [ 'stores' , storeId ]
182
171
if ( ! map . hasIn ( path ) ) {
183
- map . setIn ( path , Immutable . Set ( [ ] ) )
172
+ map . setIn ( path , Immutable . Set ( ) )
184
173
}
185
174
map . updateIn ( [ 'stores' , storeId ] , observerIds => observerIds . add ( currId ) )
186
175
} )
@@ -197,6 +186,15 @@ exports.addObserver = function(observerState, getter, handler) {
197
186
}
198
187
}
199
188
189
+ /**
190
+ * @param {ReactorState } reactorState
191
+ * @param {String } option
192
+ * @return {Boolean }
193
+ */
194
+ export function getOption ( reactorState , option ) {
195
+ return reactorState . getIn ( [ 'options' , option ] , false )
196
+ }
197
+
200
198
/**
201
199
* Use cases
202
200
* removeObserver(observerState, [])
@@ -210,7 +208,7 @@ exports.addObserver = function(observerState, getter, handler) {
210
208
* @param {Function } handler
211
209
* @return {ObserverState }
212
210
*/
213
- exports . removeObserver = function ( observerState , getter , handler ) {
211
+ export function removeObserver ( observerState , getter , handler ) {
214
212
const entriesToRemove = observerState . get ( 'observersMap' ) . filter ( entry => {
215
213
// use the getterKey in the case of a keyPath is transformed to a getter in addObserver
216
214
let entryGetter = entry . get ( 'getterKey' )
@@ -227,7 +225,7 @@ exports.removeObserver = function(observerState, getter, handler) {
227
225
} )
228
226
229
227
return observerState . withMutations ( map => {
230
- entriesToRemove . forEach ( entry => exports . removeObserverByEntry ( map , entry ) )
228
+ entriesToRemove . forEach ( entry => removeObserverByEntry ( map , entry ) )
231
229
} )
232
230
}
233
231
@@ -237,7 +235,7 @@ exports.removeObserver = function(observerState, getter, handler) {
237
235
* @param {Immutable.Map } entry
238
236
* @return {ObserverState }
239
237
*/
240
- exports . removeObserverByEntry = function ( observerState , entry ) {
238
+ export function removeObserverByEntry ( observerState , entry ) {
241
239
return observerState . withMutations ( map => {
242
240
const id = entry . get ( 'id' )
243
241
const storeDeps = entry . get ( 'storeDeps' )
@@ -264,8 +262,7 @@ exports.removeObserverByEntry = function(observerState, entry) {
264
262
* @param {ReactorState } reactorState
265
263
* @return {ReactorState }
266
264
*/
267
- exports . reset = function ( reactorState ) {
268
- const debug = reactorState . get ( 'debug' )
265
+ export function reset ( reactorState ) {
269
266
const prevState = reactorState . get ( 'state' )
270
267
271
268
return reactorState . withMutations ( reactorState => {
@@ -274,17 +271,17 @@ exports.reset = function(reactorState) {
274
271
storeMap . forEach ( ( store , id ) => {
275
272
const storeState = prevState . get ( id )
276
273
const resetStoreState = store . handleReset ( storeState )
277
- if ( debug && resetStoreState === undefined ) {
274
+ if ( ! getOption ( reactorState , 'allowUndefinedDispatch' ) && resetStoreState === undefined ) {
278
275
throw new Error ( 'Store handleReset() must return a value, did you forget a return statement' )
279
276
}
280
- if ( debug && ! isImmutableValue ( resetStoreState ) ) {
277
+ if ( ! getOption ( reactorState , 'allowNonImmutableStores' ) && ! isImmutableValue ( resetStoreState ) ) {
281
278
throw new Error ( 'Store reset state must be an immutable value, did you forget to call toImmutable' )
282
279
}
283
280
reactorState . setIn ( [ 'state' , id ] , resetStoreState )
284
281
} )
285
282
286
283
reactorState . update ( 'storeStates' , storeStates => incrementStoreStates ( storeStates , storeIds ) )
287
- exports . resetDirtyStores ( reactorState )
284
+ resetDirtyStores ( reactorState )
288
285
} )
289
286
}
290
287
@@ -293,7 +290,7 @@ exports.reset = function(reactorState) {
293
290
* @param {KeyPath|Gettter } keyPathOrGetter
294
291
* @return {EvaluateResult }
295
292
*/
296
- exports . evaluate = function evaluate ( reactorState , keyPathOrGetter ) {
293
+ export function evaluate ( reactorState , keyPathOrGetter ) {
297
294
const state = reactorState . get ( 'state' )
298
295
299
296
if ( isKeyPath ( keyPathOrGetter ) ) {
@@ -331,7 +328,7 @@ exports.evaluate = function evaluate(reactorState, keyPathOrGetter) {
331
328
* @param {ReactorState } reactorState
332
329
* @return {Object }
333
330
*/
334
- exports . serialize = function ( reactorState ) {
331
+ export function serialize ( reactorState ) {
335
332
let serialized = { }
336
333
reactorState . get ( 'stores' ) . forEach ( ( store , id ) => {
337
334
let storeState = reactorState . getIn ( [ 'state' , id ] )
@@ -348,7 +345,7 @@ exports.serialize = function(reactorState) {
348
345
* @param {ReactorState } reactorState
349
346
* @return {ReactorState }
350
347
*/
351
- exports . resetDirtyStores = function ( reactorState ) {
348
+ export function resetDirtyStores ( reactorState ) {
352
349
return reactorState . set ( 'dirtyStores' , Immutable . Set ( ) )
353
350
}
354
351
0 commit comments