@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,159 +1,159 @@
|
|
|
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
|
-
import { useFluidObjectsContext } from "../providers";
|
|
12
|
-
import {
|
|
13
|
-
ActionContainerNotJoinedError,
|
|
14
|
-
ActionLiveDataObjectUndefinedError,
|
|
15
|
-
} from "../internal";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* React hook for using a Fluid `SharedMap`.
|
|
19
|
-
*
|
|
20
|
-
* @remarks
|
|
21
|
-
* The primary benefit of using the `useSharedMap` hook rather than the Fluid `SharedMap`
|
|
22
|
-
* directly is that it integrates nicely with React state and automates repetitive tasks.
|
|
23
|
-
* If you want to use `SharedMap` this hook creates directly, you can do that as well.
|
|
24
|
-
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
25
|
-
*
|
|
26
|
-
* @template TData Optional typing for objects stored in the SharedMap. Default is `any` type.
|
|
27
|
-
* @param uniqueKey the unique key for the `SharedMap`. If one does not yet exist, a new `SharedMap`
|
|
28
|
-
* will be created, otherwise it will use the existing one.
|
|
29
|
-
* @param initialData a JS Map, entries array, or JSON object to insert into the `SharedMap` when creating
|
|
30
|
-
* the DDS for the first time.
|
|
31
|
-
* @returns stateful `map` entries, `setEntry` callback, `deleteEntry` callback, and the Fluid `sharedMap`.
|
|
32
|
-
*/
|
|
33
|
-
export function useSharedMap<TData = any>(
|
|
34
|
-
uniqueKey: string,
|
|
35
|
-
initialData?: SharedMapInitialData<TData>
|
|
36
|
-
): IUseSharedMapResults<TData> {
|
|
37
|
-
/**
|
|
38
|
-
* Stateful readonly map (user facing) with most recent values from `SharedMap` and its setter method.
|
|
39
|
-
*/
|
|
40
|
-
const [map, setMap] = React.useState<ReadonlyMap<string, TData>>(
|
|
41
|
-
getInitialData<TData>(initialData)
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
const onFirstInitialize = React.useCallback(
|
|
45
|
-
(newDDS: SharedMap): void => {
|
|
46
|
-
/**
|
|
47
|
-
* Callback method to set the `initialData` into the map when the `SharedMap` is first created.
|
|
48
|
-
* Only should be used as a prop to useDynamicDDS.
|
|
49
|
-
*/
|
|
50
|
-
getInitialData(initialData).forEach((value, key) => {
|
|
51
|
-
newDDS.set(key, value);
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
[initialData]
|
|
55
|
-
);
|
|
56
|
-
/**
|
|
57
|
-
* User facing: dynamically load the SharedMap DDS for the given unique key.
|
|
58
|
-
*/
|
|
59
|
-
const { dds: sharedMap } = useDynamicDDS<SharedMap>(
|
|
60
|
-
uniqueKey,
|
|
61
|
-
SharedMap,
|
|
62
|
-
onFirstInitialize
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
const { container } = useFluidObjectsContext();
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* User facing: set a value to the Fluid `SharedMap`.
|
|
69
|
-
*/
|
|
70
|
-
const setEntry = React.useCallback(
|
|
71
|
-
(key: string, value: TData) => {
|
|
72
|
-
if (!container) {
|
|
73
|
-
throw new ActionContainerNotJoinedError(
|
|
74
|
-
"sharedMap",
|
|
75
|
-
"setEntry"
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
if (sharedMap === undefined) {
|
|
79
|
-
throw new ActionLiveDataObjectUndefinedError(
|
|
80
|
-
"sharedMap",
|
|
81
|
-
"setEntry"
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
sharedMap.set(key, value);
|
|
85
|
-
},
|
|
86
|
-
[container, sharedMap]
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* User facing: delete a value from the Fluid `SharedMap`.
|
|
91
|
-
*/
|
|
92
|
-
const deleteEntry = React.useCallback(
|
|
93
|
-
(key: string) => {
|
|
94
|
-
if (!container) {
|
|
95
|
-
throw new ActionContainerNotJoinedError(
|
|
96
|
-
"sharedMap",
|
|
97
|
-
"deleteEntry"
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
if (sharedMap === undefined) {
|
|
101
|
-
throw new ActionLiveDataObjectUndefinedError(
|
|
102
|
-
"sharedMap",
|
|
103
|
-
"deleteEntry"
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
sharedMap.delete(key);
|
|
107
|
-
},
|
|
108
|
-
[container, sharedMap]
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
// Setup change listeners, initial values, etc.
|
|
112
|
-
React.useEffect(() => {
|
|
113
|
-
if (!sharedMap) return;
|
|
114
|
-
|
|
115
|
-
// Register valueChanged listener for `SharedMap`.
|
|
116
|
-
const onValueChanged = () => {
|
|
117
|
-
setMap(new Map<string, TData>(sharedMap.entries()));
|
|
118
|
-
};
|
|
119
|
-
sharedMap.on("valueChanged", onValueChanged);
|
|
120
|
-
// Get initial values from `SharedMap`.
|
|
121
|
-
onValueChanged();
|
|
122
|
-
|
|
123
|
-
return () => {
|
|
124
|
-
// Cleanup on component unmount.
|
|
125
|
-
sharedMap?.off("valueChanged", onValueChanged);
|
|
126
|
-
};
|
|
127
|
-
}, [sharedMap]);
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
map,
|
|
131
|
-
setEntry,
|
|
132
|
-
deleteEntry,
|
|
133
|
-
sharedMap,
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Helper method for converting different initial data props into a Map<string, TData> to insert into the Fluid SharedMap
|
|
139
|
-
* @template TData Optional typing for objects stored in the SharedMap. Default is `object` type.
|
|
140
|
-
* @param initialData a JS Map, entries array, or JSON object.
|
|
141
|
-
* @returns A Map<string, TData> with the entries provided.
|
|
142
|
-
*/
|
|
143
|
-
function getInitialData<TData>(
|
|
144
|
-
initialData: SharedMapInitialData<TData>
|
|
145
|
-
): Map<string, TData> {
|
|
146
|
-
if (isMap(initialData)) {
|
|
147
|
-
return initialData;
|
|
148
|
-
} else if (isEntries(initialData)) {
|
|
149
|
-
return new Map<string, TData>(initialData);
|
|
150
|
-
} else if (isJSON(initialData)) {
|
|
151
|
-
const values: (readonly [string, TData])[] = Object.keys(
|
|
152
|
-
initialData
|
|
153
|
-
).map((key) => {
|
|
154
|
-
return [key, initialData[key]];
|
|
155
|
-
});
|
|
156
|
-
return new Map<string, TData>(values);
|
|
157
|
-
}
|
|
158
|
-
return new Map<string, TData>();
|
|
159
|
-
}
|
|
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
|
+
import { useFluidObjectsContext } from "../providers";
|
|
12
|
+
import {
|
|
13
|
+
ActionContainerNotJoinedError,
|
|
14
|
+
ActionLiveDataObjectUndefinedError,
|
|
15
|
+
} from "../internal";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* React hook for using a Fluid `SharedMap`.
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* The primary benefit of using the `useSharedMap` hook rather than the Fluid `SharedMap`
|
|
22
|
+
* directly is that it integrates nicely with React state and automates repetitive tasks.
|
|
23
|
+
* If you want to use `SharedMap` this hook creates directly, you can do that as well.
|
|
24
|
+
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
25
|
+
*
|
|
26
|
+
* @template TData Optional typing for objects stored in the SharedMap. Default is `any` type.
|
|
27
|
+
* @param uniqueKey the unique key for the `SharedMap`. If one does not yet exist, a new `SharedMap`
|
|
28
|
+
* will be created, otherwise it will use the existing one.
|
|
29
|
+
* @param initialData a JS Map, entries array, or JSON object to insert into the `SharedMap` when creating
|
|
30
|
+
* the DDS for the first time.
|
|
31
|
+
* @returns stateful `map` entries, `setEntry` callback, `deleteEntry` callback, and the Fluid `sharedMap`.
|
|
32
|
+
*/
|
|
33
|
+
export function useSharedMap<TData = any>(
|
|
34
|
+
uniqueKey: string,
|
|
35
|
+
initialData?: SharedMapInitialData<TData>
|
|
36
|
+
): IUseSharedMapResults<TData> {
|
|
37
|
+
/**
|
|
38
|
+
* Stateful readonly map (user facing) with most recent values from `SharedMap` and its setter method.
|
|
39
|
+
*/
|
|
40
|
+
const [map, setMap] = React.useState<ReadonlyMap<string, TData>>(
|
|
41
|
+
getInitialData<TData>(initialData)
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const onFirstInitialize = React.useCallback(
|
|
45
|
+
(newDDS: SharedMap): void => {
|
|
46
|
+
/**
|
|
47
|
+
* Callback method to set the `initialData` into the map when the `SharedMap` is first created.
|
|
48
|
+
* Only should be used as a prop to useDynamicDDS.
|
|
49
|
+
*/
|
|
50
|
+
getInitialData(initialData).forEach((value, key) => {
|
|
51
|
+
newDDS.set(key, value);
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
[initialData]
|
|
55
|
+
);
|
|
56
|
+
/**
|
|
57
|
+
* User facing: dynamically load the SharedMap DDS for the given unique key.
|
|
58
|
+
*/
|
|
59
|
+
const { dds: sharedMap } = useDynamicDDS<SharedMap>(
|
|
60
|
+
uniqueKey,
|
|
61
|
+
SharedMap,
|
|
62
|
+
onFirstInitialize
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const { container } = useFluidObjectsContext();
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* User facing: set a value to the Fluid `SharedMap`.
|
|
69
|
+
*/
|
|
70
|
+
const setEntry = React.useCallback(
|
|
71
|
+
(key: string, value: TData) => {
|
|
72
|
+
if (!container) {
|
|
73
|
+
throw new ActionContainerNotJoinedError(
|
|
74
|
+
"sharedMap",
|
|
75
|
+
"setEntry"
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (sharedMap === undefined) {
|
|
79
|
+
throw new ActionLiveDataObjectUndefinedError(
|
|
80
|
+
"sharedMap",
|
|
81
|
+
"setEntry"
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
sharedMap.set(key, value);
|
|
85
|
+
},
|
|
86
|
+
[container, sharedMap]
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* User facing: delete a value from the Fluid `SharedMap`.
|
|
91
|
+
*/
|
|
92
|
+
const deleteEntry = React.useCallback(
|
|
93
|
+
(key: string) => {
|
|
94
|
+
if (!container) {
|
|
95
|
+
throw new ActionContainerNotJoinedError(
|
|
96
|
+
"sharedMap",
|
|
97
|
+
"deleteEntry"
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
if (sharedMap === undefined) {
|
|
101
|
+
throw new ActionLiveDataObjectUndefinedError(
|
|
102
|
+
"sharedMap",
|
|
103
|
+
"deleteEntry"
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
sharedMap.delete(key);
|
|
107
|
+
},
|
|
108
|
+
[container, sharedMap]
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// Setup change listeners, initial values, etc.
|
|
112
|
+
React.useEffect(() => {
|
|
113
|
+
if (!sharedMap) return;
|
|
114
|
+
|
|
115
|
+
// Register valueChanged listener for `SharedMap`.
|
|
116
|
+
const onValueChanged = () => {
|
|
117
|
+
setMap(new Map<string, TData>(sharedMap.entries()));
|
|
118
|
+
};
|
|
119
|
+
sharedMap.on("valueChanged", onValueChanged);
|
|
120
|
+
// Get initial values from `SharedMap`.
|
|
121
|
+
onValueChanged();
|
|
122
|
+
|
|
123
|
+
return () => {
|
|
124
|
+
// Cleanup on component unmount.
|
|
125
|
+
sharedMap?.off("valueChanged", onValueChanged);
|
|
126
|
+
};
|
|
127
|
+
}, [sharedMap]);
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
map,
|
|
131
|
+
setEntry,
|
|
132
|
+
deleteEntry,
|
|
133
|
+
sharedMap,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Helper method for converting different initial data props into a Map<string, TData> to insert into the Fluid SharedMap
|
|
139
|
+
* @template TData Optional typing for objects stored in the SharedMap. Default is `object` type.
|
|
140
|
+
* @param initialData a JS Map, entries array, or JSON object.
|
|
141
|
+
* @returns A Map<string, TData> with the entries provided.
|
|
142
|
+
*/
|
|
143
|
+
function getInitialData<TData>(
|
|
144
|
+
initialData: SharedMapInitialData<TData>
|
|
145
|
+
): Map<string, TData> {
|
|
146
|
+
if (isMap(initialData)) {
|
|
147
|
+
return initialData;
|
|
148
|
+
} else if (isEntries(initialData)) {
|
|
149
|
+
return new Map<string, TData>(initialData);
|
|
150
|
+
} else if (isJSON(initialData)) {
|
|
151
|
+
const values: (readonly [string, TData])[] = Object.keys(
|
|
152
|
+
initialData
|
|
153
|
+
).map((key) => {
|
|
154
|
+
return [key, initialData[key]];
|
|
155
|
+
});
|
|
156
|
+
return new Map<string, TData>(values);
|
|
157
|
+
}
|
|
158
|
+
return new Map<string, TData>();
|
|
159
|
+
}
|
|
@@ -1,96 +1,96 @@
|
|
|
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
|
-
import { isPrevStateCallback } from "../internal";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Inspired by React's `useState` hook, `useSharedState` makes it easy to synchronize state in your app.
|
|
14
|
-
*
|
|
15
|
-
* @remarks
|
|
16
|
-
* Values set through this state is automatically attached to a `SharedMap` that the `AzureProvider`
|
|
17
|
-
* creates. If you are synchronizing complex data structures that multiple users will be setting simultaneously,
|
|
18
|
-
* consider using an optimized hook for your data structure (e.g., `useSharedMap`, `useSharedString`, etc.).
|
|
19
|
-
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
20
|
-
*
|
|
21
|
-
* @template S Typing for objects stored associated with the `uniqueKey`.
|
|
22
|
-
* @param uniqueKey the unique key for a `SharedMap`. If you use the same key for multiple components, those components will reference the same state.
|
|
23
|
-
* @param initialState a default state for the `SharedMap`.
|
|
24
|
-
*
|
|
25
|
-
* @returns a stateful value, the function to update it, and an optional dispose method to delete it from the `SharedMap`.
|
|
26
|
-
*/
|
|
27
|
-
export function useSharedState<S = any>(
|
|
28
|
-
uniqueKey: string,
|
|
29
|
-
initialState: S
|
|
30
|
-
): [S, SetSharedStateAction<S>, DisposeSharedStateAction] {
|
|
31
|
-
/**
|
|
32
|
-
* User facing: stateful value and non-user facing setter.
|
|
33
|
-
*/
|
|
34
|
-
const [localState, setLocalState] = React.useState<S>(initialState);
|
|
35
|
-
/**
|
|
36
|
-
* Unique ID reference for the component.
|
|
37
|
-
*/
|
|
38
|
-
const componentIdRef = React.useRef(uuid());
|
|
39
|
-
/**
|
|
40
|
-
* Register set state callbacks from AzureProvider and update/delete callbacks for initial object's `stateMap`.
|
|
41
|
-
*/
|
|
42
|
-
const {
|
|
43
|
-
registerSharedSetStateAction,
|
|
44
|
-
unregisterSharedSetStateAction,
|
|
45
|
-
updateSharedState,
|
|
46
|
-
deleteSharedState,
|
|
47
|
-
} = useFluidObjectsContext();
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* User facing: callback to change the shared state.
|
|
51
|
-
*/
|
|
52
|
-
const setSharedState: SetSharedStateAction<S> = React.useCallback(
|
|
53
|
-
(state: S | ((prevState: S) => S)) => {
|
|
54
|
-
setLocalState((prevState) => {
|
|
55
|
-
const valueToSet = isPrevStateCallback<S>(state)
|
|
56
|
-
? state(prevState)
|
|
57
|
-
: state;
|
|
58
|
-
updateSharedState(uniqueKey, valueToSet);
|
|
59
|
-
return valueToSet;
|
|
60
|
-
});
|
|
61
|
-
},
|
|
62
|
-
[uniqueKey, setLocalState, updateSharedState]
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* User facing: callback to dispose the shared state from the `stateMap`.
|
|
67
|
-
*/
|
|
68
|
-
const disposeSharedState: DisposeSharedStateAction =
|
|
69
|
-
React.useCallback(() => {
|
|
70
|
-
deleteSharedState(uniqueKey);
|
|
71
|
-
}, [uniqueKey, deleteSharedState]);
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Once container is available, this effect will register the setter method so that the `S` value
|
|
75
|
-
* from `stateMap` that matches `uniqueKey` can be passed back to this hook whenever changed.
|
|
76
|
-
*
|
|
77
|
-
* @see registerSharedSetStateAction to see how new values from `stateMap` are passed to this hook.
|
|
78
|
-
* @see unregisterSharedSetStateAction to see how this component stops listening to changes in the `stateMap`.
|
|
79
|
-
*/
|
|
80
|
-
React.useEffect(() => {
|
|
81
|
-
registerSharedSetStateAction(
|
|
82
|
-
uniqueKey,
|
|
83
|
-
componentIdRef.current,
|
|
84
|
-
setLocalState
|
|
85
|
-
);
|
|
86
|
-
return () => {
|
|
87
|
-
unregisterSharedSetStateAction(uniqueKey, componentIdRef.current);
|
|
88
|
-
};
|
|
89
|
-
}, [
|
|
90
|
-
uniqueKey,
|
|
91
|
-
registerSharedSetStateAction,
|
|
92
|
-
unregisterSharedSetStateAction,
|
|
93
|
-
]);
|
|
94
|
-
|
|
95
|
-
return [localState, setSharedState, disposeSharedState];
|
|
96
|
-
}
|
|
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
|
+
import { isPrevStateCallback } from "../internal";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Inspired by React's `useState` hook, `useSharedState` makes it easy to synchronize state in your app.
|
|
14
|
+
*
|
|
15
|
+
* @remarks
|
|
16
|
+
* Values set through this state is automatically attached to a `SharedMap` that the `AzureProvider`
|
|
17
|
+
* creates. If you are synchronizing complex data structures that multiple users will be setting simultaneously,
|
|
18
|
+
* consider using an optimized hook for your data structure (e.g., `useSharedMap`, `useSharedString`, etc.).
|
|
19
|
+
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
20
|
+
*
|
|
21
|
+
* @template S Typing for objects stored associated with the `uniqueKey`.
|
|
22
|
+
* @param uniqueKey the unique key for a `SharedMap`. If you use the same key for multiple components, those components will reference the same state.
|
|
23
|
+
* @param initialState a default state for the `SharedMap`.
|
|
24
|
+
*
|
|
25
|
+
* @returns a stateful value, the function to update it, and an optional dispose method to delete it from the `SharedMap`.
|
|
26
|
+
*/
|
|
27
|
+
export function useSharedState<S = any>(
|
|
28
|
+
uniqueKey: string,
|
|
29
|
+
initialState: S
|
|
30
|
+
): [S, SetSharedStateAction<S>, DisposeSharedStateAction] {
|
|
31
|
+
/**
|
|
32
|
+
* User facing: stateful value and non-user facing setter.
|
|
33
|
+
*/
|
|
34
|
+
const [localState, setLocalState] = React.useState<S>(initialState);
|
|
35
|
+
/**
|
|
36
|
+
* Unique ID reference for the component.
|
|
37
|
+
*/
|
|
38
|
+
const componentIdRef = React.useRef(uuid());
|
|
39
|
+
/**
|
|
40
|
+
* Register set state callbacks from AzureProvider and update/delete callbacks for initial object's `stateMap`.
|
|
41
|
+
*/
|
|
42
|
+
const {
|
|
43
|
+
registerSharedSetStateAction,
|
|
44
|
+
unregisterSharedSetStateAction,
|
|
45
|
+
updateSharedState,
|
|
46
|
+
deleteSharedState,
|
|
47
|
+
} = useFluidObjectsContext();
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* User facing: callback to change the shared state.
|
|
51
|
+
*/
|
|
52
|
+
const setSharedState: SetSharedStateAction<S> = React.useCallback(
|
|
53
|
+
(state: S | ((prevState: S) => S)) => {
|
|
54
|
+
setLocalState((prevState) => {
|
|
55
|
+
const valueToSet = isPrevStateCallback<S>(state)
|
|
56
|
+
? state(prevState)
|
|
57
|
+
: state;
|
|
58
|
+
updateSharedState(uniqueKey, valueToSet);
|
|
59
|
+
return valueToSet;
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
[uniqueKey, setLocalState, updateSharedState]
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* User facing: callback to dispose the shared state from the `stateMap`.
|
|
67
|
+
*/
|
|
68
|
+
const disposeSharedState: DisposeSharedStateAction =
|
|
69
|
+
React.useCallback(() => {
|
|
70
|
+
deleteSharedState(uniqueKey);
|
|
71
|
+
}, [uniqueKey, deleteSharedState]);
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Once container is available, this effect will register the setter method so that the `S` value
|
|
75
|
+
* from `stateMap` that matches `uniqueKey` can be passed back to this hook whenever changed.
|
|
76
|
+
*
|
|
77
|
+
* @see registerSharedSetStateAction to see how new values from `stateMap` are passed to this hook.
|
|
78
|
+
* @see unregisterSharedSetStateAction to see how this component stops listening to changes in the `stateMap`.
|
|
79
|
+
*/
|
|
80
|
+
React.useEffect(() => {
|
|
81
|
+
registerSharedSetStateAction(
|
|
82
|
+
uniqueKey,
|
|
83
|
+
componentIdRef.current,
|
|
84
|
+
setLocalState
|
|
85
|
+
);
|
|
86
|
+
return () => {
|
|
87
|
+
unregisterSharedSetStateAction(uniqueKey, componentIdRef.current);
|
|
88
|
+
};
|
|
89
|
+
}, [
|
|
90
|
+
uniqueKey,
|
|
91
|
+
registerSharedSetStateAction,
|
|
92
|
+
unregisterSharedSetStateAction,
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
return [localState, setSharedState, disposeSharedState];
|
|
96
|
+
}
|