@liveblocks/react 0.17.8 → 0.17.9

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.mjs CHANGED
@@ -1,573 +1,23 @@
1
- import { deprecate, errorIf } from "@liveblocks/client/internal";
2
- import * as React from "react";
3
- import { useReducer } from "react";
4
- import { LiveMap, LiveList, LiveObject } from "@liveblocks/client";
5
- const ClientContext = React.createContext(null);
6
- function LiveblocksProvider(props) {
7
- return (
8
- deprecate(
9
- "LiveblocksProvider is no longer needed in your component tree if you set up your Liveblocks context using `createRoomContext()`. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
10
- ),
11
- React.createElement(
12
- ClientContext.Provider,
13
- { value: props.client },
14
- props.children
15
- )
16
- );
17
- }
18
- function useClient() {
19
- const client = React.useContext(ClientContext);
20
- if (null == client)
21
- throw new Error("LiveblocksProvider is missing from the react tree");
22
- return client;
23
- }
24
- function __awaiter(thisArg, _arguments, P, generator) {
25
- return new (P || (P = Promise))(function (resolve, reject) {
26
- function fulfilled(value) {
27
- try {
28
- step(generator.next(value));
29
- } catch (e) {
30
- reject(e);
31
- }
32
- }
33
- function rejected(value) {
34
- try {
35
- step(generator.throw(value));
36
- } catch (e) {
37
- reject(e);
38
- }
39
- }
40
- function step(result) {
41
- var value;
42
- result.done
43
- ? resolve(result.value)
44
- : ((value = result.value),
45
- value instanceof P
46
- ? value
47
- : new P(function (resolve) {
48
- resolve(value);
49
- })).then(fulfilled, rejected);
50
- }
51
- step((generator = generator.apply(thisArg, _arguments || [])).next());
52
- });
53
- }
54
- function useRerender() {
55
- const [, update] = useReducer((x) => x + 1, 0);
56
- return update;
57
- }
58
- function useInitial(value) {
59
- return React.useRef(value).current;
60
- }
61
- function createRoomContext(client) {
62
- let useClient$1;
63
- useClient$1 = "__legacy" !== client ? () => client : useClient;
64
- const RoomContext = React.createContext(null);
65
- function useRoom() {
66
- const room = React.useContext(RoomContext);
67
- if (null == room)
68
- throw new Error("RoomProvider is missing from the react tree");
69
- return room;
70
- }
71
- function useStorage() {
72
- const room = useRoom(),
73
- [root, setState] = React.useState(null);
74
- return (
75
- React.useEffect(() => {
76
- let didCancel = !1;
77
- return (
78
- (function () {
79
- __awaiter(this, void 0, void 0, function* () {
80
- const storage = yield room.getStorage();
81
- didCancel || setState(storage.root);
82
- });
83
- })(),
84
- () => {
85
- didCancel = !0;
86
- }
87
- );
88
- }, [room]),
89
- [root]
90
- );
91
- }
92
- function useMap_deprecated(key, entries) {
93
- errorIf(
94
- entries,
95
- `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(
96
- key
97
- )}: new LiveMap(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`
98
- );
99
- const value = useStorageValue(
100
- key,
101
- new LiveMap(null != entries ? entries : void 0)
102
- );
103
- return "ok" === value.status
104
- ? value.value
105
- : (errorIf(
106
- "notfound" === value.status,
107
- `Key ${JSON.stringify(
108
- key
109
- )} 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(
110
- key
111
- )}: new LiveMap(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`
112
- ),
113
- null);
114
- }
115
- function useList_deprecated(key, items) {
116
- errorIf(
117
- items,
118
- `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(
119
- key
120
- )}: new LiveList(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`
121
- );
122
- const value = useStorageValue(key, new LiveList(items));
123
- return "ok" === value.status
124
- ? value.value
125
- : (errorIf(
126
- "notfound" === value.status,
127
- `Key ${JSON.stringify(
128
- key
129
- )} 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(
130
- key
131
- )}: new LiveList(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`
132
- ),
133
- null);
134
- }
135
- function useObject_deprecated(key, initialData) {
136
- errorIf(
137
- initialData,
138
- `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(
139
- key
140
- )}: new LiveObject(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`
141
- );
142
- const value = useStorageValue(key, new LiveObject(initialData));
143
- return "ok" === value.status
144
- ? value.value
145
- : (errorIf(
146
- "notfound" === value.status,
147
- `Key ${JSON.stringify(
148
- key
149
- )} 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(
150
- key
151
- )}: new LiveObject(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.`
152
- ),
153
- null);
154
- }
155
- function useHistory() {
156
- return useRoom().history;
157
- }
158
- function useStorageValue(key, initialValue) {
159
- const room = useRoom(),
160
- [root] = useStorage(),
161
- rerender = useRerender(),
162
- frozenInitialValue = useInitial(initialValue);
163
- if (
164
- (React.useEffect(() => {
165
- if (null == root) return;
166
- let liveValue = root.get(key);
167
- null == liveValue &&
168
- ((liveValue = frozenInitialValue), root.set(key, liveValue));
169
- let unsubscribeCrdt = room.subscribe(liveValue, rerender);
170
- const unsubscribeRoot = room.subscribe(root, function () {
171
- const newCrdt = root.get(key);
172
- newCrdt !== liveValue &&
173
- (unsubscribeCrdt(),
174
- (liveValue = newCrdt),
175
- (unsubscribeCrdt = room.subscribe(liveValue, rerender)),
176
- rerender());
177
- });
178
- return (
179
- rerender(),
180
- () => {
181
- unsubscribeRoot(), unsubscribeCrdt();
182
- }
183
- );
184
- }, [root, room, key, frozenInitialValue, rerender]),
185
- null == root)
186
- )
187
- return { status: "loading" };
188
- {
189
- const value = root.get(key);
190
- return null == value
191
- ? { status: "notfound" }
192
- : { status: "ok", value: value };
193
- }
194
- }
195
- return {
196
- RoomProvider: function (props) {
197
- const {
198
- id: roomId,
199
- initialPresence: initialPresence,
200
- initialStorage: initialStorage,
201
- defaultPresence: defaultPresence,
202
- defaultStorageRoot: defaultStorageRoot,
203
- } = props;
204
- if ("production" !== process.env.NODE_ENV) {
205
- if (null == roomId)
206
- throw new Error(
207
- "RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required"
208
- );
209
- if ("string" != typeof roomId)
210
- throw new Error("RoomProvider id property should be a string.");
211
- }
212
- errorIf(
213
- defaultPresence,
214
- "RoomProvider's `defaultPresence` prop will be removed in @liveblocks/react 0.18. Please use `initialPresence` instead. For more info, see https://bit.ly/3Niy5aP"
215
- ),
216
- errorIf(
217
- defaultStorageRoot,
218
- "RoomProvider's `defaultStorageRoot` prop will be removed in @liveblocks/react 0.18. Please use `initialStorage` instead. For more info, see https://bit.ly/3Niy5aP"
219
- );
220
- const _client = useClient$1(),
221
- frozen = useInitial({
222
- initialPresence: initialPresence,
223
- initialStorage: initialStorage,
224
- defaultPresence: defaultPresence,
225
- defaultStorageRoot: defaultStorageRoot,
226
- }),
227
- [room, setRoom] = React.useState(() =>
228
- _client.enter(roomId, {
229
- initialPresence: initialPresence,
230
- initialStorage: initialStorage,
231
- defaultPresence: defaultPresence,
232
- defaultStorageRoot: defaultStorageRoot,
233
- DO_NOT_USE_withoutConnecting: "undefined" == typeof window,
234
- })
235
- );
236
- return (
237
- React.useEffect(
238
- () => (
239
- setRoom(
240
- _client.enter(roomId, {
241
- initialPresence: frozen.initialPresence,
242
- initialStorage: frozen.initialStorage,
243
- defaultPresence: frozen.defaultPresence,
244
- defaultStorageRoot: frozen.defaultStorageRoot,
245
- DO_NOT_USE_withoutConnecting: "undefined" == typeof window,
246
- })
247
- ),
248
- () => {
249
- _client.leave(roomId);
250
- }
251
- ),
252
- [_client, roomId, frozen]
253
- ),
254
- React.createElement(
255
- RoomContext.Provider,
256
- { value: room },
257
- props.children
258
- )
259
- );
260
- },
261
- useBatch: function () {
262
- return useRoom().batch;
263
- },
264
- useBroadcastEvent: function () {
265
- const room = useRoom();
266
- return React.useCallback(
267
- (event, options = { shouldQueueEventIfNotReady: !1 }) => {
268
- room.broadcastEvent(event, options);
269
- },
270
- [room]
271
- );
272
- },
273
- useCanRedo: function () {
274
- const room = useRoom(),
275
- [canRedo, setCanRedo] = React.useState(room.history.canRedo);
276
- return (
277
- React.useEffect(() => {
278
- const unsubscribe = room.subscribe(
279
- "history",
280
- ({ canRedo: canRedo }) => setCanRedo(canRedo)
281
- );
282
- return () => {
283
- unsubscribe();
284
- };
285
- }, [room]),
286
- canRedo
287
- );
288
- },
289
- useCanUndo: function () {
290
- const room = useRoom(),
291
- [canUndo, setCanUndo] = React.useState(room.history.canUndo);
292
- return (
293
- React.useEffect(() => {
294
- const unsubscribe = room.subscribe(
295
- "history",
296
- ({ canUndo: canUndo }) => setCanUndo(canUndo)
297
- );
298
- return () => {
299
- unsubscribe();
300
- };
301
- }, [room]),
302
- canUndo
303
- );
304
- },
305
- useErrorListener: function (callback) {
306
- const room = useRoom(),
307
- savedCallback = React.useRef(callback);
308
- React.useEffect(() => {
309
- savedCallback.current = callback;
310
- }),
311
- React.useEffect(() => {
312
- const unsubscribe = room.subscribe("error", (e) =>
313
- savedCallback.current(e)
314
- );
315
- return () => {
316
- unsubscribe();
317
- };
318
- }, [room]);
319
- },
320
- useEventListener: function (callback) {
321
- const room = useRoom(),
322
- savedCallback = React.useRef(callback);
323
- React.useEffect(() => {
324
- savedCallback.current = callback;
325
- }),
326
- React.useEffect(() => {
327
- const unsubscribe = room.subscribe("event", (eventData) => {
328
- savedCallback.current(eventData);
329
- });
330
- return () => {
331
- unsubscribe();
332
- };
333
- }, [room]);
334
- },
335
- useHistory: useHistory,
336
- useList: function (key) {
337
- return useList_deprecated(key);
338
- },
339
- useMap: function (key) {
340
- return useMap_deprecated(key);
341
- },
342
- useMyPresence: function () {
343
- const room = useRoom(),
344
- presence = room.getPresence(),
345
- rerender = useRerender();
346
- return (
347
- React.useEffect(() => {
348
- const unsubscribe = room.subscribe("my-presence", rerender);
349
- return () => {
350
- unsubscribe();
351
- };
352
- }, [room, rerender]),
353
- [
354
- presence,
355
- React.useCallback(
356
- (overrides, options) => room.updatePresence(overrides, options),
357
- [room]
358
- ),
359
- ]
360
- );
361
- },
362
- useObject: function (key) {
363
- return useObject_deprecated(key);
364
- },
365
- useOthers: function () {
366
- const room = useRoom(),
367
- rerender = useRerender();
368
- return (
369
- React.useEffect(() => {
370
- const unsubscribe = room.subscribe("others", rerender);
371
- return () => {
372
- unsubscribe();
373
- };
374
- }, [room, rerender]),
375
- room.getOthers()
376
- );
377
- },
378
- useRedo: function () {
379
- return useHistory().redo;
380
- },
381
- useRoom: useRoom,
382
- useSelf: function () {
383
- const room = useRoom(),
384
- rerender = useRerender();
385
- return (
386
- React.useEffect(() => {
387
- const unsubscribePresence = room.subscribe("my-presence", rerender),
388
- unsubscribeConnection = room.subscribe("connection", rerender);
389
- return () => {
390
- unsubscribePresence(), unsubscribeConnection();
391
- };
392
- }, [room, rerender]),
393
- room.getSelf()
394
- );
395
- },
396
- useStorage: useStorage,
397
- useUndo: function () {
398
- return useHistory().undo;
399
- },
400
- useUpdateMyPresence: function () {
401
- const room = useRoom();
402
- return React.useCallback(
403
- (overrides, options) => {
404
- room.updatePresence(overrides, options);
405
- },
406
- [room]
407
- );
408
- },
409
- RoomContext: RoomContext,
410
- useList_deprecated: useList_deprecated,
411
- useMap_deprecated: useMap_deprecated,
412
- useObject_deprecated: useObject_deprecated,
413
- };
414
- }
415
- const _hooks = createRoomContext("__legacy");
416
- function RoomProvider(props) {
417
- return (
418
- deprecate(
419
- "Please use `createRoomContext()` instead of importing `RoomProvider` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
420
- ),
421
- _hooks.RoomProvider(props)
422
- );
423
- }
424
- function useBatch() {
425
- return (
426
- deprecate(
427
- "Please use `createRoomContext()` instead of importing `useBatch` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
428
- ),
429
- _hooks.useBatch()
430
- );
431
- }
432
- function useBroadcastEvent() {
433
- return (
434
- deprecate(
435
- "Please use `createRoomContext()` instead of importing `useBroadcastEvent` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
436
- ),
437
- _hooks.useBroadcastEvent()
438
- );
439
- }
440
- function useErrorListener(callback) {
441
- return (
442
- deprecate(
443
- "Please use `createRoomContext()` instead of importing `useErrorListener` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
444
- ),
445
- _hooks.useErrorListener(callback)
446
- );
447
- }
448
- function useEventListener(callback) {
449
- return (
450
- deprecate(
451
- "Please use `createRoomContext()` instead of importing `useEventListener` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
452
- ),
453
- _hooks.useEventListener(callback)
454
- );
455
- }
456
- function useHistory() {
457
- return (
458
- deprecate(
459
- "Please use `createRoomContext()` instead of importing `useHistory` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
460
- ),
461
- _hooks.useHistory()
462
- );
463
- }
464
- function useMyPresence() {
465
- return (
466
- deprecate(
467
- "Please use `createRoomContext()` instead of importing `useMyPresence` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
468
- ),
469
- _hooks.useMyPresence()
470
- );
471
- }
472
- function useOthers() {
473
- return (
474
- deprecate(
475
- "Please use `createRoomContext()` instead of importing `useOthers` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
476
- ),
477
- _hooks.useOthers()
478
- );
479
- }
480
- function useRedo() {
481
- return (
482
- deprecate(
483
- "Please use `createRoomContext()` instead of importing `useRedo` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
484
- ),
485
- _hooks.useRedo()
486
- );
487
- }
488
- function useRoom() {
489
- return (
490
- deprecate(
491
- "Please use `createRoomContext()` instead of importing `useRoom` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
492
- ),
493
- _hooks.useRoom()
494
- );
495
- }
496
- function useSelf() {
497
- return (
498
- deprecate(
499
- "Please use `createRoomContext()` instead of importing `useSelf` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
500
- ),
501
- _hooks.useSelf()
502
- );
503
- }
504
- function useStorage() {
505
- return (
506
- deprecate(
507
- "Please use `createRoomContext()` instead of importing `useStorage` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
508
- ),
509
- _hooks.useStorage()
510
- );
511
- }
512
- function useUndo() {
513
- return (
514
- deprecate(
515
- "Please use `createRoomContext()` instead of importing `useUndo` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
516
- ),
517
- _hooks.useUndo()
518
- );
519
- }
520
- function useUpdateMyPresence() {
521
- return (
522
- deprecate(
523
- "Please use `createRoomContext()` instead of importing `useUpdateMyPresence` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
524
- ),
525
- _hooks.useUpdateMyPresence()
526
- );
527
- }
528
- function useList(key, items) {
529
- return (
530
- deprecate(
531
- "Please use `createRoomContext()` instead of importing `useList` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
532
- ),
533
- _hooks.useList_deprecated(key, items)
534
- );
535
- }
536
- function useMap(key, entries) {
537
- return (
538
- deprecate(
539
- "Please use `createRoomContext()` instead of importing `useMap` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
540
- ),
541
- _hooks.useMap_deprecated(key, entries)
542
- );
543
- }
544
- function useObject(key, initialData) {
545
- return (
546
- deprecate(
547
- "Please use `createRoomContext()` instead of importing `useObject` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
548
- ),
549
- _hooks.useObject_deprecated(key, initialData)
550
- );
551
- }
552
- export {
553
- LiveblocksProvider,
554
- RoomProvider,
555
- createRoomContext,
556
- useBatch,
557
- useBroadcastEvent,
558
- useClient,
559
- useErrorListener,
560
- useEventListener,
561
- useHistory,
562
- useList,
563
- useMap,
564
- useMyPresence,
565
- useObject,
566
- useOthers,
567
- useRedo,
568
- useRoom,
569
- useSelf,
570
- useStorage,
571
- useUndo,
572
- useUpdateMyPresence,
573
- };
1
+ import mod from "./index.js";
2
+
3
+ export default mod;
4
+ export const LiveblocksProvider = mod.LiveblocksProvider;
5
+ export const RoomProvider = mod.RoomProvider;
6
+ export const createRoomContext = mod.createRoomContext;
7
+ export const useBatch = mod.useBatch;
8
+ export const useBroadcastEvent = mod.useBroadcastEvent;
9
+ export const useClient = mod.useClient;
10
+ export const useErrorListener = mod.useErrorListener;
11
+ export const useEventListener = mod.useEventListener;
12
+ export const useHistory = mod.useHistory;
13
+ export const useList = mod.useList;
14
+ export const useMap = mod.useMap;
15
+ export const useMyPresence = mod.useMyPresence;
16
+ export const useObject = mod.useObject;
17
+ export const useOthers = mod.useOthers;
18
+ export const useRedo = mod.useRedo;
19
+ export const useRoom = mod.useRoom;
20
+ export const useSelf = mod.useSelf;
21
+ export const useStorage = mod.useStorage;
22
+ export const useUndo = mod.useUndo;
23
+ export const useUpdateMyPresence = mod.useUpdateMyPresence;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/react",
3
- "version": "0.17.8",
3
+ "version": "0.17.9",
4
4
  "description": "A set of React hooks and providers to use Liveblocks declaratively.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "license": "Apache-2.0",
19
19
  "peerDependencies": {
20
- "@liveblocks/client": "0.17.8",
20
+ "@liveblocks/client": "0.17.9",
21
21
  "react": "^16.14.0 || ^17 || ^18"
22
22
  },
23
23
  "repository": {