@dxos/react-client 0.6.12 → 0.6.13-main.09887cd

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.
Files changed (85) hide show
  1. package/dist/lib/browser/{chunk-CL7JDETM.mjs → chunk-RJRDB4LF.mjs} +62 -60
  2. package/dist/lib/browser/chunk-RJRDB4LF.mjs.map +7 -0
  3. package/dist/lib/browser/devtools/index.mjs +1 -1
  4. package/dist/lib/browser/echo/index.mjs +6 -4
  5. package/dist/lib/browser/echo/index.mjs.map +3 -3
  6. package/dist/lib/browser/halo/index.mjs +1 -1
  7. package/dist/lib/browser/index.mjs +1 -3
  8. package/dist/lib/browser/index.mjs.map +1 -1
  9. package/dist/lib/browser/mesh/index.mjs +1 -1
  10. package/dist/lib/browser/meta.json +1 -1
  11. package/dist/lib/browser/testing/index.mjs +43 -32
  12. package/dist/lib/browser/testing/index.mjs.map +3 -3
  13. package/dist/lib/node/{chunk-C4EWAKPZ.cjs → chunk-YNTACPMY.cjs} +63 -60
  14. package/dist/lib/node/chunk-YNTACPMY.cjs.map +7 -0
  15. package/dist/lib/node/devtools/index.cjs +2 -2
  16. package/dist/lib/node/echo/index.cjs +7 -5
  17. package/dist/lib/node/echo/index.cjs.map +3 -3
  18. package/dist/lib/node/halo/index.cjs +5 -5
  19. package/dist/lib/node/index.cjs +12 -14
  20. package/dist/lib/node/index.cjs.map +1 -1
  21. package/dist/lib/node/mesh/index.cjs +2 -2
  22. package/dist/lib/node/meta.json +1 -1
  23. package/dist/lib/node/testing/index.cjs +44 -33
  24. package/dist/lib/node/testing/index.cjs.map +3 -3
  25. package/dist/lib/node-esm/chunk-UYJUBYMU.mjs +165 -0
  26. package/dist/lib/node-esm/chunk-UYJUBYMU.mjs.map +7 -0
  27. package/dist/lib/node-esm/chunk-V65AVUQO.mjs +295 -0
  28. package/dist/lib/node-esm/chunk-V65AVUQO.mjs.map +7 -0
  29. package/dist/lib/node-esm/devtools/index.mjs +44 -0
  30. package/dist/lib/node-esm/devtools/index.mjs.map +7 -0
  31. package/dist/lib/node-esm/echo/index.mjs +134 -0
  32. package/dist/lib/node-esm/echo/index.mjs.map +7 -0
  33. package/dist/lib/node-esm/halo/index.mjs +100 -0
  34. package/dist/lib/node-esm/halo/index.mjs.map +7 -0
  35. package/dist/lib/node-esm/index.mjs +33 -0
  36. package/dist/lib/node-esm/index.mjs.map +7 -0
  37. package/dist/lib/node-esm/invitations/index.mjs +18 -0
  38. package/dist/lib/node-esm/invitations/index.mjs.map +7 -0
  39. package/dist/lib/node-esm/mesh/index.mjs +18 -0
  40. package/dist/lib/node-esm/mesh/index.mjs.map +7 -0
  41. package/dist/lib/node-esm/meta.json +1 -0
  42. package/dist/lib/node-esm/testing/index.mjs +177 -0
  43. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  44. package/dist/lib/node-esm/worker.mjs +5 -0
  45. package/dist/lib/node-esm/worker.mjs.map +7 -0
  46. package/dist/types/src/client/ClientProvider.d.ts +59 -18
  47. package/dist/types/src/client/ClientProvider.d.ts.map +1 -1
  48. package/dist/types/src/client/context.d.ts +1 -4
  49. package/dist/types/src/client/context.d.ts.map +1 -1
  50. package/dist/types/src/client/index.d.ts +0 -1
  51. package/dist/types/src/client/index.d.ts.map +1 -1
  52. package/dist/types/src/echo/useQuery.d.ts.map +1 -1
  53. package/dist/types/src/playwright/playwright.config.d.ts +2 -2
  54. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
  55. package/dist/types/src/testing/ClientRepeater.d.ts +9 -12
  56. package/dist/types/src/testing/ClientRepeater.d.ts.map +1 -1
  57. package/dist/types/src/testing/util.d.ts +5 -3
  58. package/dist/types/src/testing/util.d.ts.map +1 -1
  59. package/dist/types/src/testing/withClientProvider.d.ts +4 -1
  60. package/dist/types/src/testing/withClientProvider.d.ts.map +1 -1
  61. package/dist/types/src/testing/withClientProvider.stories.d.ts +4 -3
  62. package/dist/types/src/testing/withClientProvider.stories.d.ts.map +1 -1
  63. package/package.json +47 -38
  64. package/project.json +3 -22
  65. package/src/client/ClientProvider.test.tsx +7 -4
  66. package/src/client/ClientProvider.tsx +126 -100
  67. package/src/client/context.ts +4 -5
  68. package/src/client/index.ts +0 -3
  69. package/src/client/useConfig.test.tsx +7 -11
  70. package/src/echo/useQuery.ts +3 -6
  71. package/src/echo/useSpaces.test.tsx +6 -8
  72. package/src/halo/useDevices.test.tsx +3 -5
  73. package/src/playwright/playwright.config.ts +13 -2
  74. package/src/playwright/smoke.spec.ts +5 -10
  75. package/src/testing/ClientRepeater.tsx +19 -20
  76. package/src/testing/util.tsx +11 -6
  77. package/src/testing/withClientProvider.stories.tsx +19 -10
  78. package/src/testing/withClientProvider.tsx +22 -18
  79. package/tsconfig.json +2 -58
  80. package/vitest.config.ts +22 -0
  81. package/dist/lib/browser/chunk-CL7JDETM.mjs.map +0 -7
  82. package/dist/lib/node/chunk-C4EWAKPZ.cjs.map +0 -7
  83. package/dist/types/src/client/useClientServices.d.ts +0 -3
  84. package/dist/types/src/client/useClientServices.d.ts.map +0 -1
  85. package/src/client/useClientServices.ts +0 -15
@@ -2,12 +2,18 @@
2
2
  // Copyright 2020 DXOS.org
3
3
  //
4
4
 
5
- import React, { type FunctionComponent, type ReactNode, useEffect, useMemo, useState } from 'react';
5
+ import React, {
6
+ type FunctionComponent,
7
+ type PropsWithChildren,
8
+ forwardRef,
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
- import { type S } from '@dxos/echo-schema';
10
- import { registerSignalFactory } from '@dxos/echo-signals/react';
16
+ import { registerSignalsRuntime } from '@dxos/echo-signals/react';
11
17
  import { log } from '@dxos/log';
12
18
  import { useControlledValue } from '@dxos/react-hooks';
13
19
  import { getAsyncProviderValue, type MaybePromise, type Provider } from '@dxos/util';
@@ -18,47 +24,47 @@ import { printBanner } from '../banner';
18
24
  /**
19
25
  * Properties for the ClientProvider.
20
26
  */
21
- export type ClientProviderProps = Pick<ClientContextProps, 'status'> &
22
- Omit<ClientOptions, 'config' | 'services'> & {
23
- children?: ReactNode;
24
-
27
+ export type ClientProviderProps = Omit<ClientOptions, 'config' | 'services'> &
28
+ Pick<ClientContextProps, 'status'> &
29
+ PropsWithChildren<{
25
30
  /**
26
- * Config object or async provider.
31
+ * Client object or async provider to enable to caller to do custom initialization.
32
+ *
33
+ * NOTE: For advanced use cases only.
27
34
  */
28
- config?: Config | Provider<Promise<Config>>;
35
+ client?: Client | Provider<Promise<Client>>;
29
36
 
30
37
  /**
31
- * Client object or async provider to enable to caller to do custom initialization.
38
+ * Config object or async provider.
32
39
  *
33
- * Most apps won't need this.
40
+ * NOTE: If a `client` is provided then `config` is ignored.
34
41
  */
35
- // TODO(wittjosiah): Remove async and just use to keep reference to client?
36
- // (Preferring `onInitialized` for custom initialization.)
37
- // TODO(burdon): Use useImperativeHandle to expose client.
38
- client?: Client | Provider<Promise<Client>>;
42
+ // TODO(burdon): Reconcile with ClientOptions.
43
+ config?: Config | Provider<Promise<Config>>;
39
44
 
40
45
  /**
41
46
  * Callback to enable the caller to create a custom ClientServicesProvider.
42
47
  *
43
- * Most apps won't need this.
48
+ * NOTE: If a `client` is provided then `services` is ignored.
44
49
  */
50
+ // TODO(burdon): Reconcile with ClientOptions.
45
51
  services?: ClientServicesProvider | ((config?: Config) => MaybePromise<ClientServicesProvider>);
46
52
 
47
53
  /**
48
- * List of schema to register.
54
+ * ReactNode to display until the client is available.
49
55
  */
50
- types?: S.Schema<any>[];
56
+ fallback?: FunctionComponent<Partial<ClientContextProps>>;
51
57
 
52
58
  /**
53
- * ReactNode to display until the client is available.
59
+ * Enable (by default) registration of Preact signals runtime for reactive ECHO objects.
60
+ * @see https://www.npmjs.com/package/@preact/signals-react
54
61
  */
55
- fallback?: FunctionComponent<Partial<ClientContextProps>>;
62
+ signalsRuntime?: boolean;
56
63
 
57
64
  /**
58
- * Set to false to stop default signal factory from being registered.
65
+ * Skip the DXOS banner.
59
66
  */
60
- // TODO(burdon): Move to options.
61
- registerSignalFactory?: boolean;
67
+ noBanner?: boolean;
62
68
 
63
69
  /**
64
70
  * Post initialization hook to enable to caller to do custom initialization.
@@ -66,91 +72,111 @@ export type ClientProviderProps = Pick<ClientContextProps, 'status'> &
66
72
  * @param Client
67
73
  */
68
74
  onInitialized?: (client: Client) => MaybePromise<void>;
69
- };
75
+ }>;
70
76
 
71
77
  /**
72
78
  * Root component that provides the DXOS client instance to child components.
73
79
  * To be used with the `useClient` hook.
74
80
  */
75
- export const ClientProvider = ({
76
- children,
77
- config: configProvider,
78
- client: clientProvider,
79
- services: servicesProvider,
80
- types,
81
- status: controlledStatus,
82
- fallback: Fallback = () => null,
83
- registerSignalFactory: _registerSignalFactory = true,
84
- onInitialized,
85
- ...options
86
- }: ClientProviderProps) => {
87
- useMemo(() => {
88
- // TODO(wittjosiah): Ideally this should be imported asynchronously because it is optional.
89
- // Unfortunately, async import seemed to break signals React instrumentation.
90
- _registerSignalFactory && registerSignalFactory();
91
- }, []);
92
-
93
- const [client, setClient] = useState(clientProvider instanceof Client ? clientProvider : undefined);
94
-
95
- const [error, setError] = useState();
96
- if (error) {
97
- throw error;
98
- }
99
-
100
- // Status subscription.
101
- const [status, setStatus] = useControlledValue(controlledStatus);
102
- useEffect(() => {
103
- if (!client) {
104
- return;
81
+ export const ClientProvider = forwardRef<Client | undefined, ClientProviderProps>(
82
+ (
83
+ {
84
+ children,
85
+ config: configProvider,
86
+ client: clientProvider,
87
+ services: servicesProvider,
88
+ status: controlledStatus,
89
+ fallback: Fallback = () => null,
90
+ signalsRuntime = true,
91
+ noBanner,
92
+ onInitialized,
93
+ ...options
94
+ },
95
+ forwardedRef,
96
+ ) => {
97
+ useEffect(() => {
98
+ // TODO(wittjosiah): Ideally this should be imported asynchronously because it is optional.
99
+ // Unfortunately, async import seemed to break signals React instrumentation.
100
+ signalsRuntime && registerSignalsRuntime();
101
+ }, []);
102
+
103
+ // The client is initialized asynchronously.
104
+ // If an error occurs during initialization, it is caught and the state is set.
105
+ // This allows the error to be thrown in the render method.
106
+ // The assumption is that a client initialization error is fatal to the app.
107
+ // This error will be caught by the nearest error boundary.
108
+ const [error, setError] = useState();
109
+ if (error) {
110
+ throw error;
105
111
  }
106
112
 
107
- const subscription = client.status.subscribe((status) => setStatus(status));
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
- }
113
+ const [client, setClient] = useState(clientProvider instanceof Client ? clientProvider : undefined);
121
114
 
122
- setClient(client);
123
- setStatus(client.status.get() ?? SystemStatus.ACTIVE);
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
- });
115
+ // Provide external access.
116
+ useImperativeHandle(forwardedRef, () => client, [client]);
143
117
 
144
- return () => {
145
- log('clean up');
146
- clearTimeout(t);
147
- void client?.destroy().catch((err) => log.catch(err));
148
- };
149
- }, [clientProvider, configProvider, servicesProvider]);
118
+ // Client status subscription.
119
+ const [status, setStatus] = useControlledValue(controlledStatus);
120
+ useEffect(() => {
121
+ if (!client) {
122
+ return;
123
+ }
150
124
 
151
- if (!client || status !== SystemStatus.ACTIVE) {
152
- return <Fallback client={client} status={status} />;
153
- }
125
+ const subscription = client.status.subscribe((status) => setStatus(status));
126
+ return () => subscription.unsubscribe();
127
+ }, [client]);
128
+
129
+ // Create and/or initialize client.
130
+ useEffect(() => {
131
+ const initialize = async (client: Client) => {
132
+ if (!client.initialized) {
133
+ await client.initialize().catch(setError);
134
+ log('client ready');
135
+ await onInitialized?.(client);
136
+ log('initialization complete');
137
+ }
138
+
139
+ setClient(client);
140
+
141
+ if (!noBanner) {
142
+ printBanner(client);
143
+ }
144
+ };
145
+
146
+ let client: Client;
147
+ const t = setTimeout(async () => {
148
+ if (clientProvider) {
149
+ // Asynchronously request client.
150
+ client = await getAsyncProviderValue(clientProvider);
151
+ await initialize(client);
152
+ } else {
153
+ // Asynchronously construct client (config may be undefined).
154
+ const config = await getAsyncProviderValue(configProvider);
155
+ log('resolved config', { config });
156
+ const services = await getAsyncProviderValue(servicesProvider, config);
157
+ log('created services', { services });
158
+ client = new Client({ config, services, ...options });
159
+ log('created client');
160
+ await initialize(client);
161
+ }
162
+ });
163
+
164
+ return () => {
165
+ log('clean up');
166
+ clearTimeout(t);
167
+ void client
168
+ ?.destroy()
169
+ .then(() => {
170
+ log('destroyed');
171
+ })
172
+ .catch((err) => log.catch(err));
173
+ };
174
+ }, [configProvider, clientProvider, servicesProvider, noBanner]);
175
+
176
+ if (!client?.initialized || status !== SystemStatus.ACTIVE) {
177
+ return <Fallback client={client} status={status} />;
178
+ }
154
179
 
155
- return <ClientContext.Provider value={{ client, status }}>{children}</ClientContext.Provider>;
156
- };
180
+ return <ClientContext.Provider value={{ client, status }}>{children}</ClientContext.Provider>;
181
+ },
182
+ );
@@ -4,17 +4,16 @@
4
4
 
5
5
  import { type Context, createContext } from 'react';
6
6
 
7
- import { type Client, type ClientServices, type SystemStatus } from '@dxos/client';
7
+ import { type Client, type SystemStatus } from '@dxos/client';
8
8
 
9
9
  export type ClientContextProps = {
10
10
  client: Client;
11
-
12
- // Optionally expose services (e.g., for devtools).
13
- services?: ClientServices;
14
-
15
11
  status?: SystemStatus | null;
16
12
  };
17
13
 
14
+ /**
15
+ * @internal Use ClientProvider to create or initialize the Client.
16
+ */
18
17
  export const ClientContext: Context<ClientContextProps | undefined> = createContext<ClientContextProps | undefined>(
19
18
  undefined,
20
19
  );
@@ -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 'expect';
7
- import React from 'react';
6
+ import { describe, expect, test } from 'vitest';
8
7
 
9
8
  import { waitForCondition } from '@dxos/async';
10
- import { Client, Config, SystemStatus, fromHost } from '@dxos/client';
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 = new Client({ services: fromHost() });
26
- await client.initialize();
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 = new Client({ config, services: fromHost(config) });
47
- await client.initialize();
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 });
@@ -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, ...(typeof filter === 'function' ? [] : filterToDepsArray(filter)), ...(deps ?? [])]);
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
- // TODO(dmaretskyi): Serialize the filter here instead.
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 'chai';
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, createContextProvider } from '../testing/util';
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 createContextProvider(client);
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 createContextProvider(client);
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 createContextProvider(client);
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 createContextProvider(client);
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 'chai';
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, createContextProvider } from '../testing/util';
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 createContextProvider(client);
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 { defaultPlaywrightConfig } from '@dxos/test/playwright';
5
+ import { nxE2EPreset } from '@nx/playwright/preset';
6
+ import { defineConfig } from '@playwright/test';
6
7
 
7
- export default defaultPlaywrightConfig;
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
- const isVisible = await page.isVisible(':has-text("initialized")');
29
- expect(isVisible).to.be.true;
24
+ await expect(page.locator(':text("initialized")')).toBeVisible();
30
25
  });
31
26
  });
@@ -2,37 +2,36 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
+ import { type Schema as S } from '@effect/schema';
5
6
  import React, { useState, type FC, useEffect, useRef } from 'react';
6
7
 
7
8
  import { Client, type PublicKey } from '@dxos/client';
8
- import { type Space } from '@dxos/client/echo';
9
9
  import { TestBuilder, performInvitation } from '@dxos/client/testing';
10
- import { type S } from '@dxos/echo-schema';
11
- import { registerSignalFactory } from '@dxos/echo-signals/react';
10
+ import { registerSignalsRuntime } from '@dxos/echo-signals/react';
12
11
  import { faker } from '@dxos/random';
13
- import { type MaybePromise } from '@dxos/util';
14
12
 
13
+ import { type WithClientProviderProps } from './withClientProvider';
15
14
  import { ClientProvider } from '../client';
16
15
 
17
16
  export type ClientRepeatedComponentProps = { id: number; count: number; spaceKey?: PublicKey };
18
17
 
18
+ export type ClientRepeaterControlsProps = { clients: Client[] };
19
+
20
+ // TODO(burdon): Reconcile with ClientProviderProps.
19
21
  export type ClientRepeaterProps<P extends ClientRepeatedComponentProps> = {
20
22
  className?: string;
21
23
  component: FC<ClientRepeatedComponentProps>;
22
- controls?: FC<{ clients: Client[] }>;
23
- clients?: Client[];
24
+ controls?: FC<ClientRepeaterControlsProps>;
24
25
  count?: number;
25
- registerSignalFactory?: boolean;
26
+ clients?: Client[];
26
27
  types?: S.Schema<any>[];
27
- createIdentity?: boolean;
28
- createSpace?: boolean;
29
- onCreateSpace?: (space: Space) => MaybePromise<void>;
30
28
  args?: Omit<P, 'id' | 'count'>;
31
- };
29
+ } & Pick<WithClientProviderProps, 'createIdentity' | 'createSpace' | 'onSpaceCreated'>;
32
30
 
33
31
  /**
34
32
  * Utility component for Storybook stories which sets up clients for n peers.
35
33
  * The `Component` property is rendered n times, once for each peer.
34
+ * @deprecated use `withClientProvider`.
36
35
  */
37
36
  // TODO(burdon): To discuss: evolve ClientRepeater with optional decorator that uses it.
38
37
  // NOTE: This is specifically not a storybook decorator because it broke stories as a decorator.
@@ -47,11 +46,11 @@ export const ClientRepeater = <P extends ClientRepeatedComponentProps>(props: Cl
47
46
  types,
48
47
  createIdentity,
49
48
  createSpace,
50
- onCreateSpace,
49
+ onSpaceCreated,
51
50
  } = props;
52
- if (props.registerSignalFactory ?? true) {
53
- registerSignalFactory();
54
- }
51
+ useEffect(() => {
52
+ registerSignalsRuntime();
53
+ }, []);
55
54
 
56
55
  const [clients, setClients] = useState(props.clients ?? []);
57
56
  const [spaceKey, setSpaceKey] = useState<PublicKey>();
@@ -60,19 +59,19 @@ export const ClientRepeater = <P extends ClientRepeatedComponentProps>(props: Cl
60
59
  useEffect(() => {
61
60
  const timeout = setTimeout(async () => {
62
61
  const clients = [...Array(count)].map(
63
- (_) => new Client({ services: testBuilder.current.createLocalClientServices() }),
62
+ (_) => new Client({ services: testBuilder.current.createLocalClientServices(), types }),
64
63
  );
65
- await Promise.all(clients.map((client) => client.initialize()));
66
- types && clients.map((client) => client.addTypes(types));
67
64
 
65
+ await Promise.all(clients.map((client) => client.initialize()));
68
66
  if (createIdentity || createSpace) {
69
67
  await Promise.all(clients.map((client) => client.halo.createIdentity()));
70
68
  }
71
69
 
72
70
  if (createSpace) {
73
- const space = await clients[0].spaces.create({ name: faker.commerce.productName() });
71
+ const client = clients[0];
72
+ const space = await client.spaces.create({ name: faker.commerce.productName() });
74
73
  setSpaceKey(space.key);
75
- await onCreateSpace?.(space);
74
+ await onSpaceCreated?.({ client, space });
76
75
  await Promise.all(
77
76
  clients.slice(1).flatMap((client) => performInvitation({ host: space, guest: client.spaces })),
78
77
  );
@@ -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
- // TODO(burdon): Change to ClientProvider.
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
+ };