Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit c01f879

Browse files
chiragpatizelnakri
authored andcommitted
Refactor setGlobalContext into a function to remove cyclic depedency
1 parent c363b4a commit c01f879

File tree

2 files changed

+72
-69
lines changed

2 files changed

+72
-69
lines changed

packages/@glimmer/core/src/environment/delegates.ts

Lines changed: 69 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,84 @@
1-
import setGlobalContext from '@glimmer/global-context';
1+
import setGlobalContextVM from '@glimmer/global-context';
22
import { EnvironmentDelegate } from '@glimmer/runtime';
33
import { Option, Destructor, Destroyable } from '@glimmer/interfaces';
44
import { IteratorDelegate } from '@glimmer/reference';
55

66
import { isNativeIterable, NativeIterator } from './iterator';
77
import { DEBUG } from '@glimmer/env';
88
import toBool from './to-bool';
9-
import { scheduleRevalidate } from '../render-component';
109

1110
let scheduledDestroyables: Destroyable[] = [];
1211
let scheduledDestructors: Destructor<object>[] = [];
1312
let scheduledFinishDestruction: (() => void)[] = [];
1413

15-
setGlobalContext({
16-
getProp(obj: Record<string, unknown>, key: string) {
17-
return obj[key];
18-
},
19-
20-
setProp(obj: Record<string, unknown>, key: string, newValue: unknown) {
21-
obj[key] = newValue;
22-
},
23-
24-
getPath(obj: Record<string, unknown>, key: string) {
25-
if (DEBUG && key.includes('.')) {
26-
throw new Error(
27-
'You attempted to get a path with a `.` in it, but Glimmer.js does not support paths with dots.'
28-
);
29-
}
30-
31-
return obj[key];
32-
},
33-
34-
setPath(obj: Record<string, unknown>, key: string, newValue: unknown) {
35-
if (DEBUG && key.includes('.')) {
36-
throw new Error(
37-
'You attempted to set a path with a `.` in it, but Glimmer.js does not support paths with dots.'
38-
);
39-
}
40-
41-
obj[key] = newValue;
42-
},
43-
44-
scheduleRevalidate,
45-
46-
toBool,
47-
48-
toIterator(value: unknown): Option<IteratorDelegate> {
49-
if (isNativeIterable(value)) {
50-
return NativeIterator.from(value);
51-
}
52-
53-
return null;
54-
},
55-
56-
scheduleDestroy(destroyable, destructor) {
57-
scheduledDestroyables.push(destroyable);
58-
scheduledDestructors.push(destructor);
59-
},
60-
61-
scheduleDestroyed(fn) {
62-
scheduledFinishDestruction.push(fn);
63-
},
64-
65-
warnIfStyleNotTrusted() {
66-
// Do nothing
67-
},
68-
69-
assert(test: unknown, msg: string) {
70-
if (!test) {
71-
throw new Error(msg);
72-
}
73-
},
74-
75-
deprecate(msg: string, test: unknown) {
76-
if (!test) {
77-
console.warn(msg);
78-
}
79-
},
80-
});
14+
export function setGlobalContext(scheduleRevalidate: () => void): void {
15+
setGlobalContextVM({
16+
getProp(obj: Record<string, unknown>, key: string) {
17+
return obj[key];
18+
},
19+
20+
setProp(obj: Record<string, unknown>, key: string, newValue: unknown) {
21+
obj[key] = newValue;
22+
},
23+
24+
getPath(obj: Record<string, unknown>, key: string) {
25+
if (DEBUG && key.includes('.')) {
26+
throw new Error(
27+
'You attempted to get a path with a `.` in it, but Glimmer.js does not support paths with dots.'
28+
);
29+
}
30+
31+
return obj[key];
32+
},
33+
34+
setPath(obj: Record<string, unknown>, key: string, newValue: unknown) {
35+
if (DEBUG && key.includes('.')) {
36+
throw new Error(
37+
'You attempted to set a path with a `.` in it, but Glimmer.js does not support paths with dots.'
38+
);
39+
}
40+
41+
obj[key] = newValue;
42+
},
43+
44+
scheduleRevalidate,
45+
46+
toBool,
47+
48+
toIterator(value: unknown): Option<IteratorDelegate> {
49+
if (isNativeIterable(value)) {
50+
return NativeIterator.from(value);
51+
}
52+
53+
return null;
54+
},
55+
56+
scheduleDestroy(destroyable, destructor) {
57+
scheduledDestroyables.push(destroyable);
58+
scheduledDestructors.push(destructor);
59+
},
60+
61+
scheduleDestroyed(fn) {
62+
scheduledFinishDestruction.push(fn);
63+
},
64+
65+
warnIfStyleNotTrusted() {
66+
// Do nothing
67+
},
68+
69+
assert(test: unknown, msg: string) {
70+
if (!test) {
71+
throw new Error(msg);
72+
}
73+
},
74+
75+
deprecate(msg: string, test: unknown) {
76+
if (!test) {
77+
console.warn(msg);
78+
}
79+
},
80+
});
81+
}
8182

8283
/**
8384
* The environment delegate base class shared by both the client and SSR

packages/@glimmer/core/src/render-component/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { artifacts } from '@glimmer/program';
1919
import { programCompilationContext } from '@glimmer/opcode-compiler';
2020

21-
import { ClientEnvDelegate } from '../environment/delegates';
21+
import { ClientEnvDelegate, setGlobalContext } from '../environment/delegates';
2222
import { CompileTimeResolver, RuntimeResolver } from './resolvers';
2323

2424
import { SimpleElement, SimpleDocument } from '@simple-dom/interface';
@@ -101,6 +101,8 @@ export function scheduleRevalidate(): void {
101101
}, 0);
102102
}
103103

104+
setGlobalContext(scheduleRevalidate);
105+
104106
function revalidate(): void {
105107
for (const result of results) {
106108
const { env } = result;

0 commit comments

Comments
 (0)