@dxos/react-client 0.6.14-staging.e15392e → 0.7.0
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/halo/index.mjs.map +2 -2
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +25 -10
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/halo/index.cjs.map +2 -2
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +26 -10
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/lib/node-esm/halo/index.mjs.map +2 -2
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +25 -10
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/halo/index.d.ts +1 -1
- package/dist/types/src/halo/index.d.ts.map +1 -1
- package/dist/types/src/testing/withClientProvider.d.ts +8 -3
- package/dist/types/src/testing/withClientProvider.d.ts.map +1 -1
- package/package.json +23 -23
- package/src/halo/index.ts +9 -1
- package/src/testing/withClientProvider.tsx +35 -16
|
@@ -10,6 +10,7 @@ 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 { raise } from '@dxos/debug';
|
|
13
14
|
import { log } from '@dxos/log';
|
|
14
15
|
import { type MaybePromise } from '@dxos/util';
|
|
15
16
|
|
|
@@ -18,7 +19,8 @@ import { ClientProvider, type ClientProviderProps } from '../client';
|
|
|
18
19
|
type InitializeProps = {
|
|
19
20
|
createIdentity?: boolean;
|
|
20
21
|
createSpace?: boolean;
|
|
21
|
-
|
|
22
|
+
onInitialized?: (client: Client) => MaybePromise<Record<string, any> | void>;
|
|
23
|
+
onSpaceCreated?: (props: { client: Client; space: Space }) => MaybePromise<Record<string, any> | void>;
|
|
22
24
|
};
|
|
23
25
|
|
|
24
26
|
/**
|
|
@@ -26,13 +28,10 @@ type InitializeProps = {
|
|
|
26
28
|
*/
|
|
27
29
|
const initializeClient = async (
|
|
28
30
|
client: Client,
|
|
29
|
-
{
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
onInitialized,
|
|
34
|
-
}: InitializeProps & Pick<WithClientProviderProps, 'onInitialized'>,
|
|
35
|
-
): Promise<Space | undefined> => {
|
|
31
|
+
{ createIdentity, createSpace, onSpaceCreated, onInitialized }: InitializeProps,
|
|
32
|
+
): Promise<StoryClientProps> => {
|
|
33
|
+
const clientData = await onInitialized?.(client);
|
|
34
|
+
|
|
36
35
|
if (createIdentity || createSpace) {
|
|
37
36
|
if (!client.halo.identity.get()) {
|
|
38
37
|
await client.halo.createIdentity();
|
|
@@ -40,16 +39,26 @@ const initializeClient = async (
|
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
let space: Space | undefined;
|
|
42
|
+
let spaceData: Record<string, any> | void = {};
|
|
43
43
|
if (createSpace) {
|
|
44
44
|
space = await client.spaces.create({ name: 'Test Space' });
|
|
45
|
-
await onSpaceCreated?.({ client, space });
|
|
45
|
+
spaceData = await onSpaceCreated?.({ client, space });
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
return { space, ...clientData, ...spaceData };
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type StoryClientProps<T extends Record<string, any> = Record<string, unknown>> = T & { space?: Space };
|
|
52
|
+
|
|
53
|
+
// TODO(wittjosiah): Add to multi-client as well.
|
|
54
|
+
const StoryClientContext = createContext<StoryClientProps | undefined>(undefined);
|
|
55
|
+
|
|
56
|
+
export const useStoryClientData = <T extends Record<string, any> = Record<string, unknown>>() => {
|
|
57
|
+
const data = useContext(StoryClientContext) ?? raise(new Error('Missing withClientProvider decorator.'));
|
|
58
|
+
return data as StoryClientProps<T>;
|
|
50
59
|
};
|
|
51
60
|
|
|
52
|
-
export type WithClientProviderProps = InitializeProps & ClientProviderProps
|
|
61
|
+
export type WithClientProviderProps = InitializeProps & Omit<ClientProviderProps, 'onInitialized'>;
|
|
53
62
|
|
|
54
63
|
/**
|
|
55
64
|
* Decorator that provides the client context.
|
|
@@ -62,14 +71,18 @@ export const withClientProvider = ({
|
|
|
62
71
|
...props
|
|
63
72
|
}: WithClientProviderProps = {}): Decorator => {
|
|
64
73
|
return (Story) => {
|
|
74
|
+
const [data, setData] = useState<StoryClientProps>({});
|
|
75
|
+
|
|
65
76
|
const handleInitialized = async (client: Client) => {
|
|
66
|
-
await initializeClient(client, { createIdentity, createSpace, onSpaceCreated, onInitialized });
|
|
77
|
+
setData(await initializeClient(client, { createIdentity, createSpace, onSpaceCreated, onInitialized }));
|
|
67
78
|
};
|
|
68
79
|
|
|
69
80
|
return (
|
|
70
81
|
<ErrorBoundary>
|
|
71
82
|
<ClientProvider onInitialized={handleInitialized} {...props}>
|
|
72
|
-
<
|
|
83
|
+
<StoryClientContext.Provider value={data}>
|
|
84
|
+
<Story />
|
|
85
|
+
</StoryClientContext.Provider>
|
|
73
86
|
</ClientProvider>
|
|
74
87
|
</ErrorBoundary>
|
|
75
88
|
);
|
|
@@ -79,7 +92,8 @@ export const withClientProvider = ({
|
|
|
79
92
|
// TODO(burdon): Implement context per client for context.
|
|
80
93
|
// TODO(burdon): Callback once all invitations have completed.
|
|
81
94
|
// TODO(burdon): Delay/jitter for creation of other clients.
|
|
82
|
-
export type WithMultiClientProviderProps = InitializeProps &
|
|
95
|
+
export type WithMultiClientProviderProps = InitializeProps &
|
|
96
|
+
Omit<ClientProviderProps, 'onInitialized'> & { numClients?: number };
|
|
83
97
|
|
|
84
98
|
const MultiClientContext = createContext<{ id: number }>({ id: 0 });
|
|
85
99
|
|
|
@@ -110,7 +124,12 @@ export const withMultiClientProvider = ({
|
|
|
110
124
|
if (createSpace) {
|
|
111
125
|
if (!hostRef.current) {
|
|
112
126
|
hostRef.current = client;
|
|
113
|
-
const space = await initializeClient(client, {
|
|
127
|
+
const { space } = await initializeClient(client, {
|
|
128
|
+
createIdentity,
|
|
129
|
+
createSpace,
|
|
130
|
+
onSpaceCreated,
|
|
131
|
+
onInitialized,
|
|
132
|
+
});
|
|
114
133
|
spaceReady.current.wake(space);
|
|
115
134
|
log.info('inviting', { index });
|
|
116
135
|
} else {
|