@dxos/echo-solid 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 +209 -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 +211 -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 +101 -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.d.ts +14 -0
- package/dist/types/src/useQuery.d.ts.map +1 -0
- package/dist/types/src/useQuery.test.d.ts +2 -0
- package/dist/types/src/useQuery.test.d.ts.map +1 -0
- package/dist/types/src/useSchema.d.ts +14 -0
- package/dist/types/src/useSchema.d.ts.map +1 -0
- package/dist/types/src/useSchema.test.d.ts +2 -0
- package/dist/types/src/useSchema.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +10 -5
- package/src/index.ts +0 -1
- package/src/useObject.test.tsx +2 -2
- package/src/useObject.ts +218 -73
- package/src/useQuery.test.tsx +17 -17
- package/src/useRef.test.tsx +0 -247
- package/src/useRef.ts +0 -48
package/src/useRef.test.tsx
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { render, waitFor } from '@solidjs/testing-library';
|
|
6
|
-
import { type JSX, createSignal } from 'solid-js';
|
|
7
|
-
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
|
|
8
|
-
|
|
9
|
-
import { Obj, Ref, Type } from '@dxos/echo';
|
|
10
|
-
import { TestSchema } from '@dxos/echo/testing';
|
|
11
|
-
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
12
|
-
import { Registry, RegistryProvider } from '@dxos/effect-atom-solid';
|
|
13
|
-
|
|
14
|
-
import { useRef } from './useRef';
|
|
15
|
-
|
|
16
|
-
const createWrapper = (registry: Registry.Registry) => {
|
|
17
|
-
return (props: { children: JSX.Element }) => {
|
|
18
|
-
return <RegistryProvider registry={registry}>{props.children}</RegistryProvider>;
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
describe('useRef', () => {
|
|
23
|
-
let testBuilder: EchoTestBuilder;
|
|
24
|
-
let db: any;
|
|
25
|
-
let registry: Registry.Registry;
|
|
26
|
-
|
|
27
|
-
beforeEach(async () => {
|
|
28
|
-
testBuilder = await new EchoTestBuilder().open();
|
|
29
|
-
const { db: database } = await testBuilder.createDatabase();
|
|
30
|
-
db = database;
|
|
31
|
-
registry = Registry.make();
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
afterEach(async () => {
|
|
35
|
-
await testBuilder.close();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('returns undefined when ref is undefined', () => {
|
|
39
|
-
const Wrapper = createWrapper(registry);
|
|
40
|
-
|
|
41
|
-
let result: any;
|
|
42
|
-
|
|
43
|
-
render(
|
|
44
|
-
() => {
|
|
45
|
-
const target = useRef(undefined);
|
|
46
|
-
result = target();
|
|
47
|
-
return (<div>test</div>) as JSX.Element;
|
|
48
|
-
},
|
|
49
|
-
{ wrapper: Wrapper },
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
expect(result).toBeUndefined();
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test('returns undefined when ref target is not loaded and loads it', async () => {
|
|
56
|
-
await db.graph.schemaRegistry.register([TestSchema.Person]);
|
|
57
|
-
|
|
58
|
-
// Create objects with a ref
|
|
59
|
-
const targetObj = Obj.make(TestSchema.Person, { name: 'Target', username: 'target', email: 'target@example.com' });
|
|
60
|
-
db.add(targetObj);
|
|
61
|
-
await db.flush({ indexes: true });
|
|
62
|
-
|
|
63
|
-
// Create a ref - target should be available since object is in memory
|
|
64
|
-
const ref = Ref.make(targetObj);
|
|
65
|
-
|
|
66
|
-
// Initially target should be available since object is in memory
|
|
67
|
-
expect(ref.target).toBeDefined();
|
|
68
|
-
|
|
69
|
-
const Wrapper = createWrapper(registry);
|
|
70
|
-
|
|
71
|
-
let targetAccessor: (() => any) | undefined;
|
|
72
|
-
|
|
73
|
-
function TestComponent() {
|
|
74
|
-
const target = useRef(ref);
|
|
75
|
-
targetAccessor = target;
|
|
76
|
-
return (<div data-testid='name'>{target()?.name || 'undefined'}</div>) as JSX.Element;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const { getByTestId } = render(() => <TestComponent />, { wrapper: Wrapper });
|
|
80
|
-
|
|
81
|
-
// Should load since target is available
|
|
82
|
-
await waitFor(() => {
|
|
83
|
-
expect(getByTestId('name').textContent).toBe('Target');
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
// Get the actual result from the accessor
|
|
87
|
-
const result = targetAccessor?.();
|
|
88
|
-
expect(result?.name).toBe('Target');
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test('returns target when ref is loaded', async () => {
|
|
92
|
-
await db.graph.schemaRegistry.register([TestSchema.Person]);
|
|
93
|
-
|
|
94
|
-
const targetObj = Obj.make(TestSchema.Person, { name: 'Target', username: 'target', email: 'target@example.com' });
|
|
95
|
-
db.add(targetObj);
|
|
96
|
-
await db.flush({ indexes: true });
|
|
97
|
-
|
|
98
|
-
const ref = Ref.make(targetObj);
|
|
99
|
-
const Wrapper = createWrapper(registry);
|
|
100
|
-
|
|
101
|
-
let targetAccessor: (() => any) | undefined;
|
|
102
|
-
|
|
103
|
-
function TestComponent() {
|
|
104
|
-
const target = useRef(ref);
|
|
105
|
-
targetAccessor = target;
|
|
106
|
-
return (<div data-testid='name'>{target()?.name || 'undefined'}</div>) as JSX.Element;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const { getByTestId } = render(() => <TestComponent />, { wrapper: Wrapper });
|
|
110
|
-
|
|
111
|
-
await waitFor(() => {
|
|
112
|
-
expect(getByTestId('name').textContent).toBe('Target');
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
// Get the actual result from the accessor
|
|
116
|
-
const result = targetAccessor?.();
|
|
117
|
-
expect(result?.name).toBe('Target');
|
|
118
|
-
expect(result?.username).toBe('target');
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
test('handles ref to Expando object', async () => {
|
|
122
|
-
const targetObj = Obj.make(Type.Expando, { name: 'Expando Target', value: 42 });
|
|
123
|
-
db.add(targetObj);
|
|
124
|
-
await db.flush({ indexes: true });
|
|
125
|
-
|
|
126
|
-
const ref = Ref.make(targetObj);
|
|
127
|
-
const Wrapper = createWrapper(registry);
|
|
128
|
-
|
|
129
|
-
let targetAccessor: (() => any) | undefined;
|
|
130
|
-
|
|
131
|
-
function TestComponent() {
|
|
132
|
-
const target = useRef(ref);
|
|
133
|
-
targetAccessor = target;
|
|
134
|
-
return (<div data-testid='name'>{target()?.name || 'undefined'}</div>) as JSX.Element;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const { getByTestId } = render(() => <TestComponent />, { wrapper: Wrapper });
|
|
138
|
-
|
|
139
|
-
await waitFor(() => {
|
|
140
|
-
expect(getByTestId('name').textContent).toBe('Expando Target');
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
// Get the actual result from the accessor
|
|
144
|
-
const result = targetAccessor?.();
|
|
145
|
-
expect(result?.name).toBe('Expando Target');
|
|
146
|
-
expect(result?.value).toBe(42);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
test('works with accessor function', async () => {
|
|
150
|
-
await db.graph.schemaRegistry.register([TestSchema.Person]);
|
|
151
|
-
|
|
152
|
-
const targetObj = Obj.make(TestSchema.Person, { name: 'Target', username: 'target', email: 'target@example.com' });
|
|
153
|
-
db.add(targetObj);
|
|
154
|
-
await db.flush({ indexes: true });
|
|
155
|
-
|
|
156
|
-
const ref = Ref.make(targetObj);
|
|
157
|
-
const Wrapper = createWrapper(registry);
|
|
158
|
-
|
|
159
|
-
let targetAccessor: (() => any) | undefined;
|
|
160
|
-
|
|
161
|
-
function TestComponent() {
|
|
162
|
-
const target = useRef(() => ref);
|
|
163
|
-
targetAccessor = target;
|
|
164
|
-
return (<div data-testid='name'>{target()?.name || 'undefined'}</div>) as JSX.Element;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const { getByTestId } = render(() => <TestComponent />, { wrapper: Wrapper });
|
|
168
|
-
|
|
169
|
-
await waitFor(() => {
|
|
170
|
-
expect(getByTestId('name').textContent).toBe('Target');
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
// Get the actual result from the accessor
|
|
174
|
-
const result = targetAccessor?.();
|
|
175
|
-
expect(result?.name).toBe('Target');
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
test('reactively tracks changes when accessor returns different ref', async () => {
|
|
179
|
-
await db.graph.schemaRegistry.register([TestSchema.Person]);
|
|
180
|
-
|
|
181
|
-
const targetObj1 = Obj.make(TestSchema.Person, {
|
|
182
|
-
name: 'Target1',
|
|
183
|
-
username: 'target1',
|
|
184
|
-
email: 'target1@example.com',
|
|
185
|
-
});
|
|
186
|
-
const targetObj2 = Obj.make(TestSchema.Person, {
|
|
187
|
-
name: 'Target2',
|
|
188
|
-
username: 'target2',
|
|
189
|
-
email: 'target2@example.com',
|
|
190
|
-
});
|
|
191
|
-
db.add(targetObj1);
|
|
192
|
-
db.add(targetObj2);
|
|
193
|
-
await db.flush({ indexes: true });
|
|
194
|
-
|
|
195
|
-
const ref1 = Ref.make(targetObj1);
|
|
196
|
-
const ref2 = Ref.make(targetObj2);
|
|
197
|
-
const [refSignal, setRefSignal] = createSignal<Ref.Ref<any> | undefined>(ref1);
|
|
198
|
-
const Wrapper = createWrapper(registry);
|
|
199
|
-
|
|
200
|
-
let targetAccessor: (() => any) | undefined;
|
|
201
|
-
|
|
202
|
-
function TestComponent() {
|
|
203
|
-
const target = useRef(refSignal);
|
|
204
|
-
targetAccessor = target;
|
|
205
|
-
return (<div data-testid='name'>{target()?.name || 'undefined'}</div>) as JSX.Element;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const { getByTestId } = render(() => <TestComponent />, { wrapper: Wrapper });
|
|
209
|
-
|
|
210
|
-
await waitFor(() => {
|
|
211
|
-
expect(getByTestId('name').textContent).toBe('Target1');
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
// Get the actual result from the accessor
|
|
215
|
-
let result = targetAccessor?.();
|
|
216
|
-
expect(result?.name).toBe('Target1');
|
|
217
|
-
|
|
218
|
-
// Change the ref via signal
|
|
219
|
-
setRefSignal(() => ref2);
|
|
220
|
-
|
|
221
|
-
await waitFor(() => {
|
|
222
|
-
expect(getByTestId('name').textContent).toBe('Target2');
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
// Get the actual result from the accessor
|
|
226
|
-
result = targetAccessor?.();
|
|
227
|
-
expect(result?.name).toBe('Target2');
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
test('handles undefined when accessor returns undefined', () => {
|
|
231
|
-
const [refSignal] = createSignal<Ref.Ref<any> | undefined>(undefined);
|
|
232
|
-
const Wrapper = createWrapper(registry);
|
|
233
|
-
|
|
234
|
-
let targetAccessor: (() => any) | undefined;
|
|
235
|
-
|
|
236
|
-
function TestComponent() {
|
|
237
|
-
const target = useRef(refSignal);
|
|
238
|
-
targetAccessor = target;
|
|
239
|
-
return (<div data-testid='name'>{target()?.name || 'undefined'}</div>) as JSX.Element;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
const { getByTestId } = render(() => <TestComponent />, { wrapper: Wrapper });
|
|
243
|
-
|
|
244
|
-
expect(getByTestId('name').textContent).toBe('undefined');
|
|
245
|
-
expect(targetAccessor?.()).toBeUndefined();
|
|
246
|
-
});
|
|
247
|
-
});
|
package/src/useRef.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type MaybeAccessor, access } from '@solid-primitives/utils';
|
|
6
|
-
import { type Accessor, createEffect, createMemo, createSignal, onCleanup } from 'solid-js';
|
|
7
|
-
|
|
8
|
-
import { type Entity, type Ref } from '@dxos/echo';
|
|
9
|
-
import { AtomRef } from '@dxos/echo-atom';
|
|
10
|
-
import { useRegistry } from '@dxos/effect-atom-solid';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Subscribe to a reference target object.
|
|
14
|
-
* Returns undefined if the reference hasn't loaded yet, and automatically updates when the target loads or changes.
|
|
15
|
-
*
|
|
16
|
-
* @param ref - The reference to subscribe to (can be reactive).
|
|
17
|
-
* @returns An accessor that returns the current target object or undefined if not loaded.
|
|
18
|
-
*/
|
|
19
|
-
export function useRef<T extends Entity.Unknown>(ref: MaybeAccessor<Ref.Ref<T> | undefined>): Accessor<T | undefined> {
|
|
20
|
-
const registry = useRegistry();
|
|
21
|
-
|
|
22
|
-
// Memoize the ref to track changes.
|
|
23
|
-
const memoizedRef = createMemo(() => access(ref));
|
|
24
|
-
|
|
25
|
-
// Store the current target in a signal.
|
|
26
|
-
const [target, setTarget] = createSignal<T | undefined>(undefined);
|
|
27
|
-
|
|
28
|
-
// Subscribe to ref target changes.
|
|
29
|
-
createEffect(() => {
|
|
30
|
-
const currentRef = memoizedRef();
|
|
31
|
-
|
|
32
|
-
const atom = AtomRef.make(currentRef);
|
|
33
|
-
const currentValue = registry.get(atom);
|
|
34
|
-
setTarget(() => currentValue);
|
|
35
|
-
|
|
36
|
-
const unsubscribe = registry.subscribe(
|
|
37
|
-
atom,
|
|
38
|
-
() => {
|
|
39
|
-
setTarget(() => registry.get(atom));
|
|
40
|
-
},
|
|
41
|
-
{ immediate: true },
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
onCleanup(unsubscribe);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
return target;
|
|
48
|
-
}
|