mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] move error handling out of fiber, fix complicated mounted issues
This commit is contained in:
committed by
Aaron Bohy
parent
1da3ecdbee
commit
f32b1deb2c
+10
-1
@@ -1,3 +1,4 @@
|
||||
import { onError, onMounted } from "../component/lifecycle_hooks";
|
||||
import { Component } from "../component/component";
|
||||
import { ComponentNode } from "../component/component_node";
|
||||
import { MountOptions } from "../component/fibers";
|
||||
@@ -64,8 +65,16 @@ export class App<T extends typeof Component = any> extends TemplateSet {
|
||||
throw new Error("Cannot mount a component on a detached dom node");
|
||||
}
|
||||
const node = new ComponentNode(this.Root, this.props, this);
|
||||
const promise: any = new Promise((resolve, reject) => {
|
||||
onMounted(() => resolve(node.component));
|
||||
onError((e) => {
|
||||
reject(e);
|
||||
throw e;
|
||||
});
|
||||
});
|
||||
this.root = node;
|
||||
return node.mountComponent(target, options);
|
||||
node.mountComponent(target, options);
|
||||
return promise;
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
||||
@@ -112,11 +112,10 @@ export class ComponentNode<T extends typeof Component = typeof Component>
|
||||
this.component.setup();
|
||||
}
|
||||
|
||||
mountComponent(target: any, options?: MountOptions): Promise<InstanceType<T>> {
|
||||
mountComponent(target: any, options?: MountOptions) {
|
||||
const fiber = new MountFiber(this, target, options);
|
||||
this.app.scheduler.addFiber(fiber);
|
||||
this.initiateRender(fiber);
|
||||
return fiber.promise.then(() => this.component);
|
||||
}
|
||||
|
||||
async initiateRender(fiber: Fiber | MountFiber) {
|
||||
@@ -227,6 +226,9 @@ export class ComponentNode<T extends typeof Component = typeof Component>
|
||||
* a mounted hook failed and was handled.
|
||||
*/
|
||||
updateDom() {
|
||||
if (!this.fiber) {
|
||||
return;
|
||||
}
|
||||
if (this.bdom === this.fiber!.bdom) {
|
||||
// If the error was handled by some child component, we need to find it to
|
||||
// apply its change
|
||||
|
||||
@@ -20,23 +20,22 @@ function _handleError(node: ComponentNode | null, error: any, isFirstRound = fal
|
||||
fiber.root.counter--;
|
||||
}
|
||||
|
||||
let propagate = true;
|
||||
let stopped = false;
|
||||
for (const h of errorHandlers) {
|
||||
try {
|
||||
h(error);
|
||||
propagate = false;
|
||||
stopped = true;
|
||||
break;
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
}
|
||||
|
||||
if (propagate) {
|
||||
return _handleError(node.parent, error);
|
||||
if (stopped) {
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return _handleError(node.parent, error);
|
||||
}
|
||||
return _handleError(node.parent, error);
|
||||
}
|
||||
|
||||
type ErrorParams = { error: any } & ({ node: ComponentNode } | { fiber: Fiber });
|
||||
|
||||
+1
-11
@@ -151,18 +151,11 @@ export interface MountOptions {
|
||||
export class MountFiber extends RootFiber {
|
||||
target: HTMLElement;
|
||||
position: Position;
|
||||
resolve: any;
|
||||
promise: Promise<any>;
|
||||
reject: any;
|
||||
|
||||
constructor(node: ComponentNode, target: HTMLElement, options: MountOptions = {}) {
|
||||
super(node, null);
|
||||
this.target = target;
|
||||
this.position = options.position || "last-child";
|
||||
this.promise = new Promise((resolve, reject) => {
|
||||
this.resolve = resolve;
|
||||
this.reject = reject;
|
||||
});
|
||||
}
|
||||
complete() {
|
||||
let current: Fiber | undefined = this;
|
||||
@@ -195,10 +188,7 @@ export class MountFiber extends RootFiber {
|
||||
}
|
||||
node.fiber = null;
|
||||
} catch (e) {
|
||||
if (!handleError({ fiber: current as Fiber, error: e })) {
|
||||
this.reject(e);
|
||||
}
|
||||
handleError({ fiber: current as Fiber, error: e });
|
||||
}
|
||||
this.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Fiber, MountFiber, RootFiber } from "./fibers";
|
||||
import { fibersInError } from "./error_handling";
|
||||
import { Fiber, RootFiber } from "./fibers";
|
||||
import { STATUS } from "./status";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -44,9 +44,6 @@ export class Scheduler {
|
||||
const hasError = fibersInError.has(fiber);
|
||||
if (hasError && fiber.counter !== 0) {
|
||||
this.tasks.delete(fiber);
|
||||
if (fiber instanceof MountFiber) {
|
||||
fiber.reject(fibersInError.get(fiber));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (fiber.node.status === STATUS.DESTROYED) {
|
||||
|
||||
Reference in New Issue
Block a user