@dxos/react-client 0.6.11 → 0.6.12-main.15a606f
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/{chunk-CL7JDETM.mjs → chunk-ESSNDYBJ.mjs} +70 -61
- package/dist/lib/browser/chunk-ESSNDYBJ.mjs.map +7 -0
- package/dist/lib/browser/devtools/index.mjs +1 -1
- package/dist/lib/browser/echo/index.mjs +6 -4
- package/dist/lib/browser/echo/index.mjs.map +3 -3
- package/dist/lib/browser/halo/index.mjs +1 -1
- package/dist/lib/browser/index.mjs +1 -3
- package/dist/lib/browser/index.mjs.map +1 -1
- package/dist/lib/browser/mesh/index.mjs +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +28 -21
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/{chunk-C4EWAKPZ.cjs → chunk-N474KOAU.cjs} +71 -61
- package/dist/lib/node/chunk-N474KOAU.cjs.map +7 -0
- package/dist/lib/node/devtools/index.cjs +2 -2
- package/dist/lib/node/echo/index.cjs +7 -5
- package/dist/lib/node/echo/index.cjs.map +3 -3
- package/dist/lib/node/halo/index.cjs +5 -5
- package/dist/lib/node/index.cjs +12 -14
- package/dist/lib/node/index.cjs.map +1 -1
- package/dist/lib/node/mesh/index.cjs +2 -2
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +30 -23
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/lib/node-esm/chunk-ACLHQASD.mjs +302 -0
- package/dist/lib/node-esm/chunk-ACLHQASD.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-UYJUBYMU.mjs +165 -0
- package/dist/lib/node-esm/chunk-UYJUBYMU.mjs.map +7 -0
- package/dist/lib/node-esm/devtools/index.mjs +44 -0
- package/dist/lib/node-esm/devtools/index.mjs.map +7 -0
- package/dist/lib/node-esm/echo/index.mjs +134 -0
- package/dist/lib/node-esm/echo/index.mjs.map +7 -0
- package/dist/lib/node-esm/halo/index.mjs +100 -0
- package/dist/lib/node-esm/halo/index.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +33 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/invitations/index.mjs +18 -0
- package/dist/lib/node-esm/invitations/index.mjs.map +7 -0
- package/dist/lib/node-esm/mesh/index.mjs +18 -0
- package/dist/lib/node-esm/mesh/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/lib/node-esm/testing/index.mjs +173 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/lib/node-esm/worker.mjs +5 -0
- package/dist/lib/node-esm/worker.mjs.map +7 -0
- package/dist/types/src/client/ClientProvider.d.ts +65 -9
- package/dist/types/src/client/ClientProvider.d.ts.map +1 -1
- package/dist/types/src/client/context.d.ts +1 -4
- package/dist/types/src/client/context.d.ts.map +1 -1
- package/dist/types/src/client/index.d.ts +0 -1
- package/dist/types/src/client/index.d.ts.map +1 -1
- package/dist/types/src/echo/useQuery.d.ts.map +1 -1
- package/dist/types/src/playwright/playwright.config.d.ts +2 -2
- package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
- package/dist/types/src/testing/util.d.ts +5 -3
- package/dist/types/src/testing/util.d.ts.map +1 -1
- package/dist/types/src/testing/withClientProvider.d.ts +4 -1
- package/dist/types/src/testing/withClientProvider.d.ts.map +1 -1
- package/dist/types/src/testing/withClientProvider.stories.d.ts +4 -3
- package/dist/types/src/testing/withClientProvider.stories.d.ts.map +1 -1
- package/package.json +47 -38
- package/project.json +3 -23
- package/src/client/ClientProvider.test.tsx +7 -4
- package/src/client/ClientProvider.tsx +130 -90
- package/src/client/context.ts +4 -4
- package/src/client/index.ts +0 -3
- package/src/client/useConfig.test.tsx +7 -11
- package/src/echo/useQuery.ts +3 -6
- package/src/echo/useSpaces.test.tsx +6 -8
- package/src/halo/useDevices.test.tsx +3 -5
- package/src/playwright/playwright.config.ts +13 -2
- package/src/playwright/smoke.spec.ts +5 -10
- package/src/testing/util.tsx +11 -6
- package/src/testing/withClientProvider.stories.tsx +19 -10
- package/src/testing/withClientProvider.tsx +22 -18
- package/tsconfig.json +4 -2
- package/vitest.config.ts +22 -0
- package/dist/lib/browser/chunk-CL7JDETM.mjs.map +0 -7
- package/dist/lib/node/chunk-C4EWAKPZ.cjs.map +0 -7
- package/dist/types/src/client/useClientServices.d.ts +0 -3
- package/dist/types/src/client/useClientServices.d.ts.map +0 -1
- package/src/client/useClientServices.ts +0 -15
package/project.json
CHANGED
|
@@ -26,32 +26,12 @@
|
|
|
26
26
|
"dependsOn": [
|
|
27
27
|
"^compile",
|
|
28
28
|
"stories:mkdist"
|
|
29
|
-
]
|
|
30
|
-
"options": {
|
|
31
|
-
"playwrightConfigPath": "{projectRoot}/src/playwright/playwright.config.ts",
|
|
32
|
-
"serve": "stories:storybook-e2e",
|
|
33
|
-
"testPatterns": [
|
|
34
|
-
"{projectRoot}/src/playwright/**/*.spec.{ts,js}"
|
|
35
|
-
],
|
|
36
|
-
"timeout": 60000,
|
|
37
|
-
"watchPatterns": [
|
|
38
|
-
"{projectRoot}/src/**/*"
|
|
39
|
-
]
|
|
40
|
-
}
|
|
29
|
+
]
|
|
41
30
|
},
|
|
42
31
|
"lint": {},
|
|
43
|
-
"test": {
|
|
44
|
-
"options": {
|
|
45
|
-
"ciEnvironments": [
|
|
46
|
-
"nodejs"
|
|
47
|
-
],
|
|
48
|
-
"domRequired": true,
|
|
49
|
-
"forceExit": true
|
|
50
|
-
}
|
|
51
|
-
}
|
|
32
|
+
"test": {}
|
|
52
33
|
},
|
|
53
34
|
"implicitDependencies": [
|
|
54
|
-
"esbuild"
|
|
55
|
-
"test"
|
|
35
|
+
"esbuild"
|
|
56
36
|
]
|
|
57
37
|
}
|
|
@@ -2,14 +2,13 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { renderHook, screen, render, act } from '@testing-library/react';
|
|
6
|
-
import expect from 'expect';
|
|
5
|
+
import { renderHook, screen, render, act, cleanup } from '@testing-library/react';
|
|
7
6
|
import React, { Component, type PropsWithChildren } from 'react';
|
|
7
|
+
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
|
|
8
8
|
|
|
9
9
|
import { waitForCondition } from '@dxos/async';
|
|
10
10
|
import { Client, Config, SystemStatus, fromHost } from '@dxos/client';
|
|
11
11
|
import { log } from '@dxos/log';
|
|
12
|
-
import { describe, test } from '@dxos/test';
|
|
13
12
|
|
|
14
13
|
import { ClientProvider } from './ClientProvider';
|
|
15
14
|
import { useClient } from './useClient';
|
|
@@ -97,6 +96,10 @@ describe('ClientProvider', () => {
|
|
|
97
96
|
await client.halo.createIdentity({ displayName: 'test-user' });
|
|
98
97
|
});
|
|
99
98
|
|
|
99
|
+
afterEach(() => {
|
|
100
|
+
cleanup();
|
|
101
|
+
});
|
|
102
|
+
|
|
100
103
|
test('Renders with children', async () => {
|
|
101
104
|
render(
|
|
102
105
|
<ClientProvider client={client}>
|
|
@@ -148,7 +151,7 @@ describe('ClientProvider', () => {
|
|
|
148
151
|
|
|
149
152
|
await act(async () => {
|
|
150
153
|
await waitForCondition({
|
|
151
|
-
condition: () => newClient.status.get() === SystemStatus.ACTIVE
|
|
154
|
+
condition: () => !client.initialized && newClient.status.get() === SystemStatus.ACTIVE,
|
|
152
155
|
});
|
|
153
156
|
});
|
|
154
157
|
expect(client.initialized).toBe(false);
|
|
@@ -2,12 +2,19 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React, {
|
|
5
|
+
import React, {
|
|
6
|
+
forwardRef,
|
|
7
|
+
type FunctionComponent,
|
|
8
|
+
type ReactNode,
|
|
9
|
+
useEffect,
|
|
10
|
+
useImperativeHandle,
|
|
11
|
+
useState,
|
|
12
|
+
} from 'react';
|
|
6
13
|
|
|
7
14
|
import { Client, type ClientOptions, type ClientServicesProvider, SystemStatus } from '@dxos/client';
|
|
8
15
|
import { type Config } from '@dxos/config';
|
|
9
16
|
import { type S } from '@dxos/echo-schema';
|
|
10
|
-
import {
|
|
17
|
+
import { registerSignalRuntime } from '@dxos/echo-signals/react';
|
|
11
18
|
import { log } from '@dxos/log';
|
|
12
19
|
import { useControlledValue } from '@dxos/react-hooks';
|
|
13
20
|
import { getAsyncProviderValue, type MaybePromise, type Provider } from '@dxos/util';
|
|
@@ -23,29 +30,30 @@ export type ClientProviderProps = Pick<ClientContextProps, 'status'> &
|
|
|
23
30
|
children?: ReactNode;
|
|
24
31
|
|
|
25
32
|
/**
|
|
26
|
-
*
|
|
33
|
+
* Client object or async provider to enable to caller to do custom initialization.
|
|
34
|
+
*
|
|
35
|
+
* NOTE: For advanced use cases only.
|
|
27
36
|
*/
|
|
28
|
-
|
|
37
|
+
client?: Client | Provider<Promise<Client>>;
|
|
29
38
|
|
|
30
39
|
/**
|
|
31
|
-
*
|
|
40
|
+
* Config object or async provider.
|
|
32
41
|
*
|
|
33
|
-
*
|
|
42
|
+
* NOTE: If a `client` is provided then `config` is ignored.
|
|
34
43
|
*/
|
|
35
|
-
|
|
36
|
-
// (Preferring `onInitialized` for custom initialization.)
|
|
37
|
-
// TODO(burdon): Use useImperativeHandle to expose client.
|
|
38
|
-
client?: Client | Provider<Promise<Client>>;
|
|
44
|
+
config?: Config | Provider<Promise<Config>>;
|
|
39
45
|
|
|
40
46
|
/**
|
|
41
47
|
* Callback to enable the caller to create a custom ClientServicesProvider.
|
|
42
48
|
*
|
|
43
|
-
*
|
|
49
|
+
* NOTE: If a `client` is provided then `services` is ignored.
|
|
44
50
|
*/
|
|
45
51
|
services?: ClientServicesProvider | ((config?: Config) => MaybePromise<ClientServicesProvider>);
|
|
46
52
|
|
|
47
53
|
/**
|
|
48
54
|
* List of schema to register.
|
|
55
|
+
*
|
|
56
|
+
* NOTE: If a `client` is provided then `types` will not be added until after the children are first rendered.
|
|
49
57
|
*/
|
|
50
58
|
types?: S.Schema<any>[];
|
|
51
59
|
|
|
@@ -55,10 +63,16 @@ export type ClientProviderProps = Pick<ClientContextProps, 'status'> &
|
|
|
55
63
|
fallback?: FunctionComponent<Partial<ClientContextProps>>;
|
|
56
64
|
|
|
57
65
|
/**
|
|
58
|
-
* Set to false to stop default signal
|
|
66
|
+
* Set to false to stop default signal runtime from being registered.
|
|
67
|
+
*
|
|
68
|
+
* The signals runtime is used to provide reactive updates to ECHO objects.
|
|
69
|
+
*/
|
|
70
|
+
registerSignalRuntime?: boolean;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Skip the DXOS banner print.
|
|
59
74
|
*/
|
|
60
|
-
|
|
61
|
-
registerSignalFactory?: boolean;
|
|
75
|
+
disableBanner?: boolean;
|
|
62
76
|
|
|
63
77
|
/**
|
|
64
78
|
* Post initialization hook to enable to caller to do custom initialization.
|
|
@@ -72,85 +86,111 @@ export type ClientProviderProps = Pick<ClientContextProps, 'status'> &
|
|
|
72
86
|
* Root component that provides the DXOS client instance to child components.
|
|
73
87
|
* To be used with the `useClient` hook.
|
|
74
88
|
*/
|
|
75
|
-
export const ClientProvider = (
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
89
|
+
export const ClientProvider = forwardRef<Client | undefined, ClientProviderProps>(
|
|
90
|
+
(
|
|
91
|
+
{
|
|
92
|
+
children,
|
|
93
|
+
config: configProvider,
|
|
94
|
+
client: clientProvider,
|
|
95
|
+
services: servicesProvider,
|
|
96
|
+
types,
|
|
97
|
+
status: controlledStatus,
|
|
98
|
+
fallback: Fallback = () => null,
|
|
99
|
+
registerSignalRuntime: _registerSignalRuntime = true,
|
|
100
|
+
disableBanner,
|
|
101
|
+
onInitialized,
|
|
102
|
+
...options
|
|
103
|
+
},
|
|
104
|
+
forwardedRef,
|
|
105
|
+
) => {
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
// TODO(wittjosiah): Ideally this should be imported asynchronously because it is optional.
|
|
108
|
+
// Unfortunately, async import seemed to break signals React instrumentation.
|
|
109
|
+
_registerSignalRuntime && registerSignalRuntime();
|
|
110
|
+
}, [_registerSignalRuntime]);
|
|
111
|
+
|
|
112
|
+
// The client is initialized asynchronously.
|
|
113
|
+
// If an error occurs during initialization, it is caught and the state is set.
|
|
114
|
+
// This allows the error to be thrown in the render method.
|
|
115
|
+
// The assumption is that a client initialization error is fatal to the app.
|
|
116
|
+
// This error will be caught by the nearest error boundary.
|
|
117
|
+
const [error, setError] = useState();
|
|
118
|
+
if (error) {
|
|
119
|
+
throw error;
|
|
105
120
|
}
|
|
106
121
|
|
|
107
|
-
const
|
|
108
|
-
return () => subscription.unsubscribe();
|
|
109
|
-
}, [client, setStatus]);
|
|
110
|
-
|
|
111
|
-
// Initialize client.
|
|
112
|
-
useEffect(() => {
|
|
113
|
-
const done = async (client: Client) => {
|
|
114
|
-
await client.initialize().catch(setError);
|
|
115
|
-
log('client ready');
|
|
116
|
-
await onInitialized?.(client);
|
|
117
|
-
log('initialization complete');
|
|
118
|
-
if (types) {
|
|
119
|
-
client.addTypes(types);
|
|
120
|
-
}
|
|
122
|
+
const [client, setClient] = useState(clientProvider instanceof Client ? clientProvider : undefined);
|
|
121
123
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
printBanner(client);
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
const t = setTimeout(async () => {
|
|
128
|
-
if (clientProvider) {
|
|
129
|
-
// Asynchronously request client.
|
|
130
|
-
const client = await getAsyncProviderValue(clientProvider);
|
|
131
|
-
await done(client);
|
|
132
|
-
} else {
|
|
133
|
-
// Asynchronously construct client (config may be undefined).
|
|
134
|
-
const config = await getAsyncProviderValue(configProvider);
|
|
135
|
-
log('resolved config', { config });
|
|
136
|
-
const services = await getAsyncProviderValue(servicesProvider, config);
|
|
137
|
-
log('created services', { services });
|
|
138
|
-
const client = new Client({ config, services, ...options });
|
|
139
|
-
log('created client');
|
|
140
|
-
await done(client);
|
|
141
|
-
}
|
|
142
|
-
});
|
|
124
|
+
// Provide external access.
|
|
125
|
+
useImperativeHandle(forwardedRef, () => client, [client]);
|
|
143
126
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
127
|
+
// Client status subscription.
|
|
128
|
+
const [status, setStatus] = useControlledValue(controlledStatus);
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
if (!client) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
150
133
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
134
|
+
const subscription = client.status.subscribe((status) => setStatus(status));
|
|
135
|
+
return () => subscription.unsubscribe();
|
|
136
|
+
}, [client]);
|
|
137
|
+
|
|
138
|
+
// Create and/or initialize client.
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
const initialize = async (client: Client) => {
|
|
141
|
+
if (!client.initialized) {
|
|
142
|
+
await client.initialize().catch(setError);
|
|
143
|
+
log('client ready');
|
|
144
|
+
if (types) {
|
|
145
|
+
client.addTypes(types);
|
|
146
|
+
}
|
|
147
|
+
await onInitialized?.(client);
|
|
148
|
+
log('initialization complete');
|
|
149
|
+
} else if (types) {
|
|
150
|
+
client.addTypes(types);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
setClient(client);
|
|
154
|
+
|
|
155
|
+
if (!disableBanner) {
|
|
156
|
+
printBanner(client);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
let client: Client;
|
|
161
|
+
const t = setTimeout(async () => {
|
|
162
|
+
if (clientProvider) {
|
|
163
|
+
// Asynchronously request client.
|
|
164
|
+
client = await getAsyncProviderValue(clientProvider);
|
|
165
|
+
await initialize(client);
|
|
166
|
+
} else {
|
|
167
|
+
// Asynchronously construct client (config may be undefined).
|
|
168
|
+
const config = await getAsyncProviderValue(configProvider);
|
|
169
|
+
log('resolved config', { config });
|
|
170
|
+
const services = await getAsyncProviderValue(servicesProvider, config);
|
|
171
|
+
log('created services', { services });
|
|
172
|
+
client = new Client({ config, services, ...options });
|
|
173
|
+
log('created client');
|
|
174
|
+
await initialize(client);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
return () => {
|
|
179
|
+
log('clean up');
|
|
180
|
+
clearTimeout(t);
|
|
181
|
+
void client
|
|
182
|
+
?.destroy()
|
|
183
|
+
.then(() => {
|
|
184
|
+
log('destroyed');
|
|
185
|
+
})
|
|
186
|
+
.catch((err) => log.catch(err));
|
|
187
|
+
};
|
|
188
|
+
}, [configProvider, clientProvider, servicesProvider, disableBanner]);
|
|
189
|
+
|
|
190
|
+
if (!client?.initialized || status !== SystemStatus.ACTIVE) {
|
|
191
|
+
return <Fallback client={client} status={status} />;
|
|
192
|
+
}
|
|
154
193
|
|
|
155
|
-
|
|
156
|
-
}
|
|
194
|
+
return <ClientContext.Provider value={{ client, status }}>{children}</ClientContext.Provider>;
|
|
195
|
+
},
|
|
196
|
+
);
|
package/src/client/context.ts
CHANGED
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
|
|
5
5
|
import { type Context, createContext } from 'react';
|
|
6
6
|
|
|
7
|
-
import { type Client, type
|
|
7
|
+
import { type Client, type SystemStatus } from '@dxos/client';
|
|
8
8
|
|
|
9
9
|
export type ClientContextProps = {
|
|
10
10
|
client: Client;
|
|
11
11
|
|
|
12
|
-
// Optionally expose services (e.g., for devtools).
|
|
13
|
-
services?: ClientServices;
|
|
14
|
-
|
|
15
12
|
status?: SystemStatus | null;
|
|
16
13
|
};
|
|
17
14
|
|
|
15
|
+
/**
|
|
16
|
+
* @internal Use ClientProvider to create or initialize the Client.
|
|
17
|
+
*/
|
|
18
18
|
export const ClientContext: Context<ClientContextProps | undefined> = createContext<ClientContextProps | undefined>(
|
|
19
19
|
undefined,
|
|
20
20
|
);
|
package/src/client/index.ts
CHANGED
|
@@ -4,12 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
export * from './AgentHostingProvider';
|
|
6
6
|
export * from './ClientProvider';
|
|
7
|
-
|
|
8
|
-
// TODO(burdon): Remove.
|
|
9
7
|
export * from './context';
|
|
10
8
|
|
|
11
9
|
export * from './useClient';
|
|
12
|
-
export * from './useClientServices';
|
|
13
10
|
export * from './useClientStatus';
|
|
14
11
|
export * from './useConfig';
|
|
15
12
|
export * from './useShell';
|
|
@@ -3,15 +3,13 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { act, renderHook } from '@testing-library/react';
|
|
6
|
-
import expect from '
|
|
7
|
-
import React from 'react';
|
|
6
|
+
import { describe, expect, test } from 'vitest';
|
|
8
7
|
|
|
9
8
|
import { waitForCondition } from '@dxos/async';
|
|
10
|
-
import {
|
|
11
|
-
import { describe, test } from '@dxos/test';
|
|
9
|
+
import { Config, SystemStatus } from '@dxos/client';
|
|
12
10
|
|
|
13
|
-
import { ClientProvider } from './ClientProvider';
|
|
14
11
|
import { useConfig } from './useConfig';
|
|
12
|
+
import { createClient, createClientContextProvider } from '../testing/util';
|
|
15
13
|
|
|
16
14
|
describe('Config hook', () => {
|
|
17
15
|
const render = () => useConfig();
|
|
@@ -22,9 +20,8 @@ describe('Config hook', () => {
|
|
|
22
20
|
});
|
|
23
21
|
|
|
24
22
|
test('should return default client config when no config is passed in a context', async () => {
|
|
25
|
-
const client =
|
|
26
|
-
await client
|
|
27
|
-
const wrapper = ({ children }: any) => <ClientProvider client={client}>{children}</ClientProvider>;
|
|
23
|
+
const { client } = await createClient();
|
|
24
|
+
const wrapper = await createClientContextProvider(client);
|
|
28
25
|
const { result } = renderHook(render, { wrapper });
|
|
29
26
|
await act(async () => {
|
|
30
27
|
await waitForCondition({ condition: () => client.status.get() === SystemStatus.ACTIVE });
|
|
@@ -43,9 +40,8 @@ describe('Config hook', () => {
|
|
|
43
40
|
},
|
|
44
41
|
},
|
|
45
42
|
});
|
|
46
|
-
const client =
|
|
47
|
-
await client
|
|
48
|
-
const wrapper = ({ children }: any) => <ClientProvider client={client}>{children}</ClientProvider>;
|
|
43
|
+
const { client } = await createClient({ config });
|
|
44
|
+
const wrapper = await createClientContextProvider(client);
|
|
49
45
|
const { result } = renderHook(render, { wrapper });
|
|
50
46
|
await act(async () => {
|
|
51
47
|
await waitForCondition({ condition: () => client.status.get() === SystemStatus.ACTIVE });
|
package/src/echo/useQuery.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { useMemo, useSyncExternalStore } from 'react';
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
|
+
Filter,
|
|
8
9
|
isSpace,
|
|
9
10
|
type Echo,
|
|
10
11
|
type EchoReactiveObject,
|
|
@@ -41,18 +42,14 @@ export const useQuery = <T extends EchoReactiveObject<any>>(
|
|
|
41
42
|
},
|
|
42
43
|
getObjects: () => (subscribed && query ? query.objects : EMPTY_ARRAY),
|
|
43
44
|
};
|
|
44
|
-
}, [spaceOrEcho, ...
|
|
45
|
+
}, [spaceOrEcho, ...filterToDepsArray(filter), ...(deps ?? [])]);
|
|
45
46
|
|
|
46
47
|
// https://beta.reactjs.org/reference/react/useSyncExternalStore
|
|
47
48
|
// NOTE: This hook will resubscribe whenever the callback passed to the first argument changes -- make sure it is stable.
|
|
48
49
|
return useSyncExternalStore<T[] | undefined>(subscribe, getObjects) ?? [];
|
|
49
50
|
};
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
const filterToDepsArray = (filter?: FilterSource<any>) =>
|
|
53
|
-
Object.entries(filter ?? {})
|
|
54
|
-
.flat(10)
|
|
55
|
-
.map((x) => (typeof x === 'function' || typeof x === 'object' ? null : x));
|
|
52
|
+
const filterToDepsArray = (filter?: FilterSource<any>) => [JSON.stringify(Filter.from(filter).toProto())];
|
|
56
53
|
|
|
57
54
|
const noop = () => {};
|
|
58
55
|
|
|
@@ -3,24 +3,22 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { act, renderHook } from '@testing-library/react';
|
|
6
|
-
import { expect } from '
|
|
7
|
-
|
|
8
|
-
import { describe, test } from '@dxos/test';
|
|
6
|
+
import { describe, expect, test } from 'vitest';
|
|
9
7
|
|
|
10
8
|
import { useSpace, useSpaces } from './useSpaces';
|
|
11
|
-
import { createClient,
|
|
9
|
+
import { createClient, createClientContextProvider } from '../testing/util';
|
|
12
10
|
|
|
13
11
|
describe('useSpaces', () => {
|
|
14
12
|
test('lists existing spaces', async () => {
|
|
15
13
|
const { client } = await createClient({ createIdentity: true });
|
|
16
|
-
const wrapper = await
|
|
14
|
+
const wrapper = await createClientContextProvider(client);
|
|
17
15
|
const { result } = renderHook(() => useSpaces(), { wrapper });
|
|
18
16
|
expect(result.current.length).to.eq(1);
|
|
19
17
|
});
|
|
20
18
|
|
|
21
19
|
test('updates when new spaces are created', async () => {
|
|
22
20
|
const { client } = await createClient({ createIdentity: true });
|
|
23
|
-
const wrapper = await
|
|
21
|
+
const wrapper = await createClientContextProvider(client);
|
|
24
22
|
const { result, rerender } = renderHook(() => useSpaces(), { wrapper });
|
|
25
23
|
expect(result.current.length).to.eq(1);
|
|
26
24
|
await act(async () => {
|
|
@@ -34,7 +32,7 @@ describe('useSpaces', () => {
|
|
|
34
32
|
describe('useSpace', () => {
|
|
35
33
|
test('gets default space', async () => {
|
|
36
34
|
const { client } = await createClient();
|
|
37
|
-
const wrapper = await
|
|
35
|
+
const wrapper = await createClientContextProvider(client);
|
|
38
36
|
const { result, rerender } = renderHook(() => useSpace(), { wrapper });
|
|
39
37
|
expect(result.current).to.be.undefined;
|
|
40
38
|
await act(async () => {
|
|
@@ -47,7 +45,7 @@ describe('useSpace', () => {
|
|
|
47
45
|
|
|
48
46
|
test('gets space by key', async () => {
|
|
49
47
|
const { client, space } = await createClient({ createIdentity: true, createSpace: true });
|
|
50
|
-
const wrapper = await
|
|
48
|
+
const wrapper = await createClientContextProvider(client);
|
|
51
49
|
const { result } = renderHook(() => useSpace(space!.key), { wrapper });
|
|
52
50
|
expect(result.current).to.not.be.undefined;
|
|
53
51
|
});
|
|
@@ -3,17 +3,15 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { renderHook } from '@testing-library/react';
|
|
6
|
-
import { expect } from '
|
|
7
|
-
|
|
8
|
-
import { describe, test } from '@dxos/test';
|
|
6
|
+
import { describe, expect, test } from 'vitest';
|
|
9
7
|
|
|
10
8
|
import { useDevices } from './useDevices';
|
|
11
|
-
import { createClient,
|
|
9
|
+
import { createClient, createClientContextProvider } from '../testing/util';
|
|
12
10
|
|
|
13
11
|
describe('useDevices', () => {
|
|
14
12
|
test('lists existing devices', async () => {
|
|
15
13
|
const { client } = await createClient({ createIdentity: true });
|
|
16
|
-
const wrapper = await
|
|
14
|
+
const wrapper = await createClientContextProvider(client);
|
|
17
15
|
const { result } = renderHook(() => useDevices(), { wrapper });
|
|
18
16
|
expect(result.current?.length).to.eq(1);
|
|
19
17
|
});
|
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { nxE2EPreset } from '@nx/playwright/preset';
|
|
6
|
+
import { defineConfig } from '@playwright/test';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
import { e2ePreset } from '@dxos/test-utils/playwright';
|
|
9
|
+
|
|
10
|
+
export default defineConfig({
|
|
11
|
+
...nxE2EPreset(__filename, { testDir: __dirname }),
|
|
12
|
+
...e2ePreset(__dirname),
|
|
13
|
+
webServer: {
|
|
14
|
+
command: 'pnpm -w nx storybook-e2e stories',
|
|
15
|
+
port: 9009,
|
|
16
|
+
reuseExistingServer: !process.env.CI,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
// Copyright 2021 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type Page, test } from '@playwright/test';
|
|
6
|
-
import { expect } from 'chai';
|
|
5
|
+
import { type Page, expect, test } from '@playwright/test';
|
|
7
6
|
|
|
8
|
-
import { setupPage } from '@dxos/test/playwright';
|
|
7
|
+
import { setupPage } from '@dxos/test-utils/playwright';
|
|
9
8
|
|
|
10
9
|
// TODO(wittjosiah): Factor out.
|
|
11
10
|
// TODO(burdon): No hard-coding of ports; reconcile all DXOS tools ports.
|
|
@@ -15,17 +14,13 @@ test.describe('Smoke test', () => {
|
|
|
15
14
|
let page: Page;
|
|
16
15
|
|
|
17
16
|
test.beforeAll(async ({ browser }) => {
|
|
18
|
-
const result = await setupPage(browser, {
|
|
19
|
-
url: storybookUrl('react-client-withclientprovider--default'),
|
|
20
|
-
waitFor: (page) => page.isVisible(':has-text("initialized")'),
|
|
21
|
-
});
|
|
22
|
-
|
|
17
|
+
const result = await setupPage(browser, { url: storybookUrl('react-client-withclientprovider--default') });
|
|
23
18
|
page = result.page;
|
|
19
|
+
await page.locator(':text("initialized")').waitFor({ state: 'visible' });
|
|
24
20
|
});
|
|
25
21
|
|
|
26
22
|
// NOTE: This test depends on connecting to the default production deployed HALO vault.
|
|
27
23
|
test('Renders remote client info', async () => {
|
|
28
|
-
|
|
29
|
-
expect(isVisible).to.be.true;
|
|
24
|
+
await expect(page.locator(':text("initialized")')).toBeVisible();
|
|
30
25
|
});
|
|
31
26
|
});
|
package/src/testing/util.tsx
CHANGED
|
@@ -4,16 +4,18 @@
|
|
|
4
4
|
|
|
5
5
|
import React from 'react';
|
|
6
6
|
|
|
7
|
-
import { Client, fromHost } from '@dxos/client';
|
|
7
|
+
import { Client, type Config, fromHost } from '@dxos/client';
|
|
8
8
|
import { type Space } from '@dxos/client/echo';
|
|
9
9
|
|
|
10
|
-
import { ClientContext } from '../client';
|
|
10
|
+
import { ClientContext, ClientProvider } from '../client';
|
|
11
11
|
|
|
12
|
+
// TODO(burdon): Reconcile with initializeClient.
|
|
12
13
|
export const createClient = async ({
|
|
14
|
+
config,
|
|
13
15
|
createIdentity,
|
|
14
16
|
createSpace,
|
|
15
|
-
}: { createIdentity?: boolean; createSpace?: boolean } = {}) => {
|
|
16
|
-
const client = new Client({ services: fromHost() });
|
|
17
|
+
}: { config?: Config; createIdentity?: boolean; createSpace?: boolean } = {}) => {
|
|
18
|
+
const client = new Client({ config, services: fromHost(config) });
|
|
17
19
|
await client.initialize();
|
|
18
20
|
if (createIdentity) {
|
|
19
21
|
await client.halo.createIdentity();
|
|
@@ -28,7 +30,10 @@ export const createClient = async ({
|
|
|
28
30
|
return { client, space };
|
|
29
31
|
};
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
export const createContextProvider = async (client: Client) => {
|
|
33
|
+
export const createClientContextProvider = async (client: Client) => {
|
|
33
34
|
return ({ children }: any) => <ClientContext.Provider value={{ client }}>{children}</ClientContext.Provider>;
|
|
34
35
|
};
|
|
36
|
+
|
|
37
|
+
export const createClientProvider = async (client: Client) => {
|
|
38
|
+
return ({ children }: any) => <ClientProvider client={client}>{children}</ClientProvider>;
|
|
39
|
+
};
|
|
@@ -6,10 +6,11 @@ import '@dxos-theme';
|
|
|
6
6
|
|
|
7
7
|
import React from 'react';
|
|
8
8
|
|
|
9
|
+
import { log } from '@dxos/log';
|
|
9
10
|
import { SyntaxHighlighter } from '@dxos/react-ui-syntax-highlighter';
|
|
10
11
|
import { withLayout, withTheme } from '@dxos/storybook-utils';
|
|
11
12
|
|
|
12
|
-
import { withClientProvider, withMultiClientProvider } from './withClientProvider';
|
|
13
|
+
import { withClientProvider, type WithClientProviderProps, withMultiClientProvider } from './withClientProvider';
|
|
13
14
|
import { useClient } from '../client';
|
|
14
15
|
import { type Space, useSpaces } from '../echo';
|
|
15
16
|
|
|
@@ -37,20 +38,28 @@ const Test = () => {
|
|
|
37
38
|
|
|
38
39
|
export default {
|
|
39
40
|
title: 'react-client/withClientProvider',
|
|
40
|
-
|
|
41
|
-
decorators: [withTheme, withClientProvider({ createIdentity: true })],
|
|
41
|
+
render: Test,
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
const clientProps: WithClientProviderProps = {
|
|
45
|
+
createIdentity: true,
|
|
46
|
+
createSpace: true,
|
|
47
|
+
onInitialized: async (client) => {
|
|
48
|
+
log.info('onInitialized', { client });
|
|
49
|
+
},
|
|
50
|
+
onSpaceCreated: async ({ client, space }) => {
|
|
51
|
+
log.info('onSpaceCreated', { client, space });
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const Default = {
|
|
56
|
+
decorators: [withClientProvider(clientProps), withTheme, withLayout({ classNames: ['flex gap-2'] })],
|
|
57
|
+
};
|
|
45
58
|
|
|
46
59
|
export const Multiple = {
|
|
47
60
|
decorators: [
|
|
48
|
-
withMultiClientProvider({
|
|
49
|
-
numClients: 3,
|
|
50
|
-
createIdentity: true,
|
|
51
|
-
createSpace: true,
|
|
52
|
-
}),
|
|
53
|
-
withLayout({ classNames: ['flex gap-2'] }),
|
|
61
|
+
withMultiClientProvider({ ...clientProps, numClients: 3 }),
|
|
54
62
|
withTheme,
|
|
63
|
+
withLayout({ classNames: ['flex gap-2'] }),
|
|
55
64
|
],
|
|
56
65
|
};
|