@dxos/echo-react 0.0.0 → 0.8.4-main.69d29f4
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/browser/index.mjs +153 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +155 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/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 +11 -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 +10 -6
- package/src/useObject.ts +79 -91
- package/src/useQuery.__test.tsx +7 -6
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.69d29f4",
|
|
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,
|
|
@@ -25,18 +29,18 @@
|
|
|
25
29
|
"src"
|
|
26
30
|
],
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"@effect-atom/atom
|
|
29
|
-
"@
|
|
30
|
-
"@dxos/echo-atom": "0.
|
|
32
|
+
"@effect-atom/atom": "^0.4.13",
|
|
33
|
+
"@effect-atom/atom-react": "^0.4.6",
|
|
34
|
+
"@dxos/echo-atom": "0.8.4-main.69d29f4",
|
|
35
|
+
"@dxos/echo": "0.8.4-main.69d29f4"
|
|
31
36
|
},
|
|
32
37
|
"devDependencies": {
|
|
33
|
-
"@effect-atom/atom": "^0.4.10",
|
|
34
38
|
"@testing-library/react": "^16.3.0",
|
|
35
39
|
"@types/react": "~19.2.7",
|
|
36
40
|
"@types/react-dom": "~19.2.3",
|
|
37
41
|
"react": "~19.2.3",
|
|
38
42
|
"react-dom": "~19.2.3",
|
|
39
|
-
"@dxos/echo-db": "0.8.
|
|
43
|
+
"@dxos/echo-db": "0.8.4-main.69d29f4"
|
|
40
44
|
},
|
|
41
45
|
"peerDependencies": {
|
|
42
46
|
"react": "~19.2.3"
|
package/src/useObject.ts
CHANGED
|
@@ -2,18 +2,21 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import * as Atom from '@effect-atom/atom/Atom';
|
|
5
6
|
import { useAtomValue } from '@effect-atom/atom-react';
|
|
6
|
-
import { useCallback,
|
|
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,7 +123,7 @@ export const useObject: {
|
|
|
89
123
|
if (obj === undefined) {
|
|
90
124
|
return;
|
|
91
125
|
}
|
|
92
|
-
Obj.change(obj
|
|
126
|
+
Obj.change(obj, (o: any) => {
|
|
93
127
|
if (typeof updateOrValue === 'function') {
|
|
94
128
|
const returnValue = updateOrValue(property !== undefined ? o[property] : o);
|
|
95
129
|
if (returnValue !== undefined) {
|
|
@@ -113,7 +147,7 @@ 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];
|
|
117
151
|
}
|
|
118
152
|
return [useObjectValue(objOrRef), callback];
|
|
119
153
|
}) as any;
|
|
@@ -122,16 +156,16 @@ export const useObject: {
|
|
|
122
156
|
* Internal hook for subscribing to an Echo object or Ref.
|
|
123
157
|
* AtomObj.make handles both objects and refs, returning snapshots.
|
|
124
158
|
*/
|
|
125
|
-
const useObjectValue = <T extends
|
|
159
|
+
const useObjectValue = <T extends Obj.Unknown>(objOrRef: T | Ref.Ref<T> | undefined): Obj.Snapshot<T> | undefined => {
|
|
126
160
|
const atom = useMemo(() => AtomObj.make(objOrRef), [objOrRef]);
|
|
127
|
-
return useAtomValue(atom);
|
|
161
|
+
return useAtomValue(atom) as Obj.Snapshot<T> | undefined;
|
|
128
162
|
};
|
|
129
163
|
|
|
130
164
|
/**
|
|
131
165
|
* Internal hook for subscribing to a specific property of an Echo object.
|
|
132
166
|
* Uses useAtomValue directly since makeProperty returns the value directly.
|
|
133
167
|
*/
|
|
134
|
-
const useObjectProperty = <T extends
|
|
168
|
+
const useObjectProperty = <T extends Obj.Unknown, K extends keyof T>(
|
|
135
169
|
obj: T | undefined,
|
|
136
170
|
property: K,
|
|
137
171
|
): T[K] | undefined => {
|
|
@@ -150,66 +184,20 @@ const useObjectProperty = <T extends Entity.Unknown, K extends keyof T>(
|
|
|
150
184
|
* @param refs - Array of Refs to dereference and subscribe to
|
|
151
185
|
* @returns Array of loaded target snapshots (excludes unloaded refs)
|
|
152
186
|
*/
|
|
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));
|
|
187
|
+
export const useObjects = <T extends Obj.Unknown>(refs: readonly Ref.Ref<T>[]): Obj.Snapshot<T>[] => {
|
|
188
|
+
const atom = useMemo(
|
|
189
|
+
() =>
|
|
190
|
+
Atom.make((get) => {
|
|
191
|
+
const results: Obj.Snapshot<T>[] = [];
|
|
192
|
+
for (const ref of refs) {
|
|
193
|
+
const value = get(AtomObj.make(ref));
|
|
194
|
+
if (value !== undefined) {
|
|
195
|
+
results.push(value as Obj.Snapshot<T>);
|
|
196
|
+
}
|
|
177
197
|
}
|
|
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;
|
|
198
|
+
return results;
|
|
199
|
+
}),
|
|
200
|
+
[refs],
|
|
201
|
+
);
|
|
202
|
+
return useAtomValue(atom);
|
|
215
203
|
};
|
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);
|