@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,213 +1,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
- // 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
+ // 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
+ }