Skip to content

Commit a572b5c

Browse files
committed
✨ node data bag + Context accessors with prototype inheritance
1 parent 78dd979 commit a572b5c

4 files changed

Lines changed: 221 additions & 1 deletion

File tree

‎packages/ui/src/core.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export {};
1+
export * from './core/context.ts';
2+
export * from './core/node.ts';
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { describe, expect, test } from 'vitest';
2+
import { createContext, MissingContextError } from './context.ts';
3+
import { Node } from './node.ts';
4+
5+
const theme = createContext<string>('theme');
6+
const count = createContext<number>('count');
7+
const locale = createContext<string>('locale', 'en');
8+
9+
describe('context', () => {
10+
describe('on a single node', () => {
11+
test('get returns undefined for a context that was never set', () => {
12+
expect(theme.get(new Node())).toBeUndefined();
13+
});
14+
15+
test('does not inherit values from Object.prototype', () => {
16+
const constructor = createContext('constructor');
17+
18+
expect(constructor.get(new Node())).toBeUndefined();
19+
});
20+
21+
test('get returns the value after it is set', () => {
22+
const n = new Node();
23+
theme.set(n, 'dark');
24+
expect(theme.get(n)).toBe('dark');
25+
});
26+
27+
test('the last set wins', () => {
28+
const n = new Node();
29+
theme.set(n, 'dark');
30+
theme.set(n, 'light');
31+
expect(theme.get(n)).toBe('light');
32+
});
33+
34+
test('keeps falsy values — 0 is stored, not treated as unset', () => {
35+
const n = new Node();
36+
count.set(n, 0);
37+
expect(count.get(n)).toBe(0);
38+
});
39+
40+
test('distinct contexts do not collide', () => {
41+
const n = new Node();
42+
theme.set(n, 'dark');
43+
expect(count.get(n)).toBeUndefined();
44+
});
45+
46+
test('expect returns the value when the context is set', () => {
47+
const n = new Node();
48+
theme.set(n, 'dark');
49+
expect(theme.expect(n)).toBe('dark');
50+
});
51+
52+
test('expect throws MissingContextError when unset with no default', () => {
53+
expect(() => theme.expect(new Node())).toThrow(MissingContextError);
54+
});
55+
56+
test('expect falls back to the default when unset', () => {
57+
expect(locale.expect(new Node())).toBe('en');
58+
});
59+
60+
test('a set value takes precedence over the default', () => {
61+
const n = new Node();
62+
locale.set(n, 'fr');
63+
expect(locale.expect(n)).toBe('fr');
64+
});
65+
66+
test('get returns the default when the context is unset', () => {
67+
expect(locale.get(new Node())).toBe('en');
68+
});
69+
70+
test('a set value overrides the default for get', () => {
71+
const n = new Node();
72+
locale.set(n, 'fr');
73+
expect(locale.get(n)).toBe('fr');
74+
});
75+
76+
test('hasOwn is false before a value is set', () => {
77+
expect(theme.hasOwn(new Node())).toBe(false);
78+
});
79+
80+
test('hasOwn is true once a value is set on the node', () => {
81+
const n = new Node();
82+
theme.set(n, 'dark');
83+
expect(theme.hasOwn(n)).toBe(true);
84+
});
85+
});
86+
87+
describe('inheritance through the node tree', () => {
88+
test('a child inherits a value set on its parent', () => {
89+
const parent = new Node();
90+
theme.set(parent, 'dark');
91+
expect(theme.get(new Node(parent))).toBe('dark');
92+
});
93+
94+
test('a value set on a child shadows the parent for that child only', () => {
95+
const parent = new Node();
96+
theme.set(parent, 'dark');
97+
const child = new Node(parent);
98+
theme.set(child, 'light');
99+
expect(theme.get(child)).toBe('light');
100+
expect(theme.get(parent)).toBe('dark');
101+
});
102+
103+
test('setting a value on a child does not leak up to the parent', () => {
104+
const parent = new Node();
105+
const child = new Node(parent);
106+
theme.set(child, 'light');
107+
expect(theme.get(parent)).toBeUndefined();
108+
});
109+
110+
test('expect inherits an ancestor value', () => {
111+
const parent = new Node();
112+
theme.set(parent, 'dark');
113+
expect(theme.expect(new Node(parent))).toBe('dark');
114+
});
115+
116+
test('an inherited value resolves through multiple levels', () => {
117+
const grand = new Node();
118+
theme.set(grand, 'dark');
119+
const parent = new Node(grand);
120+
expect(theme.get(new Node(parent))).toBe('dark');
121+
});
122+
123+
test('hasOwn is false for a value that is only inherited', () => {
124+
const parent = new Node();
125+
theme.set(parent, 'dark');
126+
expect(theme.hasOwn(new Node(parent))).toBe(false);
127+
});
128+
129+
test('hasOwn is true only for the node that owns the value', () => {
130+
const parent = new Node();
131+
theme.set(parent, 'dark');
132+
const child = new Node(parent);
133+
theme.set(child, 'light');
134+
expect(theme.hasOwn(child)).toBe(true);
135+
expect(theme.hasOwn(parent)).toBe(true);
136+
});
137+
138+
test('expect falls back to the default when neither node nor ancestors set it', () => {
139+
const parent = new Node();
140+
expect(locale.expect(new Node(parent))).toBe('en');
141+
});
142+
143+
test('get falls back to the default when neither node nor ancestors set it', () => {
144+
const parent = new Node();
145+
expect(locale.get(new Node(parent))).toBe('en');
146+
});
147+
});
148+
});

‎packages/ui/src/core/context.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { Node } from './node.ts';
2+
3+
/**
4+
* A `Context` defines a value which is in effect for a given {@link Node}.
5+
*
6+
* Unless a context value is set for a particular node, it will inherit its
7+
* value from that node's parent.
8+
*/
9+
export class Context<T> {
10+
/** A unique identifier for this context. */
11+
readonly name: string;
12+
13+
/** The value returned when this context is not present on a node. */
14+
readonly defaultValue?: T;
15+
16+
constructor(name: string, defaultValue?: T) {
17+
this.name = name;
18+
this.defaultValue = defaultValue;
19+
}
20+
21+
get(node: Node): T | undefined {
22+
const value = node.contexts[this.name] as T | undefined;
23+
return value === undefined ? this.defaultValue : value;
24+
}
25+
26+
set(node: Node, value: T): void {
27+
node.contexts[this.name] = value;
28+
}
29+
30+
expect(node: Node): T {
31+
const value = this.get(node);
32+
if (value === undefined) {
33+
throw new MissingContextError(this.name);
34+
} else {
35+
return value;
36+
}
37+
}
38+
39+
hasOwn(node: Node): boolean {
40+
return Object.hasOwn(node.contexts, this.name);
41+
}
42+
}
43+
44+
/**
45+
* Create a new {@link Context}.
46+
*
47+
* @param name - the unique name to give this context.
48+
* @param defaultValue - the value used when the context is not present on a node.
49+
* @returns the new context
50+
*/
51+
export function createContext<T>(name: string, defaultValue?: T): Context<T> {
52+
return new Context(name, defaultValue);
53+
}
54+
55+
export class MissingContextError extends Error {
56+
constructor(name: string) {
57+
super(`no value set for context "${name}"`);
58+
this.name = 'MissingContextError';
59+
}
60+
}

‎packages/ui/src/core/node.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class Node {
2+
readonly parent?: Node;
3+
readonly contexts: Record<string, unknown>;
4+
readonly children: Set<Node>;
5+
6+
constructor(parent?: Node) {
7+
this.parent = parent;
8+
this.contexts = Object.create(parent ? parent.contexts : null);
9+
this.children = new Set();
10+
}
11+
}

0 commit comments

Comments
 (0)