@liveblocks/react 0.16.4-beta2 → 0.16.6
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/index.d.ts +39 -188
- package/index.js +241 -331
- package/index.mjs +215 -394
- package/package.json +10 -9
package/index.mjs
CHANGED
|
@@ -1,409 +1,230 @@
|
|
|
1
|
-
import * as React from
|
|
2
|
-
|
|
3
|
-
import {
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { useReducer } from "react";
|
|
4
|
+
|
|
5
|
+
import { LiveMap, LiveList, LiveObject } from "@liveblocks/client";
|
|
6
|
+
|
|
7
|
+
import { deprecateIf } from "@liveblocks/client/internal";
|
|
4
8
|
|
|
5
9
|
const ClientContext = React.createContext(null);
|
|
6
|
-
|
|
7
|
-
* Makes the Liveblocks client available in the component hierarchy below.
|
|
8
|
-
*/
|
|
10
|
+
|
|
9
11
|
function LiveblocksProvider(props) {
|
|
10
|
-
|
|
12
|
+
return React.createElement(ClientContext.Provider, {
|
|
13
|
+
value: props.client
|
|
14
|
+
}, props.children);
|
|
11
15
|
}
|
|
12
|
-
|
|
13
|
-
* Returns the Client of the nearest LiveblocksProvider above in the React
|
|
14
|
-
* component tree.
|
|
15
|
-
*/
|
|
16
|
+
|
|
16
17
|
function useClient() {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
return client;
|
|
18
|
+
const client = React.useContext(ClientContext);
|
|
19
|
+
if (null == client) throw new Error("LiveblocksProvider is missing from the react tree");
|
|
20
|
+
return client;
|
|
22
21
|
}
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
23
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
24
|
+
return new (P || (P = Promise))((function(resolve, reject) {
|
|
25
|
+
function fulfilled(value) {
|
|
26
|
+
try {
|
|
27
|
+
step(generator.next(value));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
reject(e);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function rejected(value) {
|
|
33
|
+
try {
|
|
34
|
+
step(generator.throw(value));
|
|
35
|
+
} catch (e) {
|
|
36
|
+
reject(e);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function step(result) {
|
|
40
|
+
var value;
|
|
41
|
+
result.done ? resolve(result.value) : (value = result.value, value instanceof P ? value : new P((function(resolve) {
|
|
42
|
+
resolve(value);
|
|
43
|
+
}))).then(fulfilled, rejected);
|
|
44
|
+
}
|
|
45
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
46
|
+
}));
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
/**
|
|
50
|
-
* Trigger a re-render programmatically, without changing the component's
|
|
51
|
-
* state.
|
|
52
|
-
*
|
|
53
|
-
* Usage:
|
|
54
|
-
*
|
|
55
|
-
* const rerender = useRerender();
|
|
56
|
-
* return (
|
|
57
|
-
* <button onClick={rerender}>
|
|
58
|
-
* {Math.random()}
|
|
59
|
-
* </button>
|
|
60
|
-
* )
|
|
61
|
-
*
|
|
62
|
-
*/
|
|
63
49
|
function useRerender() {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
// never consumed. Simply incrementing the counter changes the component's
|
|
67
|
-
// state and, thus, trigger a re-render.
|
|
68
|
-
(x) => x + 1, 0);
|
|
69
|
-
return update;
|
|
50
|
+
const [, update] = useReducer((x => x + 1), 0);
|
|
51
|
+
return update;
|
|
70
52
|
}
|
|
71
53
|
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
const client = useClient();
|
|
89
|
-
const [room, setRoom] = React.useState(() => client.enter(roomId, {
|
|
90
|
-
defaultPresence: defaultPresence ? defaultPresence() : undefined,
|
|
91
|
-
defaultStorageRoot,
|
|
92
|
-
DO_NOT_USE_withoutConnecting: typeof window === "undefined",
|
|
54
|
+
const {RoomProvider: RoomProvider, useRoom: useRoom, useMyPresence: useMyPresence, useUpdateMyPresence: useUpdateMyPresence, useOthers: useOthers, useBroadcastEvent: useBroadcastEvent, useErrorListener: useErrorListener, useEventListener: useEventListener, useSelf: useSelf, useStorage: useStorage, useMap: useMap, useList: useList, useObject: useObject, useUndo: useUndo, useRedo: useRedo, useBatch: useBatch, useHistory: useHistory} = function() {
|
|
55
|
+
const RoomContext = React.createContext(null);
|
|
56
|
+
function useRoom() {
|
|
57
|
+
const room = React.useContext(RoomContext);
|
|
58
|
+
if (null == room) throw new Error("RoomProvider is missing from the react tree");
|
|
59
|
+
return room;
|
|
60
|
+
}
|
|
61
|
+
function useStorage() {
|
|
62
|
+
const room = useRoom(), [root, setState] = React.useState(null);
|
|
63
|
+
return React.useEffect((() => {
|
|
64
|
+
let didCancel = !1;
|
|
65
|
+
return function() {
|
|
66
|
+
__awaiter(this, void 0, void 0, (function*() {
|
|
67
|
+
const storage = yield room.getStorage();
|
|
68
|
+
didCancel || setState(storage.root);
|
|
93
69
|
}));
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
function
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
return () => {
|
|
254
|
-
unsubscribe();
|
|
255
|
-
};
|
|
256
|
-
}, [room]);
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Gets the current user once it is connected to the room.
|
|
260
|
-
*
|
|
261
|
-
* @example
|
|
262
|
-
* import { useSelf } from "@liveblocks/react";
|
|
263
|
-
*
|
|
264
|
-
* const user = useSelf();
|
|
265
|
-
*/
|
|
266
|
-
function useSelf() {
|
|
267
|
-
const room = useRoom();
|
|
268
|
-
const rerender = useRerender();
|
|
269
|
-
React.useEffect(() => {
|
|
270
|
-
const unsubscribePresence = room.subscribe("my-presence", rerender);
|
|
271
|
-
const unsubscribeConnection = room.subscribe("connection", rerender);
|
|
272
|
-
return () => {
|
|
273
|
-
unsubscribePresence();
|
|
274
|
-
unsubscribeConnection();
|
|
275
|
-
};
|
|
276
|
-
}, [room]);
|
|
277
|
-
return room.getSelf();
|
|
278
|
-
}
|
|
279
|
-
function useStorage() {
|
|
280
|
-
const room = useRoom();
|
|
281
|
-
const [root, setState] = React.useState(null);
|
|
282
|
-
React.useEffect(() => {
|
|
283
|
-
let didCancel = false;
|
|
284
|
-
function fetchStorage() {
|
|
285
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
286
|
-
const storage = yield room.getStorage();
|
|
287
|
-
if (!didCancel) {
|
|
288
|
-
setState(storage.root);
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
fetchStorage();
|
|
293
|
-
return () => {
|
|
294
|
-
didCancel = true;
|
|
295
|
-
};
|
|
296
|
-
}, [room]);
|
|
297
|
-
return [root];
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Returns the LiveMap associated with the provided key. If the LiveMap does not exist, a new empty LiveMap will be created.
|
|
301
|
-
* The hook triggers a re-render if the LiveMap is updated, however it does not triggers a re-render if a nested CRDT is updated.
|
|
302
|
-
*
|
|
303
|
-
* @param key The storage key associated with the LiveMap
|
|
304
|
-
* @param entries Optional entries that are used to create the LiveMap for the first time
|
|
305
|
-
* @returns null while the storage is loading, otherwise, returns the LiveMap associated to the storage
|
|
306
|
-
*
|
|
307
|
-
* @example
|
|
308
|
-
* const emptyMap = useMap("mapA");
|
|
309
|
-
* const mapWithItems = useMap("mapB", [["keyA", "valueA"], ["keyB", "valueB"]]);
|
|
310
|
-
*/
|
|
311
|
-
function useMap(key, entries) {
|
|
312
|
-
return useCrdt(key, new LiveMap(entries));
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Returns the LiveList associated with the provided key. If the LiveList does not exist, a new LiveList will be created.
|
|
316
|
-
* The hook triggers a re-render if the LiveList is updated, however it does not triggers a re-render if a nested CRDT is updated.
|
|
317
|
-
*
|
|
318
|
-
* @param key The storage key associated with the LiveList
|
|
319
|
-
* @param items Optional items that are used to create the LiveList for the first time
|
|
320
|
-
* @returns null while the storage is loading, otherwise, returns the LiveList associated to the storage
|
|
321
|
-
*
|
|
322
|
-
* @example
|
|
323
|
-
* const emptyList = useList("listA");
|
|
324
|
-
* const listWithItems = useList("listB", ["a", "b", "c"]);
|
|
325
|
-
*/
|
|
326
|
-
function useList(key, items) {
|
|
327
|
-
return useCrdt(key, new LiveList(items));
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Returns the LiveObject associated with the provided key. If the LiveObject does not exist, it will be created with the initialData parameter.
|
|
331
|
-
* The hook triggers a re-render if the LiveObject is updated, however it does not triggers a re-render if a nested CRDT is updated.
|
|
332
|
-
*
|
|
333
|
-
* @param key The storage key associated with the LiveObject
|
|
334
|
-
* @param initialData Optional data that is used to create the LiveObject for the first time
|
|
335
|
-
* @returns null while the storage is loading, otherwise, returns the LveObject associated to the storage
|
|
336
|
-
*
|
|
337
|
-
* @example
|
|
338
|
-
* const object = useObject("obj", {
|
|
339
|
-
* company: "Liveblocks",
|
|
340
|
-
* website: "https://liveblocks.io"
|
|
341
|
-
* });
|
|
342
|
-
*/
|
|
343
|
-
function useObject(key, initialData) {
|
|
344
|
-
return useCrdt(key, new LiveObject(initialData));
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Returns a function that undoes the last operation executed by the current client.
|
|
348
|
-
* It does not impact operations made by other clients.
|
|
349
|
-
*/
|
|
350
|
-
function useUndo() {
|
|
351
|
-
return useRoom().history.undo;
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Returns a function that redoes the last operation executed by the current client.
|
|
355
|
-
* It does not impact operations made by other clients.
|
|
356
|
-
*/
|
|
357
|
-
function useRedo() {
|
|
358
|
-
return useRoom().history.redo;
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Returns a function that batches modifications made during the given function.
|
|
362
|
-
* All the modifications are sent to other clients in a single message.
|
|
363
|
-
* All the modifications are merged in a single history item (undo/redo).
|
|
364
|
-
* All the subscribers are called only after the batch is over.
|
|
365
|
-
*/
|
|
366
|
-
function useBatch() {
|
|
367
|
-
return useRoom().batch;
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Returns the room.history
|
|
371
|
-
*/
|
|
372
|
-
function useHistory() {
|
|
373
|
-
return useRoom().history;
|
|
374
|
-
}
|
|
375
|
-
function useCrdt(key, initialCrdt) {
|
|
376
|
-
var _a;
|
|
377
|
-
const room = useRoom();
|
|
378
|
-
const [root] = useStorage();
|
|
379
|
-
const rerender = useRerender();
|
|
380
|
-
React.useEffect(() => {
|
|
381
|
-
if (root == null) {
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
let crdt = root.get(key);
|
|
385
|
-
if (crdt == null) {
|
|
386
|
-
crdt = initialCrdt;
|
|
387
|
-
root.set(key, crdt);
|
|
388
|
-
}
|
|
389
|
-
function onRootChange() {
|
|
390
|
-
const newCrdt = root.get(key);
|
|
391
|
-
if (newCrdt !== crdt) {
|
|
392
|
-
unsubscribeCrdt();
|
|
393
|
-
crdt = newCrdt;
|
|
394
|
-
unsubscribeCrdt = room.subscribe(crdt /* AbstractCrdt */, rerender);
|
|
395
|
-
rerender();
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
let unsubscribeCrdt = room.subscribe(crdt /* AbstractCrdt */, rerender);
|
|
399
|
-
const unsubscribeRoot = room.subscribe(root /* AbstractCrdt */, onRootChange);
|
|
400
|
-
rerender();
|
|
401
|
-
return () => {
|
|
402
|
-
unsubscribeRoot();
|
|
403
|
-
unsubscribeCrdt();
|
|
404
|
-
};
|
|
405
|
-
}, [root, room]);
|
|
406
|
-
return (_a = root === null || root === void 0 ? void 0 : root.get(key)) !== null && _a !== void 0 ? _a : null;
|
|
407
|
-
}
|
|
70
|
+
}(), () => {
|
|
71
|
+
didCancel = !0;
|
|
72
|
+
};
|
|
73
|
+
}), [ room ]), [ root ];
|
|
74
|
+
}
|
|
75
|
+
function useHistory() {
|
|
76
|
+
return useRoom().history;
|
|
77
|
+
}
|
|
78
|
+
function useStorageValue(key, initialValue) {
|
|
79
|
+
const room = useRoom(), [root] = useStorage(), rerender = useRerender();
|
|
80
|
+
if (React.useEffect((() => {
|
|
81
|
+
if (null == root) return;
|
|
82
|
+
let liveValue = root.get(key);
|
|
83
|
+
null == liveValue && (liveValue = initialValue, root.set(key, liveValue));
|
|
84
|
+
let unsubscribeCrdt = room.subscribe(liveValue, rerender);
|
|
85
|
+
const unsubscribeRoot = room.subscribe(root, (function() {
|
|
86
|
+
const newCrdt = root.get(key);
|
|
87
|
+
newCrdt !== liveValue && (unsubscribeCrdt(), liveValue = newCrdt, unsubscribeCrdt = room.subscribe(liveValue, rerender),
|
|
88
|
+
rerender());
|
|
89
|
+
}));
|
|
90
|
+
return rerender(), () => {
|
|
91
|
+
unsubscribeRoot(), unsubscribeCrdt();
|
|
92
|
+
};
|
|
93
|
+
}), [ root, room ]), null == root) return {
|
|
94
|
+
status: "loading"
|
|
95
|
+
};
|
|
96
|
+
{
|
|
97
|
+
const value = root.get(key);
|
|
98
|
+
return null == value ? {
|
|
99
|
+
status: "notfound"
|
|
100
|
+
} : {
|
|
101
|
+
status: "ok",
|
|
102
|
+
value: value
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
RoomProvider: function(props) {
|
|
108
|
+
const {id: roomId, initialPresence: initialPresence, initialStorage: initialStorage, defaultPresence: defaultPresence, defaultStorageRoot: defaultStorageRoot} = props;
|
|
109
|
+
if ("production" !== process.env.NODE_ENV) {
|
|
110
|
+
if (null == roomId) throw new Error("RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required");
|
|
111
|
+
if ("string" != typeof roomId) throw new Error("RoomProvider id property should be a string.");
|
|
112
|
+
}
|
|
113
|
+
deprecateIf(defaultPresence, "RoomProvider's `defaultPresence` prop will be removed in @liveblocks/react 0.18. Please use `initialPresence` instead. For more info, see https://bit.ly/3Niy5aP", "defaultPresence"),
|
|
114
|
+
deprecateIf(defaultStorageRoot, "RoomProvider's `defaultStorageRoot` prop will be removed in @liveblocks/react 0.18. Please use `initialStorage` instead. For more info, see https://bit.ly/3Niy5aP", "defaultStorageRoot");
|
|
115
|
+
const client = useClient(), [room, setRoom] = React.useState((() => client.enter(roomId, {
|
|
116
|
+
initialPresence: initialPresence,
|
|
117
|
+
initialStorage: initialStorage,
|
|
118
|
+
defaultPresence: defaultPresence,
|
|
119
|
+
defaultStorageRoot: defaultStorageRoot,
|
|
120
|
+
DO_NOT_USE_withoutConnecting: "undefined" == typeof window
|
|
121
|
+
})));
|
|
122
|
+
return React.useEffect((() => (setRoom(client.enter(roomId, {
|
|
123
|
+
initialPresence: initialPresence,
|
|
124
|
+
initialStorage: initialStorage,
|
|
125
|
+
defaultPresence: defaultPresence,
|
|
126
|
+
defaultStorageRoot: defaultStorageRoot,
|
|
127
|
+
DO_NOT_USE_withoutConnecting: "undefined" == typeof window
|
|
128
|
+
})), () => {
|
|
129
|
+
client.leave(roomId);
|
|
130
|
+
})), [ client, roomId ]), React.createElement(RoomContext.Provider, {
|
|
131
|
+
value: room
|
|
132
|
+
}, props.children);
|
|
133
|
+
},
|
|
134
|
+
useBatch: function() {
|
|
135
|
+
return useRoom().batch;
|
|
136
|
+
},
|
|
137
|
+
useBroadcastEvent: function() {
|
|
138
|
+
const room = useRoom();
|
|
139
|
+
return React.useCallback(((event, options = {
|
|
140
|
+
shouldQueueEventIfNotReady: !1
|
|
141
|
+
}) => {
|
|
142
|
+
room.broadcastEvent(event, options);
|
|
143
|
+
}), [ room ]);
|
|
144
|
+
},
|
|
145
|
+
useErrorListener: function(callback) {
|
|
146
|
+
const room = useRoom(), savedCallback = React.useRef(callback);
|
|
147
|
+
React.useEffect((() => {
|
|
148
|
+
savedCallback.current = callback;
|
|
149
|
+
})), React.useEffect((() => {
|
|
150
|
+
const unsubscribe = room.subscribe("error", (e => savedCallback.current(e)));
|
|
151
|
+
return () => {
|
|
152
|
+
unsubscribe();
|
|
153
|
+
};
|
|
154
|
+
}), [ room ]);
|
|
155
|
+
},
|
|
156
|
+
useEventListener: function(callback) {
|
|
157
|
+
const room = useRoom(), savedCallback = React.useRef(callback);
|
|
158
|
+
React.useEffect((() => {
|
|
159
|
+
savedCallback.current = callback;
|
|
160
|
+
})), React.useEffect((() => {
|
|
161
|
+
const unsubscribe = room.subscribe("event", (e => savedCallback.current(e)));
|
|
162
|
+
return () => {
|
|
163
|
+
unsubscribe();
|
|
164
|
+
};
|
|
165
|
+
}), [ room ]);
|
|
166
|
+
},
|
|
167
|
+
useHistory: useHistory,
|
|
168
|
+
useList: function(key, items) {
|
|
169
|
+
deprecateIf(items, `Support for initializing items in useList() directly will be removed in @liveblocks/react 0.18.\n\nInstead, please initialize this data where you set up your RoomProvider:\n\n import { LiveList } from "@liveblocks/client";\n\n const initialStorage = () => ({\n ${JSON.stringify(key)}: new LiveList(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`);
|
|
170
|
+
const value = useStorageValue(key, new LiveList(items));
|
|
171
|
+
return "ok" === value.status ? value.value : (deprecateIf("notfound" === value.status, `Key ${JSON.stringify(key)} was not found in Storage. Starting with 0.18, useList() will no longer automatically create this key.\n\nInstead, please initialize your storage where you set up your RoomProvider:\n\n import { LiveList } from "@liveblocks/client";\n\n const initialStorage = () => ({\n ${JSON.stringify(key)}: new LiveList(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`),
|
|
172
|
+
null);
|
|
173
|
+
},
|
|
174
|
+
useMap: function(key, entries) {
|
|
175
|
+
deprecateIf(entries, `Support for initializing entries in useMap() directly will be removed in @liveblocks/react 0.18.\n\nInstead, please initialize this data where you set up your RoomProvider:\n\n const initialStorage = () => ({\n ${JSON.stringify(key)}: new LiveMap(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`);
|
|
176
|
+
const value = useStorageValue(key, new LiveMap(null != entries ? entries : void 0));
|
|
177
|
+
return "ok" === value.status ? value.value : (deprecateIf("notfound" === value.status, `Key ${JSON.stringify(key)} was not found in Storage. Starting with 0.18, useMap() will no longer automatically create this key.\n\nInstead, please initialize your storage where you set up your RoomProvider:\n\n import { LiveMap } from "@liveblocks/client";\n\n const initialStorage = () => ({\n ${JSON.stringify(key)}: new LiveMap(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`),
|
|
178
|
+
null);
|
|
179
|
+
},
|
|
180
|
+
useMyPresence: function() {
|
|
181
|
+
const room = useRoom(), presence = room.getPresence(), rerender = useRerender();
|
|
182
|
+
return React.useEffect((() => {
|
|
183
|
+
const unsubscribe = room.subscribe("my-presence", rerender);
|
|
184
|
+
return () => {
|
|
185
|
+
unsubscribe();
|
|
186
|
+
};
|
|
187
|
+
}), [ room ]), [ presence, React.useCallback(((overrides, options) => room.updatePresence(overrides, options)), [ room ]) ];
|
|
188
|
+
},
|
|
189
|
+
useObject: function(key, initialData) {
|
|
190
|
+
deprecateIf(initialData, `Support for initializing data in useObject() directly will be removed in @liveblocks/react 0.18.\n\nInstead, please initialize this data where you set up your RoomProvider:\n\n import { LiveObject } from "@liveblocks/client";\n\n const initialStorage = () => ({\n ${JSON.stringify(key)}: new LiveObject(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`);
|
|
191
|
+
const value = useStorageValue(key, new LiveObject(initialData));
|
|
192
|
+
return "ok" === value.status ? value.value : (deprecateIf("notfound" === value.status, `Key ${JSON.stringify(key)} was not found in Storage. Starting with 0.18, useObject() will no longer automatically create this key.\n\nInstead, please initialize your storage where you set up your RoomProvider:\n\n import { LiveObject } from "@liveblocks/client";\n\n const initialStorage = () => ({\n ${JSON.stringify(key)}: new LiveObject(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`),
|
|
193
|
+
null);
|
|
194
|
+
},
|
|
195
|
+
useOthers: function() {
|
|
196
|
+
const room = useRoom(), rerender = useRerender();
|
|
197
|
+
return React.useEffect((() => {
|
|
198
|
+
const unsubscribe = room.subscribe("others", rerender);
|
|
199
|
+
return () => {
|
|
200
|
+
unsubscribe();
|
|
201
|
+
};
|
|
202
|
+
}), [ room ]), room.getOthers();
|
|
203
|
+
},
|
|
204
|
+
useRedo: function() {
|
|
205
|
+
return useHistory().redo;
|
|
206
|
+
},
|
|
207
|
+
useRoom: useRoom,
|
|
208
|
+
useSelf: function() {
|
|
209
|
+
const room = useRoom(), rerender = useRerender();
|
|
210
|
+
return React.useEffect((() => {
|
|
211
|
+
const unsubscribePresence = room.subscribe("my-presence", rerender), unsubscribeConnection = room.subscribe("connection", rerender);
|
|
212
|
+
return () => {
|
|
213
|
+
unsubscribePresence(), unsubscribeConnection();
|
|
214
|
+
};
|
|
215
|
+
}), [ room ]), room.getSelf();
|
|
216
|
+
},
|
|
217
|
+
useStorage: useStorage,
|
|
218
|
+
useUndo: function() {
|
|
219
|
+
return useHistory().undo;
|
|
220
|
+
},
|
|
221
|
+
useUpdateMyPresence: function() {
|
|
222
|
+
const room = useRoom();
|
|
223
|
+
return React.useCallback(((overrides, options) => {
|
|
224
|
+
room.updatePresence(overrides, options);
|
|
225
|
+
}), [ room ]);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}();
|
|
408
229
|
|
|
409
230
|
export { LiveblocksProvider, RoomProvider, useBatch, useBroadcastEvent, useClient, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
|