From 8a295d2adcaf37172f5928daa7dbb6eb5c97a88c Mon Sep 17 00:00:00 2001 From: Yun Lai Date: Sat, 31 Oct 2015 16:32:33 +1100 Subject: [PATCH 1/2] instead evaluate the same getter repeatly on every single notifier. group the notifier by getters, evaluate once then notify all. --- src/reactor.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/reactor.js b/src/reactor.js index b31cb21..c88f5d3 100644 --- a/src/reactor.js +++ b/src/reactor.js @@ -208,15 +208,26 @@ class Reactor { }) }) - observerIdsToNotify.forEach((observerId) => { - const entry = this.observerState.getIn(['observersMap', observerId]) - if (!entry) { - // don't notify here in the case a handler called unobserve on another observer - return + //generates a map with getter as key and an arrays of observer id as value. + const getterObserverMap = observerIdsToNotify.reduce((map, observerId) => { + const entry = this.observerState.getIn(['observersMap', observerId]); + const getter = entry.get('getter'); + const handler = entry.get('handler'); + + let handlerList = map.get(getter); + + if (!handlerList) { + handlerList = Immutable.List(); } - const getter = entry.get('getter') - const handler = entry.get('handler') + handlerList = handlerList.push(handler); + + return map.set(getter, handlerList); + + }, Immutable.Map()); + + // for each getter, evaluate the getter once, then notify all the observers relies on it + getterObserverMap.forEach((handlerList, getter) => { const prevEvaluateResult = fns.evaluate(this.prevReactorState, getter) const currEvaluateResult = fns.evaluate(this.reactorState, getter) @@ -225,9 +236,10 @@ class Reactor { const currValue = currEvaluateResult.result if (!Immutable.is(prevValue, currValue)) { - handler.call(null, currValue) + handlerList.forEach(handler => handler.call(null, currValue)); } - }) + + }); const nextReactorState = fns.resetDirtyStores(this.reactorState) From 065ea151a339d643a9655846fa1bb774450e58d7 Mon Sep 17 00:00:00 2001 From: Yun Lai Date: Sun, 1 Nov 2015 09:41:32 +1100 Subject: [PATCH 2/2] fixed the caching problem as pointed out by Jordan. --- src/reactor.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/reactor.js b/src/reactor.js index c88f5d3..6483be0 100644 --- a/src/reactor.js +++ b/src/reactor.js @@ -232,6 +232,9 @@ class Reactor { const prevEvaluateResult = fns.evaluate(this.prevReactorState, getter) const currEvaluateResult = fns.evaluate(this.reactorState, getter) + this.prevReactorState = prevEvaluateResult.reactorState + this.reactorState = currEvaluateResult.reactorState + const prevValue = prevEvaluateResult.result const currValue = currEvaluateResult.result