Skip to content

Commit e8242d9

Browse files
committed
✨ lifecycle: create/destroy/id as Apis over a hidden global
1 parent d1b6137 commit e8242d9

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

‎packages/tree/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './api.ts';
22
export * from './context.ts';
3+
export * from './lifecycle.ts';
34
export * from './node.ts';
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { describe, expect, test } from 'vitest';
2+
import { create, destroy, id, LifecycleApi } from './lifecycle.ts';
3+
import type { Node } from './node.ts';
4+
5+
describe('lifecycle', () => {
6+
describe('create', () => {
7+
test('with no parent, roots the node in the global scope', () => {
8+
const node = create();
9+
expect(node.parent).toBeDefined();
10+
});
11+
12+
test('wires the child into its parent', () => {
13+
const parent = create();
14+
const child = create(parent);
15+
expect(child.parent).toBe(parent);
16+
expect(parent.children.has(child)).toBe(true);
17+
});
18+
19+
test('autogenerates an id', () => {
20+
const node = create();
21+
expect(typeof id(node)).toBe('string');
22+
expect(id(node).length).toBeGreaterThan(0);
23+
});
24+
25+
test('autogenerated ids are distinct', () => {
26+
expect(id(create())).not.toBe(id(create()));
27+
});
28+
29+
test('a child owns its id rather than inheriting its parent’s', () => {
30+
const parent = create();
31+
const child = create(parent);
32+
expect(id(child)).not.toBe(id(parent));
33+
});
34+
35+
test('hooks fire through create middleware', () => {
36+
const created: Node[] = [];
37+
const root = create();
38+
LifecycleApi.around(root, {
39+
create: (args, next) => {
40+
const node = next(...args);
41+
created.push(node);
42+
return node;
43+
},
44+
});
45+
const a = create(root);
46+
const b = create(a);
47+
expect(created).toEqual([a, b]);
48+
});
49+
});
50+
51+
describe('destroy', () => {
52+
test('detaches a node from its parent', () => {
53+
const parent = create();
54+
const child = create(parent);
55+
destroy(child);
56+
expect(parent.children.has(child)).toBe(false);
57+
});
58+
59+
test('cascades to descendants', () => {
60+
const root = create();
61+
const parent = create(root);
62+
const child = create(parent);
63+
destroy(root);
64+
expect(root.children.has(parent)).toBe(false);
65+
expect(parent.children.has(child)).toBe(false);
66+
});
67+
68+
test('tears down bottom-up, each node running its destroy middleware', () => {
69+
const order: Node[] = [];
70+
const root = create();
71+
LifecycleApi.around(root, {
72+
destroy: (args, next) => {
73+
const result = next(...args);
74+
order.push(args[0]);
75+
return result;
76+
},
77+
});
78+
const parent = create(root);
79+
const child = create(parent);
80+
destroy(root);
81+
expect(order).toEqual([child, parent, root]);
82+
});
83+
});
84+
});

‎packages/tree/src/lifecycle.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { type Node, createNode } from './node.ts';
2+
import { createApi } from './api.ts';
3+
import { createContext } from './context.ts';
4+
5+
export const LifecycleApi = createApi('lifecycle', {
6+
create(parent) {
7+
const ids = Ids.expect(parent);
8+
const node = createNode(parent);
9+
parent.children.add(node);
10+
IdContext.set(node, `${ids.latest++}`);
11+
return node;
12+
},
13+
destroy(node) {
14+
for (const child of node.children) {
15+
destroy(child);
16+
}
17+
node.parent?.children.delete(node);
18+
},
19+
id(node) {
20+
return IdContext.expect(node);
21+
},
22+
});
23+
24+
export const { destroy, id } = LifecycleApi.methods;
25+
26+
export function create(parent: Node = global) {
27+
return LifecycleApi.methods.create(parent);
28+
}
29+
30+
const IdContext = createContext<string>('id');
31+
const Ids = createContext<{ latest: number }>('@ids');
32+
33+
const global: Node = createNode();
34+
35+
IdContext.set(global, 'root');
36+
Ids.set(global, { latest: 0 });

0 commit comments

Comments
 (0)