@dxos/echo-react 0.0.0 → 0.8.4-main.03d5cd7b56
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/dist/lib/neutral/index.mjs +154 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/useObject.d.ts +104 -0
- package/dist/types/src/useObject.d.ts.map +1 -0
- package/dist/types/src/useObject.test.d.ts +2 -0
- package/dist/types/src/useObject.test.d.ts.map +1 -0
- package/dist/types/src/useQuery.__test.d.ts +1 -0
- package/dist/types/src/useQuery.__test.d.ts.map +1 -0
- package/dist/types/src/useQuery.d.ts +13 -0
- package/dist/types/src/useQuery.d.ts.map +1 -0
- package/dist/types/src/useSchema.d.ts +6 -0
- package/dist/types/src/useSchema.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +11 -11
- package/src/useObject.test.tsx +52 -22
- package/src/useObject.ts +84 -95
- package/src/useQuery.__test.tsx +7 -6
- package/src/useQuery.ts +2 -0
- package/src/useSchema.ts +1 -1
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/echo-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.4-main.03d5cd7b56",
|
|
4
4
|
"description": "React integration for ECHO.",
|
|
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
13
|
"sideEffects": false,
|
|
@@ -12,31 +16,27 @@
|
|
|
12
16
|
".": {
|
|
13
17
|
"source": "./src/index.ts",
|
|
14
18
|
"types": "./dist/types/src/index.d.ts",
|
|
15
|
-
"
|
|
16
|
-
"node": "./dist/lib/node-esm/index.mjs"
|
|
19
|
+
"default": "./dist/lib/neutral/index.mjs"
|
|
17
20
|
}
|
|
18
21
|
},
|
|
19
22
|
"types": "dist/types/src/index.d.ts",
|
|
20
|
-
"typesVersions": {
|
|
21
|
-
"*": {}
|
|
22
|
-
},
|
|
23
23
|
"files": [
|
|
24
24
|
"dist",
|
|
25
25
|
"src"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@effect-atom/atom
|
|
29
|
-
"@
|
|
30
|
-
"@dxos/echo
|
|
28
|
+
"@effect-atom/atom": "^0.5.1",
|
|
29
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
30
|
+
"@dxos/echo": "0.8.4-main.03d5cd7b56",
|
|
31
|
+
"@dxos/echo-atom": "0.8.4-main.03d5cd7b56"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
33
|
-
"@effect-atom/atom": "^0.4.10",
|
|
34
34
|
"@testing-library/react": "^16.3.0",
|
|
35
35
|
"@types/react": "~19.2.7",
|
|
36
36
|
"@types/react-dom": "~19.2.3",
|
|
37
37
|
"react": "~19.2.3",
|
|
38
38
|
"react-dom": "~19.2.3",
|
|
39
|
-
"@dxos/echo-db": "0.8.
|
|
39
|
+
"@dxos/echo-db": "0.8.4-main.03d5cd7b56"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"react": "~19.2.3"
|
package/src/useObject.test.tsx
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import * as Registry from '@effect-atom/atom/Registry';
|
|
6
5
|
import { RegistryContext } from '@effect-atom/atom-react';
|
|
6
|
+
import * as Registry from '@effect-atom/atom/Registry';
|
|
7
7
|
import { renderHook, waitFor } from '@testing-library/react';
|
|
8
8
|
import React, { type PropsWithChildren } from 'react';
|
|
9
9
|
import { describe, expect, test } from 'vitest';
|
|
10
10
|
|
|
11
|
-
import { Obj } from '@dxos/echo';
|
|
12
|
-
import { TestSchema } from '@dxos/echo/testing';
|
|
11
|
+
import { Obj, Ref } from '@dxos/echo';
|
|
13
12
|
import { createObject } from '@dxos/echo-db';
|
|
13
|
+
import { TestSchema } from '@dxos/echo/testing';
|
|
14
14
|
|
|
15
15
|
import { useObject } from './useObject';
|
|
16
16
|
|
|
@@ -46,7 +46,6 @@ describe('useObject', () => {
|
|
|
46
46
|
const wrapper = createWrapper(registry);
|
|
47
47
|
|
|
48
48
|
const { result } = renderHook(() => useObject(obj, 'name'), { wrapper });
|
|
49
|
-
|
|
50
49
|
const [value] = result.current;
|
|
51
50
|
expect(value).toBe('Test');
|
|
52
51
|
});
|
|
@@ -59,12 +58,11 @@ describe('useObject', () => {
|
|
|
59
58
|
const wrapper = createWrapper(registry);
|
|
60
59
|
|
|
61
60
|
const { result } = renderHook(() => useObject(obj, 'name'), { wrapper });
|
|
62
|
-
|
|
63
61
|
expect(result.current[0]).toBe('Test');
|
|
64
62
|
|
|
65
|
-
// Update the property via Obj.
|
|
66
|
-
Obj.
|
|
67
|
-
|
|
63
|
+
// Update the property via Obj.update
|
|
64
|
+
Obj.update(obj, (obj) => {
|
|
65
|
+
obj.name = 'Updated';
|
|
68
66
|
});
|
|
69
67
|
|
|
70
68
|
// Wait for reactivity to update
|
|
@@ -81,12 +79,11 @@ describe('useObject', () => {
|
|
|
81
79
|
const wrapper = createWrapper(registry);
|
|
82
80
|
|
|
83
81
|
const { result } = renderHook(() => useObject(obj), { wrapper });
|
|
84
|
-
|
|
85
82
|
expect(result.current[0].name).toBe('Test');
|
|
86
83
|
|
|
87
|
-
// Update a property via Obj.
|
|
88
|
-
Obj.
|
|
89
|
-
|
|
84
|
+
// Update a property via Obj.update
|
|
85
|
+
Obj.update(obj, (obj) => {
|
|
86
|
+
obj.name = 'Updated';
|
|
90
87
|
});
|
|
91
88
|
|
|
92
89
|
// Wait for reactivity to update
|
|
@@ -103,12 +100,11 @@ describe('useObject', () => {
|
|
|
103
100
|
const wrapper = createWrapper(registry);
|
|
104
101
|
|
|
105
102
|
const { result } = renderHook(() => useObject(obj, 'name'), { wrapper });
|
|
106
|
-
|
|
107
103
|
expect(result.current[0]).toBe('Test');
|
|
108
104
|
|
|
109
|
-
// Update a different property via Obj.
|
|
110
|
-
Obj.
|
|
111
|
-
|
|
105
|
+
// Update a different property via Obj.update
|
|
106
|
+
Obj.update(obj, (obj) => {
|
|
107
|
+
obj.email = 'newemail@example.com';
|
|
112
108
|
});
|
|
113
109
|
|
|
114
110
|
// Wait a bit to ensure no update happens
|
|
@@ -142,7 +138,6 @@ describe('useObject', () => {
|
|
|
142
138
|
const wrapper = createWrapper(registry);
|
|
143
139
|
|
|
144
140
|
const { result } = renderHook(() => useObject(obj), { wrapper });
|
|
145
|
-
|
|
146
141
|
const [value, updateCallback] = result.current;
|
|
147
142
|
expect(value.name).toBe('Test');
|
|
148
143
|
|
|
@@ -165,7 +160,6 @@ describe('useObject', () => {
|
|
|
165
160
|
const wrapper = createWrapper(registry);
|
|
166
161
|
|
|
167
162
|
const { result } = renderHook(() => useObject(obj, 'name'), { wrapper });
|
|
168
|
-
|
|
169
163
|
const [value, updateCallback] = result.current;
|
|
170
164
|
expect(value).toBe('Test');
|
|
171
165
|
|
|
@@ -186,7 +180,6 @@ describe('useObject', () => {
|
|
|
186
180
|
const wrapper = createWrapper(registry);
|
|
187
181
|
|
|
188
182
|
const { result } = renderHook(() => useObject(obj, 'name'), { wrapper });
|
|
189
|
-
|
|
190
183
|
const [value, updateCallback] = result.current;
|
|
191
184
|
expect(value).toBe('Test');
|
|
192
185
|
|
|
@@ -207,7 +200,6 @@ describe('useObject', () => {
|
|
|
207
200
|
const wrapper = createWrapper(registry);
|
|
208
201
|
|
|
209
202
|
const { result, rerender } = renderHook(() => useObject(obj, 'name'), { wrapper });
|
|
210
|
-
|
|
211
203
|
const [, firstUpdateCallback] = result.current;
|
|
212
204
|
|
|
213
205
|
rerender();
|
|
@@ -233,7 +225,6 @@ describe('useObject', () => {
|
|
|
233
225
|
const wrapper = createWrapper(registry);
|
|
234
226
|
|
|
235
227
|
const { result } = renderHook(() => useObject(undefined as TestSchema.Person | undefined, 'name'), { wrapper });
|
|
236
|
-
|
|
237
228
|
const [value] = result.current;
|
|
238
229
|
expect(value).toBeUndefined();
|
|
239
230
|
});
|
|
@@ -243,7 +234,6 @@ describe('useObject', () => {
|
|
|
243
234
|
const wrapper = createWrapper(registry);
|
|
244
235
|
|
|
245
236
|
const { result } = renderHook(() => useObject(undefined as TestSchema.Person | undefined), { wrapper });
|
|
246
|
-
|
|
247
237
|
const [, updateCallback] = result.current;
|
|
248
238
|
|
|
249
239
|
// Should not throw when calling update on undefined object.
|
|
@@ -254,6 +244,46 @@ describe('useObject', () => {
|
|
|
254
244
|
}).not.toThrow();
|
|
255
245
|
});
|
|
256
246
|
|
|
247
|
+
test('snapshot is referentially stable across re-renders for object', ({ expect }) => {
|
|
248
|
+
const obj: TestSchema.Person = createObject(
|
|
249
|
+
Obj.make(TestSchema.Person, { name: 'Test', username: 'test', email: 'test@example.com' }),
|
|
250
|
+
);
|
|
251
|
+
const registry = Registry.make();
|
|
252
|
+
const wrapper = createWrapper(registry);
|
|
253
|
+
|
|
254
|
+
const { result, rerender } = renderHook(() => useObject(obj), { wrapper });
|
|
255
|
+
const [firstSnapshot] = result.current;
|
|
256
|
+
expect(firstSnapshot).toBeDefined();
|
|
257
|
+
|
|
258
|
+
rerender();
|
|
259
|
+
|
|
260
|
+
const [secondSnapshot] = result.current;
|
|
261
|
+
expect(secondSnapshot).toBe(firstSnapshot);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test('snapshot is referentially stable across re-renders for ref', ({ expect }) => {
|
|
265
|
+
const org: TestSchema.Organization = createObject(Obj.make(TestSchema.Organization, { name: 'DXOS' }));
|
|
266
|
+
const person: TestSchema.Person = createObject(
|
|
267
|
+
Obj.make(TestSchema.Person, {
|
|
268
|
+
name: 'Test',
|
|
269
|
+
username: 'test',
|
|
270
|
+
email: 'test@example.com',
|
|
271
|
+
employer: Ref.make(org),
|
|
272
|
+
}),
|
|
273
|
+
);
|
|
274
|
+
const registry = Registry.make();
|
|
275
|
+
const wrapper = createWrapper(registry);
|
|
276
|
+
|
|
277
|
+
const { result, rerender } = renderHook(() => useObject(person.employer!), { wrapper });
|
|
278
|
+
const [firstSnapshot] = result.current;
|
|
279
|
+
expect(firstSnapshot).toBeDefined();
|
|
280
|
+
|
|
281
|
+
rerender();
|
|
282
|
+
|
|
283
|
+
const [secondSnapshot] = result.current;
|
|
284
|
+
expect(secondSnapshot).toBe(firstSnapshot);
|
|
285
|
+
});
|
|
286
|
+
|
|
257
287
|
test('transitions from undefined to defined object', async () => {
|
|
258
288
|
const registry = Registry.make();
|
|
259
289
|
const wrapper = createWrapper(registry);
|
package/src/useObject.ts
CHANGED
|
@@ -3,17 +3,20 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { useAtomValue } from '@effect-atom/atom-react';
|
|
6
|
-
import
|
|
6
|
+
import * as Atom from '@effect-atom/atom/Atom';
|
|
7
|
+
import { useCallback, useMemo } from 'react';
|
|
7
8
|
|
|
8
|
-
import {
|
|
9
|
+
import { Obj, Ref } from '@dxos/echo';
|
|
9
10
|
import { AtomObj } from '@dxos/echo-atom';
|
|
10
11
|
|
|
11
12
|
export interface ObjectUpdateCallback<T> {
|
|
12
|
-
(update: (obj: T) => void): void;
|
|
13
|
-
(update: (obj: T) => T): void;
|
|
13
|
+
(update: (obj: Obj.Mutable<T>) => void): void;
|
|
14
|
+
(update: (obj: Obj.Mutable<T>) => Obj.Mutable<T>): void;
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
export interface ObjectPropUpdateCallback<T>
|
|
17
|
+
export interface ObjectPropUpdateCallback<T> {
|
|
18
|
+
(update: (value: Obj.Mutable<T>) => void): void;
|
|
19
|
+
(update: (value: Obj.Mutable<T>) => Obj.Mutable<T>): void;
|
|
17
20
|
(newValue: T): void;
|
|
18
21
|
}
|
|
19
22
|
|
|
@@ -21,63 +24,94 @@ export const useObject: {
|
|
|
21
24
|
/**
|
|
22
25
|
* Hook to subscribe to a Ref's target object.
|
|
23
26
|
* Automatically dereferences the ref and handles async loading.
|
|
24
|
-
* Returns undefined if the ref hasn't loaded yet.
|
|
27
|
+
* Returns a snapshot (undefined if the ref hasn't loaded yet).
|
|
25
28
|
*
|
|
26
29
|
* @param ref - The Ref to dereference and subscribe to
|
|
27
|
-
* @returns The current target
|
|
30
|
+
* @returns The current target snapshot (or undefined if not loaded) and update callback
|
|
28
31
|
*/
|
|
29
|
-
<T>(ref: Ref.Ref<T>): [
|
|
32
|
+
<T extends Obj.Unknown>(ref: Ref.Ref<T>): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];
|
|
30
33
|
|
|
31
34
|
/**
|
|
32
35
|
* Hook to subscribe to a Ref's target object that may be undefined.
|
|
33
|
-
* Returns undefined if the ref is undefined or hasn't loaded yet.
|
|
36
|
+
* Returns a snapshot (undefined if the ref is undefined or hasn't loaded yet).
|
|
34
37
|
*
|
|
35
38
|
* @param ref - The Ref to dereference and subscribe to (can be undefined)
|
|
36
|
-
* @returns The current target
|
|
39
|
+
* @returns The current target snapshot (or undefined) and update callback
|
|
37
40
|
*/
|
|
38
|
-
<T>(ref: Ref.Ref<T> | undefined): [
|
|
41
|
+
<T extends Obj.Unknown>(ref: Ref.Ref<T> | undefined): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];
|
|
39
42
|
|
|
40
43
|
/**
|
|
41
44
|
* Hook to subscribe to an entire Echo object.
|
|
42
|
-
* Returns the current object value and automatically re-renders when the object changes.
|
|
45
|
+
* Returns a snapshot of the current object value and automatically re-renders when the object changes.
|
|
43
46
|
*
|
|
44
|
-
* @param obj - The Echo object to subscribe to
|
|
45
|
-
* @returns The current object
|
|
47
|
+
* @param obj - The Echo object to subscribe to (objects only, not relations)
|
|
48
|
+
* @returns The current object snapshot and update callback
|
|
46
49
|
*/
|
|
47
|
-
<T extends
|
|
50
|
+
<T extends Obj.Unknown>(obj: T): [Obj.Snapshot<T>, ObjectUpdateCallback<T>];
|
|
48
51
|
|
|
49
52
|
/**
|
|
50
53
|
* Hook to subscribe to an entire Echo object that may be undefined.
|
|
51
|
-
* Returns undefined if the object is undefined.
|
|
54
|
+
* Returns a snapshot (undefined if the object is undefined).
|
|
55
|
+
*
|
|
56
|
+
* @param obj - The Echo object to subscribe to (can be undefined, objects only)
|
|
57
|
+
* @returns The current object snapshot (or undefined) and update callback
|
|
58
|
+
*/
|
|
59
|
+
<T extends Obj.Unknown>(obj: T | undefined): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Hook to subscribe to an Echo object or Ref.
|
|
63
|
+
* Handles both cases - if passed a Ref, dereferences it and subscribes to the target.
|
|
64
|
+
* Returns a snapshot (undefined if ref hasn't loaded).
|
|
52
65
|
*
|
|
53
|
-
* @param
|
|
54
|
-
* @returns The current object
|
|
66
|
+
* @param objOrRef - The Echo object or Ref to subscribe to
|
|
67
|
+
* @returns The current object snapshot (or undefined) and update callback
|
|
55
68
|
*/
|
|
56
|
-
<T extends
|
|
69
|
+
<T extends Obj.Unknown>(objOrRef: T | Ref.Ref<T>): [Obj.Snapshot<T> | undefined, ObjectUpdateCallback<T>];
|
|
57
70
|
|
|
58
71
|
/**
|
|
59
72
|
* Hook to subscribe to a specific property of an Echo object.
|
|
60
73
|
* Returns the current property value and automatically re-renders when the property changes.
|
|
61
74
|
*
|
|
62
|
-
* @param obj - The Echo object to subscribe to
|
|
75
|
+
* @param obj - The Echo object to subscribe to (objects only, not relations)
|
|
63
76
|
* @param property - Property key to subscribe to
|
|
64
77
|
* @returns The current property value and update callback
|
|
65
78
|
*/
|
|
66
|
-
<T extends
|
|
79
|
+
<T extends Obj.Unknown, K extends keyof T>(obj: T, property: K): [T[K], ObjectPropUpdateCallback<T[K]>];
|
|
67
80
|
|
|
68
81
|
/**
|
|
69
82
|
* Hook to subscribe to a specific property of an Echo object that may be undefined.
|
|
70
83
|
* Returns undefined if the object is undefined.
|
|
71
84
|
*
|
|
72
|
-
* @param obj - The Echo object to subscribe to (can be undefined)
|
|
85
|
+
* @param obj - The Echo object to subscribe to (can be undefined, objects only)
|
|
73
86
|
* @param property - Property key to subscribe to
|
|
74
87
|
* @returns The current property value (or undefined) and update callback
|
|
75
88
|
*/
|
|
76
|
-
<T extends
|
|
89
|
+
<T extends Obj.Unknown, K extends keyof T>(
|
|
77
90
|
obj: T | undefined,
|
|
78
91
|
property: K,
|
|
79
|
-
): [
|
|
80
|
-
|
|
92
|
+
): [T[K] | undefined, ObjectPropUpdateCallback<T[K]>];
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Hook to subscribe to a specific property of a Ref's target object.
|
|
96
|
+
* Automatically dereferences the ref and handles async loading.
|
|
97
|
+
* Returns undefined if the ref hasn't loaded yet.
|
|
98
|
+
*
|
|
99
|
+
* @param ref - The Ref to dereference and subscribe to
|
|
100
|
+
* @param property - Property key to subscribe to
|
|
101
|
+
* @returns The current property value (or undefined if not loaded) and update callback
|
|
102
|
+
*/
|
|
103
|
+
<T, K extends keyof T>(ref: Ref.Ref<T>, property: K): [T[K] | undefined, ObjectPropUpdateCallback<T[K]>];
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Hook to subscribe to a specific property of a Ref's target object that may be undefined.
|
|
107
|
+
* Returns undefined if the ref is undefined or hasn't loaded yet.
|
|
108
|
+
*
|
|
109
|
+
* @param ref - The Ref to dereference and subscribe to (can be undefined)
|
|
110
|
+
* @param property - Property key to subscribe to
|
|
111
|
+
* @returns The current property value (or undefined) and update callback
|
|
112
|
+
*/
|
|
113
|
+
<T, K extends keyof T>(ref: Ref.Ref<T> | undefined, property: K): [T[K] | undefined, ObjectPropUpdateCallback<T[K]>];
|
|
114
|
+
} = (<T extends Obj.Unknown, K extends keyof T>(objOrRef: T | Ref.Ref<T> | undefined, property?: K): any => {
|
|
81
115
|
// Get the live object for the callback (refs need to dereference).
|
|
82
116
|
const isRef = Ref.isRef(objOrRef);
|
|
83
117
|
const liveObj = isRef ? (objOrRef as Ref.Ref<T>)?.target : (objOrRef as T | undefined);
|
|
@@ -89,20 +123,20 @@ export const useObject: {
|
|
|
89
123
|
if (obj === undefined) {
|
|
90
124
|
return;
|
|
91
125
|
}
|
|
92
|
-
Obj.
|
|
126
|
+
Obj.update(obj, (obj: any) => {
|
|
93
127
|
if (typeof updateOrValue === 'function') {
|
|
94
|
-
const returnValue = updateOrValue(property !== undefined ?
|
|
128
|
+
const returnValue = updateOrValue(property !== undefined ? obj[property] : obj);
|
|
95
129
|
if (returnValue !== undefined) {
|
|
96
130
|
if (property === undefined) {
|
|
97
131
|
throw new Error('Cannot re-assign the entire object');
|
|
98
132
|
}
|
|
99
|
-
|
|
133
|
+
obj[property] = returnValue;
|
|
100
134
|
}
|
|
101
135
|
} else {
|
|
102
136
|
if (property === undefined) {
|
|
103
137
|
throw new Error('Cannot re-assign the entire object');
|
|
104
138
|
}
|
|
105
|
-
|
|
139
|
+
obj[property] = updateOrValue;
|
|
106
140
|
}
|
|
107
141
|
});
|
|
108
142
|
},
|
|
@@ -113,25 +147,26 @@ export const useObject: {
|
|
|
113
147
|
// For property subscriptions on refs, we subscribe to trigger re-render on load.
|
|
114
148
|
// TODO(dxos): Property subscriptions on refs may not update correctly until the ref loads.
|
|
115
149
|
useObjectValue(objOrRef);
|
|
116
|
-
return [useObjectProperty(liveObj
|
|
150
|
+
return [useObjectProperty(liveObj, property as any), callback];
|
|
151
|
+
} else {
|
|
152
|
+
return [useObjectValue(objOrRef), callback];
|
|
117
153
|
}
|
|
118
|
-
return [useObjectValue(objOrRef), callback];
|
|
119
154
|
}) as any;
|
|
120
155
|
|
|
121
156
|
/**
|
|
122
157
|
* Internal hook for subscribing to an Echo object or Ref.
|
|
123
158
|
* AtomObj.make handles both objects and refs, returning snapshots.
|
|
124
159
|
*/
|
|
125
|
-
const useObjectValue = <T extends
|
|
160
|
+
const useObjectValue = <T extends Obj.Unknown>(objOrRef: T | Ref.Ref<T> | undefined): Obj.Snapshot<T> | undefined => {
|
|
126
161
|
const atom = useMemo(() => AtomObj.make(objOrRef), [objOrRef]);
|
|
127
|
-
return useAtomValue(atom);
|
|
162
|
+
return useAtomValue(atom) as Obj.Snapshot<T> | undefined;
|
|
128
163
|
};
|
|
129
164
|
|
|
130
165
|
/**
|
|
131
166
|
* Internal hook for subscribing to a specific property of an Echo object.
|
|
132
167
|
* Uses useAtomValue directly since makeProperty returns the value directly.
|
|
133
168
|
*/
|
|
134
|
-
const useObjectProperty = <T extends
|
|
169
|
+
const useObjectProperty = <T extends Obj.Unknown, K extends keyof T>(
|
|
135
170
|
obj: T | undefined,
|
|
136
171
|
property: K,
|
|
137
172
|
): T[K] | undefined => {
|
|
@@ -150,66 +185,20 @@ const useObjectProperty = <T extends Entity.Unknown, K extends keyof T>(
|
|
|
150
185
|
* @param refs - Array of Refs to dereference and subscribe to
|
|
151
186
|
* @returns Array of loaded target snapshots (excludes unloaded refs)
|
|
152
187
|
*/
|
|
153
|
-
export const useObjects = <T extends
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const triggerUpdate = () => {
|
|
164
|
-
if (isMounted) {
|
|
165
|
-
setVersion((v) => v + 1);
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
// Function to set up subscription for a target.
|
|
170
|
-
const subscribeToTarget = (ref: Ref.Ref<T>) => {
|
|
171
|
-
if (!isMounted) return;
|
|
172
|
-
const target = ref.target;
|
|
173
|
-
if (target) {
|
|
174
|
-
const key = ref.dxn.toString();
|
|
175
|
-
if (!targetUnsubscribes.has(key)) {
|
|
176
|
-
targetUnsubscribes.set(key, Obj.subscribe(target, triggerUpdate));
|
|
188
|
+
export const useObjects = <T extends Obj.Unknown>(refs: readonly Ref.Ref<T>[]): Obj.Snapshot<T>[] => {
|
|
189
|
+
const atom = useMemo(
|
|
190
|
+
() =>
|
|
191
|
+
Atom.make((get) => {
|
|
192
|
+
const results: Obj.Snapshot<T>[] = [];
|
|
193
|
+
for (const ref of refs) {
|
|
194
|
+
const value = get(AtomObj.make(ref));
|
|
195
|
+
if (value !== undefined) {
|
|
196
|
+
results.push(value as Obj.Snapshot<T>);
|
|
197
|
+
}
|
|
177
198
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
// Subscribe to existing target if available.
|
|
184
|
-
subscribeToTarget(ref);
|
|
185
|
-
|
|
186
|
-
// Trigger async load if not already loaded.
|
|
187
|
-
if (!ref.target) {
|
|
188
|
-
void ref
|
|
189
|
-
.load()
|
|
190
|
-
.then(() => {
|
|
191
|
-
subscribeToTarget(ref);
|
|
192
|
-
triggerUpdate();
|
|
193
|
-
})
|
|
194
|
-
.catch(() => {
|
|
195
|
-
// Ignore load errors.
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return () => {
|
|
201
|
-
isMounted = false;
|
|
202
|
-
targetUnsubscribes.forEach((u) => u());
|
|
203
|
-
};
|
|
204
|
-
}, [refs]);
|
|
205
|
-
|
|
206
|
-
// Compute current snapshots by reading each ref's target.
|
|
207
|
-
const snapshots: Readonly<T>[] = [];
|
|
208
|
-
for (const ref of refs) {
|
|
209
|
-
const target = ref.target;
|
|
210
|
-
if (target !== undefined) {
|
|
211
|
-
snapshots.push(target as Readonly<T>);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return snapshots;
|
|
199
|
+
return results;
|
|
200
|
+
}),
|
|
201
|
+
[refs],
|
|
202
|
+
);
|
|
203
|
+
return useAtomValue(atom);
|
|
215
204
|
};
|
package/src/useQuery.__test.tsx
CHANGED
|
@@ -10,6 +10,7 @@ import { describe, expect, test } from 'vitest';
|
|
|
10
10
|
|
|
11
11
|
import { Filter } from '@dxos/client/echo';
|
|
12
12
|
import { Obj, Type } from '@dxos/echo';
|
|
13
|
+
import { TestSchema } from '@dxos/echo/testing';
|
|
13
14
|
|
|
14
15
|
import { createClient, createClientContextProvider } from '../testing/util';
|
|
15
16
|
|
|
@@ -23,9 +24,9 @@ describe('useQuery', () => {
|
|
|
23
24
|
const wrapper = await createClientContextProvider(client);
|
|
24
25
|
|
|
25
26
|
// Create 3 test objects: Alice, Bob, Charlie.
|
|
26
|
-
const alice = Obj.make(
|
|
27
|
-
const bob = Obj.make(
|
|
28
|
-
const charlie = Obj.make(
|
|
27
|
+
const alice = Obj.make(TestSchema.Expando, { name: 'Alice' });
|
|
28
|
+
const bob = Obj.make(TestSchema.Expando, { name: 'Bob' });
|
|
29
|
+
const charlie = Obj.make(TestSchema.Expando, { name: 'Charlie' });
|
|
29
30
|
|
|
30
31
|
space!.db.add(alice);
|
|
31
32
|
space!.db.add(bob);
|
|
@@ -38,7 +39,7 @@ describe('useQuery', () => {
|
|
|
38
39
|
// Setup useQuery hook that captures every render.
|
|
39
40
|
renderHook(
|
|
40
41
|
() => {
|
|
41
|
-
const objects = useQuery(space?.db, Filter.type(
|
|
42
|
+
const objects = useQuery(space?.db, Filter.type(TestSchema.Expando));
|
|
42
43
|
|
|
43
44
|
// Capture the names in this render.
|
|
44
45
|
const namesInThisRender = objects.map((obj) => obj.name);
|
|
@@ -85,7 +86,7 @@ describe('useQuery', () => {
|
|
|
85
86
|
const wrapper = await createClientContextProvider(client);
|
|
86
87
|
|
|
87
88
|
// Create 10 test objects: 1, 2, 3, ..., 10.
|
|
88
|
-
const objects = Array.from({ length: 10 }, (_, i) => Obj.make(
|
|
89
|
+
const objects = Array.from({ length: 10 }, (_, i) => Obj.make(TestSchema.Expando, { value: i + 1 }));
|
|
89
90
|
|
|
90
91
|
objects.forEach((obj) => space!.db.add(obj));
|
|
91
92
|
await space!.db.flush();
|
|
@@ -96,7 +97,7 @@ describe('useQuery', () => {
|
|
|
96
97
|
// Setup useQuery hook that captures every render.
|
|
97
98
|
renderHook(
|
|
98
99
|
() => {
|
|
99
|
-
const queryObjects = useQuery(space?.db, Filter.type(
|
|
100
|
+
const queryObjects = useQuery(space?.db, Filter.type(TestSchema.Expando));
|
|
100
101
|
|
|
101
102
|
// Capture the values in this render.
|
|
102
103
|
const valuesInThisRender = queryObjects.map((obj) => obj.value);
|
package/src/useQuery.ts
CHANGED
|
@@ -24,6 +24,8 @@ interface UseQueryFn {
|
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Create subscription.
|
|
27
|
+
*
|
|
28
|
+
* @param queryOrFilter - The query or filter to apply. Query is memoized based on the AST. No need to call useMemo.
|
|
27
29
|
*/
|
|
28
30
|
export const useQuery: UseQueryFn = (
|
|
29
31
|
resource: Database.Queryable | undefined,
|
package/src/useSchema.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { type Database, type Type } from '@dxos/echo';
|
|
|
9
9
|
/**
|
|
10
10
|
* Subscribe to and retrieve schema changes from a space's schema registry.
|
|
11
11
|
*/
|
|
12
|
-
export const useSchema = <T extends Type.
|
|
12
|
+
export const useSchema = <T extends Type.AnyEntity = Type.AnyEntity>(
|
|
13
13
|
db?: Database.Database,
|
|
14
14
|
typename?: string,
|
|
15
15
|
): T | undefined => {
|