@dxos/react-ui-attention 0.8.4-staging.60fe92afc8 → 0.9.1-main.c7dcc2e112

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 (76) hide show
  1. package/dist/lib/browser/chunk-4HYAJ4IO.mjs +434 -0
  2. package/dist/lib/browser/chunk-4HYAJ4IO.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-I4VO2RQM.mjs → chunk-KCQFVRZ6.mjs} +88 -112
  4. package/dist/lib/browser/chunk-KCQFVRZ6.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +29 -11
  6. package/dist/lib/browser/index.mjs.map +1 -1
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +4 -4
  9. package/dist/lib/browser/testing/index.mjs.map +2 -2
  10. package/dist/lib/browser/types/index.mjs +19 -5
  11. package/dist/lib/node-esm/{chunk-ZTUVSH4P.mjs → chunk-5JUZQDCR.mjs} +88 -112
  12. package/dist/lib/node-esm/chunk-5JUZQDCR.mjs.map +7 -0
  13. package/dist/lib/node-esm/chunk-HWN7KJDX.mjs +436 -0
  14. package/dist/lib/node-esm/chunk-HWN7KJDX.mjs.map +7 -0
  15. package/dist/lib/node-esm/index.mjs +29 -11
  16. package/dist/lib/node-esm/index.mjs.map +1 -1
  17. package/dist/lib/node-esm/meta.json +1 -1
  18. package/dist/lib/node-esm/testing/index.mjs +4 -4
  19. package/dist/lib/node-esm/testing/index.mjs.map +2 -2
  20. package/dist/lib/node-esm/types/index.mjs +19 -5
  21. package/dist/types/src/components/ViewStateProvider/ViewStateProvider.d.ts +29 -0
  22. package/dist/types/src/components/ViewStateProvider/ViewStateProvider.d.ts.map +1 -0
  23. package/dist/types/src/components/ViewStateProvider/ViewStateProvider.test.d.ts +2 -0
  24. package/dist/types/src/components/ViewStateProvider/ViewStateProvider.test.d.ts.map +1 -0
  25. package/dist/types/src/components/ViewStateProvider/index.d.ts +2 -0
  26. package/dist/types/src/components/ViewStateProvider/index.d.ts.map +1 -0
  27. package/dist/types/src/components/index.d.ts +1 -1
  28. package/dist/types/src/index.d.ts +1 -0
  29. package/dist/types/src/index.d.ts.map +1 -1
  30. package/dist/types/src/selection.d.ts +10 -40
  31. package/dist/types/src/selection.d.ts.map +1 -1
  32. package/dist/types/src/selection.test.d.ts +2 -0
  33. package/dist/types/src/selection.test.d.ts.map +1 -0
  34. package/dist/types/src/types/index.d.ts +1 -0
  35. package/dist/types/src/types/index.d.ts.map +1 -1
  36. package/dist/types/src/view-state/backends.d.ts +24 -0
  37. package/dist/types/src/view-state/backends.d.ts.map +1 -0
  38. package/dist/types/src/view-state/backends.test.d.ts +2 -0
  39. package/dist/types/src/view-state/backends.test.d.ts.map +1 -0
  40. package/dist/types/src/view-state/index.d.ts +3 -0
  41. package/dist/types/src/view-state/index.d.ts.map +1 -0
  42. package/dist/types/src/view-state/view-state.d.ts +54 -0
  43. package/dist/types/src/view-state/view-state.d.ts.map +1 -0
  44. package/dist/types/src/view-state/view-state.test.d.ts +2 -0
  45. package/dist/types/src/view-state/view-state.test.d.ts.map +1 -0
  46. package/dist/types/src/vitest-setup.d.ts +2 -0
  47. package/dist/types/src/vitest-setup.d.ts.map +1 -0
  48. package/dist/types/tsconfig.tsbuildinfo +1 -1
  49. package/package.json +15 -13
  50. package/src/components/ViewStateProvider/ViewStateProvider.test.tsx +45 -0
  51. package/src/components/ViewStateProvider/ViewStateProvider.tsx +123 -0
  52. package/src/components/ViewStateProvider/index.ts +5 -0
  53. package/src/components/index.ts +1 -1
  54. package/src/index.ts +1 -0
  55. package/src/selection.test.ts +67 -0
  56. package/src/selection.ts +39 -169
  57. package/src/testing/decorators/withAttention.ts +2 -2
  58. package/src/types/index.ts +1 -0
  59. package/src/view-state/backends.test.ts +104 -0
  60. package/src/view-state/backends.ts +145 -0
  61. package/src/view-state/index.ts +6 -0
  62. package/src/view-state/view-state.test.ts +54 -0
  63. package/src/view-state/view-state.ts +91 -0
  64. package/src/vitest-setup.ts +6 -0
  65. package/dist/lib/browser/chunk-D2BLVJ6Z.mjs +0 -421
  66. package/dist/lib/browser/chunk-D2BLVJ6Z.mjs.map +0 -7
  67. package/dist/lib/browser/chunk-I4VO2RQM.mjs.map +0 -7
  68. package/dist/lib/node-esm/chunk-6INLBC62.mjs +0 -423
  69. package/dist/lib/node-esm/chunk-6INLBC62.mjs.map +0 -7
  70. package/dist/lib/node-esm/chunk-ZTUVSH4P.mjs.map +0 -7
  71. package/dist/types/src/components/SelectionProvider/SelectionProvider.d.ts +0 -28
  72. package/dist/types/src/components/SelectionProvider/SelectionProvider.d.ts.map +0 -1
  73. package/dist/types/src/components/SelectionProvider/index.d.ts +0 -2
  74. package/dist/types/src/components/SelectionProvider/index.d.ts.map +0 -1
  75. package/src/components/SelectionProvider/SelectionProvider.tsx +0 -174
  76. package/src/components/SelectionProvider/index.ts +0 -5
@@ -0,0 +1,145 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { Atom, type Registry } from '@effect-atom/atom-react';
6
+ import * as Schema from 'effect/Schema';
7
+
8
+ import { type AspectDef, type BackendName, type ViewStateBackend } from './view-state';
9
+
10
+ // Only the stable `key` string is needed to form the map key; avoids variance issues with AspectDef<T>.
11
+ const cacheKey = (aspect: { key: string }, contextId: string) => `${aspect.key}:${contextId}`;
12
+
13
+ /** In-memory backend: state is ephemeral and scoped to the session (never persisted). */
14
+ export class MemoryBackend implements ViewStateBackend {
15
+ readonly #atoms = new Map<string, Atom.Writable<unknown>>();
16
+
17
+ atom<T>(aspect: AspectDef<T>, contextId: string): Atom.Writable<T> {
18
+ const key = cacheKey(aspect, contextId);
19
+ let atom = this.#atoms.get(key);
20
+ if (!atom) {
21
+ atom = Atom.make<unknown>(aspect.defaultValue());
22
+ this.#atoms.set(key, atom);
23
+ }
24
+ // Cast bridges the per-aspect value type erased by the shared atom map; safe by construction.
25
+ return atom as Atom.Writable<T>;
26
+ }
27
+
28
+ contexts<T>(aspect: AspectDef<T>): string[] {
29
+ const prefix = `${aspect.key}:`;
30
+ return [...this.#atoms.keys()].filter((key) => key.startsWith(prefix)).map((key) => key.slice(prefix.length));
31
+ }
32
+ }
33
+
34
+ const STORAGE_PREFIX = 'dxos:view-state:';
35
+
36
+ const storageKeyFor = (aspect: { key: string }, contextId: string) => `${STORAGE_PREFIX}${aspect.key}:${contextId}`;
37
+
38
+ // Accessing `globalThis.localStorage` can throw a SecurityError in sandboxed iframes or when
39
+ // storage is blocked; degrade to in-memory behaviour instead of crashing.
40
+ const safeLocalStorage = (): Storage | undefined => {
41
+ try {
42
+ return globalThis.localStorage;
43
+ } catch {
44
+ return undefined;
45
+ }
46
+ };
47
+
48
+ export interface LocalBackendOptions {
49
+ readonly registry: Registry.Registry;
50
+ /** Injectable for tests; defaults to `window.localStorage`. */
51
+ readonly storage?: Storage;
52
+ }
53
+
54
+ /** localStorage-backed backend: seeds atoms from storage, persists on set, syncs across tabs. */
55
+ export class LocalBackend implements ViewStateBackend {
56
+ readonly #registry: Registry.Registry;
57
+ // Absent in non-browser contexts (SSR/tests without injection); the backend then degrades to
58
+ // ephemeral, in-memory behaviour rather than crashing on a missing `localStorage`.
59
+ readonly #storage: Storage | undefined;
60
+ readonly #atoms = new Map<string, Atom.Writable<unknown>>();
61
+ // Reverse map: storage key -> (aspect, contextId) so `storage` events can target the right atom.
62
+ readonly #byStorageKey = new Map<string, { aspect: AspectDef<unknown>; contextId: string }>();
63
+ #storageListener?: (event: StorageEvent) => void;
64
+
65
+ constructor({ registry, storage }: LocalBackendOptions) {
66
+ this.#registry = registry;
67
+ this.#storage = storage ?? safeLocalStorage();
68
+ if (this.#storage && typeof globalThis.addEventListener === 'function') {
69
+ this.#storageListener = (event) => {
70
+ if (!event.key || !event.key.startsWith(STORAGE_PREFIX)) {
71
+ return;
72
+ }
73
+ const entry = this.#byStorageKey.get(event.key);
74
+ const atom = entry && this.#atoms.get(cacheKey(entry.aspect, entry.contextId));
75
+ if (entry && atom) {
76
+ this.#registry.set(atom, this.#read(entry.aspect, event.key));
77
+ }
78
+ };
79
+ globalThis.addEventListener('storage', this.#storageListener);
80
+ }
81
+ }
82
+
83
+ atom<T>(aspect: AspectDef<T>, contextId: string): Atom.Writable<T> {
84
+ const key = cacheKey(aspect, contextId);
85
+ let atom = this.#atoms.get(key);
86
+ if (!atom) {
87
+ const storageKey = storageKeyFor(aspect, contextId);
88
+ atom = Atom.make<unknown>(this.#read(aspect, storageKey));
89
+ this.#atoms.set(key, atom);
90
+ // Cast erases the per-aspect value type so the reverse map can hold aspects of any `T`; the
91
+ // stored aspect is only used to re-read/decode its own value, so the erasure is safe.
92
+ this.#byStorageKey.set(storageKey, { aspect: aspect as AspectDef<unknown>, contextId });
93
+ }
94
+ // Cast bridges the per-aspect value type erased by the shared atom map; safe by construction.
95
+ return atom as Atom.Writable<T>;
96
+ }
97
+
98
+ persist<T>(aspect: AspectDef<T>, contextId: string, value: T): void {
99
+ this.#storage?.setItem(storageKeyFor(aspect, contextId), JSON.stringify(Schema.encodeSync(aspect.schema)(value)));
100
+ }
101
+
102
+ contexts<T>(aspect: AspectDef<T>): string[] {
103
+ if (!this.#storage) {
104
+ // No persistent storage: fall back to the in-memory atoms (mirrors `atom()`'s ephemeral path).
105
+ const prefix = `${aspect.key}:`;
106
+ return [...this.#atoms.keys()].filter((key) => key.startsWith(prefix)).map((key) => key.slice(prefix.length));
107
+ }
108
+ const prefix = storageKeyFor(aspect, '');
109
+ const ids: string[] = [];
110
+ for (let index = 0; index < this.#storage.length; index++) {
111
+ const key = this.#storage.key(index);
112
+ if (key?.startsWith(prefix)) {
113
+ ids.push(key.slice(prefix.length));
114
+ }
115
+ }
116
+ return ids;
117
+ }
118
+
119
+ dispose(): void {
120
+ if (this.#storageListener && typeof globalThis.removeEventListener === 'function') {
121
+ globalThis.removeEventListener('storage', this.#storageListener);
122
+ }
123
+ // Allow safe reuse after disposal: drop cached atoms and their reverse-key entries.
124
+ this.#atoms.clear();
125
+ this.#byStorageKey.clear();
126
+ }
127
+
128
+ #read<T>(aspect: AspectDef<T>, storageKey: string): T {
129
+ const raw = this.#storage?.getItem(storageKey);
130
+ if (raw == null) {
131
+ return aspect.defaultValue();
132
+ }
133
+ try {
134
+ return Schema.decodeUnknownSync(aspect.schema)(JSON.parse(raw));
135
+ } catch {
136
+ // Tolerate stale/corrupt entries (e.g. a prior schema shape) by falling back to the default.
137
+ return aspect.defaultValue();
138
+ }
139
+ }
140
+ }
141
+
142
+ export const createDefaultBackends = (registry: Registry.Registry): Record<BackendName, ViewStateBackend> => ({
143
+ memory: new MemoryBackend(),
144
+ local: new LocalBackend({ registry }),
145
+ });
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ export * from './view-state';
6
+ export * from './backends';
@@ -0,0 +1,54 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { Registry } from '@effect-atom/atom-react';
6
+ import * as Schema from 'effect/Schema';
7
+ import { describe, test } from 'vitest';
8
+
9
+ import { createDefaultBackends } from './backends';
10
+ import { ViewStateManager, defineViewState } from './view-state';
11
+
12
+ const Counter = defineViewState({
13
+ key: 'counter',
14
+ backend: 'memory',
15
+ schema: Schema.Struct({ value: Schema.Number }).pipe(Schema.mutable),
16
+ defaultValue: () => ({ value: 0 }),
17
+ });
18
+
19
+ describe('ViewStateManager', () => {
20
+ const make = () => {
21
+ const registry = Registry.make();
22
+ return new ViewStateManager({ registry, backends: createDefaultBackends(registry) });
23
+ };
24
+
25
+ test('returns the aspect default for an unwritten context', ({ expect }) => {
26
+ const manager = make();
27
+ expect(manager.get(Counter, 'a')).toEqual({ value: 0 });
28
+ });
29
+
30
+ test('set then get round-trips per context', ({ expect }) => {
31
+ const manager = make();
32
+ manager.set(Counter, 'a', { value: 5 });
33
+ expect(manager.get(Counter, 'a')).toEqual({ value: 5 });
34
+ expect(manager.get(Counter, 'b')).toEqual({ value: 0 });
35
+ });
36
+
37
+ test('subscribe fires on change for that context', ({ expect }) => {
38
+ const manager = make();
39
+ let calls = 0;
40
+ const dispose = manager.subscribe(Counter, 'a', () => {
41
+ calls++;
42
+ });
43
+ manager.set(Counter, 'a', { value: 1 });
44
+ expect(calls).toBeGreaterThan(0);
45
+ dispose();
46
+ });
47
+
48
+ test('contexts enumerates touched contexts for an aspect', ({ expect }) => {
49
+ const manager = make();
50
+ manager.set(Counter, 'a', { value: 1 });
51
+ manager.set(Counter, 'b', { value: 2 });
52
+ expect(new Set(manager.contexts(Counter))).toEqual(new Set(['a', 'b']));
53
+ });
54
+ });
@@ -0,0 +1,91 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { type Atom, type Registry } from '@effect-atom/atom-react';
6
+ import type * as Schema from 'effect/Schema';
7
+
8
+ /**
9
+ * Persistence backend identifier. `personal` (ECHO/personal-space) is reserved for a future backend.
10
+ */
11
+ export type BackendName = 'memory' | 'local';
12
+
13
+ /**
14
+ * Declares a kind of per-context UI state. The value type `T` is inferred from the schema.
15
+ */
16
+ export interface AspectDef<T> {
17
+ readonly key: string;
18
+ readonly backend: BackendName;
19
+ // Encoded type is intentionally unconstrained: persisted backends serialize `T` through the
20
+ // schema to an arbitrary wire form, and only the decoded `T` matters to consumers.
21
+ readonly schema: Schema.Schema<T, any>;
22
+ readonly defaultValue: () => T;
23
+ }
24
+
25
+ /**
26
+ * Identity helper that pins the value type from the schema while keeping the literal `key`/`backend`.
27
+ */
28
+ export const defineViewState = <T>(def: AspectDef<T>): AspectDef<T> => def;
29
+
30
+ /**
31
+ * A backend produces a reactive, writable atom for each `(aspect, contextId)` pair. Backends may
32
+ * hydrate asynchronously (an ECHO backend would), yielding `aspect.defaultValue()` until loaded;
33
+ * the memory and local backends resolve synchronously.
34
+ */
35
+ export interface ViewStateBackend {
36
+ /** Stable atom for the pair; created (and seeded) on first access, cached thereafter. */
37
+ atom: <T>(aspect: AspectDef<T>, contextId: string) => Atom.Writable<T>;
38
+ /** Persist a value after the atom is updated. No-op for in-memory backends. */
39
+ persist?: <T>(aspect: AspectDef<T>, contextId: string, value: T) => void;
40
+ /** Context ids that currently hold a value for the aspect. */
41
+ contexts: <T>(aspect: AspectDef<T>) => string[];
42
+ /** Release listeners/timers (used by tests; app-lifetime managers do not call this). */
43
+ dispose?: () => void;
44
+ }
45
+
46
+ export interface ViewStateManagerOptions {
47
+ readonly registry: Registry.Registry;
48
+ readonly backends: Record<BackendName, ViewStateBackend>;
49
+ }
50
+
51
+ /**
52
+ * Routes per-context UI state to the backend declared by each aspect. Reads/writes go through the
53
+ * effect-atom registry so React hooks and graph atoms observe changes uniformly.
54
+ */
55
+ export class ViewStateManager {
56
+ readonly #registry: Registry.Registry;
57
+ readonly #backends: Record<BackendName, ViewStateBackend>;
58
+
59
+ constructor({ registry, backends }: ViewStateManagerOptions) {
60
+ this.#registry = registry;
61
+ this.#backends = backends;
62
+ }
63
+
64
+ /** Reactive atom for `(aspect, contextId)`; pass to `registry.get` inside derived atoms/hooks. */
65
+ atom<T>(aspect: AspectDef<T>, contextId: string): Atom.Writable<T> {
66
+ return this.#backends[aspect.backend].atom(aspect, contextId);
67
+ }
68
+
69
+ get<T>(aspect: AspectDef<T>, contextId: string): T {
70
+ return this.#registry.get(this.atom(aspect, contextId));
71
+ }
72
+
73
+ set<T>(aspect: AspectDef<T>, contextId: string, value: T): void {
74
+ const backend = this.#backends[aspect.backend];
75
+ this.#registry.set(backend.atom(aspect, contextId), value);
76
+ backend.persist?.(aspect, contextId, value);
77
+ }
78
+
79
+ update<T>(aspect: AspectDef<T>, contextId: string, fn: (prev: T) => T): void {
80
+ this.set(aspect, contextId, fn(this.get(aspect, contextId)));
81
+ }
82
+
83
+ subscribe<T>(aspect: AspectDef<T>, contextId: string, cb: (value: T) => void): () => void {
84
+ const atom = this.atom(aspect, contextId);
85
+ return this.#registry.subscribe(atom, () => cb(this.#registry.get(atom)));
86
+ }
87
+
88
+ contexts<T>(aspect: AspectDef<T>): string[] {
89
+ return this.#backends[aspect.backend].contexts(aspect);
90
+ }
91
+ }
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ // https://github.com/testing-library/jest-dom#with-vitest
6
+ import '@testing-library/jest-dom/vitest';