@microsoft/live-share-react 1.3.0-preview.0 → 1.4.0-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 (53) hide show
  1. package/.nycrc.json +9 -9
  2. package/LICENSE +86 -86
  3. package/README.md +608 -608
  4. package/bin/live-hooks/useLiveEvent.d.ts.map +1 -1
  5. package/bin/live-hooks/useLiveEvent.js +6 -0
  6. package/bin/live-hooks/useLiveEvent.js.map +1 -1
  7. package/bin/live-hooks/useLiveFollowMode.d.ts.map +1 -1
  8. package/bin/live-hooks/useLiveFollowMode.js +2 -4
  9. package/bin/live-hooks/useLiveFollowMode.js.map +1 -1
  10. package/bin/live-hooks/useLivePresence.d.ts.map +1 -1
  11. package/bin/live-hooks/useLivePresence.js +1 -3
  12. package/bin/live-hooks/useLivePresence.js.map +1 -1
  13. package/bin/live-hooks/useLiveState.d.ts.map +1 -1
  14. package/bin/live-hooks/useLiveState.js +1 -3
  15. package/bin/live-hooks/useLiveState.js.map +1 -1
  16. package/bin/live-hooks/useLiveTimer.d.ts.map +1 -1
  17. package/bin/live-hooks/useLiveTimer.js +4 -0
  18. package/bin/live-hooks/useLiveTimer.js.map +1 -1
  19. package/package.json +50 -50
  20. package/src/index.ts +6 -6
  21. package/src/interfaces/LiveInterfaceExtensions.ts +26 -26
  22. package/src/interfaces/index.ts +1 -1
  23. package/src/internal/errors.ts +57 -57
  24. package/src/internal/index.ts +2 -2
  25. package/src/internal/type-guards.ts +9 -9
  26. package/src/live-hooks/index.ts +7 -7
  27. package/src/live-hooks/useLiveCanvas.ts +191 -191
  28. package/src/live-hooks/useLiveEvent.ts +146 -140
  29. package/src/live-hooks/useLiveFollowMode.ts +315 -316
  30. package/src/live-hooks/useLivePresence.ts +146 -147
  31. package/src/live-hooks/useLiveState.ts +108 -110
  32. package/src/live-hooks/useLiveTimer.ts +217 -213
  33. package/src/live-hooks/useMediaSynchronizer.ts +295 -295
  34. package/src/providers/AzureProvider.tsx +261 -261
  35. package/src/providers/LiveShareProvider.tsx +210 -210
  36. package/src/providers/index.ts +2 -2
  37. package/src/shared-hooks/index.ts +4 -4
  38. package/src/shared-hooks/useDynamicDDS.ts +82 -82
  39. package/src/shared-hooks/useSharedMap.ts +159 -159
  40. package/src/shared-hooks/useSharedState.ts +96 -96
  41. package/src/shared-hooks/useSharedStateRegistry.ts +170 -170
  42. package/src/types/ActionTypes.ts +161 -161
  43. package/src/types/PropTypes.ts +10 -10
  44. package/src/types/ResultTypes.ts +332 -332
  45. package/src/types/index.ts +3 -3
  46. package/src/utils/index.ts +1 -1
  47. package/src/utils/type-check.ts +69 -69
  48. package/tsconfig.build.json +5 -5
  49. package/tsconfig.json +72 -72
  50. package/bin/internal/useSharedStateRegistry.d.ts +0 -31
  51. package/bin/internal/useSharedStateRegistry.d.ts.map +0 -1
  52. package/bin/internal/useSharedStateRegistry.js +0 -102
  53. package/bin/internal/useSharedStateRegistry.js.map +0 -1
@@ -1,213 +1,217 @@
1
- /*!
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import {
7
- ITimerConfig,
8
- LiveTimerEvents,
9
- UserMeetingRole,
10
- LiveTimer,
11
- LiveDataObjectInitializeState,
12
- } from "@microsoft/live-share";
13
- import React from "react";
14
- import {
15
- IUseLiveTimerResults,
16
- OnPauseTimerAction,
17
- OnPlayTimerAction,
18
- OnStartTimerAction,
19
- OnTimerDidFinishAction,
20
- OnTimerDidPauseAction,
21
- OnTimerDidPlayAction,
22
- OnTimerDidStartAction,
23
- OnTimerTickAction,
24
- } from "../types";
25
- import { useDynamicDDS } from "../shared-hooks";
26
- import { useFluidObjectsContext } from "../providers";
27
- import {
28
- ActionContainerNotJoinedError,
29
- ActionLiveDataObjectInitializedError,
30
- ActionLiveDataObjectUndefinedError,
31
- } from "../internal";
32
-
33
- /**
34
- * React hook for using Live Share's `LiveTimer` DDS.
35
- *
36
- * @remarks
37
- * Use this hook to create a synchronized countdown timer in your app or to schedule synchronized tasks.
38
- * This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
39
- *
40
- * @param uniqueKey the unique key for the `LiveTimer`. If one does not yet exist, a new one will be created, otherwise it will use the existing one.
41
- * @param allowedRoles Optional. the user roles that are allowed to start/stop/pause the timer.
42
- * @param tickRate Optional. the interval in which the `milliRemaining` state should be updated and the `onTick` callback will trigger.
43
- * @param onTick Optional. event handler callback for when an active timer ticks.
44
- * @param onStart Optional. event handler callback for when the timer is started.
45
- * @param onPause Optional. event handler callback for when the timer is paused.
46
- * @param onPlay Optional. event handler callback for when the timer is resumed.
47
- * @param onFinish Optional. event handler callback for when the timer finishes.
48
- * @returns results and callbacks exposed via the hook.
49
- */
50
- export function useLiveTimer(
51
- uniqueKey: string,
52
- allowedRoles?: UserMeetingRole[],
53
- tickRate?: number,
54
- onTick?: OnTimerTickAction,
55
- onStart?: OnTimerDidStartAction,
56
- onPause?: OnTimerDidPauseAction,
57
- onPlay?: OnTimerDidPlayAction,
58
- onFinish?: OnTimerDidFinishAction
59
- ): IUseLiveTimerResults {
60
- /**
61
- * User facing: Stateful timer config.
62
- */
63
- const [timerConfig, setTimerConfig] = React.useState<ITimerConfig>();
64
- /**
65
- * User facing: Stateful time remaining.
66
- */
67
- const [milliRemaining, setMilliRemaining] = React.useState<number>();
68
- /**
69
- * User facing: dynamically load the DDS for the given unique key.
70
- */
71
- const { dds: liveTimer } = useDynamicDDS<LiveTimer>(uniqueKey, LiveTimer);
72
-
73
- const { container } = useFluidObjectsContext();
74
-
75
- /**
76
- * Callback to send event through `LiveTimer`
77
- * @param duration the duration for the timer in milliseconds
78
- * @returns void promise that will throw when user does not have required roles
79
- */
80
- const start: OnStartTimerAction = React.useCallback(
81
- async (duration: number) => {
82
- if (!container) {
83
- throw new ActionContainerNotJoinedError("liveTimer", "start");
84
- }
85
- if (liveTimer === undefined) {
86
- throw new ActionLiveDataObjectUndefinedError(
87
- "liveTimer",
88
- "start"
89
- );
90
- }
91
- if (!liveTimer.isInitialized) {
92
- throw new ActionLiveDataObjectInitializedError(
93
- "liveTimer",
94
- "start"
95
- );
96
- }
97
- return await liveTimer.start(duration);
98
- },
99
- [container, liveTimer]
100
- );
101
-
102
- /**
103
- * Callback to send event through `LiveTimer`
104
- * @returns void promise that will throw when user does not have required roles
105
- */
106
- const play: OnPlayTimerAction = React.useCallback(async () => {
107
- if (!container) {
108
- throw new ActionContainerNotJoinedError("liveTimer", "play");
109
- }
110
- if (liveTimer === undefined) {
111
- throw new ActionLiveDataObjectUndefinedError("liveTimer", "play");
112
- }
113
- if (!liveTimer.isInitialized) {
114
- throw new ActionLiveDataObjectInitializedError("liveTimer", "play");
115
- }
116
- return await liveTimer.play();
117
- }, [container, liveTimer]);
118
-
119
- /**
120
- * Callback to send event through `LiveTimer`
121
- * @returns void promise that will throw when user does not have required roles
122
- */
123
- const pause: OnPauseTimerAction = React.useCallback(async () => {
124
- if (!container) {
125
- throw new ActionContainerNotJoinedError("liveTimer", "pause");
126
- }
127
- if (liveTimer === undefined) {
128
- throw new ActionLiveDataObjectUndefinedError("liveTimer", "pause");
129
- }
130
- if (!liveTimer.isInitialized) {
131
- throw new ActionLiveDataObjectInitializedError(
132
- "liveTimer",
133
- "pause"
134
- );
135
- }
136
- return await liveTimer.pause();
137
- }, [container, liveTimer]);
138
-
139
- /**
140
- * Setup change listeners and start `LiveTimer` if needed
141
- */
142
- React.useEffect(() => {
143
- if (liveTimer === undefined) return;
144
- // Register event listeners
145
- const onTimerConfigChange = (config: ITimerConfig) => {
146
- setTimerConfig(config);
147
- };
148
- const onDidStart = (config: ITimerConfig) => {
149
- onTimerConfigChange(config);
150
- onStart?.(config);
151
- setMilliRemaining(config.duration);
152
- };
153
- const onDidFinish = (config: ITimerConfig) => {
154
- onTimerConfigChange(config);
155
- onFinish?.(config);
156
- setMilliRemaining(0);
157
- };
158
- const onDidPlay = (config: ITimerConfig) => {
159
- onTimerConfigChange(config);
160
- onPlay?.(config);
161
- };
162
- const onDidPause = (config: ITimerConfig) => {
163
- onTimerConfigChange(config);
164
- onPause?.(config);
165
- };
166
- const onDidTick = (milliseconds: number) => {
167
- setMilliRemaining(milliseconds);
168
- onTick?.(milliseconds);
169
- };
170
- liveTimer.on(LiveTimerEvents.started, onDidStart);
171
- liveTimer.on(LiveTimerEvents.finished, onDidFinish);
172
- liveTimer.on(LiveTimerEvents.played, onDidPlay);
173
- liveTimer.on(LiveTimerEvents.paused, onDidPause);
174
- liveTimer.on(LiveTimerEvents.onTick, onDidTick);
175
- if (
176
- liveTimer.initializeState === LiveDataObjectInitializeState.needed
177
- ) {
178
- // Start live event
179
- liveTimer.initialize(allowedRoles);
180
- }
181
-
182
- return () => {
183
- // on unmount, remove event listeners
184
- liveTimer?.off(LiveTimerEvents.started, onDidStart);
185
- liveTimer?.off(LiveTimerEvents.finished, onDidFinish);
186
- liveTimer?.off(LiveTimerEvents.played, onDidPlay);
187
- liveTimer?.off(LiveTimerEvents.paused, onDidPause);
188
- liveTimer?.off(LiveTimerEvents.onTick, onDidTick);
189
- };
190
- }, [liveTimer, onTick, onStart, onFinish, onPlay, onPause]);
191
-
192
- /**
193
- * Change tick rate if changes in props
194
- */
195
- React.useEffect(() => {
196
- if (
197
- tickRate !== undefined &&
198
- liveTimer?.tickRate !== undefined &&
199
- tickRate !== liveTimer?.tickRate
200
- ) {
201
- liveTimer.tickRate = tickRate;
202
- }
203
- }, [tickRate, liveTimer?.tickRate]);
204
-
205
- return {
206
- liveTimer,
207
- milliRemaining,
208
- pause,
209
- play,
210
- start,
211
- timerConfig,
212
- };
213
- }
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import {
7
+ ITimerConfig,
8
+ LiveTimerEvents,
9
+ UserMeetingRole,
10
+ LiveTimer,
11
+ LiveDataObjectInitializeState,
12
+ } from "@microsoft/live-share";
13
+ import React from "react";
14
+ import {
15
+ IUseLiveTimerResults,
16
+ OnPauseTimerAction,
17
+ OnPlayTimerAction,
18
+ OnStartTimerAction,
19
+ OnTimerDidFinishAction,
20
+ OnTimerDidPauseAction,
21
+ OnTimerDidPlayAction,
22
+ OnTimerDidStartAction,
23
+ OnTimerTickAction,
24
+ } from "../types";
25
+ import { useDynamicDDS } from "../shared-hooks";
26
+ import { useFluidObjectsContext } from "../providers";
27
+ import {
28
+ ActionContainerNotJoinedError,
29
+ ActionLiveDataObjectInitializedError,
30
+ ActionLiveDataObjectUndefinedError,
31
+ } from "../internal";
32
+
33
+ /**
34
+ * React hook for using Live Share's `LiveTimer` DDS.
35
+ *
36
+ * @remarks
37
+ * Use this hook to create a synchronized countdown timer in your app or to schedule synchronized tasks.
38
+ * This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
39
+ *
40
+ * @param uniqueKey the unique key for the `LiveTimer`. If one does not yet exist, a new one will be created, otherwise it will use the existing one.
41
+ * @param allowedRoles Optional. the user roles that are allowed to start/stop/pause the timer.
42
+ * @param tickRate Optional. the interval in which the `milliRemaining` state should be updated and the `onTick` callback will trigger.
43
+ * @param onTick Optional. event handler callback for when an active timer ticks.
44
+ * @param onStart Optional. event handler callback for when the timer is started.
45
+ * @param onPause Optional. event handler callback for when the timer is paused.
46
+ * @param onPlay Optional. event handler callback for when the timer is resumed.
47
+ * @param onFinish Optional. event handler callback for when the timer finishes.
48
+ * @returns results and callbacks exposed via the hook.
49
+ */
50
+ export function useLiveTimer(
51
+ uniqueKey: string,
52
+ allowedRoles?: UserMeetingRole[],
53
+ tickRate?: number,
54
+ onTick?: OnTimerTickAction,
55
+ onStart?: OnTimerDidStartAction,
56
+ onPause?: OnTimerDidPauseAction,
57
+ onPlay?: OnTimerDidPlayAction,
58
+ onFinish?: OnTimerDidFinishAction
59
+ ): IUseLiveTimerResults {
60
+ /**
61
+ * User facing: Stateful timer config.
62
+ */
63
+ const [timerConfig, setTimerConfig] = React.useState<ITimerConfig>();
64
+ /**
65
+ * User facing: Stateful time remaining.
66
+ */
67
+ const [milliRemaining, setMilliRemaining] = React.useState<number>();
68
+ /**
69
+ * User facing: dynamically load the DDS for the given unique key.
70
+ */
71
+ const { dds: liveTimer } = useDynamicDDS<LiveTimer>(uniqueKey, LiveTimer);
72
+
73
+ const { container } = useFluidObjectsContext();
74
+
75
+ /**
76
+ * Callback to send event through `LiveTimer`
77
+ * @param duration the duration for the timer in milliseconds
78
+ * @returns void promise that will throw when user does not have required roles
79
+ */
80
+ const start: OnStartTimerAction = React.useCallback(
81
+ async (duration: number) => {
82
+ if (!container) {
83
+ throw new ActionContainerNotJoinedError("liveTimer", "start");
84
+ }
85
+ if (liveTimer === undefined) {
86
+ throw new ActionLiveDataObjectUndefinedError(
87
+ "liveTimer",
88
+ "start"
89
+ );
90
+ }
91
+ if (!liveTimer.isInitialized) {
92
+ throw new ActionLiveDataObjectInitializedError(
93
+ "liveTimer",
94
+ "start"
95
+ );
96
+ }
97
+ return await liveTimer.start(duration);
98
+ },
99
+ [container, liveTimer]
100
+ );
101
+
102
+ /**
103
+ * Callback to send event through `LiveTimer`
104
+ * @returns void promise that will throw when user does not have required roles
105
+ */
106
+ const play: OnPlayTimerAction = React.useCallback(async () => {
107
+ if (!container) {
108
+ throw new ActionContainerNotJoinedError("liveTimer", "play");
109
+ }
110
+ if (liveTimer === undefined) {
111
+ throw new ActionLiveDataObjectUndefinedError("liveTimer", "play");
112
+ }
113
+ if (!liveTimer.isInitialized) {
114
+ throw new ActionLiveDataObjectInitializedError("liveTimer", "play");
115
+ }
116
+ return await liveTimer.play();
117
+ }, [container, liveTimer]);
118
+
119
+ /**
120
+ * Callback to send event through `LiveTimer`
121
+ * @returns void promise that will throw when user does not have required roles
122
+ */
123
+ const pause: OnPauseTimerAction = React.useCallback(async () => {
124
+ if (!container) {
125
+ throw new ActionContainerNotJoinedError("liveTimer", "pause");
126
+ }
127
+ if (liveTimer === undefined) {
128
+ throw new ActionLiveDataObjectUndefinedError("liveTimer", "pause");
129
+ }
130
+ if (!liveTimer.isInitialized) {
131
+ throw new ActionLiveDataObjectInitializedError(
132
+ "liveTimer",
133
+ "pause"
134
+ );
135
+ }
136
+ return await liveTimer.pause();
137
+ }, [container, liveTimer]);
138
+
139
+ /**
140
+ * Setup change listeners and start `LiveTimer` if needed
141
+ */
142
+ React.useEffect(() => {
143
+ if (liveTimer === undefined) return;
144
+ // Reset default values, which is needed for when the `id` changes and a new DDS is loaded.
145
+ // React does not re-render if the values are already set to defaults (e.g., initial DDS).
146
+ setTimerConfig(undefined);
147
+ setMilliRemaining(undefined);
148
+ // Register event listeners
149
+ const onTimerConfigChange = (config: ITimerConfig) => {
150
+ setTimerConfig(config);
151
+ };
152
+ const onDidStart = (config: ITimerConfig) => {
153
+ onTimerConfigChange(config);
154
+ onStart?.(config);
155
+ setMilliRemaining(config.duration);
156
+ };
157
+ const onDidFinish = (config: ITimerConfig) => {
158
+ onTimerConfigChange(config);
159
+ onFinish?.(config);
160
+ setMilliRemaining(0);
161
+ };
162
+ const onDidPlay = (config: ITimerConfig) => {
163
+ onTimerConfigChange(config);
164
+ onPlay?.(config);
165
+ };
166
+ const onDidPause = (config: ITimerConfig) => {
167
+ onTimerConfigChange(config);
168
+ onPause?.(config);
169
+ };
170
+ const onDidTick = (milliseconds: number) => {
171
+ setMilliRemaining(milliseconds);
172
+ onTick?.(milliseconds);
173
+ };
174
+ liveTimer.on(LiveTimerEvents.started, onDidStart);
175
+ liveTimer.on(LiveTimerEvents.finished, onDidFinish);
176
+ liveTimer.on(LiveTimerEvents.played, onDidPlay);
177
+ liveTimer.on(LiveTimerEvents.paused, onDidPause);
178
+ liveTimer.on(LiveTimerEvents.onTick, onDidTick);
179
+ if (
180
+ liveTimer.initializeState === LiveDataObjectInitializeState.needed
181
+ ) {
182
+ // Start live event
183
+ liveTimer.initialize(allowedRoles);
184
+ }
185
+
186
+ return () => {
187
+ // on unmount, remove event listeners
188
+ liveTimer?.off(LiveTimerEvents.started, onDidStart);
189
+ liveTimer?.off(LiveTimerEvents.finished, onDidFinish);
190
+ liveTimer?.off(LiveTimerEvents.played, onDidPlay);
191
+ liveTimer?.off(LiveTimerEvents.paused, onDidPause);
192
+ liveTimer?.off(LiveTimerEvents.onTick, onDidTick);
193
+ };
194
+ }, [liveTimer, onTick, onStart, onFinish, onPlay, onPause]);
195
+
196
+ /**
197
+ * Change tick rate if changes in props
198
+ */
199
+ React.useEffect(() => {
200
+ if (
201
+ tickRate !== undefined &&
202
+ liveTimer?.tickRate !== undefined &&
203
+ tickRate !== liveTimer?.tickRate
204
+ ) {
205
+ liveTimer.tickRate = tickRate;
206
+ }
207
+ }, [tickRate, liveTimer?.tickRate]);
208
+
209
+ return {
210
+ liveTimer,
211
+ milliRemaining,
212
+ pause,
213
+ play,
214
+ start,
215
+ timerConfig,
216
+ };
217
+ }