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

Commit

Permalink
Refactor setGlobalContext into a function to remove cyclic depedency
Browse files Browse the repository at this point in the history
  • Loading branch information
chiragpat authored and izelnakri committed Jun 25, 2021
1 parent c363b4a commit c01f879
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 69 deletions.
137 changes: 69 additions & 68 deletions packages/@glimmer/core/src/environment/delegates.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
import setGlobalContext from '@glimmer/global-context';
import setGlobalContextVM from '@glimmer/global-context';
import { EnvironmentDelegate } from '@glimmer/runtime';
import { Option, Destructor, Destroyable } from '@glimmer/interfaces';
import { IteratorDelegate } from '@glimmer/reference';

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

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

setGlobalContext({
getProp(obj: Record<string, unknown>, key: string) {
return obj[key];
},

setProp(obj: Record<string, unknown>, key: string, newValue: unknown) {
obj[key] = newValue;
},

getPath(obj: Record<string, unknown>, key: string) {
if (DEBUG && key.includes('.')) {
throw new Error(
'You attempted to get a path with a `.` in it, but Glimmer.js does not support paths with dots.'
);
}

return obj[key];
},

setPath(obj: Record<string, unknown>, key: string, newValue: unknown) {
if (DEBUG && key.includes('.')) {
throw new Error(
'You attempted to set a path with a `.` in it, but Glimmer.js does not support paths with dots.'
);
}

obj[key] = newValue;
},

scheduleRevalidate,

toBool,

toIterator(value: unknown): Option<IteratorDelegate> {
if (isNativeIterable(value)) {
return NativeIterator.from(value);
}

return null;
},

scheduleDestroy(destroyable, destructor) {
scheduledDestroyables.push(destroyable);
scheduledDestructors.push(destructor);
},

scheduleDestroyed(fn) {
scheduledFinishDestruction.push(fn);
},

warnIfStyleNotTrusted() {
// Do nothing
},

assert(test: unknown, msg: string) {
if (!test) {
throw new Error(msg);
}
},

deprecate(msg: string, test: unknown) {
if (!test) {
console.warn(msg);
}
},
});
export function setGlobalContext(scheduleRevalidate: () => void): void {
setGlobalContextVM({
getProp(obj: Record<string, unknown>, key: string) {
return obj[key];
},

setProp(obj: Record<string, unknown>, key: string, newValue: unknown) {
obj[key] = newValue;
},

getPath(obj: Record<string, unknown>, key: string) {
if (DEBUG && key.includes('.')) {
throw new Error(
'You attempted to get a path with a `.` in it, but Glimmer.js does not support paths with dots.'
);
}

return obj[key];
},

setPath(obj: Record<string, unknown>, key: string, newValue: unknown) {
if (DEBUG && key.includes('.')) {
throw new Error(
'You attempted to set a path with a `.` in it, but Glimmer.js does not support paths with dots.'
);
}

obj[key] = newValue;
},

scheduleRevalidate,

toBool,

toIterator(value: unknown): Option<IteratorDelegate> {
if (isNativeIterable(value)) {
return NativeIterator.from(value);
}

return null;
},

scheduleDestroy(destroyable, destructor) {
scheduledDestroyables.push(destroyable);
scheduledDestructors.push(destructor);
},

scheduleDestroyed(fn) {
scheduledFinishDestruction.push(fn);
},

warnIfStyleNotTrusted() {
// Do nothing
},

assert(test: unknown, msg: string) {
if (!test) {
throw new Error(msg);
}
},

deprecate(msg: string, test: unknown) {
if (!test) {
console.warn(msg);
}
},
});
}

/**
* The environment delegate base class shared by both the client and SSR
Expand Down
4 changes: 3 additions & 1 deletion packages/@glimmer/core/src/render-component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { artifacts } from '@glimmer/program';
import { programCompilationContext } from '@glimmer/opcode-compiler';

import { ClientEnvDelegate } from '../environment/delegates';
import { ClientEnvDelegate, setGlobalContext } from '../environment/delegates';
import { CompileTimeResolver, RuntimeResolver } from './resolvers';

import { SimpleElement, SimpleDocument } from '@simple-dom/interface';
Expand Down Expand Up @@ -101,6 +101,8 @@ export function scheduleRevalidate(): void {
}, 0);
}

setGlobalContext(scheduleRevalidate);

function revalidate(): void {
for (const result of results) {
const { env } = result;
Expand Down

0 comments on commit c01f879

Please sign in to comment.