@dxos/echo-atom 0.8.4-main.d05673bc65 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/echo-atom",
3
- "version": "0.8.4-main.d05673bc65",
3
+ "version": "0.8.4-main.fcfe5033a5",
4
4
  "description": "Effect Atom wrappers for ECHO objects with explicit subscriptions.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -29,18 +29,19 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@effect-atom/atom": "^0.5.1",
32
- "@dxos/echo": "0.8.4-main.d05673bc65",
33
- "@dxos/util": "0.8.4-main.d05673bc65",
34
- "@dxos/echo-db": "0.8.4-main.d05673bc65",
35
- "@dxos/invariant": "0.8.4-main.d05673bc65"
32
+ "@dxos/echo-db": "0.8.4-main.fcfe5033a5",
33
+ "@dxos/echo": "0.8.4-main.fcfe5033a5",
34
+ "@dxos/invariant": "0.8.4-main.fcfe5033a5",
35
+ "@dxos/util": "0.8.4-main.fcfe5033a5"
36
36
  },
37
37
  "devDependencies": {
38
- "effect": "3.19.16",
39
- "@dxos/random": "0.8.4-main.d05673bc65",
40
- "@dxos/test-utils": "0.8.4-main.d05673bc65"
38
+ "effect": "3.20.0",
39
+ "@dxos/context": "0.8.4-main.fcfe5033a5",
40
+ "@dxos/random": "0.8.4-main.fcfe5033a5",
41
+ "@dxos/test-utils": "0.8.4-main.fcfe5033a5"
41
42
  },
42
43
  "peerDependencies": {
43
- "effect": "3.19.16"
44
+ "effect": "3.20.0"
44
45
  },
45
46
  "publishConfig": {
46
47
  "access": "public"
package/src/atom.test.ts CHANGED
@@ -6,9 +6,9 @@ import * as Registry from '@effect-atom/atom/Registry';
6
6
  import { afterEach, beforeEach, describe, expect, test } from 'vitest';
7
7
 
8
8
  import { Obj, Ref } from '@dxos/echo';
9
- import { TestSchema } from '@dxos/echo/testing';
10
9
  import { createObject } from '@dxos/echo-db';
11
10
  import { EchoTestBuilder } from '@dxos/echo-db/testing';
11
+ import { TestSchema } from '@dxos/echo/testing';
12
12
 
13
13
  import * as AtomObj from './atom';
14
14
 
@@ -73,8 +73,8 @@ describe('Echo Atom - Basic Functionality', () => {
73
73
  );
74
74
 
75
75
  // Mutate object via Obj.change.
76
- Obj.change(obj, (o) => {
77
- o.name = 'Updated';
76
+ Obj.change(obj, (obj) => {
77
+ obj.name = 'Updated';
78
78
  });
79
79
 
80
80
  // Subscription should have fired: immediate + update.
@@ -105,8 +105,8 @@ describe('Echo Atom - Basic Functionality', () => {
105
105
  );
106
106
 
107
107
  // Update through Obj.change.
108
- Obj.change(obj, (o) => {
109
- o.title = (o.title ?? '') + ' Updated';
108
+ Obj.change(obj, (obj) => {
109
+ obj.title = (obj.title ?? '') + ' Updated';
110
110
  });
111
111
 
112
112
  // Subscription should have fired: immediate + update.
@@ -140,8 +140,8 @@ describe('Echo Atom - Basic Functionality', () => {
140
140
  expect(propertyUpdateCount).toBe(1);
141
141
 
142
142
  // Mutate the standalone object.
143
- Obj.change(obj, (o) => {
144
- o.name = 'Updated Standalone';
143
+ Obj.change(obj, (obj) => {
144
+ obj.name = 'Updated Standalone';
145
145
  });
146
146
 
147
147
  // Both atoms should have received updates.
@@ -244,8 +244,8 @@ describe('Echo Atom - Referential Equality', () => {
244
244
  expect(updateCount).toBe(1);
245
245
 
246
246
  // Mutate the object.
247
- Obj.change(obj, (o) => {
248
- o.name = 'Updated';
247
+ Obj.change(obj, (obj) => {
248
+ obj.name = 'Updated';
249
249
  });
250
250
 
251
251
  // The subscription should still work.
@@ -276,8 +276,8 @@ describe('Echo Atom - Referential Equality', () => {
276
276
  expect(updateCount).toBe(1);
277
277
 
278
278
  // Mutate the specific property.
279
- Obj.change(obj, (o) => {
280
- o.name = 'Updated';
279
+ Obj.change(obj, (obj) => {
280
+ obj.name = 'Updated';
281
281
  });
282
282
 
283
283
  // The subscription should still work.
package/src/atom.ts CHANGED
@@ -161,7 +161,6 @@ export function makeProperty<T extends Obj.Unknown, K extends keyof T>(
161
161
  }
162
162
 
163
163
  assertArgument(Obj.isObject(obj), 'obj', 'Object must be a reactive object');
164
- assertArgument(key in obj, 'key', 'Property must exist on object');
165
164
  return propertyFamily(obj)(key);
166
165
  }
167
166
 
@@ -6,8 +6,8 @@ import * as Registry from '@effect-atom/atom/Registry';
6
6
  import { describe, expect, test } from 'vitest';
7
7
 
8
8
  import { Obj } from '@dxos/echo';
9
- import { TestSchema } from '@dxos/echo/testing';
10
9
  import { createObject } from '@dxos/echo-db';
10
+ import { TestSchema } from '@dxos/echo/testing';
11
11
 
12
12
  import * as AtomObj from './atom';
13
13
 
@@ -34,10 +34,10 @@ describe('Echo Atom - Batch Updates', () => {
34
34
  expect(initialCount).toBe(1); // Verify immediate update fired.
35
35
 
36
36
  // Make multiple updates to the same object in a single Obj.change call.
37
- Obj.change(obj, (o) => {
38
- o.name = 'Updated1';
39
- o.email = 'updated@example.com';
40
- o.username = 'updated';
37
+ Obj.change(obj, (obj) => {
38
+ obj.name = 'Updated1';
39
+ obj.email = 'updated@example.com';
40
+ obj.username = 'updated';
41
41
  });
42
42
 
43
43
  // Should have fired once for initial + once for the Obj.change (not once per property update).
@@ -72,14 +72,14 @@ describe('Echo Atom - Batch Updates', () => {
72
72
  expect(initialCount).toBe(1);
73
73
 
74
74
  // Make multiple separate Obj.change calls.
75
- Obj.change(obj, (o) => {
76
- o.name = 'Updated1';
75
+ Obj.change(obj, (obj) => {
76
+ obj.name = 'Updated1';
77
77
  });
78
- Obj.change(obj, (o) => {
79
- o.email = 'updated@example.com';
78
+ Obj.change(obj, (obj) => {
79
+ obj.email = 'updated@example.com';
80
80
  });
81
- Obj.change(obj, (o) => {
82
- o.username = 'updated';
81
+ Obj.change(obj, (obj) => {
82
+ obj.username = 'updated';
83
83
  });
84
84
 
85
85
  // Should have fired once for initial + once per Obj.change call.
@@ -114,10 +114,10 @@ describe('Echo Atom - Batch Updates', () => {
114
114
  expect(initialCount).toBe(1);
115
115
 
116
116
  // Make multiple updates to the same property in a single Obj.change call.
117
- Obj.change(obj, (o) => {
118
- o.name = 'Updated1';
119
- o.name = 'Updated2';
120
- o.name = 'Updated3';
117
+ Obj.change(obj, (obj) => {
118
+ obj.name = 'Updated1';
119
+ obj.name = 'Updated2';
120
+ obj.name = 'Updated3';
121
121
  });
122
122
 
123
123
  // Should have fired once for initial + once for the Obj.change (not once per assignment).
@@ -6,11 +6,12 @@ import * as Registry from '@effect-atom/atom/Registry';
6
6
  import * as Schema from 'effect/Schema';
7
7
  import { afterEach, beforeEach, describe, expect, test } from 'vitest';
8
8
 
9
+ import { sleep } from '@dxos/async';
9
10
  import { Obj, type QueryResult, Type } from '@dxos/echo';
10
11
  import { Filter, Query } from '@dxos/echo';
11
- import { TestSchema } from '@dxos/echo/testing';
12
- import { type EchoDatabase } from '@dxos/echo-db';
12
+ import { type EchoDatabase, RuntimeSchemaRegistry } from '@dxos/echo-db';
13
13
  import { EchoTestBuilder } from '@dxos/echo-db/testing';
14
+ import { TestSchema } from '@dxos/echo/testing';
14
15
  import { SpaceId } from '@dxos/keys';
15
16
 
16
17
  import * as AtomQuery from './query-atom';
@@ -261,3 +262,127 @@ describe('AtomQuery with queues', () => {
261
262
  expect(results[0].name).toEqual('jane');
262
263
  });
263
264
  });
265
+
266
+ const SchemaA = Schema.Struct({
267
+ name: Schema.String,
268
+ }).pipe(
269
+ Type.object({
270
+ typename: 'com.example.type.a',
271
+ version: '0.1.0',
272
+ }),
273
+ );
274
+
275
+ const SchemaB = Schema.Struct({
276
+ value: Schema.Number,
277
+ }).pipe(
278
+ Type.object({
279
+ typename: 'com.example.type.b',
280
+ version: '0.1.0',
281
+ }),
282
+ );
283
+
284
+ describe('AtomQuery.fromQuery with schema registry', () => {
285
+ let schemaRegistry: RuntimeSchemaRegistry;
286
+ let registry: Registry.Registry;
287
+
288
+ beforeEach(() => {
289
+ schemaRegistry = new RuntimeSchemaRegistry([]);
290
+ registry = Registry.make();
291
+ });
292
+
293
+ test('creates atom with initial results from schema query', async ({ expect }) => {
294
+ await schemaRegistry.register([SchemaA]);
295
+
296
+ const queryResult = schemaRegistry.query();
297
+ const atom = AtomQuery.fromQuery(queryResult);
298
+ const results = registry.get(atom);
299
+
300
+ expect(results).toHaveLength(1);
301
+ expect(Type.getTypename(results[0])).toBe('com.example.type.a');
302
+ });
303
+
304
+ test('atom updates when new schemas are registered', async ({ expect }) => {
305
+ await schemaRegistry.register([SchemaA]);
306
+
307
+ const queryResult = schemaRegistry.query();
308
+ const atom = AtomQuery.fromQuery(queryResult);
309
+
310
+ // Get initial results and subscribe.
311
+ const initialResults = registry.get(atom);
312
+ expect(initialResults).toHaveLength(1);
313
+
314
+ let updateCount = 0;
315
+ let latestResults: Type.AnyEntity[] = [];
316
+ registry.subscribe(atom, () => {
317
+ updateCount++;
318
+ latestResults = registry.get(atom);
319
+ });
320
+
321
+ // Allow reactive query to start (deferred via queueMicrotask).
322
+ await sleep(10);
323
+
324
+ // Register a new schema.
325
+ await schemaRegistry.register([SchemaB]);
326
+
327
+ expect(updateCount).toBeGreaterThan(0);
328
+ expect(latestResults).toHaveLength(2);
329
+ expect(latestResults.map(Type.getTypename).sort()).toEqual(['com.example.type.a', 'com.example.type.b']);
330
+ });
331
+
332
+ test('atom works with empty initial results', ({ expect }) => {
333
+ const queryResult = schemaRegistry.query();
334
+ const atom = AtomQuery.fromQuery(queryResult);
335
+ const results = registry.get(atom);
336
+
337
+ expect(results).toHaveLength(0);
338
+ });
339
+
340
+ test('atom with filtered query only reflects matching schemas', async ({ expect }) => {
341
+ const queryResult = schemaRegistry.query({ typename: 'com.example.type.a' });
342
+ const atom = AtomQuery.fromQuery(queryResult);
343
+
344
+ // Get initial (empty) results and subscribe.
345
+ const initialResults = registry.get(atom);
346
+ expect(initialResults).toHaveLength(0);
347
+
348
+ let latestResults: Type.AnyEntity[] = [];
349
+ registry.subscribe(atom, () => {
350
+ latestResults = registry.get(atom);
351
+ });
352
+
353
+ await sleep(10);
354
+
355
+ // Register non-matching schema.
356
+ await schemaRegistry.register([SchemaB]);
357
+ // Results updated but still empty for this filter.
358
+ expect(latestResults).toHaveLength(0);
359
+
360
+ // Register matching schema.
361
+ await schemaRegistry.register([SchemaA]);
362
+ expect(latestResults).toHaveLength(1);
363
+ expect(Type.getTypename(latestResults[0])).toBe('com.example.type.a');
364
+ });
365
+
366
+ test('unsubscribing from atom stops updates', async ({ expect }) => {
367
+ const queryResult = schemaRegistry.query();
368
+ const atom = AtomQuery.fromQuery(queryResult);
369
+
370
+ registry.get(atom);
371
+
372
+ let updateCount = 0;
373
+ const unsubscribe = registry.subscribe(atom, () => {
374
+ updateCount++;
375
+ });
376
+
377
+ await sleep(10);
378
+
379
+ await schemaRegistry.register([SchemaA]);
380
+ const countAfterFirst = updateCount;
381
+ expect(countAfterFirst).toBeGreaterThan(0);
382
+
383
+ unsubscribe();
384
+
385
+ await schemaRegistry.register([SchemaB]);
386
+ expect(updateCount).toBe(countAfterFirst);
387
+ });
388
+ });
package/src/query-atom.ts CHANGED
@@ -8,7 +8,7 @@ import { DXN, Database, type Entity, type Filter, Query, type QueryResult } from
8
8
  import { WeakDictionary } from '@dxos/util';
9
9
 
10
10
  /**
11
- * Create a self-updating atom from an existing QueryResult.
11
+ * Create a self-updating atom from any QueryResult (e.g. schema registry queries).
12
12
  * Internally subscribes to queryResult and uses get.setSelf to update.
13
13
  * Cleanup is handled via get.addFinalizer.
14
14
  *
@@ -17,18 +17,13 @@ import { WeakDictionary } from '@dxos/util';
17
17
  * @param queryResult - The QueryResult to wrap.
18
18
  * @returns An atom that automatically updates when query results change.
19
19
  */
20
- export const fromQuery = <T extends Entity.Unknown>(queryResult: QueryResult.QueryResult<T>): Atom.Atom<T[]> =>
20
+ export const fromQuery = <T>(queryResult: QueryResult.QueryResult<T>): Atom.Atom<T[]> =>
21
21
  Atom.make((get) => {
22
- // TODO(wittjosiah): Consider subscribing to individual objects here as well, and grabbing their snapshots.
23
- // Subscribe to QueryResult changes.
24
22
  const unsubscribe = queryResult.subscribe(() => {
25
- get.setSelf(queryResult.results);
23
+ get.setSelf(queryResult.runSync());
26
24
  });
27
-
28
- // Register cleanup for when atom is no longer used.
29
25
  get.addFinalizer(unsubscribe);
30
-
31
- return queryResult.results;
26
+ return queryResult.runSync();
32
27
  });
33
28
 
34
29
  // Registry: key → Queryable (WeakRef with auto-cleanup when GC'd).
@@ -6,8 +6,8 @@ import * as Registry from '@effect-atom/atom/Registry';
6
6
  import { describe, expect, test } from 'vitest';
7
7
 
8
8
  import { Obj } from '@dxos/echo';
9
- import { TestSchema } from '@dxos/echo/testing';
10
9
  import { createObject } from '@dxos/echo-db';
10
+ import { TestSchema } from '@dxos/echo/testing';
11
11
  import { arrayMove } from '@dxos/util';
12
12
 
13
13
  import * as AtomObj from './atom';
@@ -29,8 +29,8 @@ describe('Echo Atom - Reactivity', () => {
29
29
  registry.subscribe(atom, () => {});
30
30
 
31
31
  // Update the object via Obj.change.
32
- Obj.change(obj, (o) => {
33
- o.name = 'Updated';
32
+ Obj.change(obj, (obj) => {
33
+ obj.name = 'Updated';
34
34
  });
35
35
 
36
36
  const updatedSnapshot = registry.get(atom);
@@ -51,8 +51,8 @@ describe('Echo Atom - Reactivity', () => {
51
51
  registry.subscribe(atom, () => {});
52
52
 
53
53
  // Update the property via Obj.change.
54
- Obj.change(obj, (o) => {
55
- o.name = 'Updated';
54
+ Obj.change(obj, (obj) => {
55
+ obj.name = 'Updated';
56
56
  });
57
57
 
58
58
  expect(registry.get(atom)).toBe('Updated');
@@ -83,8 +83,8 @@ describe('Echo Atom - Reactivity', () => {
83
83
  });
84
84
 
85
85
  // Update only email property via Obj.change.
86
- Obj.change(obj, (o) => {
87
- o.email = 'updated@example.com';
86
+ Obj.change(obj, (obj) => {
87
+ obj.email = 'updated@example.com';
88
88
  });
89
89
 
90
90
  // Name atom should NOT have changed.
@@ -110,9 +110,9 @@ describe('Echo Atom - Reactivity', () => {
110
110
  registry.subscribe(emailAtom, () => {});
111
111
 
112
112
  // Update multiple properties via Obj.change.
113
- Obj.change(obj, (o) => {
114
- o.name = 'Updated';
115
- o.email = 'updated@example.com';
113
+ Obj.change(obj, (obj) => {
114
+ obj.name = 'Updated';
115
+ obj.email = 'updated@example.com';
116
116
  });
117
117
 
118
118
  expect(registry.get(nameAtom)).toBe('Updated');
@@ -141,11 +141,11 @@ describe('Echo Atom - Reactivity', () => {
141
141
  expect(initialCount).toBe(1);
142
142
 
143
143
  // Update object via Obj.change.
144
- Obj.change(obj, (o) => {
145
- o.name = 'Updated';
144
+ Obj.change(obj, (obj) => {
145
+ obj.name = 'Updated';
146
146
  });
147
- Obj.change(obj, (o) => {
148
- o.email = 'updated@example.com';
147
+ Obj.change(obj, (obj) => {
148
+ obj.email = 'updated@example.com';
149
149
  });
150
150
 
151
151
  // Updates fire through Obj.subscribe (one per Obj.change call).
@@ -167,8 +167,8 @@ describe('Echo Atom - Reactivity', () => {
167
167
  });
168
168
 
169
169
  actions.push('before');
170
- Obj.change(obj, (o) => {
171
- o.name = 'Updated';
170
+ Obj.change(obj, (obj) => {
171
+ obj.name = 'Updated';
172
172
  });
173
173
  actions.push('after');
174
174
 
@@ -191,8 +191,8 @@ describe('Echo Atom - Reactivity', () => {
191
191
  });
192
192
 
193
193
  actions.push('before');
194
- Obj.change(obj, (o) => {
195
- o.stringArray!.splice(1, 1);
194
+ Obj.change(obj, (obj) => {
195
+ obj.stringArray!.splice(1, 1);
196
196
  });
197
197
  actions.push('after');
198
198
 
@@ -6,9 +6,9 @@ import * as Registry from '@effect-atom/atom/Registry';
6
6
  import { afterEach, beforeEach, describe, expect, test } from 'vitest';
7
7
 
8
8
  import { Obj, Ref } from '@dxos/echo';
9
- import { TestSchema } from '@dxos/echo/testing';
10
9
  import { type EchoDatabase } from '@dxos/echo-db';
11
10
  import { EchoTestBuilder } from '@dxos/echo-db/testing';
11
+ import { TestSchema } from '@dxos/echo/testing';
12
12
 
13
13
  import * as AtomRef from './ref-atom';
14
14
 
@@ -60,8 +60,8 @@ describe('AtomRef - Basic Functionality', () => {
60
60
  expect(updateCount).toBe(1);
61
61
 
62
62
  // Mutate target - ref atom does NOT react to this.
63
- Obj.change(targetObj, (o) => {
64
- o.name = 'Updated';
63
+ Obj.change(targetObj, (targetObj) => {
64
+ targetObj.name = 'Updated';
65
65
  });
66
66
 
67
67
  // Update count should still be 1 - ref atom doesn't subscribe to target changes.