@microsoft/live-share-react 1.2.2-preview.0 → 1.3.1-preview.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/.nycrc.json +9 -9
- package/LICENSE +86 -86
- package/README.md +608 -608
- package/bin/internal/index.d.ts +0 -1
- package/bin/internal/index.d.ts.map +1 -1
- package/bin/internal/index.js +0 -1
- package/bin/internal/index.js.map +1 -1
- package/bin/providers/AzureProvider.d.ts +1 -1
- package/bin/providers/AzureProvider.d.ts.map +1 -1
- package/bin/providers/AzureProvider.js +1 -1
- package/bin/providers/AzureProvider.js.map +1 -1
- package/bin/providers/LiveShareProvider.js +1 -1
- package/bin/providers/LiveShareProvider.js.map +1 -1
- package/bin/shared-hooks/index.d.ts +1 -0
- package/bin/shared-hooks/index.d.ts.map +1 -1
- package/bin/shared-hooks/index.js +1 -0
- package/bin/shared-hooks/index.js.map +1 -1
- package/bin/{internal → shared-hooks}/useSharedStateRegistry.d.ts +6 -0
- package/bin/shared-hooks/useSharedStateRegistry.d.ts.map +1 -0
- package/bin/{internal → shared-hooks}/useSharedStateRegistry.js +3 -0
- package/bin/shared-hooks/useSharedStateRegistry.js.map +1 -0
- package/package.json +50 -50
- package/src/index.ts +6 -6
- package/src/interfaces/LiveInterfaceExtensions.ts +26 -26
- package/src/interfaces/index.ts +1 -1
- package/src/internal/errors.ts +57 -57
- package/src/internal/index.ts +2 -3
- package/src/internal/type-guards.ts +9 -9
- package/src/live-hooks/index.ts +7 -7
- package/src/live-hooks/useLiveCanvas.ts +191 -191
- package/src/live-hooks/useLiveEvent.ts +140 -140
- package/src/live-hooks/useLiveFollowMode.ts +316 -316
- package/src/live-hooks/useLivePresence.ts +147 -147
- package/src/live-hooks/useLiveState.ts +110 -110
- package/src/live-hooks/useLiveTimer.ts +213 -213
- package/src/live-hooks/useMediaSynchronizer.ts +295 -295
- package/src/providers/AzureProvider.tsx +261 -261
- package/src/providers/LiveShareProvider.tsx +210 -210
- package/src/providers/index.ts +2 -2
- package/src/shared-hooks/index.ts +4 -3
- package/src/shared-hooks/useDynamicDDS.ts +82 -82
- package/src/shared-hooks/useSharedMap.ts +159 -159
- package/src/shared-hooks/useSharedState.ts +96 -96
- package/src/{internal → shared-hooks}/useSharedStateRegistry.ts +170 -164
- package/src/types/ActionTypes.ts +161 -161
- package/src/types/PropTypes.ts +10 -10
- package/src/types/ResultTypes.ts +332 -332
- package/src/types/index.ts +3 -3
- package/src/utils/index.ts +1 -1
- package/src/utils/type-check.ts +69 -69
- package/tsconfig.build.json +5 -5
- package/tsconfig.json +72 -72
- package/bin/internal/useSharedStateRegistry.d.ts.map +0 -1
- package/bin/internal/useSharedStateRegistry.js.map +0 -1
|
@@ -1,261 +1,261 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { IFluidContainer, LoadableObjectClassRecord } from "fluid-framework";
|
|
7
|
-
import React from "react";
|
|
8
|
-
import {
|
|
9
|
-
AzureClientProps,
|
|
10
|
-
AzureContainerServices,
|
|
11
|
-
} from "@fluidframework/azure-client";
|
|
12
|
-
import { IAzureContainerResults } from "../types";
|
|
13
|
-
import {
|
|
14
|
-
ISharedStateRegistryResponse,
|
|
15
|
-
useSharedStateRegistry,
|
|
16
|
-
} from "../
|
|
17
|
-
import {
|
|
18
|
-
AzureTurboClient,
|
|
19
|
-
IFluidTurboClient,
|
|
20
|
-
} from "@microsoft/live-share-turbo";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* React Context provider values for `<AzureProvider>` and `<LiveShareProvider>`.
|
|
24
|
-
*
|
|
25
|
-
* @remarks
|
|
26
|
-
* To get the latest values, use the {@link useFluidObjectsContext} hook.
|
|
27
|
-
*/
|
|
28
|
-
export interface IFluidContext extends ISharedStateRegistryResponse {
|
|
29
|
-
/**
|
|
30
|
-
* The Fluid Turbo client used for connecting to the Fluid container.
|
|
31
|
-
*/
|
|
32
|
-
clientRef: React.MutableRefObject<IFluidTurboClient>;
|
|
33
|
-
/**
|
|
34
|
-
* Stateful Fluid container.
|
|
35
|
-
*/
|
|
36
|
-
container: IFluidContainer | undefined;
|
|
37
|
-
/**
|
|
38
|
-
* The Azure container services (e.g., audience).
|
|
39
|
-
*/
|
|
40
|
-
services: AzureContainerServices | undefined;
|
|
41
|
-
/**
|
|
42
|
-
* A stateful error object that is set if there was an error connecting to the Fluid container.
|
|
43
|
-
*/
|
|
44
|
-
joinError: Error | undefined;
|
|
45
|
-
/**
|
|
46
|
-
* React callback function to connect to an existing Fluid container.
|
|
47
|
-
*
|
|
48
|
-
* @remarks
|
|
49
|
-
* The results will also be set to their stateful counterparts for `container` and `services`.
|
|
50
|
-
*
|
|
51
|
-
* @param containerId the containerId to connect to.
|
|
52
|
-
* @param initialObjects Optional. initial object schema, which should match that passed to `createContainer()`.
|
|
53
|
-
* @returns promise that returns a results object once complete (e.g., container, services, etc.)
|
|
54
|
-
*/
|
|
55
|
-
getContainer: (
|
|
56
|
-
containerId: string,
|
|
57
|
-
initialObjects?: LoadableObjectClassRecord
|
|
58
|
-
) => Promise<IAzureContainerResults>;
|
|
59
|
-
/**
|
|
60
|
-
* React callback function to create and connect to a new Fluid container.
|
|
61
|
-
*
|
|
62
|
-
* @remarks
|
|
63
|
-
* The results will also be set to their stateful counterparts for `container` and `services`.
|
|
64
|
-
*
|
|
65
|
-
* @param initialObjects Optional. The initial object schema to apply to the container.
|
|
66
|
-
* @param onInitializeContainer Optional. A callback for when the container is first initialized, which is useful for setting default values to objects in `initialObjects`.
|
|
67
|
-
* @returns promise that returns a results object once complete (e.g., container, services, etc.)
|
|
68
|
-
*/
|
|
69
|
-
createContainer: (
|
|
70
|
-
initialObjects?: LoadableObjectClassRecord,
|
|
71
|
-
onInitializeContainer?: (container: IFluidContainer) => void
|
|
72
|
-
) => Promise<IAzureContainerResults>;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* @hidden
|
|
77
|
-
*/
|
|
78
|
-
export const FluidContext = React.createContext<IFluidContext>(
|
|
79
|
-
{} as IFluidContext
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Hook to get the latest React context state for `FluidContext`.
|
|
84
|
-
*
|
|
85
|
-
* @remarks
|
|
86
|
-
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
87
|
-
*
|
|
88
|
-
* @returns current state of `LiveShareContext`
|
|
89
|
-
*/
|
|
90
|
-
export const useFluidObjectsContext = (): IFluidContext => {
|
|
91
|
-
const context = React.useContext(FluidContext);
|
|
92
|
-
if (!isFluidObjectsContext(context)) {
|
|
93
|
-
throw new Error(
|
|
94
|
-
"@microsoft/live-share-react: attempting to use `useFluidObjectsContext()` from a component that is not a child of `<LiveShareProvider>` or `<AzureProvider>`.\nTo fix this error, ensure that you are only using Live Share hooks (e.g., `useLiveState`) from a child component of `<LiveShareProvider>` or `<AzureProvider>`."
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
return context;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Prop types for {@link AzureProvider} component.
|
|
102
|
-
*/
|
|
103
|
-
export interface IAzureProviderProps {
|
|
104
|
-
/**
|
|
105
|
-
* Optional. React children node for the React Context Provider.
|
|
106
|
-
*/
|
|
107
|
-
children?: React.ReactNode;
|
|
108
|
-
/**
|
|
109
|
-
* Props for initializing a new `AzureClient` instance.
|
|
110
|
-
*/
|
|
111
|
-
clientOptions: AzureClientProps;
|
|
112
|
-
/**
|
|
113
|
-
* The `containerId` to connect to when {@link joinOnLoad} is true.
|
|
114
|
-
*
|
|
115
|
-
* @remarks
|
|
116
|
-
* If you pass in an `undefined` value when {@link createOnLoad} is true, a new container will be created.
|
|
117
|
-
*/
|
|
118
|
-
containerId?: string;
|
|
119
|
-
/**
|
|
120
|
-
* Flag to control whether or not a new container should be created on first mount.
|
|
121
|
-
*
|
|
122
|
-
* @remarks
|
|
123
|
-
* If no {@link containerId} is set when the component first mounts, setting this to `true` will automatically create a new container.
|
|
124
|
-
* This prop does not do anything if you are using `<LiveShareProvider>` instead of `<AzureProvider>`.
|
|
125
|
-
*/
|
|
126
|
-
createOnLoad?: boolean;
|
|
127
|
-
/**
|
|
128
|
-
* The initial object schema to use when {@link joinOnLoad} or {@link createOnLoad} is true.
|
|
129
|
-
*/
|
|
130
|
-
initialObjects?: LoadableObjectClassRecord;
|
|
131
|
-
/**
|
|
132
|
-
* Flag to control whether or not to connect to an existing container on first mount.
|
|
133
|
-
*
|
|
134
|
-
* @remarks
|
|
135
|
-
* Setting this to true will connect to the container if {@link containerId} is provided as a prop.
|
|
136
|
-
*/
|
|
137
|
-
joinOnLoad?: boolean;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* React Context provider component for using Fluid data objects & joining/creating a Fluid document `AzureClient`.
|
|
142
|
-
*/
|
|
143
|
-
export const AzureProvider: React.FC<IAzureProviderProps> = (props) => {
|
|
144
|
-
const startedRef = React.useRef<boolean>(false);
|
|
145
|
-
const clientRef = React.useRef<AzureTurboClient>(
|
|
146
|
-
new AzureTurboClient(props.clientOptions)
|
|
147
|
-
);
|
|
148
|
-
const [results, setResults] = React.useState<
|
|
149
|
-
IAzureContainerResults | undefined
|
|
150
|
-
>();
|
|
151
|
-
const [joinError, setJoinError] = React.useState<Error | undefined>();
|
|
152
|
-
|
|
153
|
-
const stateRegistryCallbacks = useSharedStateRegistry(results);
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Get the container for a given containerId using AzureClient
|
|
157
|
-
*/
|
|
158
|
-
const getContainer = React.useCallback(
|
|
159
|
-
async (
|
|
160
|
-
containerId: string,
|
|
161
|
-
initialObjects?: LoadableObjectClassRecord
|
|
162
|
-
): Promise<IAzureContainerResults> => {
|
|
163
|
-
return new Promise(async (resolve, reject) => {
|
|
164
|
-
try {
|
|
165
|
-
const results: IAzureContainerResults =
|
|
166
|
-
await clientRef.current.getContainer(
|
|
167
|
-
containerId,
|
|
168
|
-
initialObjects
|
|
169
|
-
);
|
|
170
|
-
setResults(results);
|
|
171
|
-
resolve(results);
|
|
172
|
-
} catch (error: any) {
|
|
173
|
-
if (error instanceof Error) {
|
|
174
|
-
setJoinError(error);
|
|
175
|
-
}
|
|
176
|
-
reject(error);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
},
|
|
180
|
-
[props.containerId, setResults]
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Create container callback to create a new container using AzureClient
|
|
185
|
-
*/
|
|
186
|
-
const createContainer = React.useCallback(
|
|
187
|
-
async (
|
|
188
|
-
initialObjects?: LoadableObjectClassRecord,
|
|
189
|
-
onInitializeContainer?: (container: IFluidContainer) => void
|
|
190
|
-
): Promise<IAzureContainerResults> => {
|
|
191
|
-
return new Promise(async (resolve, reject) => {
|
|
192
|
-
try {
|
|
193
|
-
const results: IAzureContainerResults =
|
|
194
|
-
await clientRef.current.createContainer(initialObjects);
|
|
195
|
-
if (onInitializeContainer) {
|
|
196
|
-
onInitializeContainer(results.container);
|
|
197
|
-
}
|
|
198
|
-
const containerId = await results.container.attach();
|
|
199
|
-
window.location.hash = containerId;
|
|
200
|
-
setResults(results);
|
|
201
|
-
resolve(results);
|
|
202
|
-
} catch (error: any) {
|
|
203
|
-
if (error instanceof Error) {
|
|
204
|
-
setJoinError(error);
|
|
205
|
-
}
|
|
206
|
-
reject(error);
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
},
|
|
210
|
-
[props.containerId, setResults]
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Joins a container if `props.joinOnLoad` is true and `props.containerId` is known. Creates a new container if `props.createOnLoad` is true
|
|
215
|
-
* and `props.containerId` is not known.
|
|
216
|
-
*/
|
|
217
|
-
React.useEffect(() => {
|
|
218
|
-
// This hook should only be called once, so we use a ref to track if it has been called.
|
|
219
|
-
// This is a workaround for the fact that useEffect is called twice on initial render in React V18.
|
|
220
|
-
// We are not doing this here for backwards compatibility. View the README for more information.
|
|
221
|
-
if (
|
|
222
|
-
results?.container?.connectionState !== undefined ||
|
|
223
|
-
startedRef.current
|
|
224
|
-
)
|
|
225
|
-
return;
|
|
226
|
-
startedRef.current = true;
|
|
227
|
-
if (props.containerId && props.joinOnLoad) {
|
|
228
|
-
getContainer(props.containerId, props.initialObjects);
|
|
229
|
-
} else if (!props.containerId && props.createOnLoad) {
|
|
230
|
-
createContainer(props.initialObjects);
|
|
231
|
-
}
|
|
232
|
-
}, [
|
|
233
|
-
results?.container?.connectionState,
|
|
234
|
-
props.containerId,
|
|
235
|
-
props.createOnLoad,
|
|
236
|
-
props.joinOnLoad,
|
|
237
|
-
props.initialObjects,
|
|
238
|
-
getContainer,
|
|
239
|
-
createContainer,
|
|
240
|
-
]);
|
|
241
|
-
|
|
242
|
-
return (
|
|
243
|
-
<FluidContext.Provider
|
|
244
|
-
value={{
|
|
245
|
-
clientRef,
|
|
246
|
-
container: results?.container,
|
|
247
|
-
services: results?.services,
|
|
248
|
-
joinError,
|
|
249
|
-
createContainer,
|
|
250
|
-
getContainer,
|
|
251
|
-
...stateRegistryCallbacks,
|
|
252
|
-
}}
|
|
253
|
-
>
|
|
254
|
-
{props.children}
|
|
255
|
-
</FluidContext.Provider>
|
|
256
|
-
);
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
function isFluidObjectsContext(value: any): value is IFluidContext {
|
|
260
|
-
return !!value?.clientRef;
|
|
261
|
-
}
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { IFluidContainer, LoadableObjectClassRecord } from "fluid-framework";
|
|
7
|
+
import React from "react";
|
|
8
|
+
import {
|
|
9
|
+
AzureClientProps,
|
|
10
|
+
AzureContainerServices,
|
|
11
|
+
} from "@fluidframework/azure-client";
|
|
12
|
+
import { IAzureContainerResults } from "../types";
|
|
13
|
+
import {
|
|
14
|
+
ISharedStateRegistryResponse,
|
|
15
|
+
useSharedStateRegistry,
|
|
16
|
+
} from "../shared-hooks";
|
|
17
|
+
import {
|
|
18
|
+
AzureTurboClient,
|
|
19
|
+
IFluidTurboClient,
|
|
20
|
+
} from "@microsoft/live-share-turbo";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* React Context provider values for `<AzureProvider>` and `<LiveShareProvider>`.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* To get the latest values, use the {@link useFluidObjectsContext} hook.
|
|
27
|
+
*/
|
|
28
|
+
export interface IFluidContext extends ISharedStateRegistryResponse {
|
|
29
|
+
/**
|
|
30
|
+
* The Fluid Turbo client used for connecting to the Fluid container.
|
|
31
|
+
*/
|
|
32
|
+
clientRef: React.MutableRefObject<IFluidTurboClient>;
|
|
33
|
+
/**
|
|
34
|
+
* Stateful Fluid container.
|
|
35
|
+
*/
|
|
36
|
+
container: IFluidContainer | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The Azure container services (e.g., audience).
|
|
39
|
+
*/
|
|
40
|
+
services: AzureContainerServices | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* A stateful error object that is set if there was an error connecting to the Fluid container.
|
|
43
|
+
*/
|
|
44
|
+
joinError: Error | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* React callback function to connect to an existing Fluid container.
|
|
47
|
+
*
|
|
48
|
+
* @remarks
|
|
49
|
+
* The results will also be set to their stateful counterparts for `container` and `services`.
|
|
50
|
+
*
|
|
51
|
+
* @param containerId the containerId to connect to.
|
|
52
|
+
* @param initialObjects Optional. initial object schema, which should match that passed to `createContainer()`.
|
|
53
|
+
* @returns promise that returns a results object once complete (e.g., container, services, etc.)
|
|
54
|
+
*/
|
|
55
|
+
getContainer: (
|
|
56
|
+
containerId: string,
|
|
57
|
+
initialObjects?: LoadableObjectClassRecord
|
|
58
|
+
) => Promise<IAzureContainerResults>;
|
|
59
|
+
/**
|
|
60
|
+
* React callback function to create and connect to a new Fluid container.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* The results will also be set to their stateful counterparts for `container` and `services`.
|
|
64
|
+
*
|
|
65
|
+
* @param initialObjects Optional. The initial object schema to apply to the container.
|
|
66
|
+
* @param onInitializeContainer Optional. A callback for when the container is first initialized, which is useful for setting default values to objects in `initialObjects`.
|
|
67
|
+
* @returns promise that returns a results object once complete (e.g., container, services, etc.)
|
|
68
|
+
*/
|
|
69
|
+
createContainer: (
|
|
70
|
+
initialObjects?: LoadableObjectClassRecord,
|
|
71
|
+
onInitializeContainer?: (container: IFluidContainer) => void
|
|
72
|
+
) => Promise<IAzureContainerResults>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @hidden
|
|
77
|
+
*/
|
|
78
|
+
export const FluidContext = React.createContext<IFluidContext>(
|
|
79
|
+
{} as IFluidContext
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Hook to get the latest React context state for `FluidContext`.
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
87
|
+
*
|
|
88
|
+
* @returns current state of `LiveShareContext`
|
|
89
|
+
*/
|
|
90
|
+
export const useFluidObjectsContext = (): IFluidContext => {
|
|
91
|
+
const context = React.useContext(FluidContext);
|
|
92
|
+
if (!isFluidObjectsContext(context)) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
"@microsoft/live-share-react: attempting to use `useFluidObjectsContext()` from a component that is not a child of `<LiveShareProvider>` or `<AzureProvider>`.\nTo fix this error, ensure that you are only using Live Share hooks (e.g., `useLiveState`) from a child component of `<LiveShareProvider>` or `<AzureProvider>`."
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
return context;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Prop types for {@link AzureProvider} component.
|
|
102
|
+
*/
|
|
103
|
+
export interface IAzureProviderProps {
|
|
104
|
+
/**
|
|
105
|
+
* Optional. React children node for the React Context Provider.
|
|
106
|
+
*/
|
|
107
|
+
children?: React.ReactNode;
|
|
108
|
+
/**
|
|
109
|
+
* Props for initializing a new `AzureClient` instance.
|
|
110
|
+
*/
|
|
111
|
+
clientOptions: AzureClientProps;
|
|
112
|
+
/**
|
|
113
|
+
* The `containerId` to connect to when {@link joinOnLoad} is true.
|
|
114
|
+
*
|
|
115
|
+
* @remarks
|
|
116
|
+
* If you pass in an `undefined` value when {@link createOnLoad} is true, a new container will be created.
|
|
117
|
+
*/
|
|
118
|
+
containerId?: string;
|
|
119
|
+
/**
|
|
120
|
+
* Flag to control whether or not a new container should be created on first mount.
|
|
121
|
+
*
|
|
122
|
+
* @remarks
|
|
123
|
+
* If no {@link containerId} is set when the component first mounts, setting this to `true` will automatically create a new container.
|
|
124
|
+
* This prop does not do anything if you are using `<LiveShareProvider>` instead of `<AzureProvider>`.
|
|
125
|
+
*/
|
|
126
|
+
createOnLoad?: boolean;
|
|
127
|
+
/**
|
|
128
|
+
* The initial object schema to use when {@link joinOnLoad} or {@link createOnLoad} is true.
|
|
129
|
+
*/
|
|
130
|
+
initialObjects?: LoadableObjectClassRecord;
|
|
131
|
+
/**
|
|
132
|
+
* Flag to control whether or not to connect to an existing container on first mount.
|
|
133
|
+
*
|
|
134
|
+
* @remarks
|
|
135
|
+
* Setting this to true will connect to the container if {@link containerId} is provided as a prop.
|
|
136
|
+
*/
|
|
137
|
+
joinOnLoad?: boolean;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* React Context provider component for using Fluid data objects & joining/creating a Fluid document `AzureClient`.
|
|
142
|
+
*/
|
|
143
|
+
export const AzureProvider: React.FC<IAzureProviderProps> = (props) => {
|
|
144
|
+
const startedRef = React.useRef<boolean>(false);
|
|
145
|
+
const clientRef = React.useRef<AzureTurboClient>(
|
|
146
|
+
new AzureTurboClient(props.clientOptions)
|
|
147
|
+
);
|
|
148
|
+
const [results, setResults] = React.useState<
|
|
149
|
+
IAzureContainerResults | undefined
|
|
150
|
+
>();
|
|
151
|
+
const [joinError, setJoinError] = React.useState<Error | undefined>();
|
|
152
|
+
|
|
153
|
+
const stateRegistryCallbacks = useSharedStateRegistry(results);
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Get the container for a given containerId using AzureClient
|
|
157
|
+
*/
|
|
158
|
+
const getContainer = React.useCallback(
|
|
159
|
+
async (
|
|
160
|
+
containerId: string,
|
|
161
|
+
initialObjects?: LoadableObjectClassRecord
|
|
162
|
+
): Promise<IAzureContainerResults> => {
|
|
163
|
+
return new Promise(async (resolve, reject) => {
|
|
164
|
+
try {
|
|
165
|
+
const results: IAzureContainerResults =
|
|
166
|
+
await clientRef.current.getContainer(
|
|
167
|
+
containerId,
|
|
168
|
+
initialObjects
|
|
169
|
+
);
|
|
170
|
+
setResults(results);
|
|
171
|
+
resolve(results);
|
|
172
|
+
} catch (error: any) {
|
|
173
|
+
if (error instanceof Error) {
|
|
174
|
+
setJoinError(error);
|
|
175
|
+
}
|
|
176
|
+
reject(error);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
[props.containerId, setResults]
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Create container callback to create a new container using AzureClient
|
|
185
|
+
*/
|
|
186
|
+
const createContainer = React.useCallback(
|
|
187
|
+
async (
|
|
188
|
+
initialObjects?: LoadableObjectClassRecord,
|
|
189
|
+
onInitializeContainer?: (container: IFluidContainer) => void
|
|
190
|
+
): Promise<IAzureContainerResults> => {
|
|
191
|
+
return new Promise(async (resolve, reject) => {
|
|
192
|
+
try {
|
|
193
|
+
const results: IAzureContainerResults =
|
|
194
|
+
await clientRef.current.createContainer(initialObjects);
|
|
195
|
+
if (onInitializeContainer) {
|
|
196
|
+
onInitializeContainer(results.container);
|
|
197
|
+
}
|
|
198
|
+
const containerId = await results.container.attach();
|
|
199
|
+
window.location.hash = containerId;
|
|
200
|
+
setResults(results);
|
|
201
|
+
resolve(results);
|
|
202
|
+
} catch (error: any) {
|
|
203
|
+
if (error instanceof Error) {
|
|
204
|
+
setJoinError(error);
|
|
205
|
+
}
|
|
206
|
+
reject(error);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
[props.containerId, setResults]
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Joins a container if `props.joinOnLoad` is true and `props.containerId` is known. Creates a new container if `props.createOnLoad` is true
|
|
215
|
+
* and `props.containerId` is not known.
|
|
216
|
+
*/
|
|
217
|
+
React.useEffect(() => {
|
|
218
|
+
// This hook should only be called once, so we use a ref to track if it has been called.
|
|
219
|
+
// This is a workaround for the fact that useEffect is called twice on initial render in React V18.
|
|
220
|
+
// We are not doing this here for backwards compatibility. View the README for more information.
|
|
221
|
+
if (
|
|
222
|
+
results?.container?.connectionState !== undefined ||
|
|
223
|
+
startedRef.current
|
|
224
|
+
)
|
|
225
|
+
return;
|
|
226
|
+
startedRef.current = true;
|
|
227
|
+
if (props.containerId && props.joinOnLoad) {
|
|
228
|
+
getContainer(props.containerId, props.initialObjects);
|
|
229
|
+
} else if (!props.containerId && props.createOnLoad) {
|
|
230
|
+
createContainer(props.initialObjects);
|
|
231
|
+
}
|
|
232
|
+
}, [
|
|
233
|
+
results?.container?.connectionState,
|
|
234
|
+
props.containerId,
|
|
235
|
+
props.createOnLoad,
|
|
236
|
+
props.joinOnLoad,
|
|
237
|
+
props.initialObjects,
|
|
238
|
+
getContainer,
|
|
239
|
+
createContainer,
|
|
240
|
+
]);
|
|
241
|
+
|
|
242
|
+
return (
|
|
243
|
+
<FluidContext.Provider
|
|
244
|
+
value={{
|
|
245
|
+
clientRef,
|
|
246
|
+
container: results?.container,
|
|
247
|
+
services: results?.services,
|
|
248
|
+
joinError,
|
|
249
|
+
createContainer,
|
|
250
|
+
getContainer,
|
|
251
|
+
...stateRegistryCallbacks,
|
|
252
|
+
}}
|
|
253
|
+
>
|
|
254
|
+
{props.children}
|
|
255
|
+
</FluidContext.Provider>
|
|
256
|
+
);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
function isFluidObjectsContext(value: any): value is IFluidContext {
|
|
260
|
+
return !!value?.clientRef;
|
|
261
|
+
}
|