Skip to content

Commit 424db11

Browse files
committed
still something wrong
1 parent 081bcbe commit 424db11

File tree

4 files changed

+98
-56
lines changed

4 files changed

+98
-56
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nuclear-js",
33
"version": "1.1.2",
44
"description": "Immutable, reactive Flux architecture. UI Agnostic.",
5-
"main": "dist/nuclear.js",
5+
"main": "src/main.js",
66
"scripts": {
77
"test": "grunt ci"
88
},

src/reactor.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class Reactor {
195195
return
196196
}
197197

198-
let observerIdsToNotify = Immutable.Set().withMutations(set => {
198+
let gettersToNotify = Immutable.Set().withMutations(set => {
199199
// notify all observers
200200
set.union(this.observerState.get('any'))
201201

@@ -208,15 +208,8 @@ class Reactor {
208208
})
209209
})
210210

211-
observerIdsToNotify.forEach((observerId) => {
212-
const entry = this.observerState.getIn(['observersMap', observerId])
213-
if (!entry) {
214-
// don't notify here in the case a handler called unobserve on another observer
215-
return
216-
}
217-
218-
const getter = entry.get('getter')
219-
const handler = entry.get('handler')
211+
gettersToNotify.forEach((getterId) => {
212+
const getter = this.observerState.get('getters')[getterId];
220213

221214
const prevEvaluateResult = fns.evaluate(this.prevReactorState, getter)
222215
const currEvaluateResult = fns.evaluate(this.reactorState, getter)
@@ -225,7 +218,11 @@ class Reactor {
225218
const currValue = currEvaluateResult.result
226219

227220
if (!Immutable.is(prevValue, currValue)) {
228-
handler.call(null, currValue)
221+
const handlers = this.observerState.getIn(['gettersMap', getterId])
222+
.map(observerId => this.observerState.getIn(['observersMap', observerId, 'handler']))
223+
// don't notify here in the case a handler called unobserve on another observer
224+
225+
handlers.forEach(handler => handler.call(null, currValue))
229226
}
230227
})
231228

src/reactor/fns.js

Lines changed: 83 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -156,45 +156,73 @@ exports.loadState = function(reactorState, state) {
156156
*/
157157
exports.addObserver = function(observerState, getter, handler) {
158158
// use the passed in getter as the key so we can rely on a byreference call for unobserve
159-
const getterKey = getter
160-
if (isKeyPath(getter)) {
161-
getter = fromKeyPath(getter)
162-
}
159+
try {
160+
const getterKey = getter
161+
if (isKeyPath(getter)) {
162+
getter = fromKeyPath(getter)
163+
}
163164

164-
const currId = observerState.get('nextId')
165-
const storeDeps = getStoreDeps(getter)
166-
const entry = Immutable.Map({
167-
id: currId,
168-
storeDeps: storeDeps,
169-
getterKey: getterKey,
170-
getter: getter,
171-
handler: handler,
172-
})
165+
const currId = observerState.get('nextId')
166+
const storeDeps = getStoreDeps(getter)
167+
const entry = Immutable.Map({
168+
id: currId,
169+
storeDeps: storeDeps,
170+
getterKey: getterKey,
171+
getter: getter,
172+
handler: handler,
173+
})
173174

174-
let updatedObserverState
175-
if (storeDeps.size === 0) {
176-
// no storeDeps means the observer is dependent on any of the state changing
177-
updatedObserverState = observerState.update('any', observerIds => observerIds.add(currId))
178-
} else {
179-
updatedObserverState = observerState.withMutations(map => {
180-
storeDeps.forEach(storeId => {
181-
let path = ['stores', storeId]
182-
if (!map.hasIn(path)) {
183-
map.setIn(path, Immutable.Set([]))
184-
}
185-
map.updateIn(['stores', storeId], observerIds => observerIds.add(currId))
175+
let updatedObserverState
176+
177+
let existingGetters = observerState.get('getters');
178+
179+
let getterId = existingGetters.indexOf(getter);
180+
181+
if (getterId < 0) {
182+
existingGetters.push(getter);
183+
getterId = existingGetters.length - 1;
184+
}
185+
//update getterMap
186+
187+
let observerIdsForGetter = observerState.getIn(['gettersMap', getterId])
188+
189+
if (!observerIdsForGetter) {
190+
observerIdsForGetter = Immutable.Set([])
191+
}
192+
193+
observerIdsForGetter = observerIdsForGetter.add(currId);
194+
195+
updatedObserverState = observerState.setIn(['gettersMap', getterId], observerIdsForGetter);
196+
197+
if (storeDeps.size === 0) {
198+
// no storeDeps means the observer is dependent on any of the state changing
199+
200+
updatedObserverState = updatedObserverState.updateIn(['any'], getters => getters.add(getterId))
201+
} else {
202+
updatedObserverState = updatedObserverState.withMutations(map => {
203+
storeDeps.forEach(storeId => {
204+
let path = ['stores', storeId]
205+
if (!map.hasIn(path)) {
206+
map.setIn(path, Immutable.Set([]))
207+
}
208+
map.updateIn(['stores', storeId], getters => getters.add(getterId))
209+
})
186210
})
187-
})
188-
}
211+
}
189212

190-
updatedObserverState = updatedObserverState
191-
.set('nextId', currId + 1)
192-
.setIn(['observersMap', currId], entry)
213+
updatedObserverState = updatedObserverState
214+
.set('nextId', currId + 1)
215+
.setIn(['observersMap', currId], entry)
193216

194-
return {
195-
observerState: updatedObserverState,
196-
entry: entry,
217+
return {
218+
observerState: updatedObserverState,
219+
entry: entry,
220+
}
221+
} catch (e) {
222+
debugger;
197223
}
224+
225+
198226
}
199227

200228
/**
@@ -239,18 +267,31 @@ exports.removeObserver = function(observerState, getter, handler) {
239267
*/
240268
exports.removeObserverByEntry = function(observerState, entry) {
241269
return observerState.withMutations(map => {
242-
const id = entry.get('id')
243-
const storeDeps = entry.get('storeDeps')
270+
try {
271+
const id = entry.get('id')
272+
const getter = entry.get('getter')
273+
const storeDeps = entry.get('storeDeps')
244274

245-
if (storeDeps.size === 0) {
246-
map.update('any', anyObsevers => anyObsevers.remove(id))
247-
} else {
248-
storeDeps.forEach(storeId => {
249-
map.updateIn(['stores', storeId], observers => observers.remove(id))
250-
})
275+
const existingGetters = observerState.get('getters');
276+
277+
const getterId = existingGetters.indexOf(getter);
278+
279+
//cleaning the gettersMap
280+
map.updateIn(['gettersMap', getterId], observerIds => observerIds.remove(id));
281+
282+
if (storeDeps.size === 0 && map.getIn(['gettersMap', getterId]).size === 0) {
283+
map.update('any', anyGetters => anyGetters.remove(getterId))
284+
} else {
285+
storeDeps.forEach(storeId => {
286+
map.updateIn(['stores', storeId], getters => getters.remove(getterId))
287+
})
288+
}
289+
290+
map.removeIn(['observersMap', id])
291+
} catch (e) {
292+
debugger;
251293
}
252294

253-
map.removeIn(['observersMap', id])
254295
})
255296
}
256297

src/reactor/records.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ const ReactorState = Immutable.Record({
1212
})
1313

1414
const ObserverState = Immutable.Record({
15-
// observers registered to any store change
15+
// getters registered to any store change
1616
any: Immutable.Set([]),
17-
// observers registered to specific store changes
17+
// getters registered to specific store changes
1818
stores: Immutable.Map({}),
1919

20+
getters: [],
21+
22+
gettersMap: Immutable.Map({}),
23+
2024
observersMap: Immutable.Map({}),
2125

2226
nextId: 1,

0 commit comments

Comments
 (0)