1
- import Immutable from 'immutable'
1
+ import { Map , is , Set } from 'immutable'
2
2
import createReactMixin from './create-react-mixin'
3
3
import * as fns from './reactor/fns'
4
4
import { DefaultCache } from './reactor/cache'
@@ -7,9 +7,9 @@ import { isKeyPath } from './key-path'
7
7
import { isGetter , getCanonicalKeypathDeps } from './getter'
8
8
import { toJS } from './immutable-helpers'
9
9
import { extend , toFactory } from './utils'
10
+ import ObserverState from './reactor/observer-state'
10
11
import {
11
12
ReactorState ,
12
- ObserverState ,
13
13
DEBUG_OPTIONS ,
14
14
PROD_OPTIONS ,
15
15
} from './reactor/records'
@@ -45,8 +45,6 @@ class Reactor {
45
45
this . reactorState = initialReactorState
46
46
this . observerState = new ObserverState ( )
47
47
48
- this . observerState = this . observerState . asMutable ( )
49
-
50
48
this . ReactMixin = createReactMixin ( this )
51
49
52
50
// keep track of the depth of batch nesting
@@ -62,21 +60,22 @@ class Reactor {
62
60
* @return {* }
63
61
*/
64
62
evaluate ( keyPathOrGetter ) {
65
- // look through the keypathStates and see if any of the getters dependencies are dirty, if so resolve
66
- // against the previous reactor state
67
- let updatedReactorState = this . reactorState
68
- if ( ! isKeyPath ( keyPathOrGetter ) ) {
69
- const maxCacheDepth = fns . getOption ( updatedReactorState , 'maxCacheDepth' )
70
- let res = fns . resolveDirtyKeypathStates (
71
- this . prevReactorState ,
72
- this . reactorState ,
73
- getCanonicalKeypathDeps ( keyPathOrGetter , maxCacheDepth )
74
- )
75
- updatedReactorState = res . reactorState
76
- }
63
+ let result
64
+
65
+ this . reactorState = this . reactorState . withMutations ( reactorState => {
66
+ if ( ! isKeyPath ( keyPathOrGetter ) ) {
67
+ // look through the keypathStates and see if any of the getters dependencies are dirty, if so resolve
68
+ // against the previous reactor state
69
+ const maxCacheDepth = fns . getOption ( reactorState , 'maxCacheDepth' )
70
+ fns . resolveDirtyKeypathStates (
71
+ this . prevReactorState ,
72
+ reactorState ,
73
+ getCanonicalKeypathDeps ( keyPathOrGetter , maxCacheDepth )
74
+ )
75
+ }
76
+ result = fns . evaluate ( reactorState , keyPathOrGetter )
77
+ } )
77
78
78
- let { result, reactorState } = fns . evaluate ( updatedReactorState , keyPathOrGetter )
79
- this . reactorState = reactorState
80
79
return result
81
80
}
82
81
@@ -110,9 +109,9 @@ class Reactor {
110
109
handler = getter
111
110
getter = [ ]
112
111
}
113
- let entry = fns . addObserver ( this . reactorState , this . observerState , getter , handler )
112
+ const entry = this . observerState . addObserver ( this . reactorState , getter , handler )
114
113
return ( ) => {
115
- fns . removeObserverByEntry ( this . reactorState , this . observerState , entry )
114
+ this . observerState . removeObserverByEntry ( this . reactorState , entry )
116
115
}
117
116
}
118
117
@@ -124,7 +123,7 @@ class Reactor {
124
123
throw new Error ( 'Must call unobserve with a Getter' )
125
124
}
126
125
127
- fns . removeObserver ( this . reactorState , this . observerState , getter , handler )
126
+ this . observerState . removeObserver ( this . reactorState , getter , handler )
128
127
}
129
128
130
129
/**
@@ -227,15 +226,6 @@ class Reactor {
227
226
this . observerState = new ObserverState ( )
228
227
}
229
228
230
- /**
231
- * Denotes a new state, via a store registration, dispatch or some other method
232
- * Resolves any outstanding keypath states and sets a new reactorState
233
- * @private
234
- */
235
- __nextState ( newState ) {
236
- // TODO(jordan): determine if this is actually needed
237
- }
238
-
239
229
/**
240
230
* Notifies all change observers with the current state
241
231
* @private
@@ -246,29 +236,24 @@ class Reactor {
246
236
return
247
237
}
248
238
239
+ this . prevReactorState = this . prevReactorState . asMutable ( )
240
+ this . reactorState = this . reactorState . asMutable ( )
241
+
249
242
fns . getLoggerFunction ( this . reactorState , 'notifyStart' ) ( this . reactorState , this . observerState )
250
243
251
- const keypathsToResolve = this . observerState . get ( 'trackedKeypaths' )
252
- const { reactorState , changedKeypaths } = fns . resolveDirtyKeypathStates (
244
+ const keypathsToResolve = this . observerState . getTrackedKeypaths ( )
245
+ const changedKeypaths = fns . resolveDirtyKeypathStates (
253
246
this . prevReactorState ,
254
247
this . reactorState ,
255
248
keypathsToResolve ,
256
249
true // increment all dirty states (this should leave no unknown state in the keypath tracker map):
257
250
)
258
- this . reactorState = reactorState
259
251
260
252
// get observers to notify based on the keypaths that changed
261
- let observersToNotify = Immutable . Set ( ) . withMutations ( set => {
262
- changedKeypaths . forEach ( keypath => {
263
- const entries = this . observerState . getIn ( [ 'keypathToEntries' , keypath ] )
264
- if ( entries && entries . size > 0 ) {
265
- set . union ( entries )
266
- }
267
- } )
268
- } )
253
+ const observersToNotify = this . observerState . getObserversToNotify ( changedKeypaths )
269
254
270
255
observersToNotify . forEach ( ( observer ) => {
271
- if ( ! this . observerState . get ( 'observers' ) . has ( observer ) ) {
256
+ if ( ! this . observerState . hasObserver ( observer ) ) {
272
257
// the observer was removed in a hander function
273
258
return
274
259
}
@@ -279,23 +264,20 @@ class Reactor {
279
264
280
265
fns . getLoggerFunction ( this . reactorState , 'notifyEvaluateStart' ) ( this . reactorState , getter )
281
266
282
- const prevEvaluateResult = fns . evaluate ( this . prevReactorState , getter )
283
- const currEvaluateResult = fns . evaluate ( this . reactorState , getter )
267
+ const prevValue = fns . evaluate ( this . prevReactorState , getter )
268
+ const currValue = fns . evaluate ( this . reactorState , getter )
284
269
285
- this . prevReactorState = prevEvaluateResult . reactorState
286
- this . reactorState = currEvaluateResult . reactorState
287
-
288
- const prevValue = prevEvaluateResult . result
289
- const currValue = currEvaluateResult . result
290
-
291
- // TODO pull some comparator function out of the reactorState
292
- if ( ! Immutable . is ( prevValue , currValue ) ) {
270
+ // TODO(jordan) pull some comparator function out of the reactorState
271
+ if ( ! is ( prevValue , currValue ) ) {
293
272
handler . call ( null , currValue )
294
273
didCall = true
295
274
}
296
275
fns . getLoggerFunction ( this . reactorState , 'notifyEvaluateEnd' ) ( this . reactorState , getter , didCall , currValue )
297
276
} )
298
277
278
+ this . prevReactorState = this . prevReactorState . asImmutable ( )
279
+ this . reactorState = this . reactorState . asImmutable ( )
280
+
299
281
fns . getLoggerFunction ( this . reactorState , 'notifyEnd' ) ( this . reactorState , this . observerState )
300
282
}
301
283
0 commit comments