@dxos/react-ui-attention 0.8.4-main.f9ba587 → 0.8.4-main.fcfe5033a5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/dist/lib/browser/chunk-4SEURHOL.mjs +721 -0
  2. package/dist/lib/browser/chunk-4SEURHOL.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-GMKHOVJ5.mjs +1 -0
  4. package/dist/lib/browser/chunk-GMKHOVJ5.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +87 -13
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +20 -7
  9. package/dist/lib/browser/testing/index.mjs.map +3 -3
  10. package/dist/lib/browser/types.mjs +2 -0
  11. package/dist/lib/browser/types.mjs.map +7 -0
  12. package/dist/lib/node-esm/chunk-6PQ4P7AL.mjs +723 -0
  13. package/dist/lib/node-esm/chunk-6PQ4P7AL.mjs.map +7 -0
  14. package/dist/lib/node-esm/chunk-UHVMYYIB.mjs +2 -0
  15. package/dist/lib/node-esm/chunk-UHVMYYIB.mjs.map +7 -0
  16. package/dist/lib/node-esm/index.mjs +87 -13
  17. package/dist/lib/node-esm/index.mjs.map +4 -4
  18. package/dist/lib/node-esm/meta.json +1 -1
  19. package/dist/lib/node-esm/testing/index.mjs +20 -7
  20. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  21. package/dist/lib/node-esm/types.mjs +3 -0
  22. package/dist/lib/node-esm/types.mjs.map +7 -0
  23. package/dist/types/src/attention.d.ts +56 -20
  24. package/dist/types/src/attention.d.ts.map +1 -1
  25. package/dist/types/src/components/AttentionGlyph.d.ts.map +1 -1
  26. package/dist/types/src/components/AttentionGlyph.stories.d.ts +9 -20
  27. package/dist/types/src/components/AttentionGlyph.stories.d.ts.map +1 -1
  28. package/dist/types/src/components/AttentionProvider.d.ts +15 -16
  29. package/dist/types/src/components/AttentionProvider.d.ts.map +1 -1
  30. package/dist/types/src/components/AttentionProvider.stories.d.ts +6 -4
  31. package/dist/types/src/components/AttentionProvider.stories.d.ts.map +1 -1
  32. package/dist/types/src/components/SelectionProvider.d.ts +6 -5
  33. package/dist/types/src/components/SelectionProvider.d.ts.map +1 -1
  34. package/dist/types/src/index.d.ts +1 -0
  35. package/dist/types/src/index.d.ts.map +1 -1
  36. package/dist/types/src/selection.d.ts +49 -17
  37. package/dist/types/src/selection.d.ts.map +1 -1
  38. package/dist/types/src/testing/decorators/withAttention.d.ts +5 -1
  39. package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -1
  40. package/dist/types/src/types.d.ts +9 -0
  41. package/dist/types/src/types.d.ts.map +1 -0
  42. package/dist/types/tsconfig.tsbuildinfo +1 -1
  43. package/package.json +38 -24
  44. package/src/attention.test.ts +188 -27
  45. package/src/attention.ts +175 -74
  46. package/src/components/AttentionGlyph.stories.tsx +17 -14
  47. package/src/components/AttentionGlyph.tsx +14 -7
  48. package/src/components/AttentionProvider.stories.tsx +12 -9
  49. package/src/components/AttentionProvider.tsx +80 -97
  50. package/src/components/SelectionProvider.tsx +66 -19
  51. package/src/index.ts +1 -0
  52. package/src/selection.ts +135 -65
  53. package/src/testing/decorators/withAttention.ts +21 -4
  54. package/src/types.ts +13 -0
  55. package/dist/lib/browser/chunk-QPEFFJ4E.mjs +0 -616
  56. package/dist/lib/browser/chunk-QPEFFJ4E.mjs.map +0 -7
  57. package/dist/lib/node-esm/chunk-F2JRKDYV.mjs +0 -618
  58. package/dist/lib/node-esm/chunk-F2JRKDYV.mjs.map +0 -7
package/package.json CHANGED
@@ -1,20 +1,32 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-attention",
3
- "version": "0.8.4-main.f9ba587",
3
+ "version": "0.8.4-main.fcfe5033a5",
4
4
  "description": "Signifier components and providers for attention system.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "DXOS.org",
9
- "sideEffects": true,
13
+ "sideEffects": false,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  ".": {
17
+ "source": "./src/index.ts",
13
18
  "types": "./dist/types/src/index.d.ts",
14
19
  "browser": "./dist/lib/browser/index.mjs",
15
20
  "node": "./dist/lib/node-esm/index.mjs"
16
21
  },
22
+ "./types": {
23
+ "source": "./src/types.ts",
24
+ "types": "./dist/types/src/types.d.ts",
25
+ "browser": "./dist/lib/browser/types.mjs",
26
+ "node": "./dist/lib/node-esm/types.mjs"
27
+ },
17
28
  "./testing": {
29
+ "source": "./src/testing/index.ts",
18
30
  "types": "./dist/types/src/testing/index.d.ts",
19
31
  "browser": "./dist/lib/browser/testing/index.mjs",
20
32
  "node": "./dist/lib/node-esm/testing/index.mjs"
@@ -23,6 +35,9 @@
23
35
  "types": "dist/types/src/index.d.ts",
24
36
  "typesVersions": {
25
37
  "*": {
38
+ "types": [
39
+ "dist/types/src/types.d.ts"
40
+ ],
26
41
  "testing": [
27
42
  "dist/types/src/testing/index.d.ts"
28
43
  ]
@@ -33,8 +48,7 @@
33
48
  "src"
34
49
  ],
35
50
  "dependencies": {
36
- "@preact-signals/safe-react": "^0.9.0",
37
- "@preact/signals-core": "^1.9.0",
51
+ "@effect-atom/atom-react": "^0.5.0",
38
52
  "@radix-ui/primitive": "1.1.1",
39
53
  "@radix-ui/react-compose-refs": "1.1.1",
40
54
  "@radix-ui/react-context": "1.1.1",
@@ -42,30 +56,30 @@
42
56
  "@radix-ui/react-primitive": "2.0.2",
43
57
  "@radix-ui/react-slot": "1.1.2",
44
58
  "@radix-ui/react-use-controllable-state": "1.1.0",
45
- "effect": "3.16.13",
46
- "@dxos/invariant": "0.8.4-main.f9ba587",
47
- "@dxos/echo-schema": "0.8.4-main.f9ba587",
48
- "@dxos/live-object": "0.8.4-main.f9ba587",
49
- "@dxos/log": "0.8.4-main.f9ba587",
50
- "@dxos/util": "0.8.4-main.f9ba587"
59
+ "effect": "3.20.0",
60
+ "@dxos/echo": "0.8.4-main.fcfe5033a5",
61
+ "@dxos/invariant": "0.8.4-main.fcfe5033a5",
62
+ "@dxos/react-hooks": "0.8.4-main.fcfe5033a5",
63
+ "@dxos/log": "0.8.4-main.fcfe5033a5",
64
+ "@dxos/util": "0.8.4-main.fcfe5033a5"
51
65
  },
52
66
  "devDependencies": {
53
- "@types/react": "~18.2.0",
54
- "@types/react-dom": "~18.2.0",
55
- "react": "~18.2.0",
56
- "react-dom": "~18.2.0",
57
- "vite": "5.4.7",
58
- "@dxos/react-ui": "0.8.4-main.f9ba587",
59
- "@dxos/react-ui-theme": "0.8.4-main.f9ba587",
60
- "@dxos/random": "0.8.4-main.f9ba587",
61
- "@dxos/storybook-utils": "0.8.4-main.f9ba587"
67
+ "@types/react": "~19.2.7",
68
+ "@types/react-dom": "~19.2.3",
69
+ "react": "~19.2.3",
70
+ "react-dom": "~19.2.3",
71
+ "vite": "^7.1.11",
72
+ "@dxos/random": "0.8.4-main.fcfe5033a5",
73
+ "@dxos/react-ui": "0.8.4-main.fcfe5033a5",
74
+ "@dxos/storybook-utils": "0.8.4-main.fcfe5033a5",
75
+ "@dxos/ui-theme": "0.8.4-main.fcfe5033a5"
62
76
  },
63
77
  "peerDependencies": {
64
- "effect": "^3.13.3",
65
- "react": "~18.2.0",
66
- "react-dom": "~18.2.0",
67
- "@dxos/react-ui": "0.8.4-main.f9ba587",
68
- "@dxos/react-ui-theme": "0.8.4-main.f9ba587"
78
+ "effect": "3.20.0",
79
+ "react": "~19.2.3",
80
+ "react-dom": "~19.2.3",
81
+ "@dxos/react-ui": "0.8.4-main.fcfe5033a5",
82
+ "@dxos/ui-theme": "0.8.4-main.fcfe5033a5"
69
83
  },
70
84
  "publishConfig": {
71
85
  "access": "public"
@@ -2,46 +2,207 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { describe, test, expect } from 'vitest';
5
+ import { Registry } from '@effect-atom/atom-react';
6
+ import { describe, test } from 'vitest';
6
7
 
7
- import { AttentionManager } from './attention';
8
+ import {
9
+ AttentionManager,
10
+ expandAttendableId,
11
+ getLinkedVariant,
12
+ getParentId,
13
+ getSegmentId,
14
+ isLinkedSegment,
15
+ linkedSegment,
16
+ } from './attention';
8
17
 
9
18
  describe('AttentionManager', () => {
10
- test('takes an initial attendable', () => {
11
- const attention = new AttentionManager(['a', 'b', 'c']);
12
- expect(attention.current).to.deep.equal(['a', 'b', 'c']);
13
- expect(attention.keys()).to.deep.equal([['a'], ['b', 'c'], ['c'], ['a', 'b', 'c']]);
14
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
15
- expect(attention.get(['b', 'c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
16
- expect(attention.get(['c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
17
- expect(attention.get(['a', 'b', 'c'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
18
- expect(attention.get(['b'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
19
+ test('takes an initial attended id', ({ expect }) => {
20
+ const registry = Registry.make();
21
+ const attention = new AttentionManager(registry, ['root/a/b']);
22
+
23
+ expect(attention.getCurrent()).to.deep.equal(['root/a/b']);
24
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
25
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
26
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
27
+ expect(attention.get('unrelated')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
19
28
  });
20
29
 
21
- test('current returns last updated key', () => {
22
- const attention = new AttentionManager();
23
- expect(attention.current).to.deep.equal([]);
30
+ test('current returns last updated ids', ({ expect }) => {
31
+ const registry = Registry.make();
32
+ const attention = new AttentionManager(registry);
33
+ expect(attention.getCurrent()).to.deep.equal([]);
24
34
 
25
- attention.update(['a', 'b', 'c']);
26
- expect(attention.current).to.deep.equal(['a', 'b', 'c']);
35
+ attention.update(['root/a/b/c']);
36
+ expect(attention.getCurrent()).to.deep.equal(['root/a/b/c']);
27
37
  });
28
38
 
29
- test('keys returns all stored attendables', () => {
30
- const attention = new AttentionManager();
39
+ test('keys returns all tracked qualified ids', ({ expect }) => {
40
+ const registry = Registry.make();
41
+ const attention = new AttentionManager(registry);
31
42
  expect(attention.keys()).to.deep.equal([]);
32
43
 
33
- attention.update(['a']);
34
- expect(attention.keys()).to.deep.equal([['a']]);
44
+ attention.update(['root/a']);
45
+ expect(attention.keys()).to.include('root');
46
+ expect(attention.keys()).to.include('root/a');
47
+ });
48
+
49
+ test('get returns attention state for a given qualified id', ({ expect }) => {
50
+ const registry = Registry.make();
51
+ const attention = new AttentionManager(registry);
52
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
35
53
 
36
- attention.update(['a', 'b']);
37
- expect(attention.keys()).to.deep.equal([['a'], ['b'], ['a', 'b']]);
54
+ attention.update(['root/a']);
55
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
56
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
38
57
  });
39
58
 
40
- test('get returns attention object for a given key', () => {
41
- const attention = new AttentionManager();
42
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
59
+ test('clears previous attention on update', ({ expect }) => {
60
+ const registry = Registry.make();
61
+ const attention = new AttentionManager(registry);
62
+
63
+ attention.update(['root/a/b']);
64
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
65
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
66
+
67
+ attention.update(['root/x/y']);
68
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
69
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
70
+ expect(attention.get('root/x/y')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
71
+ expect(attention.get('root/x')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
72
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
73
+ });
74
+
75
+ test('linked segment marks parent with isRelated and isAncestor', ({ expect }) => {
76
+ const registry = Registry.make();
77
+ const attention = new AttentionManager(registry);
78
+
79
+ attention.update(['root/space/obj/~settings']);
80
+ expect(attention.get('root/space/obj/~settings')).to.deep.equal({
81
+ hasAttention: true,
82
+ isAncestor: false,
83
+ isRelated: false,
84
+ });
85
+ expect(attention.get('root/space/obj')).to.deep.equal({
86
+ hasAttention: false,
87
+ isAncestor: true,
88
+ isRelated: true,
89
+ });
90
+ expect(attention.get('root/space')).to.deep.equal({
91
+ hasAttention: false,
92
+ isAncestor: true,
93
+ isRelated: false,
94
+ });
95
+ expect(attention.get('root')).to.deep.equal({
96
+ hasAttention: false,
97
+ isAncestor: true,
98
+ isRelated: false,
99
+ });
100
+ });
101
+
102
+ test('switching from linked segment to non-linked clears parent isRelated', ({ expect }) => {
103
+ const registry = Registry.make();
104
+ const attention = new AttentionManager(registry);
105
+
106
+ attention.update(['root/space/obj/~settings']);
107
+ expect(attention.get('root/space/obj')).to.deep.equal({
108
+ hasAttention: false,
109
+ isAncestor: true,
110
+ isRelated: true,
111
+ });
112
+
113
+ attention.update(['root/space/obj']);
114
+ expect(attention.get('root/space/obj')).to.deep.equal({
115
+ hasAttention: true,
116
+ isAncestor: false,
117
+ isRelated: false,
118
+ });
119
+ expect(attention.get('root/space/obj/~settings')).to.deep.equal({
120
+ hasAttention: false,
121
+ isAncestor: false,
122
+ isRelated: false,
123
+ });
124
+ });
125
+
126
+ test('keys sharing the same segment id are marked isRelated', ({ expect }) => {
127
+ const registry = Registry.make();
128
+ const attention = new AttentionManager(registry);
129
+
130
+ // Pre-register the alternate path so the manager knows about it.
131
+ attention.get('root/z/y/c');
132
+
133
+ attention.update(['root/a/b/c']);
134
+ expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
135
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
136
+ });
137
+
138
+ test('related keys are cleared when attention moves to a different segment id', ({ expect }) => {
139
+ const registry = Registry.make();
140
+ const attention = new AttentionManager(registry);
141
+
142
+ attention.get('root/z/y/c');
143
+ attention.update(['root/a/b/c']);
144
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
145
+
146
+ attention.update(['root/x/y/d']);
147
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
148
+ expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
149
+ });
150
+ });
151
+
152
+ describe('expandAttendableId', () => {
153
+ test('produces progressive prefixes', ({ expect }) => {
154
+ expect(expandAttendableId('root/space/types/doc')).to.deep.equal([
155
+ 'root',
156
+ 'root/space',
157
+ 'root/space/types',
158
+ 'root/space/types/doc',
159
+ ]);
160
+ });
161
+
162
+ test('single segment returns itself', ({ expect }) => {
163
+ expect(expandAttendableId('root')).to.deep.equal(['root']);
164
+ });
165
+ });
166
+
167
+ describe('isLinkedSegment', () => {
168
+ test('detects linked prefix on last segment', ({ expect }) => {
169
+ expect(isLinkedSegment('root/space/obj/~settings')).toBe(true);
170
+ expect(isLinkedSegment('root/space/obj')).toBe(false);
171
+ expect(isLinkedSegment('~standalone')).toBe(true);
172
+ });
173
+ });
174
+
175
+ describe('linkedSegment', () => {
176
+ test('builds a linked segment', ({ expect }) => {
177
+ expect(linkedSegment('settings')).toBe('~settings');
178
+ expect(linkedSegment('comments')).toBe('~comments');
179
+ });
180
+ });
181
+
182
+ describe('getLinkedVariant', () => {
183
+ test('extracts variant from linked last segment', ({ expect }) => {
184
+ expect(getLinkedVariant('root/space/obj/~settings')).toBe('settings');
185
+ expect(getLinkedVariant('root/a/~comments')).toBe('comments');
186
+ });
187
+
188
+ test('returns segment as-is when not linked', ({ expect }) => {
189
+ expect(getLinkedVariant('root/space/obj')).toBe('obj');
190
+ expect(getLinkedVariant('root')).toBe('root');
191
+ });
192
+ });
193
+
194
+ describe('getParentId', () => {
195
+ test('returns parent qualified id', ({ expect }) => {
196
+ expect(getParentId('root/space/obj/~settings')).toBe('root/space/obj');
197
+ expect(getParentId('root/space')).toBe('root');
198
+ expect(getParentId('root')).toBeUndefined();
199
+ });
200
+ });
43
201
 
44
- attention.update(['a']);
45
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
202
+ describe('getSegmentId', () => {
203
+ test('returns the last path segment', ({ expect }) => {
204
+ expect(getSegmentId('root/space/obj')).toBe('obj');
205
+ expect(getSegmentId('root')).toBe('root');
206
+ expect(getSegmentId('root/a/~settings')).toBe('~settings');
46
207
  });
47
208
  });
package/src/attention.ts CHANGED
@@ -2,119 +2,162 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { untracked } from '@preact/signals-core';
6
- import { Schema } from 'effect';
5
+ import { Atom, type Registry } from '@effect-atom/atom-react';
7
6
 
8
- import { live, type Live } from '@dxos/live-object';
9
- import { ComplexMap } from '@dxos/util';
10
-
11
- // NOTE: Chosen from RFC 1738’s `safe` characters: http://www.faqs.org/rfcs/rfc1738.html
12
- export const ATTENDABLE_PATH_SEPARATOR = '~';
13
-
14
- export const AttentionSchema = Schema.mutable(
15
- Schema.Struct({
16
- hasAttention: Schema.Boolean,
17
- isAncestor: Schema.Boolean,
18
- isRelated: Schema.Boolean,
19
- }),
20
- );
21
-
22
- export type Attention = Schema.Schema.Type<typeof AttentionSchema>;
23
-
24
- // TODO(wittjosiah): Use mosaic path utility?
25
- const stringKey = (key: string[]) => key.join(',');
7
+ import { type Attention } from './types';
26
8
 
27
9
  /**
28
10
  * Manages attention state for an application.
11
+ * Attention keys are slash-qualified graph IDs; ancestry is derived from progressive prefixes.
29
12
  */
30
- // TODO(wittjosiah): Write unit tests.
31
13
  export class AttentionManager {
32
- // Each attention path is associated with an attention object.
33
- // The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
34
- private readonly _map = new ComplexMap<string[], Live<Attention>>(stringKey);
35
- private readonly _state = live<{ current: string[] }>({ current: [] });
36
-
37
- constructor(initial: string[] = []) {
14
+ private readonly _map = new Map<string, Atom.Writable<Attention>>();
15
+ private readonly _currentAtom: Atom.Writable<string[]>;
16
+
17
+ constructor(
18
+ private readonly _registry: Registry.Registry,
19
+ initial: string[] = [],
20
+ ) {
21
+ this._currentAtom = Atom.make<string[]>([]).pipe(Atom.keepAlive);
38
22
  if (initial.length > 0) {
39
23
  this.update(initial);
40
24
  }
41
25
  }
42
26
 
43
27
  /**
44
- * Currently attended element.
45
- *
46
- * @reactive
28
+ * Atom for the currently attended element IDs.
29
+ */
30
+ get current(): Atom.Atom<string[]> {
31
+ return this._currentAtom;
32
+ }
33
+
34
+ /**
35
+ * Gets the currently attended element IDs.
36
+ */
37
+ getCurrent(): readonly string[] {
38
+ return this._registry.get(this._currentAtom);
39
+ }
40
+
41
+ /**
42
+ * Subscribe to changes in the current attention IDs.
47
43
  */
48
- get current(): Live<readonly string[]> {
49
- return this._state.current;
44
+ subscribeCurrent(cb: (current: readonly string[]) => void): () => void {
45
+ this._registry.get(this._currentAtom);
46
+ return this._registry.subscribe(this._currentAtom, () => {
47
+ cb(this._registry.get(this._currentAtom));
48
+ });
50
49
  }
51
50
 
52
51
  /**
53
- * All attention paths.
52
+ * All tracked qualified IDs.
54
53
  */
55
- keys(): string[][] {
54
+ keys(): string[] {
56
55
  return Array.from(this._map.keys());
57
56
  }
58
57
 
59
58
  /**
60
- * Get the attention state for a given path.
59
+ * Get the attention state for a given qualified ID.
61
60
  */
62
- get(key: string[]): Live<Attention> {
63
- const object = this._map.get(key);
64
- if (object) {
65
- return object;
61
+ get(id: string): Attention {
62
+ const atom = this._getAtom(id);
63
+ return this._registry.get(atom);
64
+ }
65
+
66
+ /**
67
+ * Subscribe to changes in the attention state for a given qualified ID.
68
+ */
69
+ subscribe(id: string, cb: (attention: Attention) => void): () => void {
70
+ const atom = this._getAtom(id);
71
+ this._registry.get(atom);
72
+ return this._registry.subscribe(atom, () => {
73
+ cb(this._registry.get(atom));
74
+ });
75
+ }
76
+
77
+ private _getAtom(id: string): Atom.Writable<Attention> {
78
+ const existing = this._map.get(id);
79
+ if (existing) {
80
+ return existing;
66
81
  }
67
82
 
68
- const newObject = live(AttentionSchema, { hasAttention: false, isAncestor: false, isRelated: false });
69
- this._map.set(key, newObject);
70
- return newObject;
83
+ const newAtom = Atom.make<Attention>({ hasAttention: false, isAncestor: false, isRelated: false }).pipe(
84
+ Atom.keepAlive,
85
+ );
86
+ this._map.set(id, newAtom);
87
+ return newAtom;
71
88
  }
72
89
 
73
90
  /**
74
91
  * Update the currently attended element.
92
+ * Takes the array of qualified IDs collected from the DOM; the first element is the primary attended item.
93
+ * Ancestry is derived from the progressive prefixes of the primary ID.
94
+ * Relatedness is derived from the segment ID: any tracked key whose last `/` segment matches the
95
+ * attended ID's segment ID is marked `isRelated`. Additionally, if the primary ID is a linked segment
96
+ * (starts with `~`), its immediate parent gets `isRelated` alongside `isAncestor`.
75
97
  *
76
98
  * @internal
77
99
  */
78
- update(nextKey: string[]): void {
79
- const currentKey = untracked(() => this.current);
80
- const currentRelatedTo = currentKey[0];
81
- const nextRelatedTo = nextKey[0];
100
+ update(nextIds: string[]): void {
101
+ const primaryId = nextIds[0];
102
+ if (!primaryId) {
103
+ return;
104
+ }
82
105
 
83
- // Ensure related attention object is initialized.
84
- this.get([nextRelatedTo]);
106
+ const currentIds = this.getCurrent();
107
+ const prevPrimaryId = currentIds[0];
108
+ // Clear previous attention state: ancestors, primary, and related keys.
109
+ if (prevPrimaryId) {
110
+ const prevPrefixes = expandAttendableId(prevPrimaryId);
111
+ for (const prefix of prevPrefixes) {
112
+ this._set(prefix, {});
113
+ }
114
+ const prevSegmentId = getSegmentId(prevPrimaryId);
115
+ for (const key of this.keys()) {
116
+ if (getSegmentId(key) === prevSegmentId) {
117
+ this._set(key, {});
118
+ }
119
+ }
120
+ }
85
121
 
86
- const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
87
- const nextRelatedKeys = this.keys().filter(
88
- (key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey),
89
- );
90
- const prevRelatedKeys = currentRelatedKeys.filter(
91
- (curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)),
92
- );
122
+ this._registry.set(this._currentAtom, nextIds);
93
123
 
94
- const currentAncestorKeys = currentKey.slice(1).map((_, i) => currentKey.slice(i + 1, currentKey.length));
95
- const nextAncestorKeys = nextKey.slice(1).map((_, i) => nextKey.slice(i + 1, nextKey.length));
96
- const prevAncestorKeys = currentAncestorKeys.filter(
97
- (curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)),
98
- );
124
+ // Set ancestors and primary.
125
+ const prefixes = expandAttendableId(primaryId);
126
+ const prefixSet = new Set(prefixes);
127
+ const linkedParent = isLinkedSegment(primaryId) ? getParentId(primaryId) : undefined;
128
+
129
+ for (const prefix of prefixes) {
130
+ if (prefix === primaryId) {
131
+ this._set(prefix, { hasAttention: true });
132
+ } else if (prefix === linkedParent) {
133
+ this._set(prefix, { isAncestor: true, isRelated: true });
134
+ } else {
135
+ this._set(prefix, { isAncestor: true });
136
+ }
137
+ }
99
138
 
100
- this._state.current = nextKey;
101
- prevRelatedKeys.forEach((key) => this._set(key, {}));
102
- prevAncestorKeys.forEach((key) => this._set(key, {}));
103
- nextRelatedKeys.forEach((key) => this._set(key, { isRelated: true }));
104
- nextAncestorKeys.forEach((key) => this._set(key, { isAncestor: true }));
105
- this._set(nextKey, { hasAttention: true });
139
+ // Set related keys: any tracked key sharing the same segment ID.
140
+ const segmentId = getSegmentId(primaryId);
141
+ for (const key of this.keys()) {
142
+ if (!prefixSet.has(key) && getSegmentId(key) === segmentId) {
143
+ this._set(key, { isRelated: true });
144
+ }
145
+ }
106
146
  }
107
147
 
108
- private _set(key: string[], attention: Partial<Attention>): void {
109
- const object = this.get(key);
110
- object.hasAttention = attention.hasAttention ?? false;
111
- object.isAncestor = attention.isAncestor ?? false;
112
- object.isRelated = attention.isRelated ?? false;
148
+ private _set(id: string, attention: Partial<Attention>): void {
149
+ const atom = this._getAtom(id);
150
+ this._registry.set(atom, {
151
+ hasAttention: attention.hasAttention ?? false,
152
+ isAncestor: attention.isAncestor ?? false,
153
+ isRelated: attention.isRelated ?? false,
154
+ });
113
155
  }
114
156
  }
115
157
 
116
158
  /**
117
- * Accumulates all attendable id’s between the element provided and the root, inclusive.
159
+ * Accumulates all attendable IDs between the element provided and the root, inclusive.
160
+ * Each `data-attendable-id` value is treated as a single qualified ID (no splitting).
118
161
  */
119
162
  export const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
120
163
  // Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
@@ -130,8 +173,7 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
130
173
  return getAttendables(selector, trigger, acc);
131
174
  }
132
175
  } else {
133
- acc.push(...attendableId.split(ATTENDABLE_PATH_SEPARATOR));
134
- // TODO: Attempt tail recursion.
176
+ acc.push(attendableId);
135
177
  return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
136
178
  }
137
179
  }
@@ -142,3 +184,62 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
142
184
  export type AttendableId = { attendableId?: string };
143
185
 
144
186
  export type Related = { related?: boolean };
187
+
188
+ /**
189
+ * Prefix for linked segments. A linked segment's attention state is shared with its parent:
190
+ * when a linked node has attention, the parent is marked as both ancestor and related.
191
+ */
192
+ const LINKED_PREFIX = '~';
193
+
194
+ /**
195
+ * Decompose a qualified graph ID into progressive prefixes for ancestry tracking.
196
+ * e.g. `root/a/b/c` yields `['root', 'root/a', 'root/a/b', 'root/a/b/c']`.
197
+ */
198
+ export const expandAttendableId = (qualifiedId: string): string[] => {
199
+ const segments = qualifiedId.split('/');
200
+ return segments.map((_, index) => segments.slice(0, index + 1).join('/'));
201
+ };
202
+
203
+ /**
204
+ * Whether the last segment of a qualified ID is a linked segment (starts with `~`).
205
+ * Linked segments share their attention state with the parent node.
206
+ */
207
+ export const isLinkedSegment = (qualifiedId: string): boolean => {
208
+ const lastSegment = qualifiedId.split('/').pop() ?? '';
209
+ return lastSegment.startsWith(LINKED_PREFIX);
210
+ };
211
+
212
+ /**
213
+ * Build a linked segment ID from a variant name (e.g., `'settings'` -> `'~settings'`).
214
+ * Nodes identified by linked segments share their attention state with the parent node.
215
+ */
216
+ export const linkedSegment = (variant: string): string => `${LINKED_PREFIX}${variant}`;
217
+
218
+ /**
219
+ * Extract the variant name from the last segment of a qualified ID, stripping the linked prefix.
220
+ * e.g. `'root/space/obj/~settings'` -> `'settings'`.
221
+ * If the segment is not linked (no `~` prefix), returns the segment as-is.
222
+ */
223
+ export const getLinkedVariant = (qualifiedId: string): string => {
224
+ const lastSegment = qualifiedId.split('/').pop() ?? '';
225
+ return lastSegment.startsWith(LINKED_PREFIX) ? lastSegment.slice(LINKED_PREFIX.length) : lastSegment;
226
+ };
227
+
228
+ /**
229
+ * Get the parent qualified ID (everything before the last `/` segment).
230
+ */
231
+ export const getParentId = (qualifiedId: string | undefined): string | undefined => {
232
+ if (!qualifiedId) {
233
+ return undefined;
234
+ }
235
+
236
+ const lastSlash = qualifiedId.lastIndexOf('/');
237
+ return lastSlash > 0 ? qualifiedId.slice(0, lastSlash) : undefined;
238
+ };
239
+
240
+ /**
241
+ * Extract the segment ID (last segment) of a qualified ID.
242
+ */
243
+ export const getSegmentId = (qualifiedId: string): string => {
244
+ return qualifiedId.split('/').pop() ?? qualifiedId;
245
+ };