Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster __notify implementation #182

Open
wants to merge 2 commits into
base: jordan/refactor-everything!
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/reactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,41 @@ 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)

this.prevReactorState = prevEvaluateResult.reactorState
this.reactorState = currEvaluateResult.reactorState

const prevValue = prevEvaluateResult.result
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)

Expand Down