[REF] observer: notify subscribers directly

This rev. moves the logic of batching the notifications of changes
occurring in the same microtask tick, from the observer, to the
listeners (the context, and the render function of components).
By doing so in render, we ensure that similar renderings are
batched in a single one, wherever they come from (state change,
store change, direct call...).
This commit is contained in:
Géry Debongnie
2019-11-14 14:09:42 +01:00
parent e5940b4b6b
commit 56087b95cd
8 changed files with 33 additions and 34 deletions
+1 -11
View File
@@ -24,19 +24,9 @@ import { Observer } from "./core/observer";
export function useState<T>(state: T): T {
const component: Component<any, any> = Component.current!;
const __owl__ = component.__owl__;
const renderFn = __owl__.renderFn;
let lastRenderRevNumber;
__owl__.renderFn = function(comp, params) {
lastRenderRevNumber = __owl__.observer!.rev;
return renderFn(comp, params);
};
if (!__owl__.observer) {
__owl__.observer = new Observer();
__owl__.observer.notifyCB = () => {
if (lastRenderRevNumber < __owl__.observer!.rev) {
component.render();
}
};
__owl__.observer.notifyCB = component.render.bind(component);
}
return __owl__.observer.observe(state);
}