mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[REF] component: remove onDestroyed, implement onWillDestroy
This commit is contained in:
committed by
Aaron Bohy
parent
8c600fa539
commit
779003e715
@@ -8,7 +8,6 @@ import {
|
||||
MountFiber,
|
||||
MountOptions,
|
||||
RootFiber,
|
||||
__internal__destroyed,
|
||||
} from "./fibers";
|
||||
import { handleError, fibersInError } from "./error_handling";
|
||||
import { applyDefaultProps } from "./props_validation";
|
||||
@@ -95,7 +94,7 @@ export class ComponentNode<T extends typeof Component = typeof Component>
|
||||
mounted: LifecycleHook[] = [];
|
||||
willPatch: LifecycleHook[] = [];
|
||||
patched: LifecycleHook[] = [];
|
||||
destroyed: LifecycleHook[] = [];
|
||||
willDestroy: LifecycleHook[] = [];
|
||||
|
||||
constructor(C: T, props: any, app: App, parent?: ComponentNode) {
|
||||
currentNode = this;
|
||||
@@ -180,34 +179,27 @@ export class ComponentNode<T extends typeof Component = typeof Component>
|
||||
}
|
||||
|
||||
destroy() {
|
||||
if (this.status === STATUS.MOUNTED) {
|
||||
callWillUnmount(this);
|
||||
let shouldRemove = this.status === STATUS.MOUNTED;
|
||||
this._destroy();
|
||||
if (shouldRemove) {
|
||||
this.bdom!.remove();
|
||||
}
|
||||
callDestroyed(this);
|
||||
}
|
||||
|
||||
function callWillUnmount(node: ComponentNode) {
|
||||
const component = node.component;
|
||||
for (let cb of node.willUnmount) {
|
||||
cb.call(component);
|
||||
}
|
||||
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) {
|
||||
_destroy() {
|
||||
const component = this.component;
|
||||
if (this.status === STATUS.MOUNTED) {
|
||||
for (let cb of this.willUnmount) {
|
||||
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) {
|
||||
@@ -260,26 +252,10 @@ export class ComponentNode<T extends typeof Component = typeof Component>
|
||||
}
|
||||
|
||||
beforeRemove() {
|
||||
visitRemovedNodes(this);
|
||||
this._destroy();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,14 +126,6 @@ export class RootFiber extends Fiber {
|
||||
node.bdom!.patch(this.bdom!, Object.keys(node.children).length > 0);
|
||||
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
|
||||
let mountedFibers = this.mounted;
|
||||
while ((current = mountedFibers.pop())) {
|
||||
@@ -165,8 +157,6 @@ export class RootFiber extends Fiber {
|
||||
}
|
||||
}
|
||||
|
||||
export let __internal__destroyed: ComponentNode[] = [];
|
||||
|
||||
type Position = "first-child" | "last-child";
|
||||
|
||||
export interface MountOptions {
|
||||
|
||||
@@ -35,9 +35,9 @@ export function onWillUnmount(fn: () => Promise<void> | void | any) {
|
||||
node.willUnmount.unshift(fn);
|
||||
}
|
||||
|
||||
export function onDestroyed(fn: () => Promise<void> | void | any) {
|
||||
export function onWillDestroy(fn: () => Promise<void> | void | any) {
|
||||
const node = getCurrent()!;
|
||||
node.destroyed.push(fn);
|
||||
node.willDestroy.push(fn);
|
||||
}
|
||||
|
||||
export function onWillRender(fn: () => void | any) {
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ export {
|
||||
onPatched,
|
||||
onWillRender,
|
||||
onRendered,
|
||||
onDestroyed,
|
||||
onWillDestroy,
|
||||
onError,
|
||||
} from "./component/lifecycle_hooks";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user