@dxos/react-client 0.6.13 → 0.6.14-main.2b6a0f3

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 (110) hide show
  1. package/dist/lib/browser/chunk-DXE5O7SV.mjs +8 -0
  2. package/dist/lib/browser/chunk-DXE5O7SV.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-KX3DKZQL.mjs +16 -0
  4. package/dist/lib/browser/chunk-KX3DKZQL.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-OFVJLJ3V.mjs +167 -0
  6. package/dist/lib/browser/chunk-OFVJLJ3V.mjs.map +7 -0
  7. package/dist/lib/browser/devtools/index.mjs +2 -1
  8. package/dist/lib/browser/devtools/index.mjs.map +1 -1
  9. package/dist/lib/browser/echo/index.mjs +8 -5
  10. package/dist/lib/browser/echo/index.mjs.map +3 -3
  11. package/dist/lib/browser/halo/index.mjs +2 -1
  12. package/dist/lib/browser/halo/index.mjs.map +1 -1
  13. package/dist/lib/browser/index.mjs +114 -14
  14. package/dist/lib/browser/index.mjs.map +4 -4
  15. package/dist/lib/browser/mesh/index.mjs +2 -1
  16. package/dist/lib/browser/mesh/index.mjs.map +1 -1
  17. package/dist/lib/browser/meta.json +1 -1
  18. package/dist/lib/browser/testing/index.mjs +92 -39
  19. package/dist/lib/browser/testing/index.mjs.map +3 -3
  20. package/dist/lib/node/chunk-DVAPCZTK.cjs +35 -0
  21. package/dist/lib/node/chunk-DVAPCZTK.cjs.map +7 -0
  22. package/dist/lib/node/chunk-VIZOXSRY.cjs +30 -0
  23. package/dist/lib/node/chunk-VIZOXSRY.cjs.map +7 -0
  24. package/dist/lib/node/chunk-ZUY26MIE.cjs +192 -0
  25. package/dist/lib/node/chunk-ZUY26MIE.cjs.map +7 -0
  26. package/dist/lib/node/devtools/index.cjs +3 -2
  27. package/dist/lib/node/devtools/index.cjs.map +1 -1
  28. package/dist/lib/node/echo/index.cjs +9 -6
  29. package/dist/lib/node/echo/index.cjs.map +3 -3
  30. package/dist/lib/node/halo/index.cjs +6 -5
  31. package/dist/lib/node/halo/index.cjs.map +1 -1
  32. package/dist/lib/node/index.cjs +120 -14
  33. package/dist/lib/node/index.cjs.map +4 -4
  34. package/dist/lib/node/mesh/index.cjs +3 -2
  35. package/dist/lib/node/mesh/index.cjs.map +1 -1
  36. package/dist/lib/node/meta.json +1 -1
  37. package/dist/lib/node/testing/index.cjs +90 -37
  38. package/dist/lib/node/testing/index.cjs.map +3 -3
  39. package/dist/lib/node-esm/chunk-KVEEO6SV.mjs +168 -0
  40. package/dist/lib/node-esm/chunk-KVEEO6SV.mjs.map +7 -0
  41. package/dist/lib/node-esm/chunk-QNHFZM3T.mjs +17 -0
  42. package/dist/lib/node-esm/chunk-QNHFZM3T.mjs.map +7 -0
  43. package/dist/lib/node-esm/chunk-UPS353EF.mjs +10 -0
  44. package/dist/lib/node-esm/chunk-UPS353EF.mjs.map +7 -0
  45. package/dist/lib/node-esm/chunk-UYJUBYMU.mjs +165 -0
  46. package/dist/lib/node-esm/chunk-UYJUBYMU.mjs.map +7 -0
  47. package/dist/lib/node-esm/devtools/index.mjs +45 -0
  48. package/dist/lib/node-esm/devtools/index.mjs.map +7 -0
  49. package/dist/lib/node-esm/echo/index.mjs +135 -0
  50. package/dist/lib/node-esm/echo/index.mjs.map +7 -0
  51. package/dist/lib/node-esm/halo/index.mjs +101 -0
  52. package/dist/lib/node-esm/halo/index.mjs.map +7 -0
  53. package/dist/lib/node-esm/index.mjs +135 -0
  54. package/dist/lib/node-esm/index.mjs.map +7 -0
  55. package/dist/lib/node-esm/invitations/index.mjs +18 -0
  56. package/dist/lib/node-esm/invitations/index.mjs.map +7 -0
  57. package/dist/lib/node-esm/mesh/index.mjs +19 -0
  58. package/dist/lib/node-esm/mesh/index.mjs.map +7 -0
  59. package/dist/lib/node-esm/meta.json +1 -0
  60. package/dist/lib/node-esm/testing/index.mjs +219 -0
  61. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  62. package/dist/lib/node-esm/worker.mjs +5 -0
  63. package/dist/lib/node-esm/worker.mjs.map +7 -0
  64. package/dist/types/src/client/AgentHostingProvider.d.ts +1 -1
  65. package/dist/types/src/client/AgentHostingProvider.d.ts.map +1 -1
  66. package/dist/types/src/client/ClientProvider.d.ts +59 -18
  67. package/dist/types/src/client/ClientProvider.d.ts.map +1 -1
  68. package/dist/types/src/client/context.d.ts +1 -4
  69. package/dist/types/src/client/context.d.ts.map +1 -1
  70. package/dist/types/src/client/index.d.ts +0 -1
  71. package/dist/types/src/client/index.d.ts.map +1 -1
  72. package/dist/types/src/echo/useQuery.d.ts.map +1 -1
  73. package/dist/types/src/playwright/playwright.config.d.ts +2 -2
  74. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
  75. package/dist/types/src/testing/ClientRepeater.d.ts +9 -12
  76. package/dist/types/src/testing/ClientRepeater.d.ts.map +1 -1
  77. package/dist/types/src/testing/util.d.ts +5 -3
  78. package/dist/types/src/testing/util.d.ts.map +1 -1
  79. package/dist/types/src/testing/withClientProvider.d.ts +4 -1
  80. package/dist/types/src/testing/withClientProvider.d.ts.map +1 -1
  81. package/dist/types/src/testing/withClientProvider.stories.d.ts +4 -3
  82. package/dist/types/src/testing/withClientProvider.stories.d.ts.map +1 -1
  83. package/package.json +48 -41
  84. package/project.json +3 -22
  85. package/src/client/AgentHostingProvider.tsx +1 -1
  86. package/src/client/ClientProvider.test.tsx +7 -4
  87. package/src/client/ClientProvider.tsx +126 -100
  88. package/src/client/context.ts +4 -5
  89. package/src/client/index.ts +0 -3
  90. package/src/client/useConfig.test.tsx +10 -12
  91. package/src/echo/useQuery.ts +3 -6
  92. package/src/echo/useSpaces.test.tsx +7 -9
  93. package/src/echo/useSpaces.ts +1 -1
  94. package/src/halo/useDevices.test.tsx +3 -5
  95. package/src/playwright/playwright.config.ts +13 -2
  96. package/src/playwright/smoke.spec.ts +5 -10
  97. package/src/testing/ClientRepeater.stories.tsx +1 -1
  98. package/src/testing/ClientRepeater.tsx +19 -20
  99. package/src/testing/util.tsx +12 -7
  100. package/src/testing/withClientProvider.stories.tsx +20 -11
  101. package/src/testing/withClientProvider.tsx +56 -30
  102. package/tsconfig.json +2 -58
  103. package/vitest.config.ts +22 -0
  104. package/dist/lib/browser/chunk-CL7JDETM.mjs +0 -291
  105. package/dist/lib/browser/chunk-CL7JDETM.mjs.map +0 -7
  106. package/dist/lib/node/chunk-C4EWAKPZ.cjs +0 -312
  107. package/dist/lib/node/chunk-C4EWAKPZ.cjs.map +0 -7
  108. package/dist/types/src/client/useClientServices.d.ts +0 -3
  109. package/dist/types/src/client/useClientServices.d.ts.map +0 -1
  110. package/src/client/useClientServices.ts +0 -15
@@ -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,12 +32,12 @@ 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 () => {
41
39
  await client.halo.createIdentity();
42
- await client.spaces.isReady.wait();
40
+ await client.spaces.waitUntilReady();
43
41
  });
44
42
  rerender();
45
43
  expect(result.current).to.not.be.undefined;
@@ -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
  });
@@ -30,7 +30,7 @@ export const useSpace = (spaceKeyLike?: PublicKeyLike): Space | undefined => {
30
30
  }
31
31
 
32
32
  const timeout = setTimeout(async () => {
33
- await client.spaces.isReady.wait();
33
+ await client.spaces.waitUntilReady();
34
34
  setReady(true);
35
35
  });
36
36
 
@@ -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
  });
@@ -15,7 +15,7 @@ import { useClient } from '../client';
15
15
  import { useSpace } from '../echo';
16
16
 
17
17
  export default {
18
- title: 'react-client/ClientRepeater',
18
+ title: 'sdk/react-client/ClientRepeater',
19
19
  decorators: [withTheme],
20
20
  };
21
21
 
@@ -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();
@@ -22,13 +24,16 @@ export const createClient = async ({
22
24
  let space: Space | undefined;
23
25
  if (createSpace) {
24
26
  space = await client.spaces.create();
25
- await client.spaces.isReady.wait();
27
+ await client.spaces.waitUntilReady();
26
28
  }
27
29
 
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
+ };
@@ -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
 
@@ -36,21 +37,29 @@ const Test = () => {
36
37
  };
37
38
 
38
39
  export default {
39
- title: 'react-client/withClientProvider',
40
- component: Test,
41
- decorators: [withTheme, withClientProvider({ createIdentity: true })],
40
+ title: 'sdk/react-client/withClientProvider',
41
+ render: Test,
42
42
  };
43
43
 
44
- export const Default = {};
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
  };
@@ -3,13 +3,14 @@
3
3
  //
4
4
 
5
5
  import { type Decorator } from '@storybook/react';
6
- import React, { useRef } from 'react';
7
- import { type FallbackProps, ErrorBoundary } from 'react-error-boundary';
6
+ import React, { type PropsWithChildren, useEffect, useRef, useState } from 'react';
7
+ import { type FallbackProps, ErrorBoundary as NativeErrorBoundary } from 'react-error-boundary';
8
8
 
9
9
  import { Trigger } from '@dxos/async';
10
10
  import { type Client } from '@dxos/client';
11
11
  import { type Space } from '@dxos/client/echo';
12
12
  import { performInvitation, TestBuilder } from '@dxos/client/testing';
13
+ import { log } from '@dxos/log';
13
14
  import { type MaybePromise } from '@dxos/util';
14
15
 
15
16
  import { ClientProvider, type ClientProviderProps } from '../client';
@@ -17,7 +18,7 @@ import { ClientProvider, type ClientProviderProps } from '../client';
17
18
  type InitializeProps = {
18
19
  createIdentity?: boolean;
19
20
  createSpace?: boolean;
20
- onSpaceCreated?: (client: Client, space: Space) => MaybePromise<void>;
21
+ onSpaceCreated?: (props: { client: Client; space: Space }) => MaybePromise<void>;
21
22
  };
22
23
 
23
24
  /**
@@ -32,14 +33,16 @@ const initializeClient = async (
32
33
  onInitialized,
33
34
  }: InitializeProps & Pick<WithClientProviderProps, 'onInitialized'>,
34
35
  ): Promise<Space | undefined> => {
35
- if (createIdentity) {
36
- await client.halo.createIdentity();
36
+ if (createIdentity || createSpace) {
37
+ if (!client.halo.identity.get()) {
38
+ await client.halo.createIdentity();
39
+ }
37
40
  }
38
41
 
39
42
  let space: Space | undefined;
40
43
  if (createSpace) {
41
44
  space = await client.spaces.create({ name: 'Test Space' });
42
- await onSpaceCreated?.(client, space);
45
+ await onSpaceCreated?.({ client, space });
43
46
  }
44
47
 
45
48
  await onInitialized?.(client);
@@ -58,17 +61,19 @@ export const withClientProvider = ({
58
61
  onInitialized,
59
62
  ...props
60
63
  }: WithClientProviderProps = {}): Decorator => {
61
- const handleInitialized = async (client: Client) => {
62
- await initializeClient(client, { createIdentity, createSpace, onSpaceCreated, onInitialized });
63
- };
64
+ return (Story) => {
65
+ const handleInitialized = async (client: Client) => {
66
+ await initializeClient(client, { createIdentity, createSpace, onSpaceCreated, onInitialized });
67
+ };
64
68
 
65
- return (Story) => (
66
- <ErrorBoundary FallbackComponent={ErrorFallback}>
67
- <ClientProvider onInitialized={handleInitialized} {...props}>
68
- <Story />
69
- </ClientProvider>
70
- </ErrorBoundary>
71
- );
69
+ return (
70
+ <ErrorBoundary>
71
+ <ClientProvider onInitialized={handleInitialized} {...props}>
72
+ <Story />
73
+ </ClientProvider>
74
+ </ErrorBoundary>
75
+ );
76
+ };
72
77
  };
73
78
 
74
79
  // TODO(burdon): Implement context per client for context.
@@ -95,16 +100,20 @@ export const withMultiClientProvider = ({
95
100
  const spaceReady = useRef(new Trigger<Space | undefined>());
96
101
 
97
102
  // Handle invitations.
98
- const handleClientInitialized = async (client: Client) => {
103
+ // NOTE: The zeroth client isn't necessarily the first to be initialized.
104
+ const handleInitialized = async (client: Client, index: number) => {
105
+ log.info('initialized', { index });
99
106
  if (createSpace) {
100
107
  if (!hostRef.current) {
101
108
  hostRef.current = client;
102
109
  const space = await initializeClient(client, { createIdentity, createSpace, onSpaceCreated, onInitialized });
103
110
  spaceReady.current.wake(space);
111
+ log.info('inviting', { index });
104
112
  } else {
105
113
  await initializeClient(client, { createIdentity, onInitialized });
106
114
  const space = await spaceReady.current.wait();
107
115
  if (space) {
116
+ log.info('joining', { index });
108
117
  await Promise.all(performInvitation({ host: space, guest: client.spaces }));
109
118
  }
110
119
  }
@@ -112,31 +121,48 @@ export const withMultiClientProvider = ({
112
121
  };
113
122
 
114
123
  return (
115
- <>
124
+ <ErrorBoundary>
116
125
  {Array.from({ length: numClients }).map((_, index) => (
117
- <ErrorBoundary key={index} FallbackComponent={ErrorFallback}>
118
- <ClientProvider
119
- services={builder.current.createLocalClientServices()}
120
- onInitialized={handleClientInitialized}
121
- {...props}
122
- >
123
- <Story />
124
- </ClientProvider>
125
- </ErrorBoundary>
126
+ <ClientProvider
127
+ key={index}
128
+ services={builder.current.createLocalClientServices()}
129
+ onInitialized={(client) => handleInitialized(client, index)}
130
+ {...props}
131
+ >
132
+ <Story />
133
+ </ClientProvider>
126
134
  ))}
127
- </>
135
+ </ErrorBoundary>
128
136
  );
129
137
  };
130
138
  };
131
139
 
140
+ const ErrorBoundary = ({ children }: PropsWithChildren) => {
141
+ const [error, setError] = useState<Error>();
142
+ useEffect(() => {
143
+ const handleError = (event: PromiseRejectionEvent) => {
144
+ setError(event.reason);
145
+ };
146
+
147
+ window.addEventListener('unhandledrejection', handleError);
148
+ return () => window.removeEventListener('unhandledrejection', handleError);
149
+ }, []);
150
+
151
+ if (error) {
152
+ return <ErrorFallback error={error} resetErrorBoundary={() => setError(undefined)} />;
153
+ }
154
+
155
+ return <NativeErrorBoundary FallbackComponent={ErrorFallback}>{children}</NativeErrorBoundary>;
156
+ };
157
+
132
158
  const ErrorFallback = ({ error }: FallbackProps) => {
133
159
  const { name, message, stack } =
134
160
  error instanceof Error ? error : { name: 'Error', message: String(error), stack: undefined };
135
161
  return (
136
- <div role='alert' className='flex flex-col gap-4'>
162
+ <div role='alert' className='flex flex-col p-4 gap-4 overflow-auto'>
137
163
  <h1 className='text-xl text-red-500'>{name}</h1>
138
164
  <div className='text-lg'>{message}</div>
139
- {stack && <pre className='text-sm text-subdued'>{stack}</pre>}
165
+ {stack && <pre className='whitespace-pre-wrap text-sm text-subdued'>{stack}</pre>}
140
166
  </div>
141
167
  );
142
168
  };
package/tsconfig.json CHANGED
@@ -6,67 +6,11 @@
6
6
  "DOM",
7
7
  "ESNext"
8
8
  ],
9
- "module": "ESNext",
10
- "outDir": "dist/types"
9
+ "module": "ESNext"
11
10
  },
12
11
  "include": [
13
12
  "playwright",
14
13
  "src"
15
14
  ],
16
- "references": [
17
- {
18
- "path": "../../common/async"
19
- },
20
- {
21
- "path": "../../common/codec-protobuf"
22
- },
23
- {
24
- "path": "../../common/debug"
25
- },
26
- {
27
- "path": "../../common/invariant"
28
- },
29
- {
30
- "path": "../../common/log"
31
- },
32
- {
33
- "path": "../../common/node-std"
34
- },
35
- {
36
- "path": "../../common/random"
37
- },
38
- {
39
- "path": "../../common/util"
40
- },
41
- {
42
- "path": "../../core/echo/echo-schema"
43
- },
44
- {
45
- "path": "../../core/echo/echo-signals"
46
- },
47
- {
48
- "path": "../../core/protocols"
49
- },
50
- {
51
- "path": "../../ui/primitives/react-hooks"
52
- },
53
- {
54
- "path": "../../ui/react-ui"
55
- },
56
- {
57
- "path": "../../ui/react-ui-syntax-highlighter"
58
- },
59
- {
60
- "path": "../../ui/react-ui-theme"
61
- },
62
- {
63
- "path": "../../ui/storybook-utils"
64
- },
65
- {
66
- "path": "../client"
67
- },
68
- {
69
- "path": "../config"
70
- }
71
- ]
15
+ "references": []
72
16
  }
@@ -0,0 +1,22 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { defineConfig, mergeConfig } from 'vitest/config';
6
+
7
+ import { baseConfig } from '../../../vitest.shared';
8
+
9
+ // TODO(wittjosiah): Factor out to shared config as an option.
10
+ const specifiedEnv = (process.env.VITEST_ENV ?? 'node').toLowerCase();
11
+ const environment = specifiedEnv === 'node' ? 'jsdom' : undefined;
12
+
13
+ export default mergeConfig(
14
+ baseConfig({ cwd: __dirname }),
15
+ defineConfig({
16
+ test: {
17
+ environment,
18
+ // TODO(dmaretskyi): Enabled because client tests were flaky. Remove when that's not the case.
19
+ retry: 2,
20
+ },
21
+ }),
22
+ );