[REF] component: remove onDestroyed, implement onWillDestroy

This commit is contained in:
Géry Debongnie
2021-11-29 14:06:53 +01:00
committed by Samuel Degueldre
parent 03f34a38dc
commit 0831bb54e3
10 changed files with 83 additions and 117 deletions
+17 -41
View File
@@ -8,7 +8,6 @@ import {
MountFiber, MountFiber,
MountOptions, MountOptions,
RootFiber, RootFiber,
__internal__destroyed,
} from "./fibers"; } from "./fibers";
import { handleError, fibersInError } from "./error_handling"; import { handleError, fibersInError } from "./error_handling";
import { applyDefaultProps } from "./props_validation"; import { applyDefaultProps } from "./props_validation";
@@ -95,7 +94,7 @@ export class ComponentNode<T extends typeof Component = typeof Component>
mounted: LifecycleHook[] = []; mounted: LifecycleHook[] = [];
willPatch: LifecycleHook[] = []; willPatch: LifecycleHook[] = [];
patched: LifecycleHook[] = []; patched: LifecycleHook[] = [];
destroyed: LifecycleHook[] = []; willDestroy: LifecycleHook[] = [];
constructor(C: T, props: any, app: App, parent?: ComponentNode) { constructor(C: T, props: any, app: App, parent?: ComponentNode) {
currentNode = this; currentNode = this;
@@ -180,34 +179,27 @@ export class ComponentNode<T extends typeof Component = typeof Component>
} }
destroy() { destroy() {
if (this.status === STATUS.MOUNTED) { let shouldRemove = this.status === STATUS.MOUNTED;
callWillUnmount(this); this._destroy();
if (shouldRemove) {
this.bdom!.remove(); this.bdom!.remove();
} }
callDestroyed(this); }
function callWillUnmount(node: ComponentNode) { _destroy() {
const component = node.component; const component = this.component;
for (let cb of node.willUnmount) { if (this.status === STATUS.MOUNTED) {
cb.call(component); for (let cb of this.willUnmount) {
}
for (let child of Object.values(node.children)) {
if (child.status === STATUS.MOUNTED) {
callWillUnmount(child);
}
}
}
function callDestroyed(node: ComponentNode) {
const component = node.component;
node.status = STATUS.DESTROYED;
for (let child of Object.values(node.children)) {
callDestroyed(child);
}
for (let cb of node.destroyed) {
cb.call(component); cb.call(component);
} }
} }
for (let child of Object.values(this.children)) {
child._destroy();
}
for (let cb of this.willDestroy) {
cb.call(component);
}
this.status = STATUS.DESTROYED;
} }
async updateAndRender(props: any, parentFiber: Fiber) { async updateAndRender(props: any, parentFiber: Fiber) {
@@ -260,26 +252,10 @@ export class ComponentNode<T extends typeof Component = typeof Component>
} }
beforeRemove() { beforeRemove() {
visitRemovedNodes(this); this._destroy();
} }
remove() { remove() {
this.bdom!.remove(); this.bdom!.remove();
} }
} }
function visitRemovedNodes(node: ComponentNode) {
if (node.status === STATUS.MOUNTED) {
const component = node.component;
for (let cb of node.willUnmount) {
cb.call(component);
}
}
for (let child of Object.values(node.children)) {
visitRemovedNodes(child);
}
node.status = STATUS.DESTROYED;
if (node.destroyed.length) {
__internal__destroyed.push(node);
}
}
-10
View File
@@ -126,14 +126,6 @@ export class RootFiber extends Fiber {
node.bdom!.patch(this.bdom!, Object.keys(node.children).length > 0); node.bdom!.patch(this.bdom!, Object.keys(node.children).length > 0);
this.appliedToDom = true; this.appliedToDom = true;
// Step 3: calling all destroyed hooks
for (let node of __internal__destroyed) {
for (let cb of node.destroyed) {
cb();
}
}
__internal__destroyed.length = 0;
// Step 4: calling all mounted lifecycle hooks // Step 4: calling all mounted lifecycle hooks
let mountedFibers = this.mounted; let mountedFibers = this.mounted;
while ((current = mountedFibers.pop())) { while ((current = mountedFibers.pop())) {
@@ -165,8 +157,6 @@ export class RootFiber extends Fiber {
} }
} }
export let __internal__destroyed: ComponentNode[] = [];
type Position = "first-child" | "last-child"; type Position = "first-child" | "last-child";
export interface MountOptions { export interface MountOptions {
+2 -2
View File
@@ -35,9 +35,9 @@ export function onWillUnmount(fn: () => Promise<void> | void | any) {
node.willUnmount.unshift(fn); node.willUnmount.unshift(fn);
} }
export function onDestroyed(fn: () => Promise<void> | void | any) { export function onWillDestroy(fn: () => Promise<void> | void | any) {
const node = getCurrent()!; const node = getCurrent()!;
node.destroyed.push(fn); node.willDestroy.push(fn);
} }
export function onWillRender(fn: () => void | any) { export function onWillRender(fn: () => void | any) {
+1 -1
View File
@@ -68,7 +68,7 @@ export {
onPatched, onPatched,
onWillRender, onWillRender,
onRendered, onRendered,
onDestroyed, onWillDestroy,
onError, onError,
} from "./component/lifecycle_hooks"; } from "./component/lifecycle_hooks";
@@ -107,36 +107,6 @@ exports[`lifecycle hooks component semantics 6`] = `
}" }"
`; `;
exports[`lifecycle hooks components are unmounted and destroyed if no longer in DOM 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component } = bdom;
let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber, safeOutput } = helpers;
let block1 = createBlock(\`<div/>\`);
return function template(ctx, node, key = \\"\\") {
return block1();
}
}"
`;
exports[`lifecycle hooks components are unmounted and destroyed if no longer in DOM 2`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component } = bdom;
let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber, safeOutput } = helpers;
return function template(ctx, node, key = \\"\\") {
let b2;
if (ctx['state'].ok) {
b2 = component(\`Child\`, {}, key + \`__1\`, node, ctx);
}
return multi([b2]);
}
}"
`;
exports[`lifecycle hooks components are unmounted and destroyed if no longer in DOM, even after updateprops 1`] = ` exports[`lifecycle hooks components are unmounted and destroyed if no longer in DOM, even after updateprops 1`] = `
"function anonymous(bdom, helpers "function anonymous(bdom, helpers
) { ) {
@@ -171,6 +141,36 @@ exports[`lifecycle hooks components are unmounted and destroyed if no longer in
}" }"
`; `;
exports[`lifecycle hooks components are unmounted destroyed if no longer in DOM 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component } = bdom;
let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber, safeOutput } = helpers;
let block1 = createBlock(\`<div/>\`);
return function template(ctx, node, key = \\"\\") {
return block1();
}
}"
`;
exports[`lifecycle hooks components are unmounted destroyed if no longer in DOM 2`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component } = bdom;
let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber, safeOutput } = helpers;
return function template(ctx, node, key = \\"\\") {
let b2;
if (ctx['state'].ok) {
b2 = component(\`Child\`, {}, key + \`__1\`, node, ctx);
}
return multi([b2]);
}
}"
`;
exports[`lifecycle hooks hooks are called in proper order in widget creation/destruction 1`] = ` exports[`lifecycle hooks hooks are called in proper order in widget creation/destruction 1`] = `
"function anonymous(bdom, helpers "function anonymous(bdom, helpers
) { ) {
+11 -11
View File
@@ -64,7 +64,7 @@ describe("async rendering", () => {
def.resolve(); def.resolve();
await nextTick(); await nextTick();
expect(status(w)).toBe("destroyed"); expect(status(w)).toBe("destroyed");
expect(["W:setup", "W:willStart", "W:destroyed"]).toBeLogged(); expect(["W:setup", "W:willStart", "W:willDestroy"]).toBeLogged();
}); });
}); });
@@ -112,7 +112,7 @@ test("destroying/recreating a subwidget with different props (if start is not ov
expect([ expect([
"W:willRender", "W:willRender",
"Child:destroyed", "Child:willDestroy",
"Child:setup", "Child:setup",
"Child:willStart", "Child:willStart",
"W:rendered", "W:rendered",
@@ -190,7 +190,7 @@ test("creating two async components, scenario 1", async () => {
expect(fixture.innerHTML).toBe(""); expect(fixture.innerHTML).toBe("");
expect([ expect([
"Parent:willRender", "Parent:willRender",
"ChildA:destroyed", "ChildA:willDestroy",
"ChildA:setup", "ChildA:setup",
"ChildA:willStart", "ChildA:willStart",
"ChildB:setup", "ChildB:setup",
@@ -583,8 +583,8 @@ test("properly behave when destroyed/unmounted while rendering ", async () => {
"Parent:willPatch", "Parent:willPatch",
"Child:willUnmount", "Child:willUnmount",
"SubChild:willUnmount", "SubChild:willUnmount",
"SubChild:destroyed", "SubChild:willDestroy",
"Child:destroyed", "Child:willDestroy",
"Parent:patched", "Parent:patched",
]).toBeLogged(); ]).toBeLogged();
@@ -642,7 +642,7 @@ test("rendering component again in next microtick", async () => {
"Child:willStart", "Child:willStart",
"Parent:rendered", "Parent:rendered",
"Parent:willRender", "Parent:willRender",
"Child:destroyed", "Child:willDestroy",
"Child:setup", "Child:setup",
"Child:willStart", "Child:willStart",
"Parent:rendered", "Parent:rendered",
@@ -1694,7 +1694,7 @@ test("concurrent renderings scenario 10", async () => {
expect(rendered).toBe(1); expect(rendered).toBe(1);
expect([ expect([
"ComponentB:willRender", "ComponentB:willRender",
"ComponentC:destroyed", "ComponentC:willDestroy",
"ComponentC:setup", "ComponentC:setup",
"ComponentC:willStart", "ComponentC:willStart",
"ComponentB:rendered", "ComponentB:rendered",
@@ -2249,7 +2249,7 @@ test.skip("concurrent renderings scenario 16", async () => {
"C:willUpdateProps", "C:willUpdateProps",
"C:willRender", "C:willRender",
"C:rendered", "C:rendered",
"D:destroyed", "D:willDestroy",
"D:setup", "D:setup",
"D:willStart", "D:willStart",
"D:willRender", "D:willRender",
@@ -2324,7 +2324,7 @@ test("calling render in destroy", async () => {
"B:willRender", "B:willRender",
"B:rendered", "B:rendered",
"B:willUnmount", "B:willUnmount",
"B:destroyed", "B:willDestroy",
"B:mounted", "B:mounted",
"B:willRender", "B:willRender",
"B:rendered", "B:rendered",
@@ -2522,7 +2522,7 @@ test("two renderings initiated between willPatch and patched", async () => {
"Panel:rendered", "Panel:rendered",
"Parent:willPatch", "Parent:willPatch",
"Panel:willUnmount", "Panel:willUnmount",
"Panel:destroyed", "Panel:willDestroy",
"Panel:mounted", "Panel:mounted",
"Parent:patched", "Parent:patched",
]).toBeLogged(); ]).toBeLogged();
@@ -2535,7 +2535,7 @@ test("two renderings initiated between willPatch and patched", async () => {
"Parent:rendered", "Parent:rendered",
"Parent:willPatch", "Parent:willPatch",
"Panel:willUnmount", "Panel:willUnmount",
"Panel:destroyed", "Panel:willDestroy",
"Parent:patched", "Parent:patched",
]).toBeLogged(); ]).toBeLogged();
}); });
+16 -16
View File
@@ -313,7 +313,7 @@ describe("lifecycle hooks", () => {
Object.freeze(steps); Object.freeze(steps);
}); });
test("components are unmounted and destroyed if no longer in DOM", async () => { test("components are unmounted destroyed if no longer in DOM", async () => {
let steps: string[] = []; let steps: string[] = [];
class Child extends Component { class Child extends Component {
@@ -410,7 +410,7 @@ describe("lifecycle hooks", () => {
"Parent:rendered", "Parent:rendered",
"Parent:willPatch", "Parent:willPatch",
"Child:willUnmount", "Child:willUnmount",
"Child:destroyed", "Child:willDestroy",
"Parent:patched", "Parent:patched",
]).toBeLogged(); ]).toBeLogged();
}); });
@@ -450,8 +450,8 @@ describe("lifecycle hooks", () => {
expect([ expect([
"Parent:willUnmount", "Parent:willUnmount",
"Child:willUnmount", "Child:willUnmount",
"Child:destroyed", "Child:willDestroy",
"Parent:destroyed", "Parent:willDestroy",
]).toBeLogged(); ]).toBeLogged();
}); });
@@ -568,8 +568,8 @@ describe("lifecycle hooks", () => {
expect([ expect([
"Parent:willUnmount", "Parent:willUnmount",
"Child:willUnmount", "Child:willUnmount",
"Child:destroyed", "Child:willDestroy",
"Parent:destroyed", "Parent:willDestroy",
]).toBeLogged(); ]).toBeLogged();
}); });
@@ -631,9 +631,9 @@ describe("lifecycle hooks", () => {
"Parent:willUnmount", "Parent:willUnmount",
"Child:willUnmount", "Child:willUnmount",
"GrandChild:willUnmount", "GrandChild:willUnmount",
"GrandChild:destroyed", "GrandChild:willDestroy",
"Child:destroyed", "Child:willDestroy",
"Parent:destroyed", "Parent:willDestroy",
]).toBeLogged(); ]).toBeLogged();
}); });
@@ -675,7 +675,7 @@ describe("lifecycle hooks", () => {
// immediately destroy everything // immediately destroy everything
app.destroy(); app.destroy();
await nextTick(); await nextTick();
expect(["Parent:willUnmount", "Parent:destroyed"]).toBeLogged(); expect(["Parent:willUnmount", "Parent:willDestroy"]).toBeLogged();
}); });
test("lifecycle semantics, part 4", async () => { test("lifecycle semantics, part 4", async () => {
@@ -732,9 +732,9 @@ describe("lifecycle hooks", () => {
app.destroy(); app.destroy();
expect([ expect([
"Parent:willUnmount", "Parent:willUnmount",
"GrandChild:destroyed", "GrandChild:willDestroy",
"Child:destroyed", "Child:willDestroy",
"Parent:destroyed", "Parent:willDestroy",
]).toBeLogged(); ]).toBeLogged();
}); });
@@ -776,7 +776,7 @@ describe("lifecycle hooks", () => {
"Parent:rendered", "Parent:rendered",
"Parent:willPatch", "Parent:willPatch",
"Child:willUnmount", "Child:willUnmount",
"Child:destroyed", "Child:willDestroy",
"Parent:patched", "Parent:patched",
]).toBeLogged(); ]).toBeLogged();
}); });
@@ -1016,7 +1016,7 @@ describe("lifecycle hooks", () => {
"C:willPatch", "C:willPatch",
"D:willPatch", "D:willPatch",
"E:willUnmount", "E:willUnmount",
"E:destroyed", "E:willDestroy",
"F:mounted", "F:mounted",
"D:patched", "D:patched",
"C:patched", "C:patched",
@@ -1061,7 +1061,7 @@ describe("lifecycle hooks", () => {
"Parent:rendered", "Parent:rendered",
"Parent:willPatch", "Parent:willPatch",
"Child:willUnmount", "Child:willUnmount",
"Child:destroyed", "Child:willDestroy",
"Parent:patched", "Parent:patched",
]).toBeLogged(); ]).toBeLogged();
+1 -1
View File
@@ -95,7 +95,7 @@ describe("t-component", () => {
"ChildB:rendered", "ChildB:rendered",
"Parent:willPatch", "Parent:willPatch",
"ChildA:willUnmount", "ChildA:willUnmount",
"ChildA:destroyed", "ChildA:willDestroy",
"ChildB:mounted", "ChildB:mounted",
"Parent:patched", "Parent:patched",
]).toBeLogged(); ]).toBeLogged();
+4 -4
View File
@@ -1,7 +1,7 @@
import { import {
App, App,
Component, Component,
onDestroyed, onWillDestroy,
onMounted, onMounted,
onPatched, onPatched,
onWillRender, onWillRender,
@@ -210,9 +210,9 @@ export function useLogLifecycle() {
logStep(`${name}:willUnmount`); logStep(`${name}:willUnmount`);
}); });
onDestroyed(() => { onWillDestroy(() => {
expect(name + ": " + status(component)).toBe(name + ": " + "destroyed"); expect(status(component)).not.toBe("destroyed");
logStep(`${name}:destroyed`); logStep(`${name}:willDestroy`);
}); });
} }
+1 -1
View File
@@ -1492,7 +1492,7 @@ describe("Reactivity: useState", () => {
"Parent:rendered", "Parent:rendered",
"Parent:willPatch", "Parent:willPatch",
"Child:willUnmount", "Child:willUnmount",
"Child:destroyed", "Child:willDestroy",
"Parent:patched", "Parent:patched",
]).toBeLogged(); ]).toBeLogged();