Skip to content

Commit b1c080c

Browse files
committed
feat: get rid of context property
1 parent aaa6cda commit b1c080c

File tree

8 files changed

+24
-55
lines changed

8 files changed

+24
-55
lines changed

src/tests/utils.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { resetNodeCounter, createRoot, $_c } from '@/utils/dom';
88
import { renderInBrowser } from '@/utils/ssr/ssr';
99
import { runDestructors } from '@/utils/component';
1010
import { $_fin, Root } from '../utils';
11-
import { $context, addToTree, PARENT, RENDERED_NODES_PROPERTY, TREE } from '@/utils/shared';
11+
import { addToTree, PARENT, RENDERED_NODES_PROPERTY, TREE } from '@/utils/shared';
1212
import { cleanupFastContext } from '@/utils/context';
1313

1414
let ROOT: null | Root = null;
@@ -89,9 +89,6 @@ export function createTestComponent(component: ComponentReturnType, owner: Root)
8989
addToTree(owner, this);
9090
return $_fin([$_c(component, {
9191
...args,
92-
...{
93-
[$context]: this,
94-
}
9592
// @ts-expect-error
9693
}, this)], this);
9794
}
@@ -116,9 +113,6 @@ export async function render(component: ComponentReturnType) {
116113
let renderResult = renderComponent(
117114
createTestComponent(component, owner),
118115
{
119-
args: {
120-
[$context]: owner,
121-
},
122116
element: targetElement,
123117
owner,
124118
}

src/utils/component.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
TREE,
2727
CHILD,
2828
PARENT,
29-
$context,
3029
} from './shared';
3130
import { resolveRenderable, Root, $_c } from './dom';
3231
import {
@@ -138,13 +137,7 @@ export function renderComponent(
138137
);
139138
}
140139

141-
const args = {
142-
...componentArgs,
143-
...{
144-
[$context]: appRoot,
145-
},
146-
};
147-
const instance = $_c(component, args, appRoot);
140+
const instance = $_c(component, componentArgs, appRoot);
148141

149142
const dom = initDOM(appRoot);
150143
const children = instance[$nodes];

src/utils/context.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { registerDestructor } from './glimmer/destroyable';
22
import { Component } from './component';
33
import {
4-
$context,
54
COMPONENT_ID_PROPERTY,
65
isFn,
76
PARENT,
@@ -84,9 +83,6 @@ export function getContext<T>(
8483
// TODO: add fancy error message about missing provider in dev mode,
8584
// we may track context usage and provide a better error message
8685
if (import.meta.env.DEV && !current && !(ctx instanceof Root)) {
87-
if (ctx?.args?.[$context]) {
88-
return getContext(ctx?.args?.[$context], key);
89-
}
9086
console.log('`Unable to resolve parent for ', ctx, key);
9187
console.log('Lookup tree:', lookupTree);
9288
debugger;

src/utils/control-flow/if.ts

-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
isPrimitive,
1717
isTagLike,
1818
addToTree,
19-
$context,
2019
RENDERED_NODES_PROPERTY,
2120
cId,
2221
COMPONENT_ID_PROPERTY,
@@ -25,9 +24,6 @@ import { opcodeFor } from '@/utils/vm';
2524
import { initDOM } from '@/utils/context';
2625

2726
export class IfCondition {
28-
declare args: {
29-
[$context]: Component<any>
30-
}
3127
isDestructorRunning = false;
3228
prevComponent: GenericReturnType | null = null;
3329
condition!: MergedCell | Cell<boolean>;
@@ -55,9 +51,6 @@ export class IfCondition {
5551
this.setupCondition(maybeCondition);
5652
this.trueBranch = trueBranch;
5753
this.falseBranch = falseBranch;
58-
this.args = {
59-
[$context]: parentContext,
60-
}
6154
// @ts-expect-error typings error
6255
addToTree(parentContext, this, 'from if constructor');
6356
this.destructors.push(opcodeFor(this.condition, this.syncState.bind(this)));

src/utils/control-flow/list.ts

-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
isTagLike,
1919
LISTS_FOR_HMR,
2020
addToTree,
21-
$context,
2221
RENDERED_NODES_PROPERTY,
2322
COMPONENT_ID_PROPERTY,
2423
cId,
@@ -120,19 +119,13 @@ export class BasicListComponent<T extends { id: number }> {
120119
}
121120
}
122121
declare api: DOMApi;
123-
declare args: {
124-
[$context]: Component<any>;
125-
};
126122
constructor(
127123
{ tag, ctx, key, ItemComponent }: ListComponentArgs<T>,
128124
outlet: RenderTarget,
129125
topMarker: Comment,
130126
) {
131127
this.api = initDOM(ctx);
132128
this.ItemComponent = ItemComponent;
133-
this.args = {
134-
[$context]: ctx,
135-
};
136129
// @ts-expect-error typings error
137130
addToTree(ctx, this, 'from list constructor');
138131
const mainNode = outlet;

src/utils/dom.ts

+20-19
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import {
4040
IN_SSR_ENV,
4141
COMPONENTS_HMR,
4242
isEmpty,
43-
$context,
4443
RENDERING_CONTEXT_PROPERTY,
4544
RENDERED_NODES_PROPERTY,
4645
COMPONENT_ID_PROPERTY,
@@ -789,6 +788,15 @@ export const $_maybeHelper = (
789788
return value;
790789
};
791790

791+
let parentContext: Root | Component<any> | null = null;
792+
793+
export const setParentContext = (value: Root | Component<any> | null) => {
794+
parentContext = value;
795+
}
796+
export const getParentContext = () => {
797+
return parentContext;
798+
}
799+
792800
function component(
793801
comp: ComponentReturnType | Component | typeof Component,
794802
args: Record<string, unknown>,
@@ -820,6 +828,7 @@ function component(
820828
}
821829
// @ts-expect-error uniqSymbol as index
822830
const fw = args[$PROPS_SYMBOL] as unknown as FwType;
831+
setParentContext(ctx);
823832
return _component(comp, args, fw, ctx);
824833
} catch (e) {
825834
if (import.meta.env.SSR) {
@@ -862,14 +871,20 @@ function component(
862871
};
863872
}
864873
} finally {
874+
setParentContext(null);
865875
if (IS_DEV_MODE) {
866876
$DEBUG_REACTIVE_CONTEXTS.pop();
867877
}
868878
}
869879
} else {
870880
// @ts-expect-error uniqSymbol as index
871881
const fw = args[$PROPS_SYMBOL] as unknown as FwType;
872-
return _component(comp, args, fw, ctx);
882+
try {
883+
setParentContext(ctx);
884+
return _component(comp, args, fw, ctx);
885+
} finally {
886+
setParentContext(null);
887+
}
873888
}
874889
}
875890
// hello, basic component manager
@@ -879,7 +894,6 @@ function _component(
879894
fw: FwType,
880895
ctx: Component<any> | Root,
881896
) {
882-
args[$context] = ctx;
883897
let startTagId = 0;
884898
if (IS_DEV_MODE) {
885899
startTagId = getTagId();
@@ -975,9 +989,7 @@ function createSlot(
975989
$DEBUG_REACTIVE_CONTEXTS.push(`:${name}`);
976990
}
977991
const slotContext = {
978-
[$args]: {
979-
[$context]: ctx,
980-
},
992+
[$args]: {},
981993
[RENDERED_NODES_PROPERTY]: [],
982994
[COMPONENT_ID_PROPERTY]: cId(),
983995
[RENDERING_CONTEXT_PROPERTY]: null,
@@ -1154,12 +1166,7 @@ const ArgProxyHandler: ProxyHandler<{}> = {
11541166
}
11551167
return undefined;
11561168
},
1157-
set(target, prop, value) {
1158-
if (prop === $context) {
1159-
// @ts-expect-error unknown property
1160-
target[prop] = value;
1161-
return true;
1162-
}
1169+
set() {
11631170
if (IS_DEV_MODE) {
11641171
throw new Error('args are readonly');
11651172
}
@@ -1170,13 +1177,7 @@ export function $_GET_ARGS(ctx: Component<any>, args: IArguments) {
11701177
ctx[$args] = ctx[$args] || args[0] || {};
11711178
ctx[RENDERED_NODES_PROPERTY] = ctx[RENDERED_NODES_PROPERTY] ?? [];
11721179
ctx[COMPONENT_ID_PROPERTY] = ctx[COMPONENT_ID_PROPERTY] ?? cId();
1173-
const parentContext = ctx[$args][$context] || args[0][$context];
1174-
if (IS_DEV_MODE) {
1175-
if (!parentContext) {
1176-
throw new Error(`Unable to resolve parent context`);
1177-
}
1178-
}
1179-
addToTree(parentContext, ctx);
1180+
addToTree(getParentContext()!, ctx);
11801181
}
11811182
export function $_GET_SLOTS(ctx: any, args: any) {
11821183
return (args[0] || {})[$SLOTS_SYMBOL] || ctx[$args]?.[$SLOTS_SYMBOL] || {};

src/utils/inspector/ember-inspector.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as backburner from 'backburner.js';
2-
import { $_debug_args, $context, $nodes, CHILD, COMPONENT_ID_PROPERTY, getBounds, isArray, PARENT, RENDERED_NODES_PROPERTY, TREE } from '../shared';
2+
import { $_debug_args, $nodes, CHILD, COMPONENT_ID_PROPERTY, getBounds, isArray, PARENT, RENDERED_NODES_PROPERTY, TREE } from '../shared';
33
import { Component } from '..';
44
import { Cell, MergedCell, getCells, getMergedCells } from '../reactive';
55
import { $args } from '../shared';
@@ -466,7 +466,7 @@ const EmberProxy: any = new Proxy(
466466
const hasArgs = component && $args in component;
467467
const hasDebugArgs = component && $_debug_args in component;
468468
const hasArgsOrDebugArgs = hasArgs || hasDebugArgs;
469-
const argsToHide: string[] = [$context];
469+
const argsToHide: string[] = [];
470470
const argsToAdd: Array<[string, unknown]> = [];
471471
// const isUnstableChildWrapper = component && component.debugName && component.debugName.startsWith('UnstableChildWrapper');
472472
// if (component && !isUnstableChildWrapper && !hasArgs && !hasDebugArgs) {

src/utils/shared.ts

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export function cId() {
1919
}
2020

2121
export const $template = 'template' as const;
22-
export const $context = '_context' as const;
2322
export const $nodes = 'nodes' as const;
2423
export const $args = 'args' as const;
2524
export const $_debug_args = '_debug_args' as const;

0 commit comments

Comments
 (0)