@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.
- package/.nycrc.json +9 -9
- package/LICENSE +86 -86
- package/README.md +608 -608
- package/package.json +50 -50
- package/src/index.ts +6 -6
- package/src/interfaces/LiveInterfaceExtensions.ts +26 -26
- package/src/interfaces/index.ts +1 -1
- package/src/internal/errors.ts +57 -57
- package/src/internal/index.ts +2 -2
- package/src/internal/type-guards.ts +9 -9
- package/src/live-hooks/index.ts +7 -7
- package/src/live-hooks/useLiveCanvas.ts +191 -191
- package/src/live-hooks/useLiveEvent.ts +140 -140
- package/src/live-hooks/useLiveFollowMode.ts +316 -316
- package/src/live-hooks/useLivePresence.ts +147 -147
- package/src/live-hooks/useLiveState.ts +110 -110
- package/src/live-hooks/useLiveTimer.ts +213 -213
- package/src/live-hooks/useMediaSynchronizer.ts +295 -295
- package/src/providers/AzureProvider.tsx +261 -261
- package/src/providers/LiveShareProvider.tsx +210 -210
- package/src/providers/index.ts +2 -2
- package/src/shared-hooks/index.ts +4 -4
- package/src/shared-hooks/useDynamicDDS.ts +82 -82
- package/src/shared-hooks/useSharedMap.ts +159 -159
- package/src/shared-hooks/useSharedState.ts +96 -96
- package/src/shared-hooks/useSharedStateRegistry.ts +170 -170
- package/src/types/ActionTypes.ts +161 -161
- package/src/types/PropTypes.ts +10 -10
- package/src/types/ResultTypes.ts +332 -332
- package/src/types/index.ts +3 -3
- package/src/utils/index.ts +1 -1
- package/src/utils/type-check.ts +69 -69
- package/tsconfig.build.json +5 -5
- package/tsconfig.json +72 -72
- package/bin/internal/useSharedStateRegistry.d.ts +0 -31
- package/bin/internal/useSharedStateRegistry.d.ts.map +0 -1
- package/bin/internal/useSharedStateRegistry.js +0 -102
- package/bin/internal/useSharedStateRegistry.js.map +0 -1
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
LivePresenceUser,
|
|
8
|
-
PresenceState,
|
|
9
|
-
LivePresence,
|
|
10
|
-
UserMeetingRole,
|
|
11
|
-
LiveDataObjectInitializeState,
|
|
12
|
-
} from "@microsoft/live-share";
|
|
13
|
-
import React from "react";
|
|
14
|
-
import { IUseLivePresenceResults, OnUpdateLivePresenceAction } from "../types";
|
|
15
|
-
import { useDynamicDDS } from "../shared-hooks";
|
|
16
|
-
import { useFluidObjectsContext } from "../providers";
|
|
17
|
-
import {
|
|
18
|
-
ActionContainerNotJoinedError,
|
|
19
|
-
ActionLiveDataObjectInitializedError,
|
|
20
|
-
ActionLiveDataObjectUndefinedError,
|
|
21
|
-
} from "../internal";
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* React hook for using a Live Share `LivePresence`.
|
|
25
|
-
*
|
|
26
|
-
* @remarks
|
|
27
|
-
* Use this hook if you want to track presence for who is currently using this component, such as
|
|
28
|
-
* who is online or who is viewing a specific document. With presence, you can sent along any custom
|
|
29
|
-
* user data. This is useful for rendering a list of users, profile pictures, cursor positions, and more.
|
|
30
|
-
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
31
|
-
*
|
|
32
|
-
* @template TData Optional typing for the custom user presence data object. Default is `object` type.
|
|
33
|
-
* @param uniqueKey The unique key for `LivePresence`. If one does not yet exist, a new one will be created.
|
|
34
|
-
* @param initialData Optional. Initial presence data object for the user. Can be value or a function to get the value.
|
|
35
|
-
* @param initialPresenceState Optional. Initial status of the user's presence. Default is online.
|
|
36
|
-
* @param allowedRoles Optional. the user roles that are allowed to mutate the synchronized state
|
|
37
|
-
* will be created, otherwise it will use the existing one. Default value is ":<dds-default>"
|
|
38
|
-
* @returns stateful `localUser`, `otherUsers` list, and `allUsers` list. Also returns a callback method
|
|
39
|
-
* to update the local user's presence and the `LivePresence` Fluid object.
|
|
40
|
-
*/
|
|
41
|
-
export function useLivePresence<TData extends object = object>(
|
|
42
|
-
uniqueKey: string,
|
|
43
|
-
initialData?: TData | (() => TData) | undefined,
|
|
44
|
-
initialPresenceState: PresenceState = PresenceState.online,
|
|
45
|
-
allowedRoles?: UserMeetingRole[]
|
|
46
|
-
): IUseLivePresenceResults<TData> {
|
|
47
|
-
/**
|
|
48
|
-
* Stateful all user presence list and its non-user-facing setter method.
|
|
49
|
-
*/
|
|
50
|
-
const [allUsers, setAllUsers] = React.useState<LivePresenceUser<TData>[]>(
|
|
51
|
-
[]
|
|
52
|
-
);
|
|
53
|
-
/**
|
|
54
|
-
* User facing: dynamically load the DDS for the given unique key.
|
|
55
|
-
*/
|
|
56
|
-
const { dds: livePresence } = useDynamicDDS<LivePresence<TData>>(
|
|
57
|
-
uniqueKey,
|
|
58
|
-
LivePresence<TData>
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* User facing: list of non-local user's presence objects.
|
|
63
|
-
*/
|
|
64
|
-
const otherUsers = allUsers.filter((user) => !user.isLocalUser);
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* User facing: local user's presence object.
|
|
68
|
-
*/
|
|
69
|
-
const localUser = allUsers.find((user) => user.isLocalUser);
|
|
70
|
-
|
|
71
|
-
const { container } = useFluidObjectsContext();
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* User facing: callback to update the local user's presence.
|
|
75
|
-
*/
|
|
76
|
-
const updatePresence: OnUpdateLivePresenceAction<TData> = React.useCallback(
|
|
77
|
-
async (data?: TData | undefined, state?: PresenceState | undefined) => {
|
|
78
|
-
if (!container) {
|
|
79
|
-
throw new ActionContainerNotJoinedError(
|
|
80
|
-
"livePresence",
|
|
81
|
-
"updatePresence"
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
if (livePresence === undefined) {
|
|
85
|
-
throw new ActionLiveDataObjectUndefinedError(
|
|
86
|
-
"livePresence",
|
|
87
|
-
"updatePresence"
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
if (!livePresence.isInitialized) {
|
|
91
|
-
throw new ActionLiveDataObjectInitializedError(
|
|
92
|
-
"livePresence",
|
|
93
|
-
"updatePresence"
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
return await livePresence.update(data, state);
|
|
97
|
-
},
|
|
98
|
-
[container, livePresence]
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Setup change listeners and start `LivePresence` if needed
|
|
103
|
-
*/
|
|
104
|
-
React.useEffect(() => {
|
|
105
|
-
if (livePresence === undefined) return;
|
|
106
|
-
|
|
107
|
-
const onPresenceChanged = () => {
|
|
108
|
-
const updatedLocalUsers: LivePresenceUser<TData>[] = [];
|
|
109
|
-
livePresence?.getUsers().forEach((user) => {
|
|
110
|
-
updatedLocalUsers.push(user);
|
|
111
|
-
});
|
|
112
|
-
setAllUsers(updatedLocalUsers);
|
|
113
|
-
};
|
|
114
|
-
livePresence.on("presenceChanged", onPresenceChanged);
|
|
115
|
-
|
|
116
|
-
if (
|
|
117
|
-
livePresence.initializeState ===
|
|
118
|
-
LiveDataObjectInitializeState.needed
|
|
119
|
-
) {
|
|
120
|
-
livePresence.initialize(
|
|
121
|
-
isInitialDataCallback<TData>(initialData)
|
|
122
|
-
? initialData()
|
|
123
|
-
: initialData,
|
|
124
|
-
initialPresenceState,
|
|
125
|
-
allowedRoles
|
|
126
|
-
);
|
|
127
|
-
} else {
|
|
128
|
-
onPresenceChanged();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return () => {
|
|
132
|
-
livePresence?.off("presenceChanged", onPresenceChanged);
|
|
133
|
-
};
|
|
134
|
-
}, [livePresence]);
|
|
135
|
-
|
|
136
|
-
return {
|
|
137
|
-
localUser,
|
|
138
|
-
otherUsers,
|
|
139
|
-
allUsers,
|
|
140
|
-
livePresence,
|
|
141
|
-
updatePresence,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function isInitialDataCallback<TData>(value: any): value is () => TData {
|
|
146
|
-
return typeof value === "function";
|
|
147
|
-
}
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
LivePresenceUser,
|
|
8
|
+
PresenceState,
|
|
9
|
+
LivePresence,
|
|
10
|
+
UserMeetingRole,
|
|
11
|
+
LiveDataObjectInitializeState,
|
|
12
|
+
} from "@microsoft/live-share";
|
|
13
|
+
import React from "react";
|
|
14
|
+
import { IUseLivePresenceResults, OnUpdateLivePresenceAction } from "../types";
|
|
15
|
+
import { useDynamicDDS } from "../shared-hooks";
|
|
16
|
+
import { useFluidObjectsContext } from "../providers";
|
|
17
|
+
import {
|
|
18
|
+
ActionContainerNotJoinedError,
|
|
19
|
+
ActionLiveDataObjectInitializedError,
|
|
20
|
+
ActionLiveDataObjectUndefinedError,
|
|
21
|
+
} from "../internal";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* React hook for using a Live Share `LivePresence`.
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* Use this hook if you want to track presence for who is currently using this component, such as
|
|
28
|
+
* who is online or who is viewing a specific document. With presence, you can sent along any custom
|
|
29
|
+
* user data. This is useful for rendering a list of users, profile pictures, cursor positions, and more.
|
|
30
|
+
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
31
|
+
*
|
|
32
|
+
* @template TData Optional typing for the custom user presence data object. Default is `object` type.
|
|
33
|
+
* @param uniqueKey The unique key for `LivePresence`. If one does not yet exist, a new one will be created.
|
|
34
|
+
* @param initialData Optional. Initial presence data object for the user. Can be value or a function to get the value.
|
|
35
|
+
* @param initialPresenceState Optional. Initial status of the user's presence. Default is online.
|
|
36
|
+
* @param allowedRoles Optional. the user roles that are allowed to mutate the synchronized state
|
|
37
|
+
* will be created, otherwise it will use the existing one. Default value is ":<dds-default>"
|
|
38
|
+
* @returns stateful `localUser`, `otherUsers` list, and `allUsers` list. Also returns a callback method
|
|
39
|
+
* to update the local user's presence and the `LivePresence` Fluid object.
|
|
40
|
+
*/
|
|
41
|
+
export function useLivePresence<TData extends object = object>(
|
|
42
|
+
uniqueKey: string,
|
|
43
|
+
initialData?: TData | (() => TData) | undefined,
|
|
44
|
+
initialPresenceState: PresenceState = PresenceState.online,
|
|
45
|
+
allowedRoles?: UserMeetingRole[]
|
|
46
|
+
): IUseLivePresenceResults<TData> {
|
|
47
|
+
/**
|
|
48
|
+
* Stateful all user presence list and its non-user-facing setter method.
|
|
49
|
+
*/
|
|
50
|
+
const [allUsers, setAllUsers] = React.useState<LivePresenceUser<TData>[]>(
|
|
51
|
+
[]
|
|
52
|
+
);
|
|
53
|
+
/**
|
|
54
|
+
* User facing: dynamically load the DDS for the given unique key.
|
|
55
|
+
*/
|
|
56
|
+
const { dds: livePresence } = useDynamicDDS<LivePresence<TData>>(
|
|
57
|
+
uniqueKey,
|
|
58
|
+
LivePresence<TData>
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* User facing: list of non-local user's presence objects.
|
|
63
|
+
*/
|
|
64
|
+
const otherUsers = allUsers.filter((user) => !user.isLocalUser);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* User facing: local user's presence object.
|
|
68
|
+
*/
|
|
69
|
+
const localUser = allUsers.find((user) => user.isLocalUser);
|
|
70
|
+
|
|
71
|
+
const { container } = useFluidObjectsContext();
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* User facing: callback to update the local user's presence.
|
|
75
|
+
*/
|
|
76
|
+
const updatePresence: OnUpdateLivePresenceAction<TData> = React.useCallback(
|
|
77
|
+
async (data?: TData | undefined, state?: PresenceState | undefined) => {
|
|
78
|
+
if (!container) {
|
|
79
|
+
throw new ActionContainerNotJoinedError(
|
|
80
|
+
"livePresence",
|
|
81
|
+
"updatePresence"
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
if (livePresence === undefined) {
|
|
85
|
+
throw new ActionLiveDataObjectUndefinedError(
|
|
86
|
+
"livePresence",
|
|
87
|
+
"updatePresence"
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (!livePresence.isInitialized) {
|
|
91
|
+
throw new ActionLiveDataObjectInitializedError(
|
|
92
|
+
"livePresence",
|
|
93
|
+
"updatePresence"
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return await livePresence.update(data, state);
|
|
97
|
+
},
|
|
98
|
+
[container, livePresence]
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Setup change listeners and start `LivePresence` if needed
|
|
103
|
+
*/
|
|
104
|
+
React.useEffect(() => {
|
|
105
|
+
if (livePresence === undefined) return;
|
|
106
|
+
|
|
107
|
+
const onPresenceChanged = () => {
|
|
108
|
+
const updatedLocalUsers: LivePresenceUser<TData>[] = [];
|
|
109
|
+
livePresence?.getUsers().forEach((user) => {
|
|
110
|
+
updatedLocalUsers.push(user);
|
|
111
|
+
});
|
|
112
|
+
setAllUsers(updatedLocalUsers);
|
|
113
|
+
};
|
|
114
|
+
livePresence.on("presenceChanged", onPresenceChanged);
|
|
115
|
+
|
|
116
|
+
if (
|
|
117
|
+
livePresence.initializeState ===
|
|
118
|
+
LiveDataObjectInitializeState.needed
|
|
119
|
+
) {
|
|
120
|
+
livePresence.initialize(
|
|
121
|
+
isInitialDataCallback<TData>(initialData)
|
|
122
|
+
? initialData()
|
|
123
|
+
: initialData,
|
|
124
|
+
initialPresenceState,
|
|
125
|
+
allowedRoles
|
|
126
|
+
);
|
|
127
|
+
} else {
|
|
128
|
+
onPresenceChanged();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return () => {
|
|
132
|
+
livePresence?.off("presenceChanged", onPresenceChanged);
|
|
133
|
+
};
|
|
134
|
+
}, [livePresence]);
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
localUser,
|
|
138
|
+
otherUsers,
|
|
139
|
+
allUsers,
|
|
140
|
+
livePresence,
|
|
141
|
+
updatePresence,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function isInitialDataCallback<TData>(value: any): value is () => TData {
|
|
146
|
+
return typeof value === "function";
|
|
147
|
+
}
|
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
LiveDataObjectInitializeState,
|
|
8
|
-
LiveState,
|
|
9
|
-
UserMeetingRole,
|
|
10
|
-
} from "@microsoft/live-share";
|
|
11
|
-
import React from "react";
|
|
12
|
-
import { SetLiveStateAction } from "../types";
|
|
13
|
-
import { useDynamicDDS } from "../shared-hooks";
|
|
14
|
-
import { useFluidObjectsContext } from "../providers";
|
|
15
|
-
import {
|
|
16
|
-
ActionContainerNotJoinedError,
|
|
17
|
-
ActionLiveDataObjectInitializedError,
|
|
18
|
-
ActionLiveDataObjectUndefinedError,
|
|
19
|
-
isPrevStateCallback,
|
|
20
|
-
} from "../internal";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* React hook for using a Live Share `LiveState`.
|
|
24
|
-
*
|
|
25
|
-
* @remarks
|
|
26
|
-
* Use this hook if you want to synchronize app state that will reset when all users leave the session.
|
|
27
|
-
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
28
|
-
*
|
|
29
|
-
* @param uniqueKey the unique key for the `LiveEvent`. If one does not yet exist, a new one will be created, otherwise it will use the existing one.
|
|
30
|
-
* @param initialState Optional. the initial state value of type TState
|
|
31
|
-
* @param allowedRoles Optional. the user roles that are allowed to mutate the synchronized state
|
|
32
|
-
* @returns ordered values: first value is the synchronized state value and the second is a setter to change the state value.
|
|
33
|
-
* The setter returns a void promise, which will throw if the user does not have the required roles to set.
|
|
34
|
-
*/
|
|
35
|
-
export function useLiveState<TState = any>(
|
|
36
|
-
uniqueKey: string,
|
|
37
|
-
initialState: TState,
|
|
38
|
-
allowedRoles?: UserMeetingRole[]
|
|
39
|
-
): [TState, SetLiveStateAction<TState>, LiveState<TState> | undefined] {
|
|
40
|
-
const [currentState, setCurrentState] =
|
|
41
|
-
React.useState<TState>(initialState);
|
|
42
|
-
/**
|
|
43
|
-
* User facing: dynamically load the DDS for the given unique key.
|
|
44
|
-
*/
|
|
45
|
-
const { dds: liveState } = useDynamicDDS<LiveState<TState>>(
|
|
46
|
-
uniqueKey,
|
|
47
|
-
LiveState<TState>
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
const { container } = useFluidObjectsContext();
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Change state callback that is user facing
|
|
54
|
-
* @param state TState to set
|
|
55
|
-
* @returns void promise, which will throw if the user does not have the required roles
|
|
56
|
-
*/
|
|
57
|
-
const setState = React.useCallback(
|
|
58
|
-
async (state: TState | ((prevState: TState) => TState)) => {
|
|
59
|
-
if (!container) {
|
|
60
|
-
throw new ActionContainerNotJoinedError(
|
|
61
|
-
"liveState",
|
|
62
|
-
"setState"
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
if (liveState === undefined) {
|
|
66
|
-
throw new ActionLiveDataObjectUndefinedError(
|
|
67
|
-
"liveState",
|
|
68
|
-
"setState"
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
if (!liveState.isInitialized) {
|
|
72
|
-
throw new ActionLiveDataObjectInitializedError(
|
|
73
|
-
"liveState",
|
|
74
|
-
"setState"
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
const valueToSet = isPrevStateCallback<TState>(state)
|
|
78
|
-
? state(liveState.state)
|
|
79
|
-
: state;
|
|
80
|
-
return await liveState.set(valueToSet);
|
|
81
|
-
},
|
|
82
|
-
[container, liveState]
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Setup change listeners and start `LiveState` if needed
|
|
87
|
-
*/
|
|
88
|
-
React.useEffect(() => {
|
|
89
|
-
if (liveState === undefined) return;
|
|
90
|
-
|
|
91
|
-
const onStateChanged = (state: TState) => {
|
|
92
|
-
setCurrentState(state);
|
|
93
|
-
};
|
|
94
|
-
liveState.on("stateChanged", onStateChanged);
|
|
95
|
-
if (
|
|
96
|
-
liveState.initializeState === LiveDataObjectInitializeState.needed
|
|
97
|
-
) {
|
|
98
|
-
liveState.initialize(initialState, allowedRoles);
|
|
99
|
-
}
|
|
100
|
-
if (JSON.stringify(liveState.state) !== JSON.stringify(initialState)) {
|
|
101
|
-
onStateChanged(liveState.state);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return () => {
|
|
105
|
-
liveState?.off("stateChanged", onStateChanged);
|
|
106
|
-
};
|
|
107
|
-
}, [liveState]);
|
|
108
|
-
|
|
109
|
-
return [currentState, setState, liveState];
|
|
110
|
-
}
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
LiveDataObjectInitializeState,
|
|
8
|
+
LiveState,
|
|
9
|
+
UserMeetingRole,
|
|
10
|
+
} from "@microsoft/live-share";
|
|
11
|
+
import React from "react";
|
|
12
|
+
import { SetLiveStateAction } from "../types";
|
|
13
|
+
import { useDynamicDDS } from "../shared-hooks";
|
|
14
|
+
import { useFluidObjectsContext } from "../providers";
|
|
15
|
+
import {
|
|
16
|
+
ActionContainerNotJoinedError,
|
|
17
|
+
ActionLiveDataObjectInitializedError,
|
|
18
|
+
ActionLiveDataObjectUndefinedError,
|
|
19
|
+
isPrevStateCallback,
|
|
20
|
+
} from "../internal";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* React hook for using a Live Share `LiveState`.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* Use this hook if you want to synchronize app state that will reset when all users leave the session.
|
|
27
|
+
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
28
|
+
*
|
|
29
|
+
* @param uniqueKey the unique key for the `LiveEvent`. If one does not yet exist, a new one will be created, otherwise it will use the existing one.
|
|
30
|
+
* @param initialState Optional. the initial state value of type TState
|
|
31
|
+
* @param allowedRoles Optional. the user roles that are allowed to mutate the synchronized state
|
|
32
|
+
* @returns ordered values: first value is the synchronized state value and the second is a setter to change the state value.
|
|
33
|
+
* The setter returns a void promise, which will throw if the user does not have the required roles to set.
|
|
34
|
+
*/
|
|
35
|
+
export function useLiveState<TState = any>(
|
|
36
|
+
uniqueKey: string,
|
|
37
|
+
initialState: TState,
|
|
38
|
+
allowedRoles?: UserMeetingRole[]
|
|
39
|
+
): [TState, SetLiveStateAction<TState>, LiveState<TState> | undefined] {
|
|
40
|
+
const [currentState, setCurrentState] =
|
|
41
|
+
React.useState<TState>(initialState);
|
|
42
|
+
/**
|
|
43
|
+
* User facing: dynamically load the DDS for the given unique key.
|
|
44
|
+
*/
|
|
45
|
+
const { dds: liveState } = useDynamicDDS<LiveState<TState>>(
|
|
46
|
+
uniqueKey,
|
|
47
|
+
LiveState<TState>
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const { container } = useFluidObjectsContext();
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Change state callback that is user facing
|
|
54
|
+
* @param state TState to set
|
|
55
|
+
* @returns void promise, which will throw if the user does not have the required roles
|
|
56
|
+
*/
|
|
57
|
+
const setState = React.useCallback(
|
|
58
|
+
async (state: TState | ((prevState: TState) => TState)) => {
|
|
59
|
+
if (!container) {
|
|
60
|
+
throw new ActionContainerNotJoinedError(
|
|
61
|
+
"liveState",
|
|
62
|
+
"setState"
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
if (liveState === undefined) {
|
|
66
|
+
throw new ActionLiveDataObjectUndefinedError(
|
|
67
|
+
"liveState",
|
|
68
|
+
"setState"
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (!liveState.isInitialized) {
|
|
72
|
+
throw new ActionLiveDataObjectInitializedError(
|
|
73
|
+
"liveState",
|
|
74
|
+
"setState"
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
const valueToSet = isPrevStateCallback<TState>(state)
|
|
78
|
+
? state(liveState.state)
|
|
79
|
+
: state;
|
|
80
|
+
return await liveState.set(valueToSet);
|
|
81
|
+
},
|
|
82
|
+
[container, liveState]
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Setup change listeners and start `LiveState` if needed
|
|
87
|
+
*/
|
|
88
|
+
React.useEffect(() => {
|
|
89
|
+
if (liveState === undefined) return;
|
|
90
|
+
|
|
91
|
+
const onStateChanged = (state: TState) => {
|
|
92
|
+
setCurrentState(state);
|
|
93
|
+
};
|
|
94
|
+
liveState.on("stateChanged", onStateChanged);
|
|
95
|
+
if (
|
|
96
|
+
liveState.initializeState === LiveDataObjectInitializeState.needed
|
|
97
|
+
) {
|
|
98
|
+
liveState.initialize(initialState, allowedRoles);
|
|
99
|
+
}
|
|
100
|
+
if (JSON.stringify(liveState.state) !== JSON.stringify(initialState)) {
|
|
101
|
+
onStateChanged(liveState.state);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return () => {
|
|
105
|
+
liveState?.off("stateChanged", onStateChanged);
|
|
106
|
+
};
|
|
107
|
+
}, [liveState]);
|
|
108
|
+
|
|
109
|
+
return [currentState, setState, liveState];
|
|
110
|
+
}
|