Skip to content

Commit

Permalink
[REF] hooks: better implementation of onMounted/onWillUnmount
Browse files Browse the repository at this point in the history
  • Loading branch information
ged-odoo committed Sep 28, 2019
1 parent 090880f commit f6820bb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ interface Internal<T extends Env, Props> {
observer: Observer | null;
render: CompiledTemplate | null;
mountedHandlers: { [key: number]: Function };
willUnmountCB: Function | null;
classObj: { [key: string]: boolean } | null;
refs: { [key: string]: Component<T, any> | HTMLElement | undefined } | null;
}
Expand Down Expand Up @@ -185,6 +186,7 @@ export class Component<T extends Env, Props extends {}> {
currentFiber: null,
boundHandlers: {},
mountedHandlers: {},
willUnmountCB: null,
observer: null,
render: null,
classObj: null,
Expand Down Expand Up @@ -481,19 +483,22 @@ export class Component<T extends Env, Props extends {}> {
}
__owl__.isMounted = true;
const handlers = __owl__.mountedHandlers;
for (let key in handlers) {
handlers[key]();
}
try {
this.mounted();
for (let key in handlers) {
handlers[key]();
}
} catch (e) {
errorHandler(e, this);
}
}

__callWillUnmount() {
this.willUnmount();
const __owl__ = this.__owl__;
if (__owl__.willUnmountCB) {
__owl__.willUnmountCB();
}
this.willUnmount();
__owl__.isMounted = false;
const children = __owl__.children;
for (let id in children) {
Expand Down
22 changes: 12 additions & 10 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ export function useState<T>(state: T): T {
* Mounted hook. The callback will be called when the current component is
* mounted. Note that the component mounted method is called first.
*/
let nextID = 1;

export function onMounted(cb) {
const component: Component<any, any> = Component._current;
const current = component.mounted;
component.mounted = function() {
current.call(component);
cb();
};
component.__owl__.mountedHandlers[`h${nextID++}`] = cb;
}

/**
Expand All @@ -48,11 +46,15 @@ export function onMounted(cb) {
*/
export function onWillUnmount(cb) {
const component: Component<any, any> = Component._current;
const current = component.willUnmount;
component.willUnmount = function() {
cb();
current.call(component);
};
if (component.__owl__.willUnmountCB) {
const current = component.__owl__.willUnmountCB;
component.__owl__.willUnmountCB = function() {
cb.call(component);
current.call(component);
};
} else {
component.__owl__.willUnmountCB = cb;
}
}

/**
Expand Down
Empty file added src/store/hooks.ts
Empty file.
2 changes: 2 additions & 0 deletions tests/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ describe("hooks", () => {
}
const component = new MyComponent(env);
await component.mount(fixture);
expect(component).not.toHaveProperty("mounted");
expect(component).not.toHaveProperty("willUnmount");
expect(fixture.innerHTML).toBe("<div>hey</div>");
expect(steps).toEqual(["mounted"]);
component.unmount();
Expand Down

0 comments on commit f6820bb

Please sign in to comment.