Skip to content

Commit 71efb0d

Browse files
mcm-odooged-odoo
authored andcommitted
[IMP] hooks: remove env related hooks
1 parent 7fc0b79 commit 71efb0d

18 files changed

+69
-534
lines changed

src/runtime/app.ts

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { OwlError } from "../common/owl_error";
22
import { version } from "../version";
3-
import { Component, ComponentConstructor, Props } from "./component";
3+
import { Component, ComponentConstructor } from "./component";
44
import { ComponentNode, saveCurrent } from "./component_node";
55
import { handleError, nodeErrorHandlers } from "./rendering/error_handling";
66
import { Fiber, MountOptions, RootFiber } from "./rendering/fibers";
@@ -9,21 +9,20 @@ import { proxy, toRaw } from "./reactivity/proxy";
99
import { Scheduler } from "./rendering/scheduler";
1010
import { TemplateSet, TemplateSetConfig } from "./template_set";
1111
import { validateTarget } from "./utils";
12+
import { GetProps } from "./props";
1213

1314
// reimplement dev mode stuff see last change in 0f7a8289a6fb8387c3c1af41c6664b2a8448758f
1415

15-
export interface Env {
16-
[key: string]: any;
17-
}
16+
type ComponentInstance<C extends ComponentConstructor> = C extends new (...args: any) => infer T
17+
? T
18+
: never;
1819

19-
interface RootConfig<P, E> {
20-
env?: E;
20+
interface RootConfig<P> {
2121
pluginManager?: PluginManager;
2222
props?: P;
2323
}
2424

25-
export interface AppConfig<E> extends TemplateSetConfig {
26-
env?: E;
25+
export interface AppConfig extends TemplateSetConfig {
2726
name?: string;
2827
pluginManager?: PluginManager;
2928
test?: boolean;
@@ -47,27 +46,26 @@ declare global {
4746

4847
type MountTarget = HTMLElement | ShadowRoot;
4948

50-
interface Root<P extends Props, E, T extends abstract new (...args: any) => any = any> {
51-
node: ComponentNode<P, E>;
52-
promise: Promise<any>;
53-
mount(target: MountTarget, options?: MountOptions): Promise<Component<P, E> & InstanceType<T>>;
49+
interface Root<T extends ComponentConstructor> {
50+
node: ComponentNode;
51+
promise: Promise<ComponentInstance<T>>;
52+
mount(target: MountTarget, options?: MountOptions): Promise<ComponentInstance<T>>;
5453
destroy(): void;
5554
}
5655

5756
window.__OWL_DEVTOOLS__ ||= { apps, Fiber, RootFiber, toRaw, proxy };
5857

59-
export class App<E = any> extends TemplateSet {
58+
export class App extends TemplateSet {
6059
static validateTarget = validateTarget;
6160
static apps = apps;
6261
static version = version;
6362

6463
name: string;
65-
env: E;
6664
scheduler = new Scheduler();
67-
roots: Set<Root<any, any>> = new Set();
65+
roots: Set<Root<any>> = new Set();
6866
pluginManager: PluginManager;
6967

70-
constructor(config: AppConfig<E> = {}) {
68+
constructor(config: AppConfig = {}) {
7169
super(config);
7270
this.name = config.name || "";
7371
apps.add(this);
@@ -79,31 +77,18 @@ export class App<E = any> extends TemplateSet {
7977
console.info(`Owl is running in 'dev' mode.`);
8078
hasBeenLogged = true;
8179
}
82-
const env = config.env || {};
83-
const descrs = Object.getOwnPropertyDescriptors(env);
84-
this.env = Object.freeze(Object.create(Object.getPrototypeOf(env), descrs));
8580
}
8681

87-
createRoot<Props extends object, SubEnv = any>(
88-
Root: ComponentConstructor<Props, E>,
89-
config: RootConfig<Props, SubEnv> = {}
90-
): Root<Props, SubEnv> {
91-
const props = config.props || ({} as Props);
92-
// hack to make sure the sub root get the sub env if necessary. for owl 3,
93-
// would be nice to rethink the initialization process to make sure that
94-
// we can create a ComponentNode and give it explicitely the env, instead
95-
// of looking it up in the app
96-
const env = this.env;
97-
if (config.env) {
98-
this.env = config.env as any;
99-
}
82+
createRoot<T extends ComponentConstructor>(
83+
Root: T,
84+
config: RootConfig<GetProps<ComponentInstance<T>>> = {}
85+
): Root<T> {
86+
const props = config.props || ({} as any);
10087

10188
const restore = saveCurrent();
10289
const node = this.makeNode(Root, props);
10390
restore();
104-
if (config.env) {
105-
this.env = env;
106-
}
91+
10792
let resolve!: (value: any) => void;
10893
let reject!: (reason?: any) => void;
10994
const promise = new Promise<any>((res, rej) => {
@@ -128,7 +113,10 @@ export class App<E = any> extends TemplateSet {
128113
return root;
129114
}
130115

131-
makeNode(Component: ComponentConstructor, props: any): ComponentNode {
116+
makeNode<T extends ComponentConstructor>(
117+
Component: T,
118+
props: GetProps<ComponentInstance<T>>
119+
): ComponentNode {
132120
return new ComponentNode(Component, props, this, null, null);
133121
}
134122

@@ -169,20 +157,20 @@ export class App<E = any> extends TemplateSet {
169157
apps.delete(this);
170158
}
171159

172-
createComponent<P extends Props>(
160+
createComponent<P extends Record<string, any>>(
173161
name: string | null,
174162
isStatic: boolean,
175163
hasSlotsProp: boolean,
176164
hasDynamicPropList: boolean,
177165
propList: string[]
178166
) {
179167
const isDynamic = !isStatic;
180-
let arePropsDifferent: (p1: Object, p2: Object) => boolean;
168+
let arePropsDifferent: (p1: P, p2: P) => boolean;
181169
const hasNoProp = propList.length === 0;
182170
if (hasSlotsProp) {
183171
arePropsDifferent = (_1, _2) => true;
184172
} else if (hasDynamicPropList) {
185-
arePropsDifferent = function (props1: Props, props2: Props) {
173+
arePropsDifferent = function (props1: P, props2: P) {
186174
for (let k in props1) {
187175
if (props1[k] !== props2[k]) {
188176
return true;
@@ -193,7 +181,7 @@ export class App<E = any> extends TemplateSet {
193181
} else if (hasNoProp) {
194182
arePropsDifferent = (_1: any, _2: any) => false;
195183
} else {
196-
arePropsDifferent = function (props1: Props, props2: Props) {
184+
arePropsDifferent = function (props1: P, props2: P) {
197185
for (let p of propList) {
198186
if (props1[p] !== props2[p]) {
199187
return true;
@@ -250,20 +238,10 @@ export class App<E = any> extends TemplateSet {
250238
}
251239
}
252240

253-
type ComponentInstance<C extends ComponentConstructor<any, any>> = C extends new (
254-
...args: any
255-
) => infer T
256-
? T
257-
: never;
258-
259-
export async function mount<
260-
T extends ComponentConstructor<any, any>,
261-
P extends object = any,
262-
E = any
263-
>(
264-
C: T & ComponentConstructor<P, E>,
241+
export async function mount<T extends ComponentConstructor>(
242+
C: T,
265243
target: MountTarget,
266-
config: AppConfig<E> & RootConfig<P, E> & MountOptions = {}
244+
config: AppConfig & RootConfig<GetProps<ComponentInstance<T>>> & MountOptions = {}
267245
): Promise<ComponentInstance<T>> {
268246
const app = new App(config);
269247
const root = app.createRoot(C, config);

src/runtime/component.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,23 @@ import type { ComponentNode } from "./component_node";
44
// Component Class
55
// -----------------------------------------------------------------------------
66

7-
export type Props = { [key: string]: any };
8-
97
interface StaticComponentProperties {
108
template: string;
119
defaultProps?: any;
1210
components?: { [componentName: string]: ComponentConstructor };
1311
}
1412

15-
export interface ComponentConstructor<P extends Props = any, E = any>
16-
extends StaticComponentProperties {
17-
new (env: E, node: ComponentNode): Component<P, E>;
13+
export interface ComponentConstructor extends StaticComponentProperties {
14+
new (node: ComponentNode): Component;
1815
}
1916

20-
export class Component<Props = any, Env = any> {
17+
export class Component {
2118
static template: string = "";
2219
static defaultProps?: any;
2320

24-
__props: Props = {} as any; // TODO: remove. it's just to keep types for now
25-
env: Env;
2621
__owl__: ComponentNode;
2722

28-
constructor(env: Env, node: ComponentNode) {
29-
this.env = env;
23+
constructor(node: ComponentNode) {
3024
this.__owl__ = node;
3125
}
3226

src/runtime/component_node.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { OwlError } from "../common/owl_error";
2-
import type { App, Env } from "./app";
2+
import type { App } from "./app";
33
import { BDom, VNode } from "./blockdom";
4-
import { Component, ComponentConstructor, Props } from "./component";
4+
import { Component, ComponentConstructor } from "./component";
55
import { PluginManager } from "./plugins";
66
import {
77
Atom,
@@ -53,21 +53,20 @@ function applyDefaultProps<P extends object>(props: P, defaultProps: Partial<P>)
5353

5454
type LifecycleHook = Function;
5555

56-
export class ComponentNode<P extends Props = any, E = any> implements VNode<ComponentNode<P, E>> {
56+
export class ComponentNode implements VNode<ComponentNode> {
5757
el?: HTMLElement | Text | undefined;
5858
app: App;
5959
fiber: Fiber | null = null;
60-
component: Component<P, E>;
60+
component: Component;
6161
bdom: BDom | null = null;
6262
status: STATUS = STATUS.NEW;
6363
forceNextRender: boolean = false;
6464
parentKey: string | null;
6565
name: string; // TODO: remove
66-
props: P;
66+
props: Record<string, any>;
6767

6868
renderFn: Function;
6969
parent: ComponentNode | null;
70-
childEnv: Env;
7170
children: { [key: string]: ComponentNode } = Object.create(null);
7271
refs: any = {};
7372

@@ -83,8 +82,8 @@ export class ComponentNode<P extends Props = any, E = any> implements VNode<Comp
8382
pluginManager: PluginManager;
8483

8584
constructor(
86-
C: ComponentConstructor<P, E>,
87-
props: P,
85+
C: ComponentConstructor,
86+
props: Record<string, any>,
8887
app: App,
8988
parent: ComponentNode | null,
9089
parentKey: string | null
@@ -107,11 +106,9 @@ export class ComponentNode<P extends Props = any, E = any> implements VNode<Comp
107106
applyDefaultProps(props, defaultProps);
108107
}
109108
this.props = props;
110-
const env = (parent && parent.childEnv) || app.env;
111-
this.childEnv = env;
112109
const previousComputation = getCurrentComputation();
113110
setComputation(this.signalComputation);
114-
this.component = new C(env, this);
111+
this.component = new C(this);
115112
const ctx = Object.assign(Object.create(this.component), { this: this.component });
116113
this.renderFn = app.getTemplate(C.template).bind(this.component, ctx, this);
117114
this.component.setup();
@@ -239,7 +236,7 @@ export class ComponentNode<P extends Props = any, E = any> implements VNode<Comp
239236
this.status = STATUS.DESTROYED;
240237
}
241238

242-
async updateAndRender(props: P, parentFiber: Fiber) {
239+
async updateAndRender(props: Record<string, any>, parentFiber: Fiber) {
243240
props = Object.assign({}, props);
244241
// update
245242
const fiber = makeChildFiber(this, parentFiber);
@@ -330,7 +327,7 @@ export class ComponentNode<P extends Props = any, E = any> implements VNode<Comp
330327
this.bdom!.moveBeforeDOMNode(node, parent);
331328
}
332329

333-
moveBeforeVNode(other: ComponentNode<P, E> | null, afterNode: Node | null) {
330+
moveBeforeVNode(other: ComponentNode | null, afterNode: Node | null) {
334331
this.bdom!.moveBeforeVNode(other ? other.bdom : null, afterNode);
335332
}
336333

tests/app/__snapshots__/app.test.ts.snap

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`app App supports env with getters/setters 1`] = `
4-
"function anonymous(app, bdom, helpers
5-
) {
6-
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
7-
8-
let block1 = createBlock(\`<div><block-text-0/> <block-text-1/></div>\`);
9-
10-
return function template(ctx, node, key = "") {
11-
let txt1 = ctx['env'].someVal;
12-
let txt2 = Object.keys(ctx['env'].services);
13-
return block1([txt1, txt2]);
14-
}
15-
}"
16-
`;
17-
183
exports[`app app: clear scheduler tasks and destroy cancelled nodes immediately on destroy 1`] = `
194
"function anonymous(app, bdom, helpers
205
) {

tests/app/__snapshots__/sub_root.test.ts.snap

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,6 @@ exports[`destroy a subroot while another component is mounted in main app 4`] =
5757
}"
5858
`;
5959
60-
exports[`subroot by default, env is the same in sub root 1`] = `
61-
"function anonymous(app, bdom, helpers
62-
) {
63-
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
64-
65-
let block1 = createBlock(\`<div>main app</div>\`);
66-
67-
return function template(ctx, node, key = "") {
68-
return block1();
69-
}
70-
}"
71-
`;
72-
73-
exports[`subroot by default, env is the same in sub root 2`] = `
74-
"function anonymous(app, bdom, helpers
75-
) {
76-
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
77-
78-
let block1 = createBlock(\`<div>sub root</div>\`);
79-
80-
return function template(ctx, node, key = "") {
81-
return block1();
82-
}
83-
}"
84-
`;
85-
8660
exports[`subroot can create a root in a setup function, then use a hook 1`] = `
8761
"function anonymous(app, bdom, helpers
8862
) {
@@ -157,32 +131,6 @@ exports[`subroot can mount subroot inside own dom 2`] = `
157131
}"
158132
`;
159133
160-
exports[`subroot env can be specified for sub roots 1`] = `
161-
"function anonymous(app, bdom, helpers
162-
) {
163-
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
164-
165-
let block1 = createBlock(\`<div>main app</div>\`);
166-
167-
return function template(ctx, node, key = "") {
168-
return block1();
169-
}
170-
}"
171-
`;
172-
173-
exports[`subroot env can be specified for sub roots 2`] = `
174-
"function anonymous(app, bdom, helpers
175-
) {
176-
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
177-
178-
let block1 = createBlock(\`<div>sub root</div>\`);
179-
180-
return function template(ctx, node, key = "") {
181-
return block1();
182-
}
183-
}"
184-
`;
185-
186134
exports[`subroot subcomponents can be destroyed, and it properly cleanup the subroots 1`] = `
187135
"function anonymous(app, bdom, helpers
188136
) {

0 commit comments

Comments
 (0)