Skip to content

Commit 7fc0b79

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

File tree

3 files changed

+1
-207
lines changed

3 files changed

+1
-207
lines changed

src/runtime/hooks.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Env } from "./app";
21
import { getCurrent } from "./component_node";
32
import { onMounted, onPatched, onWillDestroy, onWillUnmount } from "./lifecycle_hooks";
43
import { PluginConstructor, PluginManager } from "./plugins";
@@ -24,39 +23,6 @@ export function useRef<T extends HTMLElement = HTMLElement>(name: string): { el:
2423
};
2524
}
2625

27-
// -----------------------------------------------------------------------------
28-
// useEnv and useSubEnv
29-
// -----------------------------------------------------------------------------
30-
31-
/**
32-
* This hook is useful as a building block for some customized hooks, that may
33-
* need a reference to the env of the component calling them.
34-
*/
35-
export function useEnv<E extends Env>(): E {
36-
return getCurrent().component.env as any;
37-
}
38-
39-
function extendEnv(currentEnv: Object, extension: Object): Object {
40-
const env = Object.create(currentEnv);
41-
const descrs = Object.getOwnPropertyDescriptors(extension);
42-
return Object.freeze(Object.defineProperties(env, descrs));
43-
}
44-
45-
/**
46-
* This hook is a simple way to let components use a sub environment. Note that
47-
* like for all hooks, it is important that this is only called in the
48-
* constructor method.
49-
*/
50-
export function useSubEnv(envExtension: Env) {
51-
const node = getCurrent();
52-
node.component.env = extendEnv(node.component.env as any, envExtension);
53-
useChildSubEnv(envExtension);
54-
}
55-
56-
export function useChildSubEnv(envExtension: Env) {
57-
const node = getCurrent();
58-
node.childEnv = extendEnv(node.childEnv, envExtension);
59-
}
6026
// -----------------------------------------------------------------------------
6127
// useEffect
6228
// -----------------------------------------------------------------------------

src/runtime/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,7 @@ export { untrack } from "./reactivity/computations";
4747
export { signal } from "./reactivity/signal";
4848
export { derived } from "./reactivity/derived";
4949
export { effect } from "./reactivity/effect";
50-
export {
51-
useEffect,
52-
useEnv,
53-
useListener,
54-
useRef,
55-
useChildSubEnv,
56-
useSubEnv,
57-
usePlugins,
58-
} from "./hooks";
50+
export { useEffect, useListener, useRef, usePlugins } from "./hooks";
5951
export { batched, EventBus, htmlEscape, whenReady, markup } from "./utils";
6052
export {
6153
onWillStart,

tests/components/hooks.test.ts

Lines changed: 0 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ import {
1010
onWillUpdateProps,
1111
useComponent,
1212
useEffect,
13-
useEnv,
1413
useListener,
1514
useRef,
1615
proxy,
17-
useChildSubEnv,
18-
useSubEnv,
1916
xml,
2017
OwlError,
2118
props,
@@ -177,115 +174,6 @@ describe("hooks", () => {
177174
});
178175
});
179176

180-
test("can use useEnv", async () => {
181-
expect.assertions(3);
182-
class Test extends Component {
183-
static template = xml`<div><t t-esc="env.val"/></div>`;
184-
setup() {
185-
expect(useEnv()).toBe(this.env);
186-
}
187-
}
188-
const env = { val: 1 };
189-
await mount(Test, fixture, { env });
190-
expect(fixture.innerHTML).toBe("<div>1</div>");
191-
});
192-
193-
test("useSubEnv modifies user env", async () => {
194-
class Test extends Component {
195-
static template = xml`<div><t t-esc="env.val"/></div>`;
196-
setup() {
197-
useSubEnv({ val2: 1 });
198-
}
199-
}
200-
const env = { val: 3 };
201-
const component = await mount(Test, fixture, { env });
202-
expect(fixture.innerHTML).toBe("<div>3</div>");
203-
expect(component.env).toHaveProperty("val2");
204-
expect(component.env).toHaveProperty("val");
205-
});
206-
207-
test("useChildSubEnv does not pollute user env", async () => {
208-
class Test extends Component {
209-
static template = xml`<div><t t-esc="env.val"/></div>`;
210-
setup() {
211-
useChildSubEnv({ val2: 1 });
212-
}
213-
}
214-
const env = { val: 3 };
215-
const component = await mount(Test, fixture, { env });
216-
expect(fixture.innerHTML).toBe("<div>3</div>");
217-
expect(component.env).not.toHaveProperty("val2");
218-
expect(component.env).toHaveProperty("val");
219-
});
220-
221-
test("useSubEnv supports arbitrary descriptor", async () => {
222-
let someVal = "maggot";
223-
let someVal2 = "brain";
224-
225-
class Child extends Component {
226-
static template = xml`<div><t t-esc="env.someVal" /> <t t-esc="env.someVal2" /></div>`;
227-
}
228-
229-
class Test extends Component {
230-
static template = xml`<Child />`;
231-
static components = { Child };
232-
setup() {
233-
useSubEnv({
234-
get someVal2() {
235-
return someVal2;
236-
},
237-
});
238-
}
239-
}
240-
241-
const env = {
242-
get someVal() {
243-
return someVal;
244-
},
245-
};
246-
const component = await mount(Test, fixture, { env });
247-
expect(fixture.innerHTML).toBe("<div>maggot brain</div>");
248-
someVal = "brain";
249-
someVal2 = "maggot";
250-
component.render(true);
251-
await nextTick();
252-
expect(fixture.innerHTML).toBe("<div>brain maggot</div>");
253-
});
254-
255-
test("useChildSubEnv supports arbitrary descriptor", async () => {
256-
let someVal = "maggot";
257-
let someVal2 = "brain";
258-
259-
class Child extends Component {
260-
static template = xml`<div><t t-esc="env.someVal" /> <t t-esc="env.someVal2" /></div>`;
261-
}
262-
263-
class Test extends Component {
264-
static template = xml`<Child />`;
265-
static components = { Child };
266-
setup() {
267-
useChildSubEnv({
268-
get someVal2() {
269-
return someVal2;
270-
},
271-
});
272-
}
273-
}
274-
someVal = "maggot";
275-
const env = {
276-
get someVal() {
277-
return someVal;
278-
},
279-
};
280-
const component = await mount(Test, fixture, { env });
281-
expect(fixture.innerHTML).toBe("<div>maggot brain</div>");
282-
someVal = "brain";
283-
someVal2 = "maggot";
284-
component.render(true);
285-
await nextTick();
286-
expect(fixture.innerHTML).toBe("<div>brain maggot</div>");
287-
});
288-
289177
test("can use useComponent", async () => {
290178
expect.assertions(2);
291179
class Test extends Component {
@@ -297,58 +185,6 @@ describe("hooks", () => {
297185
await mount(Test, fixture);
298186
});
299187

300-
test("parent and child env (with useSubEnv)", async () => {
301-
class Child extends Component {
302-
static template = xml`<div><t t-esc="env.val"/></div>`;
303-
}
304-
305-
class Parent extends Component {
306-
static template = xml`<t t-esc="env.val"/><Child/>`;
307-
static components = { Child };
308-
setup() {
309-
useSubEnv({ val: 5 });
310-
}
311-
}
312-
const env = { val: 3 };
313-
await mount(Parent, fixture, { env });
314-
expect(fixture.innerHTML).toBe("5<div>5</div>");
315-
});
316-
317-
test("parent and child env (with useChildSubEnv)", async () => {
318-
class Child extends Component {
319-
static template = xml`<div><t t-esc="env.val"/></div>`;
320-
}
321-
322-
class Parent extends Component {
323-
static template = xml`<t t-esc="env.val"/><Child/>`;
324-
static components = { Child };
325-
setup() {
326-
useChildSubEnv({ val: 5 });
327-
}
328-
}
329-
const env = { val: 3 };
330-
await mount(Parent, fixture, { env });
331-
expect(fixture.innerHTML).toBe("3<div>5</div>");
332-
});
333-
334-
test("parent and child env (with useChildSubEnv then useSubEnv)", async () => {
335-
class Child extends Component {
336-
static template = xml`<div t-if="env.hasParent"><t t-esc="env.val"/></div>`;
337-
}
338-
339-
class Parent extends Component {
340-
static template = xml`<t t-esc="env.val"/><Child/>`;
341-
static components = { Child };
342-
setup() {
343-
useChildSubEnv({ hasParent: true });
344-
useSubEnv({ val: 5 });
345-
}
346-
}
347-
const env = { val: 3 };
348-
await mount(Parent, fixture, { env });
349-
expect(fixture.innerHTML).toBe("5<div>5</div>");
350-
});
351-
352188
test("can use onWillStart, onWillUpdateProps", async () => {
353189
const steps: string[] = [];
354190
async function slow(): Promise<string> {

0 commit comments

Comments
 (0)