@@ -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 , 'throwOnNonImmutableStore' ) && ! 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 , 'throwOnUndefinedDispatch' ) && 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,19 @@ 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
+ const value = reactorState . getIn ( [ 'options' , option ] )
196
+ if ( value === undefined ) {
197
+ throw new Error ( 'Invalid option: ' + option )
198
+ }
199
+ return value
200
+ }
201
+
200
202
/**
201
203
* Use cases
202
204
* removeObserver(observerState, [])
@@ -210,7 +212,7 @@ exports.addObserver = function(observerState, getter, handler) {
210
212
* @param {Function } handler
211
213
* @return {ObserverState }
212
214
*/
213
- exports . removeObserver = function ( observerState , getter , handler ) {
215
+ export function removeObserver ( observerState , getter , handler ) {
214
216
const entriesToRemove = observerState . get ( 'observersMap' ) . filter ( entry => {
215
217
// use the getterKey in the case of a keyPath is transformed to a getter in addObserver
216
218
let entryGetter = entry . get ( 'getterKey' )
@@ -227,7 +229,7 @@ exports.removeObserver = function(observerState, getter, handler) {
227
229
} )
228
230
229
231
return observerState . withMutations ( map => {
230
- entriesToRemove . forEach ( entry => exports . removeObserverByEntry ( map , entry ) )
232
+ entriesToRemove . forEach ( entry => removeObserverByEntry ( map , entry ) )
231
233
} )
232
234
}
233
235
@@ -237,7 +239,7 @@ exports.removeObserver = function(observerState, getter, handler) {
237
239
* @param {Immutable.Map } entry
238
240
* @return {ObserverState }
239
241
*/
240
- exports . removeObserverByEntry = function ( observerState , entry ) {
242
+ export function removeObserverByEntry ( observerState , entry ) {
241
243
return observerState . withMutations ( map => {
242
244
const id = entry . get ( 'id' )
243
245
const storeDeps = entry . get ( 'storeDeps' )
@@ -264,8 +266,7 @@ exports.removeObserverByEntry = function(observerState, entry) {
264
266
* @param {ReactorState } reactorState
265
267
* @return {ReactorState }
266
268
*/
267
- exports . reset = function ( reactorState ) {
268
- const debug = reactorState . get ( 'debug' )
269
+ export function reset ( reactorState ) {
269
270
const prevState = reactorState . get ( 'state' )
270
271
271
272
return reactorState . withMutations ( reactorState => {
@@ -274,17 +275,17 @@ exports.reset = function(reactorState) {
274
275
storeMap . forEach ( ( store , id ) => {
275
276
const storeState = prevState . get ( id )
276
277
const resetStoreState = store . handleReset ( storeState )
277
- if ( debug && resetStoreState === undefined ) {
278
+ if ( getOption ( reactorState , 'throwOnUndefinedDispatch' ) && resetStoreState === undefined ) {
278
279
throw new Error ( 'Store handleReset() must return a value, did you forget a return statement' )
279
280
}
280
- if ( debug && ! isImmutableValue ( resetStoreState ) ) {
281
+ if ( getOption ( reactorState , 'throwOnNonImmutableStore' ) && ! isImmutableValue ( resetStoreState ) ) {
281
282
throw new Error ( 'Store reset state must be an immutable value, did you forget to call toImmutable' )
282
283
}
283
284
reactorState . setIn ( [ 'state' , id ] , resetStoreState )
284
285
} )
285
286
286
287
reactorState . update ( 'storeStates' , storeStates => incrementStoreStates ( storeStates , storeIds ) )
287
- exports . resetDirtyStores ( reactorState )
288
+ resetDirtyStores ( reactorState )
288
289
} )
289
290
}
290
291
@@ -293,7 +294,7 @@ exports.reset = function(reactorState) {
293
294
* @param {KeyPath|Gettter } keyPathOrGetter
294
295
* @return {EvaluateResult }
295
296
*/
296
- exports . evaluate = function evaluate ( reactorState , keyPathOrGetter ) {
297
+ export function evaluate ( reactorState , keyPathOrGetter ) {
297
298
const state = reactorState . get ( 'state' )
298
299
299
300
if ( isKeyPath ( keyPathOrGetter ) ) {
@@ -331,7 +332,7 @@ exports.evaluate = function evaluate(reactorState, keyPathOrGetter) {
331
332
* @param {ReactorState } reactorState
332
333
* @return {Object }
333
334
*/
334
- exports . serialize = function ( reactorState ) {
335
+ export function serialize ( reactorState ) {
335
336
let serialized = { }
336
337
reactorState . get ( 'stores' ) . forEach ( ( store , id ) => {
337
338
let storeState = reactorState . getIn ( [ 'state' , id ] )
@@ -348,7 +349,7 @@ exports.serialize = function(reactorState) {
348
349
* @param {ReactorState } reactorState
349
350
* @return {ReactorState }
350
351
*/
351
- exports . resetDirtyStores = function ( reactorState ) {
352
+ export function resetDirtyStores ( reactorState ) {
352
353
return reactorState . set ( 'dirtyStores' , Immutable . Set ( ) )
353
354
}
354
355
0 commit comments