@microsoft/live-share-react 1.0.0-preview.10

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 (233) hide show
  1. package/.nycrc.json +10 -0
  2. package/LICENSE +87 -0
  3. package/README.md +544 -0
  4. package/bin/index.d.ts +7 -0
  5. package/bin/index.d.ts.map +1 -0
  6. package/bin/index.js +7 -0
  7. package/bin/index.js.map +1 -0
  8. package/bin/interfaces/LiveInterfaceExtensions.d.ts +9 -0
  9. package/bin/interfaces/LiveInterfaceExtensions.d.ts.map +1 -0
  10. package/bin/interfaces/LiveInterfaceExtensions.js +6 -0
  11. package/bin/interfaces/LiveInterfaceExtensions.js.map +1 -0
  12. package/bin/interfaces/index.d.ts +2 -0
  13. package/bin/interfaces/index.d.ts.map +1 -0
  14. package/bin/interfaces/index.js +2 -0
  15. package/bin/interfaces/index.js.map +1 -0
  16. package/bin/internal-hooks/index.d.ts +2 -0
  17. package/bin/internal-hooks/index.d.ts.map +1 -0
  18. package/bin/internal-hooks/index.js +2 -0
  19. package/bin/internal-hooks/index.js.map +1 -0
  20. package/bin/internal-hooks/useDynamicDDSRegistry.d.ts +14 -0
  21. package/bin/internal-hooks/useDynamicDDSRegistry.d.ts.map +1 -0
  22. package/bin/internal-hooks/useDynamicDDSRegistry.js +142 -0
  23. package/bin/internal-hooks/useDynamicDDSRegistry.js.map +1 -0
  24. package/bin/internal-hooks/useSharedStateRegistry.d.ts +31 -0
  25. package/bin/internal-hooks/useSharedStateRegistry.d.ts.map +1 -0
  26. package/bin/internal-hooks/useSharedStateRegistry.js +100 -0
  27. package/bin/internal-hooks/useSharedStateRegistry.js.map +1 -0
  28. package/bin/internal-hooks/useStateRef.d.ts +7 -0
  29. package/bin/internal-hooks/useStateRef.d.ts.map +1 -0
  30. package/bin/internal-hooks/useStateRef.js +15 -0
  31. package/bin/internal-hooks/useStateRef.js.map +1 -0
  32. package/bin/live-hooks/index.d.ts +7 -0
  33. package/bin/live-hooks/index.d.ts.map +1 -0
  34. package/bin/live-hooks/index.js +7 -0
  35. package/bin/live-hooks/index.js.map +1 -0
  36. package/bin/live-hooks/useLiveAICompletion.d.ts +29 -0
  37. package/bin/live-hooks/useLiveAICompletion.d.ts.map +1 -0
  38. package/bin/live-hooks/useLiveAICompletion.js +186 -0
  39. package/bin/live-hooks/useLiveAICompletion.js.map +1 -0
  40. package/bin/live-hooks/useLiveCanvas.d.ts +28 -0
  41. package/bin/live-hooks/useLiveCanvas.d.ts.map +1 -0
  42. package/bin/live-hooks/useLiveCanvas.js +167 -0
  43. package/bin/live-hooks/useLiveCanvas.js.map +1 -0
  44. package/bin/live-hooks/useLiveCoPilot.d.ts +28 -0
  45. package/bin/live-hooks/useLiveCoPilot.d.ts.map +1 -0
  46. package/bin/live-hooks/useLiveCoPilot.js +178 -0
  47. package/bin/live-hooks/useLiveCoPilot.js.map +1 -0
  48. package/bin/live-hooks/useLiveEvent.d.ts +22 -0
  49. package/bin/live-hooks/useLiveEvent.d.ts.map +1 -0
  50. package/bin/live-hooks/useLiveEvent.js +86 -0
  51. package/bin/live-hooks/useLiveEvent.js.map +1 -0
  52. package/bin/live-hooks/useLivePresence.d.ts +26 -0
  53. package/bin/live-hooks/useLivePresence.d.ts.map +1 -0
  54. package/bin/live-hooks/useLivePresence.js +93 -0
  55. package/bin/live-hooks/useLivePresence.js.map +1 -0
  56. package/bin/live-hooks/useLiveState.d.ts +19 -0
  57. package/bin/live-hooks/useLiveState.d.ts.map +1 -0
  58. package/bin/live-hooks/useLiveState.js +61 -0
  59. package/bin/live-hooks/useLiveState.js.map +1 -0
  60. package/bin/live-hooks/useLiveTimer.d.ts +8 -0
  61. package/bin/live-hooks/useLiveTimer.d.ts.map +1 -0
  62. package/bin/live-hooks/useLiveTimer.js +132 -0
  63. package/bin/live-hooks/useLiveTimer.js.map +1 -0
  64. package/bin/live-hooks/useMediaSynchronizer.d.ts +23 -0
  65. package/bin/live-hooks/useMediaSynchronizer.d.ts.map +1 -0
  66. package/bin/live-hooks/useMediaSynchronizer.js +162 -0
  67. package/bin/live-hooks/useMediaSynchronizer.js.map +1 -0
  68. package/bin/live-share-turbo/AzureTurboClient.d.ts +39 -0
  69. package/bin/live-share-turbo/AzureTurboClient.d.ts.map +1 -0
  70. package/bin/live-share-turbo/AzureTurboClient.js +56 -0
  71. package/bin/live-share-turbo/AzureTurboClient.js.map +1 -0
  72. package/bin/live-share-turbo/LiveShareTurboClient.d.ts +53 -0
  73. package/bin/live-share-turbo/LiveShareTurboClient.d.ts.map +1 -0
  74. package/bin/live-share-turbo/LiveShareTurboClient.js +69 -0
  75. package/bin/live-share-turbo/LiveShareTurboClient.js.map +1 -0
  76. package/bin/live-share-turbo/dds-objects/TurboDataObject.d.ts +31 -0
  77. package/bin/live-share-turbo/dds-objects/TurboDataObject.d.ts.map +1 -0
  78. package/bin/live-share-turbo/dds-objects/TurboDataObject.js +75 -0
  79. package/bin/live-share-turbo/dds-objects/TurboDataObject.js.map +1 -0
  80. package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.d.ts +48 -0
  81. package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.d.ts.map +1 -0
  82. package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.js +104 -0
  83. package/bin/live-share-turbo/dds-objects/TurboLiveCanvas.js.map +1 -0
  84. package/bin/live-share-turbo/dds-objects/TurboLiveEvent.d.ts +28 -0
  85. package/bin/live-share-turbo/dds-objects/TurboLiveEvent.d.ts.map +1 -0
  86. package/bin/live-share-turbo/dds-objects/TurboLiveEvent.js +68 -0
  87. package/bin/live-share-turbo/dds-objects/TurboLiveEvent.js.map +1 -0
  88. package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.d.ts +60 -0
  89. package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.d.ts.map +1 -0
  90. package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.js +111 -0
  91. package/bin/live-share-turbo/dds-objects/TurboLiveMediaSession.js.map +1 -0
  92. package/bin/live-share-turbo/dds-objects/TurboLivePresence.d.ts +77 -0
  93. package/bin/live-share-turbo/dds-objects/TurboLivePresence.d.ts.map +1 -0
  94. package/bin/live-share-turbo/dds-objects/TurboLivePresence.js +137 -0
  95. package/bin/live-share-turbo/dds-objects/TurboLivePresence.js.map +1 -0
  96. package/bin/live-share-turbo/dds-objects/TurboLiveState.d.ts +30 -0
  97. package/bin/live-share-turbo/dds-objects/TurboLiveState.d.ts.map +1 -0
  98. package/bin/live-share-turbo/dds-objects/TurboLiveState.js +74 -0
  99. package/bin/live-share-turbo/dds-objects/TurboLiveState.js.map +1 -0
  100. package/bin/live-share-turbo/dds-objects/TurboSharedMap.d.ts +61 -0
  101. package/bin/live-share-turbo/dds-objects/TurboSharedMap.d.ts.map +1 -0
  102. package/bin/live-share-turbo/dds-objects/TurboSharedMap.js +108 -0
  103. package/bin/live-share-turbo/dds-objects/TurboSharedMap.js.map +1 -0
  104. package/bin/live-share-turbo/dds-objects/index.d.ts +8 -0
  105. package/bin/live-share-turbo/dds-objects/index.d.ts.map +1 -0
  106. package/bin/live-share-turbo/dds-objects/index.js +8 -0
  107. package/bin/live-share-turbo/dds-objects/index.js.map +1 -0
  108. package/bin/live-share-turbo/index.d.ts +5 -0
  109. package/bin/live-share-turbo/index.d.ts.map +1 -0
  110. package/bin/live-share-turbo/index.js +5 -0
  111. package/bin/live-share-turbo/index.js.map +1 -0
  112. package/bin/live-share-turbo/interfaces/IFluidTurboClient.d.ts +15 -0
  113. package/bin/live-share-turbo/interfaces/IFluidTurboClient.d.ts.map +1 -0
  114. package/bin/live-share-turbo/interfaces/IFluidTurboClient.js +2 -0
  115. package/bin/live-share-turbo/interfaces/IFluidTurboClient.js.map +1 -0
  116. package/bin/live-share-turbo/interfaces/index.d.ts +3 -0
  117. package/bin/live-share-turbo/interfaces/index.d.ts.map +1 -0
  118. package/bin/live-share-turbo/interfaces/index.js +3 -0
  119. package/bin/live-share-turbo/interfaces/index.js.map +1 -0
  120. package/bin/live-share-turbo/internals/FluidTurboClient.d.ts +39 -0
  121. package/bin/live-share-turbo/internals/FluidTurboClient.d.ts.map +1 -0
  122. package/bin/live-share-turbo/internals/FluidTurboClient.js +121 -0
  123. package/bin/live-share-turbo/internals/FluidTurboClient.js.map +1 -0
  124. package/bin/live-share-turbo/internals/index.d.ts +2 -0
  125. package/bin/live-share-turbo/internals/index.d.ts.map +1 -0
  126. package/bin/live-share-turbo/internals/index.js +2 -0
  127. package/bin/live-share-turbo/internals/index.js.map +1 -0
  128. package/bin/live-share-turbo/internals/types.d.ts +4 -0
  129. package/bin/live-share-turbo/internals/types.d.ts.map +1 -0
  130. package/bin/live-share-turbo/internals/types.js +2 -0
  131. package/bin/live-share-turbo/internals/types.js.map +1 -0
  132. package/bin/providers/AzureProvider.d.ts +34 -0
  133. package/bin/providers/AzureProvider.d.ts.map +1 -0
  134. package/bin/providers/AzureProvider.js +102 -0
  135. package/bin/providers/AzureProvider.js.map +1 -0
  136. package/bin/providers/FluidContextProvider.d.ts +34 -0
  137. package/bin/providers/FluidContextProvider.d.ts.map +1 -0
  138. package/bin/providers/FluidContextProvider.js +99 -0
  139. package/bin/providers/FluidContextProvider.js.map +1 -0
  140. package/bin/providers/LiveShareContextProvider.d.ts +42 -0
  141. package/bin/providers/LiveShareContextProvider.d.ts.map +1 -0
  142. package/bin/providers/LiveShareContextProvider.js +63 -0
  143. package/bin/providers/LiveShareContextProvider.js.map +1 -0
  144. package/bin/providers/LiveShareProvider.d.ts +42 -0
  145. package/bin/providers/LiveShareProvider.d.ts.map +1 -0
  146. package/bin/providers/LiveShareProvider.js +66 -0
  147. package/bin/providers/LiveShareProvider.js.map +1 -0
  148. package/bin/providers/index.d.ts +3 -0
  149. package/bin/providers/index.d.ts.map +1 -0
  150. package/bin/providers/index.js +3 -0
  151. package/bin/providers/index.js.map +1 -0
  152. package/bin/shared-hooks/index.d.ts +5 -0
  153. package/bin/shared-hooks/index.d.ts.map +1 -0
  154. package/bin/shared-hooks/index.js +5 -0
  155. package/bin/shared-hooks/index.js.map +1 -0
  156. package/bin/shared-hooks/useDynamicDDS.d.ts +19 -0
  157. package/bin/shared-hooks/useDynamicDDS.d.ts.map +1 -0
  158. package/bin/shared-hooks/useDynamicDDS.js +68 -0
  159. package/bin/shared-hooks/useDynamicDDS.js.map +1 -0
  160. package/bin/shared-hooks/useLockedTask.d.ts +11 -0
  161. package/bin/shared-hooks/useLockedTask.d.ts.map +1 -0
  162. package/bin/shared-hooks/useLockedTask.js +82 -0
  163. package/bin/shared-hooks/useLockedTask.js.map +1 -0
  164. package/bin/shared-hooks/useSharedMap.d.ts +22 -0
  165. package/bin/shared-hooks/useSharedMap.d.ts.map +1 -0
  166. package/bin/shared-hooks/useSharedMap.js +106 -0
  167. package/bin/shared-hooks/useSharedMap.js.map +1 -0
  168. package/bin/shared-hooks/useSharedState.d.ts +21 -0
  169. package/bin/shared-hooks/useSharedState.d.ts.map +1 -0
  170. package/bin/shared-hooks/useSharedState.js +63 -0
  171. package/bin/shared-hooks/useSharedState.js.map +1 -0
  172. package/bin/shared-hooks/useTaskManager.d.ts +13 -0
  173. package/bin/shared-hooks/useTaskManager.d.ts.map +1 -0
  174. package/bin/shared-hooks/useTaskManager.js +86 -0
  175. package/bin/shared-hooks/useTaskManager.js.map +1 -0
  176. package/bin/types/ActionTypes.d.ts +30 -0
  177. package/bin/types/ActionTypes.d.ts.map +1 -0
  178. package/bin/types/ActionTypes.js +6 -0
  179. package/bin/types/ActionTypes.js.map +1 -0
  180. package/bin/types/PropTypes.d.ts +8 -0
  181. package/bin/types/PropTypes.d.ts.map +1 -0
  182. package/bin/types/PropTypes.js +6 -0
  183. package/bin/types/PropTypes.js.map +1 -0
  184. package/bin/types/ResultTypes.d.ts +163 -0
  185. package/bin/types/ResultTypes.d.ts.map +1 -0
  186. package/bin/types/ResultTypes.js +6 -0
  187. package/bin/types/ResultTypes.js.map +1 -0
  188. package/bin/types/index.d.ts +4 -0
  189. package/bin/types/index.d.ts.map +1 -0
  190. package/bin/types/index.js +4 -0
  191. package/bin/types/index.js.map +1 -0
  192. package/bin/utils/getContainerSchema.d.ts +8 -0
  193. package/bin/utils/getContainerSchema.d.ts.map +1 -0
  194. package/bin/utils/getContainerSchema.js +41 -0
  195. package/bin/utils/getContainerSchema.js.map +1 -0
  196. package/bin/utils/index.d.ts +2 -0
  197. package/bin/utils/index.d.ts.map +1 -0
  198. package/bin/utils/index.js +2 -0
  199. package/bin/utils/index.js.map +1 -0
  200. package/bin/utils/type-check.d.ts +15 -0
  201. package/bin/utils/type-check.d.ts.map +1 -0
  202. package/bin/utils/type-check.js +54 -0
  203. package/bin/utils/type-check.js.map +1 -0
  204. package/package.json +47 -0
  205. package/src/index.ts +6 -0
  206. package/src/interfaces/LiveInterfaceExtensions.ts +9 -0
  207. package/src/interfaces/index.ts +1 -0
  208. package/src/internal-hooks/index.ts +1 -0
  209. package/src/internal-hooks/useSharedStateRegistry.ts +163 -0
  210. package/src/live-hooks/index.ts +6 -0
  211. package/src/live-hooks/useLiveCanvas.ts +198 -0
  212. package/src/live-hooks/useLiveEvent.ts +117 -0
  213. package/src/live-hooks/useLivePresence.ts +124 -0
  214. package/src/live-hooks/useLiveState.ts +87 -0
  215. package/src/live-hooks/useLiveTimer.ts +184 -0
  216. package/src/live-hooks/useMediaSynchronizer.ts +200 -0
  217. package/src/providers/AzureProvider.tsx +177 -0
  218. package/src/providers/LiveShareProvider.tsx +140 -0
  219. package/src/providers/index.ts +2 -0
  220. package/src/shared-hooks/index.ts +4 -0
  221. package/src/shared-hooks/useDynamicDDS.ts +71 -0
  222. package/src/shared-hooks/useSharedMap.ts +132 -0
  223. package/src/shared-hooks/useSharedState.ts +85 -0
  224. package/src/shared-hooks/useTaskManager.ts +82 -0
  225. package/src/types/ActionTypes.ts +75 -0
  226. package/src/types/PropTypes.ts +10 -0
  227. package/src/types/ResultTypes.ts +187 -0
  228. package/src/types/index.ts +3 -0
  229. package/src/utils/index.ts +1 -0
  230. package/src/utils/type-check.ts +69 -0
  231. package/tinylicious.log +4 -0
  232. package/tsconfig.build.json +6 -0
  233. package/tsconfig.json +72 -0
@@ -0,0 +1,4 @@
1
+ export * from "./useSharedState";
2
+ export * from "./useSharedMap";
3
+ export * from "./useDynamicDDS";
4
+ export * from "./useTaskManager";
@@ -0,0 +1,71 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import React from "react";
7
+ import { FluidObject, IFluidLoadable } from "@fluidframework/core-interfaces";
8
+ import { useFluidObjectsContext } from "../providers";
9
+ import { LoadableObjectClass } from "fluid-framework";
10
+
11
+ /**
12
+ * Hook to gets or creates a DDS that corresponds to a given uniqueKey string.
13
+ * @template T Type of Fluid LoadableObjectClass type to load. Must conform to IFluidLoadable interface.
14
+ * @param uniqueKey uniqueKey value for the data object
15
+ * @param loadableObjectClass Fluid LoadableObjectClass<T> to create/load.
16
+ * @param onFirstInitialize Optional. Callback function for when the DDS is first loaded
17
+ * @returns the DDS object, which is of type T when loaded and undefined while loading
18
+ */
19
+ export function useDynamicDDS<T extends IFluidLoadable = FluidObject<any> & IFluidLoadable>(
20
+ uniqueKey: string,
21
+ loadableObjectClass: LoadableObjectClass<T>,
22
+ onFirstInitialize?: (dds: T) => void,
23
+ ): {
24
+ dds: T | undefined;
25
+ error: Error | undefined;
26
+ } {
27
+ const [dds, setDDS] = React.useState<T>();
28
+ const [error, setError] = React.useState<Error>();
29
+ /**
30
+ * Import container and DDS object register callbacks from AzureProvider.
31
+ */
32
+ const {
33
+ container,
34
+ clientRef,
35
+ } = useFluidObjectsContext();
36
+
37
+ /**
38
+ * Once container is available, this effect will register the setter method so that the DDS loaded
39
+ * from `dynamicObjects` that matches `uniqueKey` can be passed back to this hook. If one does not yet exist,
40
+ * a new DDS is automatically created. If multiple users try to create a DDS at the same time when this component first
41
+ * mounts, `live-share-turbo` ensures it will ultimately self correct using last-writer wins.
42
+ */
43
+ React.useEffect(() => {
44
+ if (container === undefined) return;
45
+ let mounted = true;
46
+ // Callback method to set the `initialData` into the map when the DDS is first created.
47
+ const onGetDDS = async () => {
48
+ try {
49
+ const dds = await clientRef.current.getDDS<T>(uniqueKey, loadableObjectClass, onFirstInitialize);
50
+ if (mounted) {
51
+ setDDS(dds);
52
+ }
53
+ } catch (error: unknown) {
54
+ if (error instanceof Error) {
55
+ setError(error);
56
+ } else {
57
+ setError(new Error("useDynamicDDS: an unknown error occurred while getting the DDS"));
58
+ }
59
+ }
60
+ };
61
+ onGetDDS();
62
+ return () => {
63
+ mounted = false;
64
+ };
65
+ }, [container, uniqueKey, onFirstInitialize]);
66
+
67
+ return {
68
+ dds,
69
+ error,
70
+ };
71
+ }
@@ -0,0 +1,132 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import React from "react";
7
+ import { isEntries, isJSON, isMap } from "../utils";
8
+ import { IUseSharedMapResults, SharedMapInitialData } from "../types";
9
+ import { useDynamicDDS } from "./useDynamicDDS";
10
+ import { SharedMap } from "fluid-framework";
11
+
12
+ /**
13
+ * React hook for using a Fluid `SharedMap`.
14
+ *
15
+ * @remarks
16
+ * The primary benefit of using the `useSharedMap` hook rather than the Fluid `SharedMap`
17
+ * directly is that it integrates nicely with React state and automates repetitive tasks.
18
+ * If you want to use `SharedMap` this hook creates directly, you can do that as well.
19
+ *
20
+ * @template TData Optional typing for objects stored in the SharedMap. Default is `object` type.
21
+ * @param uniqueKey the unique key for the `SharedMap`. If one does not yet exist, a new `SharedMap`
22
+ * will be created, otherwise it will use the existing one.
23
+ * @param initialData a JS Map, entries array, or JSON object to insert into the `SharedMap` when creating
24
+ * the DDS for the first time.
25
+ * @returns stateful `map` entries, `setEntry` callback, `deleteEntry` callback, and the Fluid `sharedMap`.
26
+ */
27
+ export function useSharedMap<TData extends object = object>(
28
+ uniqueKey: string,
29
+ initialData?: SharedMapInitialData<TData>
30
+ ): IUseSharedMapResults<TData> {
31
+ /**
32
+ * Stateful readonly map (user facing) with most recent values from `SharedMap` and its setter method.
33
+ */
34
+ const [map, setMap] = React.useState<ReadonlyMap<string, TData>>(
35
+ getInitialData<TData>(initialData)
36
+ );
37
+
38
+ const onFirstInitialize = React.useCallback((newDDS: SharedMap): void => {
39
+ /**
40
+ * Callback method to set the `initialData` into the map when the `SharedMap` is first created.
41
+ * Only should be used as a prop to useDynamicDDS.
42
+ */
43
+ getInitialData(initialData).forEach((value, key) => {
44
+ newDDS.set(key, value);
45
+ });
46
+ }, [initialData]);
47
+ /**
48
+ * User facing: dynamically load the SharedMap DDS for the given unique key.
49
+ */
50
+ const { dds: sharedMap } = useDynamicDDS<SharedMap>(uniqueKey, SharedMap, onFirstInitialize);
51
+
52
+ /**
53
+ * User facing: set a value to the Fluid `SharedMap`.
54
+ */
55
+ const setEntry = React.useCallback(
56
+ (key: string, value: TData) => {
57
+ if (!sharedMap) {
58
+ console.error(
59
+ new Error("Cannot call set when sharedMap is undefined")
60
+ );
61
+ return;
62
+ }
63
+ sharedMap.set(key, value);
64
+ },
65
+ [sharedMap]
66
+ );
67
+
68
+ /**
69
+ * User facing: delete a value from the Fluid `SharedMap`.
70
+ */
71
+ const deleteEntry = React.useCallback(
72
+ (key: string) => {
73
+ if (!sharedMap) {
74
+ console.error(
75
+ new Error("Cannot call remove when sharedMap is undefined")
76
+ );
77
+ return;
78
+ }
79
+ sharedMap.delete(key);
80
+ },
81
+ [sharedMap]
82
+ );
83
+
84
+ // Setup change listeners, initial values, etc.
85
+ React.useEffect(() => {
86
+ if (!sharedMap) return;
87
+
88
+ // Register valueChanged listener for `SharedMap`.
89
+ const onValueChanged = () => {
90
+ setMap(new Map<string, TData>(sharedMap.entries()));
91
+ };
92
+ sharedMap.on("valueChanged", onValueChanged);
93
+ // Get initial values from `SharedMap`.
94
+ onValueChanged();
95
+
96
+ return () => {
97
+ // Cleanup on component unmount.
98
+ sharedMap?.off("valueChanged", onValueChanged);
99
+ };
100
+ }, [sharedMap]);
101
+
102
+ return {
103
+ map,
104
+ setEntry,
105
+ deleteEntry,
106
+ sharedMap,
107
+ };
108
+ }
109
+
110
+ /**
111
+ * Helper method for converting different initial data props into a Map<string, TData> to insert into the Fluid SharedMap
112
+ * @template TData Optional typing for objects stored in the SharedMap. Default is `object` type.
113
+ * @param initialData a JS Map, entries array, or JSON object.
114
+ * @returns A Map<string, TData> with the entries provided.
115
+ */
116
+ function getInitialData<TData>(
117
+ initialData: SharedMapInitialData<TData>
118
+ ): Map<string, TData> {
119
+ if (isMap(initialData)) {
120
+ return initialData;
121
+ } else if (isEntries(initialData)) {
122
+ return new Map<string, TData>(initialData);
123
+ } else if (isJSON(initialData)) {
124
+ const values: (readonly [string, TData])[] = Object.keys(
125
+ initialData
126
+ ).map((key) => {
127
+ return [key, initialData[key]];
128
+ });
129
+ return new Map<string, TData>(values);
130
+ }
131
+ return new Map<string, TData>();
132
+ }
@@ -0,0 +1,85 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import React from "react";
7
+ import { useFluidObjectsContext } from "../providers";
8
+ import { DisposeSharedStateAction, SetSharedStateAction } from "../types";
9
+ import { v4 as uuid } from "uuid";
10
+
11
+ /**
12
+ * Inspired by React's `useState` hook, `useSharedState` makes it easy to synchronize state in your app.
13
+ *
14
+ * @remarks
15
+ * Values set through this state is automatically attached to a `SharedMap` that the `AzureProvider`
16
+ * creates. If you are synchronizing complex data structures that multiple users will be setting simultaneously,
17
+ * consider using an optimized hook for your data structure (e.g., `useSharedMap`, `useSharedString`, etc.).
18
+ *
19
+ * @template S Typing for objects stored associated with the `uniqueKey`.
20
+ * @param uniqueKey the unique key for a `SharedMap`. If you use the same key for multiple components, those components will reference the same state.
21
+ * @param initialState a default state for the `SharedMap`.
22
+ *
23
+ * @returns a stateful value, the function to update it, and an optional dispose method to delete it from the `SharedMap`.
24
+ */
25
+ export function useSharedState<S>(
26
+ uniqueKey: string,
27
+ initialState: S
28
+ ): [S, SetSharedStateAction<S>, DisposeSharedStateAction] {
29
+ /**
30
+ * User facing: stateful value and non-user facing setter.
31
+ */
32
+ const [localState, setLocalState] = React.useState<S>(initialState);
33
+ /**
34
+ * Unique ID reference for the component.
35
+ */
36
+ const componentIdRef = React.useRef(uuid());
37
+ /**
38
+ * Register set state callbacks from AzureProvider and update/delete callbacks for initial object's `stateMap`.
39
+ */
40
+ const {
41
+ registerSharedSetStateAction,
42
+ unregisterSharedSetStateAction,
43
+ updateSharedState,
44
+ deleteSharedState,
45
+ } = useFluidObjectsContext();
46
+
47
+ /**
48
+ * User facing: callback to change the shared state.
49
+ */
50
+ const setSharedState: SetSharedStateAction<S> = React.useCallback(
51
+ (updatedState: S) => {
52
+ setLocalState(updatedState);
53
+ updateSharedState(uniqueKey, updatedState);
54
+ },
55
+ [uniqueKey, setLocalState, updateSharedState]
56
+ );
57
+
58
+ /**
59
+ * User facing: callback to dispose the shared state from the `stateMap`.
60
+ */
61
+ const disposeSharedState: DisposeSharedStateAction =
62
+ React.useCallback(() => {
63
+ deleteSharedState(uniqueKey);
64
+ }, [uniqueKey, deleteSharedState]);
65
+
66
+ /**
67
+ * Once container is available, this effect will register the setter method so that the `S` value
68
+ * from `stateMap` that matches `uniqueKey` can be passed back to this hook whenever changed.
69
+ *
70
+ * @see registerSharedSetStateAction to see how new values from `stateMap` are passed to this hook.
71
+ * @see unregisterSharedSetStateAction to see how this component stops listening to changes in the `stateMap`.
72
+ */
73
+ React.useEffect(() => {
74
+ registerSharedSetStateAction(
75
+ uniqueKey,
76
+ componentIdRef.current,
77
+ setLocalState
78
+ );
79
+ return () => {
80
+ unregisterSharedSetStateAction(uniqueKey, componentIdRef.current);
81
+ };
82
+ }, [uniqueKey, registerSharedSetStateAction, unregisterSharedSetStateAction]);
83
+
84
+ return [localState, setSharedState, disposeSharedState];
85
+ }
@@ -0,0 +1,82 @@
1
+ import React from "react";
2
+ import { useDynamicDDS } from "./useDynamicDDS";
3
+ import { TaskManager } from "@fluid-experimental/task-manager";
4
+ import { DynamicObjectRegistry } from "@microsoft/live-share";
5
+
6
+ // Register TaskManager as dynamic object
7
+ DynamicObjectRegistry.registerObjectClass(TaskManager, TaskManager.getFactory().type);
8
+
9
+ /**
10
+ * A hook for joining a queue to lock tasks for a given id. Guaranteed to have only one user assigned to a task at a time.
11
+ *
12
+ * @param uniqueKey the unique key for the TaskManager DDS
13
+ * @param taskId the task id to lock
14
+ * @returns stateful data about the status of the task lock
15
+ */
16
+ export const useTaskManager = (uniqueKey: string, taskId?: string): {
17
+ lockedTask: boolean;
18
+ taskManager: TaskManager | undefined;
19
+ } => {
20
+ /**
21
+ * TaskId currently in queue for
22
+ */
23
+ const currentTaskIdRef = React.useRef<string | undefined>(undefined);
24
+ /**
25
+ * Stateful boolean that is true when the user is currently assigned the task
26
+ */
27
+ const [lockedTask, setLockedTask] = React.useState<boolean>(false);
28
+
29
+ /**
30
+ * User facing: dynamically load the TaskManager DDS for the given unique key.
31
+ */
32
+ const { dds: taskManager } = useDynamicDDS<TaskManager>(uniqueKey, TaskManager);
33
+
34
+ /**
35
+ * When the task id changes, lock the task. When the task id is undefined, abandon the task.
36
+ */
37
+ React.useEffect(() => {
38
+ let mounted = true;
39
+ if (taskManager) {
40
+ if (taskId && currentTaskIdRef.current !== taskId) {
41
+ if (currentTaskIdRef.current) {
42
+ taskManager.abandon(currentTaskIdRef.current);
43
+ setLockedTask(false);
44
+ }
45
+ currentTaskIdRef.current = taskId;
46
+ const onLockTask = async () => {
47
+ try {
48
+ await taskManager.lockTask(taskId);
49
+ if (mounted) {
50
+ setLockedTask(true);
51
+ }
52
+ } catch {
53
+ if (mounted) {
54
+ setLockedTask(false);
55
+ currentTaskIdRef.current = undefined;
56
+ }
57
+ }
58
+ }
59
+ onLockTask();
60
+ } else if (!taskId && currentTaskIdRef.current) {
61
+ taskManager.abandon(currentTaskIdRef.current);
62
+ setLockedTask(false);
63
+ currentTaskIdRef.current = undefined;
64
+ }
65
+ }
66
+ /**
67
+ * When the component unmounts, abandon the task if it is still locked
68
+ */
69
+ return () => {
70
+ mounted = false;
71
+ if (currentTaskIdRef.current) {
72
+ taskManager?.abandon(currentTaskIdRef.current);
73
+ }
74
+ currentTaskIdRef.current = undefined;
75
+ }
76
+ }, [taskManager, taskId]);
77
+
78
+ return {
79
+ lockedTask,
80
+ taskManager,
81
+ };
82
+ };
@@ -0,0 +1,75 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import React from "react";
7
+ import { IFluidLoadable } from "@fluidframework/core-interfaces";
8
+ import { LoadableObjectClass } from "fluid-framework";
9
+ import { ITimerConfig, PresenceState } from "@microsoft/live-share";
10
+
11
+ // React actions
12
+ export type SetSharedStateAction<T> = (state: T) => void;
13
+ export type SetLocalStateAction = React.Dispatch<React.SetStateAction<any>>;
14
+
15
+ // Fluid actions
16
+ export type RegisterDDSSetStateAction = <T extends IFluidLoadable>(
17
+ uniqueKey: string,
18
+ componentId: string,
19
+ objectClass: LoadableObjectClass<T>,
20
+ setLocalStateAction: SetLocalStateAction,
21
+ onDidFirstInitialize?: (dds: T) => void
22
+ ) => void;
23
+
24
+ export type UnregisterDDSSetStateAction = (
25
+ uniqueKey: string,
26
+ componentId: string
27
+ ) => void;
28
+
29
+ export type RegisterSharedSetStateAction = (
30
+ uniqueKey: string,
31
+ componentId: string,
32
+ setLocalStateAction: SetLocalStateAction
33
+ ) => void;
34
+
35
+ export type UnregisterSharedSetStateAction = (
36
+ uniqueKey: string,
37
+ componentId: string
38
+ ) => void;
39
+
40
+ export type UpdateSharedStateAction = (uniqueKey: string, value: any) => void;
41
+
42
+ export type DeleteSharedStateAction = (uniqueKey: string) => void;
43
+ export type DisposeSharedStateAction = () => void;
44
+
45
+ // Live Share actions
46
+
47
+ export type SetLiveStateAction<TState = undefined> = (
48
+ state: TState,
49
+ ) => void;
50
+
51
+ export type SendLiveEventAction<TEvent> = (event: TEvent) => void;
52
+
53
+ export type OnReceivedLiveEventAction<TEvent> = (
54
+ event: TEvent,
55
+ local: boolean
56
+ ) => void;
57
+
58
+ export type OnUpdateLivePresenceAction<TData extends object = object> = (
59
+ state?: PresenceState | undefined,
60
+ data?: TData | undefined
61
+ ) => void;
62
+
63
+ export type OnStartTimerAction = (
64
+ duration: number
65
+ ) => void;
66
+
67
+ export type OnPlayTimerAction = () => void;
68
+
69
+ export type OnPauseTimerAction = () => void;
70
+
71
+ export type OnTimerTickAction = (milliRemaining: number) => void;
72
+ export type OnTimerDidStartAction = (timerConfig: ITimerConfig) => void;
73
+ export type OnTimerDidPauseAction = (timerConfig: ITimerConfig) => void;
74
+ export type OnTimerDidPlayAction = (timerConfig: ITimerConfig) => void;
75
+ export type OnTimerDidFinishAction = (timerConfig: ITimerConfig) => void;
@@ -0,0 +1,10 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ export type SharedMapInitialData<T> =
7
+ | Map<string, T>
8
+ | readonly (readonly [string, T])[]
9
+ | { [key: string]: T }
10
+ | undefined;
@@ -0,0 +1,187 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import { AzureContainerServices } from "@fluidframework/azure-client";
7
+ import {
8
+ ITimerConfig,
9
+ LivePresenceUser,
10
+ LiveEvent,
11
+ LivePresence,
12
+ LiveTimer,
13
+ } from "@microsoft/live-share";
14
+ import { InkingManager, LiveCanvas } from "@microsoft/live-share-canvas";
15
+ import {
16
+ CoordinationWaitPoint,
17
+ ExtendedMediaMetadata,
18
+ MediaPlayerSynchronizer,
19
+ } from "@microsoft/live-share-media";
20
+ import { IFluidContainer, SharedMap } from "fluid-framework";
21
+ import { IReceiveLiveEvent } from "../interfaces";
22
+ import {
23
+ OnPauseTimerAction,
24
+ OnPlayTimerAction,
25
+ OnStartTimerAction,
26
+ OnUpdateLivePresenceAction,
27
+ SendLiveEventAction,
28
+ } from "./ActionTypes";
29
+
30
+ export interface IAzureContainerResults {
31
+ /**
32
+ * Fluid Container.
33
+ */
34
+ container: IFluidContainer;
35
+ /**
36
+ * Azure container services which has information such as current socket connections.
37
+ */
38
+ services: AzureContainerServices;
39
+ }
40
+
41
+ export interface ILiveShareContainerResults extends IAzureContainerResults {
42
+ /**
43
+ * Whether the local user/client initially created the container.
44
+ */
45
+ created: boolean;
46
+ }
47
+
48
+ export interface IUseSharedMapResults<TData> {
49
+ /**
50
+ * Stateful map of most recent values from `SharedMap`.
51
+ */
52
+ map: ReadonlyMap<string, TData>;
53
+ /**
54
+ * Callback method to set/replace new entries in the `SharedMap`.
55
+ */
56
+ setEntry: (key: string, value: TData) => void;
57
+ /**
58
+ * Callback method to delete an existing entry in the `SharedMap`.
59
+ */
60
+ deleteEntry: (key: string) => void;
61
+ /**
62
+ * The Fluid `SharedMap` object, should you want to use it directly.
63
+ */
64
+ sharedMap: SharedMap | undefined;
65
+ }
66
+
67
+ export interface IUseLiveEventResults<TEvent extends object = object> {
68
+ /**
69
+ * The most recent event that has been received in the session.
70
+ */
71
+ latestEvent: IReceiveLiveEvent<TEvent> | undefined;
72
+ /**
73
+ * All received events since initializing this component, sorted from oldest -> newest.
74
+ */
75
+ allEvents: IReceiveLiveEvent<TEvent>[];
76
+ /**
77
+ * Callback method to send a new event to users in the session.
78
+ */
79
+ sendEvent: SendLiveEventAction<TEvent>;
80
+ /**
81
+ * The `LiveEvent` object, should you want to use it directly.
82
+ */
83
+ liveEvent: LiveEvent | undefined;
84
+ }
85
+
86
+ export interface IUseLiveTimerResults {
87
+ /**
88
+ * The current timer configuration.
89
+ */
90
+ timerConfig: ITimerConfig | undefined;
91
+ /**
92
+ * The time remaining in milliseconds.
93
+ */
94
+ milliRemaining: number | undefined;
95
+ /**
96
+ * The `LiveTimer` object, should you want to use it directly.
97
+ */
98
+ liveTimer: LiveTimer | undefined;
99
+ /**
100
+ * Start timer callback.
101
+ */
102
+ start: OnStartTimerAction;
103
+ /**
104
+ * Play timer callback.
105
+ */
106
+ play: OnPlayTimerAction;
107
+ /**
108
+ * Pause timer callback.
109
+ */
110
+ pause: OnPauseTimerAction;
111
+ }
112
+
113
+ export interface IUseLivePresenceResults<TData extends object = object> {
114
+ /**
115
+ * The local user's presence object.
116
+ */
117
+ localUser: LivePresenceUser<TData> | undefined;
118
+ /**
119
+ * List of non-local user's presence objects.
120
+ */
121
+ otherUsers: LivePresenceUser<TData>[];
122
+ /**
123
+ * List of all user's presence objects.
124
+ */
125
+ allUsers: LivePresenceUser<TData>[];
126
+ /**
127
+ * Live Share `LivePresence` object, should you want to use it directly.
128
+ */
129
+ livePresence: LivePresence<TData> | undefined;
130
+ /**
131
+ * Callback method to update the local user's presence.
132
+ */
133
+ updatePresence: OnUpdateLivePresenceAction<TData>;
134
+ }
135
+
136
+ export interface IUseMediaSynchronizerResults {
137
+ /**
138
+ * Stateful boolean on whether the session has an active suspension.
139
+ */
140
+ suspended: boolean;
141
+ /**
142
+ * Callback to initiate a play action for the group media session.
143
+ */
144
+ play: () => void;
145
+ /**
146
+ * Callback to initiate a pause action for the group media session.
147
+ */
148
+ pause: () => void;
149
+ /**
150
+ * Callback to initiate a seek action for the group media session.
151
+ *
152
+ * @param time timestamp of the video in seconds to seek to
153
+ */
154
+ seekTo: (time: number) => void;
155
+ /**
156
+ * Callback to change the track for the group media session.
157
+ * @param track media metadata object, track src string, or null
158
+ */
159
+ setTrack: (track: Partial<ExtendedMediaMetadata> | string | null) => void;
160
+ /**
161
+ * Begin a new suspension. If a wait point is not set, the suspension will only impact the
162
+ * local user.
163
+ *
164
+ * @param waitPoint Optional. Point in track to set the suspension at.
165
+ * @see CoordinationWaitPoint
166
+ */
167
+ beginSuspension: (waitPoint?: CoordinationWaitPoint) => void;
168
+ /**
169
+ * End the currently active exception.
170
+ */
171
+ endSuspension: () => void;
172
+ /**
173
+ * Live Share `MediaPlayerSynchronizer` object, should you want to use it directly.
174
+ */
175
+ mediaSynchronizer: MediaPlayerSynchronizer | undefined;
176
+ }
177
+
178
+ export interface IUseLiveCanvasResults {
179
+ /**
180
+ * Inking manager for the canvas, which is synchronized by `liveCanvas`. It is set when loaded and undefined while loading.
181
+ */
182
+ inkingManager: InkingManager | undefined;
183
+ /**
184
+ * Live Canvas data object which synchronizes inking & cursors in the session. It is set when loaded and undefined while loading.
185
+ */
186
+ liveCanvas: LiveCanvas | undefined;
187
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./ActionTypes";
2
+ export * from "./ResultTypes";
3
+ export * from "./PropTypes";
@@ -0,0 +1 @@
1
+ export * from "./type-check";