[REF] component: make observer and state optional

This commit is contained in:
Géry Debongnie
2019-04-18 10:10:18 +02:00
parent 29111a5c10
commit 3b3fd8a6c9
2 changed files with 12 additions and 8 deletions
+11 -7
View File
@@ -35,7 +35,7 @@ export interface Meta<T extends Env, Props> {
renderProps: Props | null; renderProps: Props | null;
renderPromise: Promise<VNode> | null; renderPromise: Promise<VNode> | null;
boundHandlers: { [key: number]: any }; boundHandlers: { [key: number]: any };
observer: Observer; observer?: Observer;
} }
const patch = init([sdListeners, sdAttrs, sdProps]); const patch = init([sdListeners, sdAttrs, sdProps]);
@@ -62,7 +62,7 @@ export class Component<
} }
env: T; env: T;
state: State = <State>{}; state?: State;
props: Props; props: Props;
refs: { refs: {
[key: string]: Component<T, any, any> | HTMLElement | undefined; [key: string]: Component<T, any, any> | HTMLElement | undefined;
@@ -120,8 +120,7 @@ export class Component<
renderId: 1, renderId: 1,
renderPromise: null, renderPromise: null,
renderProps: props || null, renderProps: props || null,
boundHandlers: {}, boundHandlers: {}
observer: new Observer()
}; };
} }
@@ -366,13 +365,17 @@ export class Component<
this.__owl__.renderId++; this.__owl__.renderId++;
const promises: Promise<void>[] = []; const promises: Promise<void>[] = [];
const template = this.inlineTemplate || this.template; const template = this.inlineTemplate || this.template;
this.__owl__.observer.allowMutations = false; if (this.__owl__.observer) {
this.__owl__.observer.allowMutations = false;
}
let vnode = this.env.qweb.render(template, this, { let vnode = this.env.qweb.render(template, this, {
promises, promises,
handlers: this.__owl__.boundHandlers, handlers: this.__owl__.boundHandlers,
forceUpdate: force forceUpdate: force
}); });
this.__owl__.observer.allowMutations = true; if (this.__owl__.observer) {
this.__owl__.observer.allowMutations = true;
}
// this part is critical for the patching process to be done correctly. The // this part is critical for the patching process to be done correctly. The
// tricky part is that a child widget can be rerendered on its own, which // tricky part is that a child widget can be rerendered on its own, which
@@ -422,7 +425,8 @@ export class Component<
} }
_observeState() { _observeState() {
if (Object.keys(this.state).length) { if (this.state) {
this.__owl__.observer = new Observer();
this.__owl__.observer.observe(this.state); this.__owl__.observer.observe(this.state);
this.__owl__.observer.notifyCB = this.render.bind(this); this.__owl__.observer.notifyCB = this.render.bind(this);
} }
+1 -1
View File
@@ -72,7 +72,7 @@ describe("basic widget properties", () => {
test("props and state are properly defined", async () => { test("props and state are properly defined", async () => {
const widget = new Widget(env); const widget = new Widget(env);
expect(widget.props).toEqual({}); expect(widget.props).toEqual({});
expect(widget.state).toEqual({}); expect(widget.state).toEqual(undefined);
}); });
test("has no el after creation", async () => { test("has no el after creation", async () => {