@microsoft/live-share-react 1.3.0-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.
Files changed (38) hide show
  1. package/.nycrc.json +9 -9
  2. package/LICENSE +86 -86
  3. package/README.md +608 -608
  4. package/package.json +50 -50
  5. package/src/index.ts +6 -6
  6. package/src/interfaces/LiveInterfaceExtensions.ts +26 -26
  7. package/src/interfaces/index.ts +1 -1
  8. package/src/internal/errors.ts +57 -57
  9. package/src/internal/index.ts +2 -2
  10. package/src/internal/type-guards.ts +9 -9
  11. package/src/live-hooks/index.ts +7 -7
  12. package/src/live-hooks/useLiveCanvas.ts +191 -191
  13. package/src/live-hooks/useLiveEvent.ts +140 -140
  14. package/src/live-hooks/useLiveFollowMode.ts +316 -316
  15. package/src/live-hooks/useLivePresence.ts +147 -147
  16. package/src/live-hooks/useLiveState.ts +110 -110
  17. package/src/live-hooks/useLiveTimer.ts +213 -213
  18. package/src/live-hooks/useMediaSynchronizer.ts +295 -295
  19. package/src/providers/AzureProvider.tsx +261 -261
  20. package/src/providers/LiveShareProvider.tsx +210 -210
  21. package/src/providers/index.ts +2 -2
  22. package/src/shared-hooks/index.ts +4 -4
  23. package/src/shared-hooks/useDynamicDDS.ts +82 -82
  24. package/src/shared-hooks/useSharedMap.ts +159 -159
  25. package/src/shared-hooks/useSharedState.ts +96 -96
  26. package/src/shared-hooks/useSharedStateRegistry.ts +170 -170
  27. package/src/types/ActionTypes.ts +161 -161
  28. package/src/types/PropTypes.ts +10 -10
  29. package/src/types/ResultTypes.ts +332 -332
  30. package/src/types/index.ts +3 -3
  31. package/src/utils/index.ts +1 -1
  32. package/src/utils/type-check.ts +69 -69
  33. package/tsconfig.build.json +5 -5
  34. package/tsconfig.json +72 -72
  35. package/bin/internal/useSharedStateRegistry.d.ts +0 -31
  36. package/bin/internal/useSharedStateRegistry.d.ts.map +0 -1
  37. package/bin/internal/useSharedStateRegistry.js +0 -102
  38. package/bin/internal/useSharedStateRegistry.js.map +0 -1
@@ -1,191 +1,191 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import {
7
- CanvasReferencePoint,
8
- IBrush,
9
- InkingManager,
10
- InkingTool,
11
- IPoint,
12
- LiveCanvas,
13
- } from "@microsoft/live-share-canvas";
14
- import React from "react";
15
- import { useDynamicDDS } from "../shared-hooks";
16
- import { IUseLiveCanvasResults } from "../types";
17
- import { isRefObject } from "../utils";
18
-
19
- /**
20
- * React hook for using a Live Share Canvas `LiveCanvas` and `InkingManager`.
21
- *
22
- * @remarks
23
- * Use this hook to set up an `LiveCanvas` object, which is used for collaborative inking. It takes several input parameters, including a unique key,
24
- * a reference to a canvas element, and various settings for the inking tool, brush, and canvas offset and scale.
25
- * This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
26
- *
27
- * @param uniqueKey the unique key for the `LiveCanvas`. If one does not yet exist, a new one.
28
- * @param canvasElementRef the HTML div element ref or document ID that `InkingManager` will use for canvas-based collaboration.
29
- * @param active Optional. Stateful boolean that will activate/de-activate `InkingManager` accordingly.
30
- * @param tool Optional. Stateful enum for what tool to use in the `InkingManager`.
31
- * @param lineBrush Optional. Stateful lineBrush object for the selected lineBrush options to use in `InkingManager`.
32
- * @param offset Optional. Stateful offset point to use in the `InkingManager`. Gets the viewport offset. Defaults to 0,0.
33
- * @param scale Optional. Stateful scale number to use in the `InkingManager`. Defaults to 1 and must be greater than 0.
34
- * @param referencePoint Optional. Stateful reference point enum to use in the `InkingManger`. Defaults to "center".
35
- * @param isCursorShared Optional. Stateful boolean flag for whether cursor should be shared in `LiveCanvas`. Defaults to false.
36
- * @param localUserPictureUrl Optional. url string for the local user to display alongside their cursor. Defaults to undefined.
37
- * @returns IUseLiveCanvasResults object that contains the `liveCanvas` data object and `inkingManager`.
38
- */
39
- export function useLiveCanvas(
40
- uniqueKey: string,
41
- canvasElementRef: React.RefObject<HTMLElement | null> | string,
42
- active?: boolean,
43
- tool?: InkingTool,
44
- lineBrush?: IBrush,
45
- offset?: IPoint,
46
- scale?: number,
47
- referencePoint?: CanvasReferencePoint,
48
- isCursorShared?: boolean,
49
- localUserPictureUrl?: string
50
- ): IUseLiveCanvasResults {
51
- /**
52
- * User facing: inking manager instance
53
- */
54
- const [inkingManager, setInkingManager] = React.useState<InkingManager>();
55
-
56
- /**
57
- * User facing: dynamically load the DDS for the given unique key.
58
- */
59
- const { dds: liveCanvas } = useDynamicDDS<LiveCanvas>(
60
- uniqueKey,
61
- LiveCanvas
62
- );
63
-
64
- /**
65
- * Setup the InkingManager and LiveCanvas
66
- */
67
- React.useEffect(() => {
68
- // if the component is already listening or liveCanvas is not yet initialized, return
69
- if (!liveCanvas) return;
70
- // get the canvas element from the ref or document
71
- let htmlElement: HTMLElement | null;
72
- if (isRefObject(canvasElementRef)) {
73
- htmlElement = canvasElementRef.current;
74
- } else {
75
- htmlElement = document.getElementById(canvasElementRef);
76
- }
77
- if (htmlElement === null) return;
78
- // Create the InkingManager and initialize the liveCanvas with it
79
- const inkingManager = new InkingManager(htmlElement);
80
- setInkingManager(inkingManager);
81
- liveCanvas.initialize(inkingManager);
82
-
83
- // cleanup function to be called when the component is unmount
84
- return () => {
85
- liveCanvas.dispose();
86
- inkingManager.removeAllListeners();
87
- };
88
- }, [liveCanvas]);
89
-
90
- /**
91
- * Activate or deactivate the inkingManager based on the 'active' prop
92
- */
93
- React.useEffect(() => {
94
- if (inkingManager && active !== undefined) {
95
- if (active) {
96
- inkingManager.activate();
97
- } else {
98
- inkingManager.deactivate();
99
- }
100
- }
101
- }, [active, inkingManager]);
102
-
103
- /**
104
- * Sets the tool of the inkingManager based on the 'tool' prop
105
- */
106
- React.useEffect(() => {
107
- if (inkingManager && tool !== undefined) {
108
- inkingManager.tool = tool;
109
- }
110
- }, [tool, inkingManager]);
111
-
112
- /**
113
- * Sets the offset of the inkingManager based on the 'offset' prop
114
- */
115
- React.useEffect(() => {
116
- if (inkingManager && offset !== undefined) {
117
- inkingManager.offset = {
118
- x: offset.x,
119
- y: offset.y,
120
- };
121
- }
122
- }, [offset?.x, offset?.y, inkingManager]);
123
-
124
- /**
125
- * Sets the scale of the inkingManager based on the 'scale' prop
126
- */
127
- React.useEffect(() => {
128
- if (inkingManager && scale !== undefined) {
129
- inkingManager.scale = scale;
130
- }
131
- }, [scale, inkingManager]);
132
-
133
- /**
134
- * Sets the referencePoint of the inkingManager based on the 'referencePoint' prop
135
- */
136
- React.useEffect(() => {
137
- if (inkingManager && referencePoint !== undefined) {
138
- inkingManager.referencePoint = referencePoint;
139
- }
140
- }, [referencePoint, inkingManager]);
141
-
142
- /**
143
- * Sets the lineBrush of the inkingManager based on the 'lineBrush' prop
144
- */
145
- React.useEffect(() => {
146
- if (inkingManager && lineBrush) {
147
- inkingManager.lineBrush = {
148
- color: lineBrush.color,
149
- tip: lineBrush.tip,
150
- tipSize: lineBrush.tipSize,
151
- type: lineBrush.type,
152
- endArrow: lineBrush.endArrow,
153
- };
154
- }
155
- }, [
156
- lineBrush?.color,
157
- lineBrush?.tip,
158
- lineBrush?.tipSize,
159
- lineBrush?.type,
160
- lineBrush?.endArrow,
161
- inkingManager,
162
- ]);
163
-
164
- /**
165
- * Sets the isCursorShared of the liveCanvas based on the 'isCursorShared' prop
166
- */
167
- React.useEffect(() => {
168
- if (liveCanvas && isCursorShared !== undefined) {
169
- liveCanvas.isCursorShared = isCursorShared;
170
- }
171
- }, [isCursorShared, liveCanvas]);
172
-
173
- /**
174
- * Sets the onGetLocalUserInfo method of the liveCanvas based on the 'localUserCursor' prop
175
- */
176
- React.useEffect(() => {
177
- if (liveCanvas && localUserPictureUrl) {
178
- liveCanvas.onGetLocalUserPictureUrl = (): string | undefined => {
179
- return localUserPictureUrl;
180
- };
181
- }
182
- }, [localUserPictureUrl, liveCanvas]);
183
-
184
- /**
185
- * Return hook response
186
- */
187
- return {
188
- inkingManager,
189
- liveCanvas,
190
- };
191
- }
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import {
7
+ CanvasReferencePoint,
8
+ IBrush,
9
+ InkingManager,
10
+ InkingTool,
11
+ IPoint,
12
+ LiveCanvas,
13
+ } from "@microsoft/live-share-canvas";
14
+ import React from "react";
15
+ import { useDynamicDDS } from "../shared-hooks";
16
+ import { IUseLiveCanvasResults } from "../types";
17
+ import { isRefObject } from "../utils";
18
+
19
+ /**
20
+ * React hook for using a Live Share Canvas `LiveCanvas` and `InkingManager`.
21
+ *
22
+ * @remarks
23
+ * Use this hook to set up an `LiveCanvas` object, which is used for collaborative inking. It takes several input parameters, including a unique key,
24
+ * a reference to a canvas element, and various settings for the inking tool, brush, and canvas offset and scale.
25
+ * This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
26
+ *
27
+ * @param uniqueKey the unique key for the `LiveCanvas`. If one does not yet exist, a new one.
28
+ * @param canvasElementRef the HTML div element ref or document ID that `InkingManager` will use for canvas-based collaboration.
29
+ * @param active Optional. Stateful boolean that will activate/de-activate `InkingManager` accordingly.
30
+ * @param tool Optional. Stateful enum for what tool to use in the `InkingManager`.
31
+ * @param lineBrush Optional. Stateful lineBrush object for the selected lineBrush options to use in `InkingManager`.
32
+ * @param offset Optional. Stateful offset point to use in the `InkingManager`. Gets the viewport offset. Defaults to 0,0.
33
+ * @param scale Optional. Stateful scale number to use in the `InkingManager`. Defaults to 1 and must be greater than 0.
34
+ * @param referencePoint Optional. Stateful reference point enum to use in the `InkingManger`. Defaults to "center".
35
+ * @param isCursorShared Optional. Stateful boolean flag for whether cursor should be shared in `LiveCanvas`. Defaults to false.
36
+ * @param localUserPictureUrl Optional. url string for the local user to display alongside their cursor. Defaults to undefined.
37
+ * @returns IUseLiveCanvasResults object that contains the `liveCanvas` data object and `inkingManager`.
38
+ */
39
+ export function useLiveCanvas(
40
+ uniqueKey: string,
41
+ canvasElementRef: React.RefObject<HTMLElement | null> | string,
42
+ active?: boolean,
43
+ tool?: InkingTool,
44
+ lineBrush?: IBrush,
45
+ offset?: IPoint,
46
+ scale?: number,
47
+ referencePoint?: CanvasReferencePoint,
48
+ isCursorShared?: boolean,
49
+ localUserPictureUrl?: string
50
+ ): IUseLiveCanvasResults {
51
+ /**
52
+ * User facing: inking manager instance
53
+ */
54
+ const [inkingManager, setInkingManager] = React.useState<InkingManager>();
55
+
56
+ /**
57
+ * User facing: dynamically load the DDS for the given unique key.
58
+ */
59
+ const { dds: liveCanvas } = useDynamicDDS<LiveCanvas>(
60
+ uniqueKey,
61
+ LiveCanvas
62
+ );
63
+
64
+ /**
65
+ * Setup the InkingManager and LiveCanvas
66
+ */
67
+ React.useEffect(() => {
68
+ // if the component is already listening or liveCanvas is not yet initialized, return
69
+ if (!liveCanvas) return;
70
+ // get the canvas element from the ref or document
71
+ let htmlElement: HTMLElement | null;
72
+ if (isRefObject(canvasElementRef)) {
73
+ htmlElement = canvasElementRef.current;
74
+ } else {
75
+ htmlElement = document.getElementById(canvasElementRef);
76
+ }
77
+ if (htmlElement === null) return;
78
+ // Create the InkingManager and initialize the liveCanvas with it
79
+ const inkingManager = new InkingManager(htmlElement);
80
+ setInkingManager(inkingManager);
81
+ liveCanvas.initialize(inkingManager);
82
+
83
+ // cleanup function to be called when the component is unmount
84
+ return () => {
85
+ liveCanvas.dispose();
86
+ inkingManager.removeAllListeners();
87
+ };
88
+ }, [liveCanvas]);
89
+
90
+ /**
91
+ * Activate or deactivate the inkingManager based on the 'active' prop
92
+ */
93
+ React.useEffect(() => {
94
+ if (inkingManager && active !== undefined) {
95
+ if (active) {
96
+ inkingManager.activate();
97
+ } else {
98
+ inkingManager.deactivate();
99
+ }
100
+ }
101
+ }, [active, inkingManager]);
102
+
103
+ /**
104
+ * Sets the tool of the inkingManager based on the 'tool' prop
105
+ */
106
+ React.useEffect(() => {
107
+ if (inkingManager && tool !== undefined) {
108
+ inkingManager.tool = tool;
109
+ }
110
+ }, [tool, inkingManager]);
111
+
112
+ /**
113
+ * Sets the offset of the inkingManager based on the 'offset' prop
114
+ */
115
+ React.useEffect(() => {
116
+ if (inkingManager && offset !== undefined) {
117
+ inkingManager.offset = {
118
+ x: offset.x,
119
+ y: offset.y,
120
+ };
121
+ }
122
+ }, [offset?.x, offset?.y, inkingManager]);
123
+
124
+ /**
125
+ * Sets the scale of the inkingManager based on the 'scale' prop
126
+ */
127
+ React.useEffect(() => {
128
+ if (inkingManager && scale !== undefined) {
129
+ inkingManager.scale = scale;
130
+ }
131
+ }, [scale, inkingManager]);
132
+
133
+ /**
134
+ * Sets the referencePoint of the inkingManager based on the 'referencePoint' prop
135
+ */
136
+ React.useEffect(() => {
137
+ if (inkingManager && referencePoint !== undefined) {
138
+ inkingManager.referencePoint = referencePoint;
139
+ }
140
+ }, [referencePoint, inkingManager]);
141
+
142
+ /**
143
+ * Sets the lineBrush of the inkingManager based on the 'lineBrush' prop
144
+ */
145
+ React.useEffect(() => {
146
+ if (inkingManager && lineBrush) {
147
+ inkingManager.lineBrush = {
148
+ color: lineBrush.color,
149
+ tip: lineBrush.tip,
150
+ tipSize: lineBrush.tipSize,
151
+ type: lineBrush.type,
152
+ endArrow: lineBrush.endArrow,
153
+ };
154
+ }
155
+ }, [
156
+ lineBrush?.color,
157
+ lineBrush?.tip,
158
+ lineBrush?.tipSize,
159
+ lineBrush?.type,
160
+ lineBrush?.endArrow,
161
+ inkingManager,
162
+ ]);
163
+
164
+ /**
165
+ * Sets the isCursorShared of the liveCanvas based on the 'isCursorShared' prop
166
+ */
167
+ React.useEffect(() => {
168
+ if (liveCanvas && isCursorShared !== undefined) {
169
+ liveCanvas.isCursorShared = isCursorShared;
170
+ }
171
+ }, [isCursorShared, liveCanvas]);
172
+
173
+ /**
174
+ * Sets the onGetLocalUserInfo method of the liveCanvas based on the 'localUserCursor' prop
175
+ */
176
+ React.useEffect(() => {
177
+ if (liveCanvas && localUserPictureUrl) {
178
+ liveCanvas.onGetLocalUserPictureUrl = (): string | undefined => {
179
+ return localUserPictureUrl;
180
+ };
181
+ }
182
+ }, [localUserPictureUrl, liveCanvas]);
183
+
184
+ /**
185
+ * Return hook response
186
+ */
187
+ return {
188
+ inkingManager,
189
+ liveCanvas,
190
+ };
191
+ }
@@ -1,140 +1,140 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import {
7
- LiveEventEvents,
8
- UserMeetingRole,
9
- LiveEvent,
10
- LiveDataObjectInitializeState,
11
- } from "@microsoft/live-share";
12
- import React from "react";
13
- import {
14
- IUseLiveEventResults,
15
- OnReceivedLiveEventAction,
16
- SendLiveEventAction,
17
- } from "../types";
18
- import { IReceiveLiveEvent } from "../interfaces";
19
- import { useDynamicDDS } from "../shared-hooks";
20
- import {
21
- ActionContainerNotJoinedError,
22
- ActionLiveDataObjectInitializedError,
23
- ActionLiveDataObjectUndefinedError,
24
- } from "../internal";
25
- import { useFluidObjectsContext } from "../providers";
26
-
27
- /**
28
- * React hook for using a Live Share `LiveEvent`.
29
- *
30
- * @remarks
31
- * Use this hook if you want to send transient JSON objects to everyone connected to the Fluid container,
32
- * such as when sending push notifications or reactions.
33
- * This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
34
- *
35
- * @template TEvent Optional typing for events sent & received. Default is `object` type.
36
- * @param uniqueKey the unique key for the `LiveEvent`. If one does not yet exist, a new one
37
- * will be created, otherwise it will use the existing one.
38
- * @param allowedRoles Optional. The meeting roles eligible to send events through this object.
39
- * @param onReceivedEvent Optional. Callback method to be called when a new notification is received.
40
- * @returns stateful `latestEvent` & `allEvents` list, `sendEvent` callback, and the `liveEvent` object.
41
- */
42
- export function useLiveEvent<TEvent = any>(
43
- uniqueKey: string,
44
- allowedRoles?: UserMeetingRole[],
45
- onReceivedEvent?: OnReceivedLiveEventAction<TEvent>
46
- ): IUseLiveEventResults<TEvent> {
47
- /**
48
- * Stateful latest event (user facing) and its non-user-facing setter method.
49
- */
50
- const [latestEvent, setLatestReceived] =
51
- React.useState<IReceiveLiveEvent<TEvent>>();
52
- /**
53
- * Reference for all received/sent events. The current value of this is user-facing. Because
54
- * this is always set at the same time as latestEvent, it is effectively a stateful value.
55
- */
56
- const allEventsRef = React.useRef<IReceiveLiveEvent<TEvent>[]>([]);
57
- /**
58
- * User facing: dynamically load the DDS for the given unique key.
59
- */
60
- const { dds: liveEvent } = useDynamicDDS<LiveEvent<TEvent>>(
61
- uniqueKey,
62
- LiveEvent<TEvent>
63
- );
64
-
65
- const { container } = useFluidObjectsContext();
66
-
67
- /**
68
- * User facing: callback to send event through `LiveEvent`
69
- */
70
- const sendEvent: SendLiveEventAction<TEvent> = React.useCallback(
71
- async (event: TEvent) => {
72
- if (!container) {
73
- throw new ActionContainerNotJoinedError(
74
- "liveEvent",
75
- "sendEvent"
76
- );
77
- }
78
- if (liveEvent === undefined) {
79
- throw new ActionLiveDataObjectUndefinedError(
80
- "liveEvent",
81
- "sendEvent"
82
- );
83
- }
84
- if (!liveEvent.isInitialized) {
85
- throw new ActionLiveDataObjectInitializedError(
86
- "liveEvent",
87
- "sendEvent"
88
- );
89
- }
90
- return await liveEvent.send(event);
91
- },
92
- [container, liveEvent]
93
- );
94
-
95
- /**
96
- * Setup change listeners and start `LiveEvent` if needed
97
- */
98
- React.useEffect(() => {
99
- if (liveEvent?.isInitialized === undefined) return;
100
- // Register event listener
101
- const onEventReceived = (
102
- event: TEvent,
103
- local: boolean,
104
- clientId: string,
105
- timestamp: number
106
- ) => {
107
- // If developer passed the optional onReceivedEvent callback, we
108
- // call it.
109
- onReceivedEvent?.(event, local);
110
- // Set the received event to our local state
111
- const received: IReceiveLiveEvent<TEvent> = {
112
- value: event,
113
- local,
114
- clientId,
115
- timestamp,
116
- };
117
- allEventsRef.current = [...allEventsRef.current, received];
118
- setLatestReceived(received);
119
- };
120
- liveEvent.on(LiveEventEvents.received, onEventReceived);
121
- if (
122
- liveEvent.initializeState === LiveDataObjectInitializeState.needed
123
- ) {
124
- // Start live event
125
- liveEvent.initialize(allowedRoles);
126
- }
127
-
128
- return () => {
129
- // on unmount, remove event listeners
130
- liveEvent?.off(LiveEventEvents.received, onEventReceived);
131
- };
132
- }, [liveEvent]);
133
-
134
- return {
135
- latestEvent,
136
- allEvents: allEventsRef.current,
137
- sendEvent,
138
- liveEvent,
139
- };
140
- }
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import {
7
+ LiveEventEvents,
8
+ UserMeetingRole,
9
+ LiveEvent,
10
+ LiveDataObjectInitializeState,
11
+ } from "@microsoft/live-share";
12
+ import React from "react";
13
+ import {
14
+ IUseLiveEventResults,
15
+ OnReceivedLiveEventAction,
16
+ SendLiveEventAction,
17
+ } from "../types";
18
+ import { IReceiveLiveEvent } from "../interfaces";
19
+ import { useDynamicDDS } from "../shared-hooks";
20
+ import {
21
+ ActionContainerNotJoinedError,
22
+ ActionLiveDataObjectInitializedError,
23
+ ActionLiveDataObjectUndefinedError,
24
+ } from "../internal";
25
+ import { useFluidObjectsContext } from "../providers";
26
+
27
+ /**
28
+ * React hook for using a Live Share `LiveEvent`.
29
+ *
30
+ * @remarks
31
+ * Use this hook if you want to send transient JSON objects to everyone connected to the Fluid container,
32
+ * such as when sending push notifications or reactions.
33
+ * This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
34
+ *
35
+ * @template TEvent Optional typing for events sent & received. Default is `object` type.
36
+ * @param uniqueKey the unique key for the `LiveEvent`. If one does not yet exist, a new one
37
+ * will be created, otherwise it will use the existing one.
38
+ * @param allowedRoles Optional. The meeting roles eligible to send events through this object.
39
+ * @param onReceivedEvent Optional. Callback method to be called when a new notification is received.
40
+ * @returns stateful `latestEvent` & `allEvents` list, `sendEvent` callback, and the `liveEvent` object.
41
+ */
42
+ export function useLiveEvent<TEvent = any>(
43
+ uniqueKey: string,
44
+ allowedRoles?: UserMeetingRole[],
45
+ onReceivedEvent?: OnReceivedLiveEventAction<TEvent>
46
+ ): IUseLiveEventResults<TEvent> {
47
+ /**
48
+ * Stateful latest event (user facing) and its non-user-facing setter method.
49
+ */
50
+ const [latestEvent, setLatestReceived] =
51
+ React.useState<IReceiveLiveEvent<TEvent>>();
52
+ /**
53
+ * Reference for all received/sent events. The current value of this is user-facing. Because
54
+ * this is always set at the same time as latestEvent, it is effectively a stateful value.
55
+ */
56
+ const allEventsRef = React.useRef<IReceiveLiveEvent<TEvent>[]>([]);
57
+ /**
58
+ * User facing: dynamically load the DDS for the given unique key.
59
+ */
60
+ const { dds: liveEvent } = useDynamicDDS<LiveEvent<TEvent>>(
61
+ uniqueKey,
62
+ LiveEvent<TEvent>
63
+ );
64
+
65
+ const { container } = useFluidObjectsContext();
66
+
67
+ /**
68
+ * User facing: callback to send event through `LiveEvent`
69
+ */
70
+ const sendEvent: SendLiveEventAction<TEvent> = React.useCallback(
71
+ async (event: TEvent) => {
72
+ if (!container) {
73
+ throw new ActionContainerNotJoinedError(
74
+ "liveEvent",
75
+ "sendEvent"
76
+ );
77
+ }
78
+ if (liveEvent === undefined) {
79
+ throw new ActionLiveDataObjectUndefinedError(
80
+ "liveEvent",
81
+ "sendEvent"
82
+ );
83
+ }
84
+ if (!liveEvent.isInitialized) {
85
+ throw new ActionLiveDataObjectInitializedError(
86
+ "liveEvent",
87
+ "sendEvent"
88
+ );
89
+ }
90
+ return await liveEvent.send(event);
91
+ },
92
+ [container, liveEvent]
93
+ );
94
+
95
+ /**
96
+ * Setup change listeners and start `LiveEvent` if needed
97
+ */
98
+ React.useEffect(() => {
99
+ if (liveEvent?.isInitialized === undefined) return;
100
+ // Register event listener
101
+ const onEventReceived = (
102
+ event: TEvent,
103
+ local: boolean,
104
+ clientId: string,
105
+ timestamp: number
106
+ ) => {
107
+ // If developer passed the optional onReceivedEvent callback, we
108
+ // call it.
109
+ onReceivedEvent?.(event, local);
110
+ // Set the received event to our local state
111
+ const received: IReceiveLiveEvent<TEvent> = {
112
+ value: event,
113
+ local,
114
+ clientId,
115
+ timestamp,
116
+ };
117
+ allEventsRef.current = [...allEventsRef.current, received];
118
+ setLatestReceived(received);
119
+ };
120
+ liveEvent.on(LiveEventEvents.received, onEventReceived);
121
+ if (
122
+ liveEvent.initializeState === LiveDataObjectInitializeState.needed
123
+ ) {
124
+ // Start live event
125
+ liveEvent.initialize(allowedRoles);
126
+ }
127
+
128
+ return () => {
129
+ // on unmount, remove event listeners
130
+ liveEvent?.off(LiveEventEvents.received, onEventReceived);
131
+ };
132
+ }, [liveEvent]);
133
+
134
+ return {
135
+ latestEvent,
136
+ allEvents: allEventsRef.current,
137
+ sendEvent,
138
+ liveEvent,
139
+ };
140
+ }