@dxos/react-client 0.6.13 → 0.6.14-main.1366248

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 (90) hide show
  1. package/dist/lib/browser/{chunk-CL7JDETM.mjs → chunk-U6WOCCWH.mjs} +62 -60
  2. package/dist/lib/browser/chunk-U6WOCCWH.mjs.map +7 -0
  3. package/dist/lib/browser/devtools/index.mjs +1 -1
  4. package/dist/lib/browser/echo/index.mjs +7 -5
  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 +98 -37
  12. package/dist/lib/browser/testing/index.mjs.map +3 -3
  13. package/dist/lib/node/{chunk-C4EWAKPZ.cjs → chunk-BYDG3FH4.cjs} +63 -60
  14. package/dist/lib/node/chunk-BYDG3FH4.cjs.map +7 -0
  15. package/dist/lib/node/devtools/index.cjs +2 -2
  16. package/dist/lib/node/echo/index.cjs +8 -6
  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 +98 -36
  24. package/dist/lib/node/testing/index.cjs.map +3 -3
  25. package/dist/lib/node-esm/chunk-E5UAY5Z4.mjs +295 -0
  26. package/dist/lib/node-esm/chunk-E5UAY5Z4.mjs.map +7 -0
  27. package/dist/lib/node-esm/chunk-UYJUBYMU.mjs +165 -0
  28. package/dist/lib/node-esm/chunk-UYJUBYMU.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 +227 -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/AgentHostingProvider.d.ts +1 -1
  47. package/dist/types/src/client/AgentHostingProvider.d.ts.map +1 -1
  48. package/dist/types/src/client/ClientProvider.d.ts +57 -20
  49. package/dist/types/src/client/ClientProvider.d.ts.map +1 -1
  50. package/dist/types/src/client/context.d.ts +1 -4
  51. package/dist/types/src/client/context.d.ts.map +1 -1
  52. package/dist/types/src/client/index.d.ts +0 -1
  53. package/dist/types/src/client/index.d.ts.map +1 -1
  54. package/dist/types/src/echo/useQuery.d.ts.map +1 -1
  55. package/dist/types/src/playwright/playwright.config.d.ts +2 -2
  56. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
  57. package/dist/types/src/testing/ClientRepeater.d.ts +9 -12
  58. package/dist/types/src/testing/ClientRepeater.d.ts.map +1 -1
  59. package/dist/types/src/testing/util.d.ts +5 -3
  60. package/dist/types/src/testing/util.d.ts.map +1 -1
  61. package/dist/types/src/testing/withClientProvider.d.ts +7 -1
  62. package/dist/types/src/testing/withClientProvider.d.ts.map +1 -1
  63. package/dist/types/src/testing/withClientProvider.stories.d.ts +4 -3
  64. package/dist/types/src/testing/withClientProvider.stories.d.ts.map +1 -1
  65. package/package.json +45 -62
  66. package/project.json +3 -22
  67. package/src/client/AgentHostingProvider.tsx +1 -1
  68. package/src/client/ClientProvider.test.tsx +7 -4
  69. package/src/client/ClientProvider.tsx +126 -102
  70. package/src/client/context.ts +4 -5
  71. package/src/client/index.ts +0 -3
  72. package/src/client/useConfig.test.tsx +10 -12
  73. package/src/echo/useQuery.ts +3 -6
  74. package/src/echo/useSpaces.test.tsx +7 -9
  75. package/src/echo/useSpaces.ts +1 -1
  76. package/src/halo/useDevices.test.tsx +3 -5
  77. package/src/playwright/playwright.config.ts +13 -2
  78. package/src/playwright/smoke.spec.ts +5 -10
  79. package/src/testing/ClientRepeater.stories.tsx +1 -1
  80. package/src/testing/ClientRepeater.tsx +19 -20
  81. package/src/testing/util.tsx +12 -7
  82. package/src/testing/withClientProvider.stories.tsx +20 -11
  83. package/src/testing/withClientProvider.tsx +55 -24
  84. package/tsconfig.json +3 -59
  85. package/vitest.config.ts +22 -0
  86. package/dist/lib/browser/chunk-CL7JDETM.mjs.map +0 -7
  87. package/dist/lib/node/chunk-C4EWAKPZ.cjs.map +0 -7
  88. package/dist/types/src/client/useClientServices.d.ts +0 -3
  89. package/dist/types/src/client/useClientServices.d.ts.map +0 -1
  90. package/src/client/useClientServices.ts +0 -15
package/package.json CHANGED
@@ -1,67 +1,52 @@
1
1
  {
2
2
  "name": "@dxos/react-client",
3
- "version": "0.6.13",
3
+ "version": "0.6.14-main.1366248",
4
4
  "description": "React client API",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
7
  "license": "MIT",
8
8
  "author": "DXOS.org",
9
+ "sideEffects": true,
9
10
  "exports": {
10
- ".": {
11
- "browser": "./dist/lib/browser/index.mjs",
12
- "node": {
13
- "default": "./dist/lib/node/index.cjs"
14
- },
15
- "types": "./dist/types/src/index.d.ts"
16
- },
17
11
  "./devtools": {
12
+ "types": "./dist/types/src/devtools/index.d.ts",
18
13
  "browser": "./dist/lib/browser/devtools/index.mjs",
19
- "node": {
20
- "default": "./dist/lib/node/devtools/index.cjs"
21
- },
22
- "types": "./dist/types/src/devtools/index.d.ts"
14
+ "node": "./dist/lib/node-esm/devtools/index.mjs"
23
15
  },
24
16
  "./echo": {
17
+ "types": "./dist/types/src/echo/index.d.ts",
25
18
  "browser": "./dist/lib/browser/echo/index.mjs",
26
- "node": {
27
- "default": "./dist/lib/node/echo/index.cjs"
28
- },
29
- "types": "./dist/types/src/echo/index.d.ts"
19
+ "node": "./dist/lib/node-esm/echo/index.mjs"
30
20
  },
31
21
  "./halo": {
22
+ "types": "./dist/types/src/halo/index.d.ts",
32
23
  "browser": "./dist/lib/browser/halo/index.mjs",
33
- "node": {
34
- "default": "./dist/lib/node/halo/index.cjs"
35
- },
36
- "types": "./dist/types/src/halo/index.d.ts"
24
+ "node": "./dist/lib/node-esm/halo/index.mjs"
25
+ },
26
+ ".": {
27
+ "types": "./dist/types/src/index.d.ts",
28
+ "browser": "./dist/lib/browser/index.mjs",
29
+ "node": "./dist/lib/node-esm/index.mjs"
37
30
  },
38
31
  "./invitations": {
32
+ "types": "./dist/types/src/invitations/index.d.ts",
39
33
  "browser": "./dist/lib/browser/invitations/index.mjs",
40
- "node": {
41
- "default": "./dist/lib/node/invitations/index.cjs"
42
- },
43
- "types": "./dist/types/src/invitations/index.d.ts"
34
+ "node": "./dist/lib/node-esm/invitations/index.mjs"
44
35
  },
45
36
  "./mesh": {
37
+ "types": "./dist/types/src/mesh/index.d.ts",
46
38
  "browser": "./dist/lib/browser/mesh/index.mjs",
47
- "node": {
48
- "default": "./dist/lib/node/mesh/index.cjs"
49
- },
50
- "types": "./dist/types/src/mesh/index.d.ts"
39
+ "node": "./dist/lib/node-esm/mesh/index.mjs"
51
40
  },
52
41
  "./testing": {
42
+ "types": "./dist/types/src/testing/index.d.ts",
53
43
  "browser": "./dist/lib/browser/testing/index.mjs",
54
- "node": {
55
- "default": "./dist/lib/node/testing/index.cjs"
56
- },
57
- "types": "./dist/types/src/testing/index.d.ts"
44
+ "node": "./dist/lib/node-esm/testing/index.mjs"
58
45
  },
59
46
  "./worker": {
47
+ "types": "./dist/types/src/worker.d.ts",
60
48
  "browser": "./dist/lib/browser/worker.mjs",
61
- "node": {
62
- "default": "./dist/lib/node/worker.cjs"
63
- },
64
- "types": "./dist/types/src/worker.d.ts"
49
+ "node": "./dist/lib/node-esm/worker.mjs"
65
50
  }
66
51
  },
67
52
  "types": "dist/types/src/index.d.ts",
@@ -92,42 +77,40 @@
92
77
  },
93
78
  "dependencies": {
94
79
  "react-error-boundary": "^4.0.13",
95
- "@dxos/async": "0.6.13",
96
- "@dxos/codec-protobuf": "0.6.13",
97
- "@dxos/debug": "0.6.13",
98
- "@dxos/config": "0.6.13",
99
- "@dxos/client": "0.6.13",
100
- "@dxos/echo-schema": "0.6.13",
101
- "@dxos/echo-signals": "0.6.13",
102
- "@dxos/invariant": "0.6.13",
103
- "@dxos/protocols": "0.6.13",
104
- "@dxos/node-std": "0.6.13",
105
- "@dxos/log": "0.6.13",
106
- "@dxos/react-hooks": "0.6.13",
107
- "@dxos/util": "0.6.13"
80
+ "@dxos/async": "0.6.14-main.1366248",
81
+ "@dxos/client": "0.6.14-main.1366248",
82
+ "@dxos/codec-protobuf": "0.6.14-main.1366248",
83
+ "@dxos/config": "0.6.14-main.1366248",
84
+ "@dxos/echo-schema": "0.6.14-main.1366248",
85
+ "@dxos/debug": "0.6.14-main.1366248",
86
+ "@dxos/echo-signals": "0.6.14-main.1366248",
87
+ "@dxos/invariant": "0.6.14-main.1366248",
88
+ "@dxos/log": "0.6.14-main.1366248",
89
+ "@dxos/node-std": "0.6.14-main.1366248",
90
+ "@dxos/react-hooks": "0.6.14-main.1366248",
91
+ "@dxos/protocols": "0.6.14-main.1366248",
92
+ "@dxos/util": "0.6.14-main.1366248"
108
93
  },
109
94
  "devDependencies": {
110
- "@testing-library/dom": "^8.17.1",
111
- "@testing-library/react": "^13.4.0",
112
- "@testing-library/user-event": "^14.4.3",
113
95
  "@types/react": "~18.2.0",
114
96
  "@types/react-dom": "~18.2.0",
115
97
  "react": "~18.2.0",
116
98
  "react-dom": "~18.2.0",
117
- "@dxos/react-ui-syntax-highlighter": "0.6.13",
118
- "@dxos/react-ui": "0.6.13",
119
- "@dxos/random": "0.6.13",
120
- "@dxos/react-ui-theme": "0.6.13",
121
- "@dxos/storybook-utils": "0.6.13"
99
+ "@dxos/react-ui": "0.6.14-main.1366248",
100
+ "@dxos/random": "0.6.14-main.1366248",
101
+ "@dxos/react-ui-syntax-highlighter": "0.6.14-main.1366248",
102
+ "@dxos/react-ui-theme": "0.6.14-main.1366248",
103
+ "@dxos/storybook-utils": "0.6.14-main.1366248",
104
+ "@dxos/test-utils": "0.6.14-main.1366248"
122
105
  },
123
106
  "peerDependencies": {
124
- "react": "^18.0.0",
125
- "react-dom": "^18.0.0",
126
- "@dxos/react-ui": "0.6.13",
127
- "@dxos/react-ui-theme": "0.6.13"
107
+ "react": "~18.2.0",
108
+ "react-dom": "~18.2.0",
109
+ "@dxos/react-ui": "0.6.14-main.1366248",
110
+ "@dxos/react-ui-theme": "0.6.14-main.1366248"
128
111
  },
129
112
  "optionalDependencies": {
130
- "@dxos/random": "0.6.13"
113
+ "@dxos/random": "0.6.14-main.1366248"
131
114
  },
132
115
  "publishConfig": {
133
116
  "access": "public"
package/project.json CHANGED
@@ -26,32 +26,13 @@
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
35
  "esbuild",
55
- "test"
36
+ "node-std"
56
37
  ]
57
38
  }
@@ -21,7 +21,7 @@ export const AgentHostingContext = createContext<AgentHostingProviderClient | nu
21
21
  * @constructor
22
22
  * @deprecated
23
23
  */
24
- export const AgentHostingProvider = (props: PropsWithChildren<{}>) => {
24
+ export const AgentHostingProvider = (props: PropsWithChildren) => {
25
25
  const client = useClient();
26
26
  const [agentHostingProviderClient] = useState(makeClient(client));
27
27
  return (
@@ -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 && !client.initialized,
154
+ condition: () => !client.initialized && newClient.status.get() === SystemStatus.ACTIVE,
152
155
  });
153
156
  });
154
157
  expect(client.initialized).toBe(false);
@@ -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
+ forwardRef,
7
+ type FunctionComponent,
8
+ type PropsWithChildren,
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,139 +24,157 @@ 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.
65
- *
66
- * @param Client
67
71
  */
68
72
  onInitialized?: (client: Client) => MaybePromise<void>;
69
- };
73
+ }>;
70
74
 
71
75
  /**
72
76
  * Root component that provides the DXOS client instance to child components.
73
77
  * To be used with the `useClient` hook.
74
78
  */
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;
79
+ export const ClientProvider = forwardRef<Client | undefined, ClientProviderProps>(
80
+ (
81
+ {
82
+ children,
83
+ config: configProvider,
84
+ client: clientProvider,
85
+ services: servicesProvider,
86
+ status: controlledStatus,
87
+ fallback: Fallback = () => null,
88
+ signalsRuntime = true,
89
+ noBanner,
90
+ onInitialized,
91
+ ...options
92
+ },
93
+ forwardedRef,
94
+ ) => {
95
+ useEffect(() => {
96
+ // TODO(wittjosiah): Ideally this should be imported asynchronously because it is optional.
97
+ // Unfortunately, async import seemed to break signals React instrumentation.
98
+ signalsRuntime && registerSignalsRuntime();
99
+ }, []);
100
+
101
+ // The client is initialized asynchronously.
102
+ // If an error occurs during initialization, it is caught and the state is set.
103
+ // This allows the error to be thrown in the render method.
104
+ // The assumption is that a client initialization error is fatal to the app.
105
+ // This error will be caught by the nearest error boundary.
106
+ const [error, setError] = useState();
107
+ if (error) {
108
+ throw error;
105
109
  }
106
110
 
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
- }
111
+ const [client, setClient] = useState(clientProvider instanceof Client ? clientProvider : undefined);
121
112
 
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
- });
113
+ // Provide external access.
114
+ useImperativeHandle(forwardedRef, () => client, [client]);
143
115
 
144
- return () => {
145
- log('clean up');
146
- clearTimeout(t);
147
- void client?.destroy().catch((err) => log.catch(err));
148
- };
149
- }, [clientProvider, configProvider, servicesProvider]);
116
+ // Client status subscription.
117
+ const [status, setStatus] = useControlledValue(controlledStatus);
118
+ useEffect(() => {
119
+ if (!client) {
120
+ return;
121
+ }
150
122
 
151
- if (!client || status !== SystemStatus.ACTIVE) {
152
- return <Fallback client={client} status={status} />;
153
- }
123
+ const subscription = client.status.subscribe((status) => setStatus(status));
124
+ return () => subscription.unsubscribe();
125
+ }, [client]);
126
+
127
+ // Create and/or initialize client.
128
+ useEffect(() => {
129
+ const initialize = async (client: Client) => {
130
+ if (!client.initialized) {
131
+ await client.initialize().catch(setError);
132
+ log('client ready');
133
+ await onInitialized?.(client);
134
+ log('initialization complete');
135
+ }
136
+
137
+ setClient(client);
138
+
139
+ if (!noBanner) {
140
+ printBanner(client);
141
+ }
142
+ };
143
+
144
+ let client: Client;
145
+ const t = setTimeout(async () => {
146
+ if (clientProvider) {
147
+ // Asynchronously request client.
148
+ client = await getAsyncProviderValue(clientProvider);
149
+ await initialize(client);
150
+ } else {
151
+ // Asynchronously construct client (config may be undefined).
152
+ const config = await getAsyncProviderValue(configProvider);
153
+ log('resolved config', { config });
154
+ const services = await getAsyncProviderValue(servicesProvider, config);
155
+ log('created services', { services });
156
+ client = new Client({ config, services, ...options });
157
+ log('created client');
158
+ await initialize(client);
159
+ }
160
+ });
161
+
162
+ return () => {
163
+ log('clean up');
164
+ clearTimeout(t);
165
+ void client
166
+ ?.destroy()
167
+ .then(() => {
168
+ log('destroyed');
169
+ })
170
+ .catch((err) => log.catch(err));
171
+ };
172
+ }, [configProvider, clientProvider, servicesProvider, noBanner]);
173
+
174
+ if (!client?.initialized || status !== SystemStatus.ACTIVE) {
175
+ return <Fallback client={client} status={status} />;
176
+ }
154
177
 
155
- return <ClientContext.Provider value={{ client, status }}>{children}</ClientContext.Provider>;
156
- };
178
+ return <ClientContext.Provider value={{ client, status }}>{children}</ClientContext.Provider>;
179
+ },
180
+ );
@@ -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 });
@@ -32,7 +29,8 @@ describe('Config hook', () => {
32
29
  expect(Object.entries(result.current).length).toBeGreaterThan(0);
33
30
  });
34
31
 
35
- test('should return custom client config when used properly in a context', async () => {
32
+ // Flaky.
33
+ test('should return custom client config when used properly in a context', { retry: 2 }, async () => {
36
34
  const config = new Config({
37
35
  version: 1,
38
36
  runtime: {
@@ -43,13 +41,13 @@ describe('Config hook', () => {
43
41
  },
44
42
  },
45
43
  });
46
- const client = new Client({ config, services: fromHost(config) });
47
- await client.initialize();
48
- const wrapper = ({ children }: any) => <ClientProvider client={client}>{children}</ClientProvider>;
44
+ const { client } = await createClient({ config });
45
+ const wrapper = await createClientContextProvider(client);
49
46
  const { result } = renderHook(render, { wrapper });
50
47
  await act(async () => {
51
48
  await waitForCondition({ condition: () => client.status.get() === SystemStatus.ACTIVE });
52
49
  });
50
+ await expect.poll(() => result.current).toBeDefined();
53
51
  expect(result.current.get('runtime.client.storage')).toEqual(config.get('runtime.client.storage'));
54
52
  });
55
53
  });
@@ -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