@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.
Files changed (4) hide show
  1. package/index.d.ts +39 -188
  2. package/index.js +241 -331
  3. package/index.mjs +215 -394
  4. package/package.json +10 -9
package/index.mjs CHANGED
@@ -1,409 +1,230 @@
1
- import * as React from 'react';
2
- import { useReducer } from 'react';
3
- import { LiveMap, LiveList, LiveObject } from '@liveblocks/client';
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
- return (React.createElement(ClientContext.Provider, { value: props.client }, props.children));
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
- const client = React.useContext(ClientContext);
18
- if (client == null) {
19
- throw new Error("LiveblocksProvider is missing from the react tree");
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
- Copyright (c) Microsoft Corporation.
26
-
27
- Permission to use, copy, modify, and/or distribute this software for any
28
- purpose with or without fee is hereby granted.
29
-
30
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
31
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
32
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
33
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
34
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
35
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
36
- PERFORMANCE OF THIS SOFTWARE.
37
- ***************************************************************************** */
38
-
39
- function __awaiter(thisArg, _arguments, P, generator) {
40
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
41
- return new (P || (P = Promise))(function (resolve, reject) {
42
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
43
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
44
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
45
- step((generator = generator.apply(thisArg, _arguments || [])).next());
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
- const [, update] = useReducer(
65
- // This implementation works by incrementing a hidden counter value that is
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 RoomContext = React.createContext(null);
73
- /**
74
- * Makes a Room available in the component hierarchy below.
75
- * When this component is unmounted, the current user leave the room.
76
- * That means that you can't have 2 RoomProvider with the same room id in your react tree.
77
- */
78
- function RoomProvider(props) {
79
- const { id: roomId, defaultPresence, defaultStorageRoot } = props;
80
- if (process.env.NODE_ENV !== "production") {
81
- if (roomId == null) {
82
- throw new Error("RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required");
83
- }
84
- if (typeof roomId !== "string") {
85
- throw new Error("RoomProvider id property should be a string.");
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
- React.useEffect(() => {
95
- setRoom(client.enter(roomId, {
96
- defaultPresence: defaultPresence ? defaultPresence() : undefined,
97
- defaultStorageRoot,
98
- DO_NOT_USE_withoutConnecting: typeof window === "undefined",
99
- }));
100
- return () => {
101
- client.leave(roomId);
102
- };
103
- }, [client, roomId]);
104
- return (React.createElement(RoomContext.Provider, { value: room }, props.children));
105
- }
106
- /**
107
- * Returns the Room of the nearest RoomProvider above in the React component
108
- * tree.
109
- */
110
- function useRoom() {
111
- const room = React.useContext(RoomContext);
112
- if (room == null) {
113
- throw new Error("RoomProvider is missing from the react tree");
114
- }
115
- return room;
116
- }
117
- /**
118
- * Returns the presence of the current user of the current room, and a function to update it.
119
- * It is different from the setState function returned by the useState hook from React.
120
- * You don't need to pass the full presence object to update it.
121
- *
122
- * @example
123
- * import { useMyPresence } from "@liveblocks/react";
124
- *
125
- * const [myPresence, updateMyPresence] = useMyPresence();
126
- * updateMyPresence({ x: 0 });
127
- * updateMyPresence({ y: 0 });
128
- *
129
- * // At the next render, "myPresence" will be equal to "{ x: 0, y: 0 }"
130
- */
131
- function useMyPresence() {
132
- const room = useRoom();
133
- const presence = room.getPresence();
134
- const rerender = useRerender();
135
- React.useEffect(() => {
136
- const unsubscribe = room.subscribe("my-presence", rerender);
137
- return () => {
138
- unsubscribe();
139
- };
140
- }, [room]);
141
- const setPresence = React.useCallback((overrides, options) => room.updatePresence(overrides, options), [room]);
142
- return [presence, setPresence];
143
- }
144
- /**
145
- * useUpdateMyPresence is similar to useMyPresence but it only returns the function to update the current user presence.
146
- * If you don't use the current user presence in your component, but you need to update it (e.g. live cursor), it's better to use useUpdateMyPresence to avoid unnecessary renders.
147
- *
148
- * @example
149
- * import { useUpdateMyPresence } from "@liveblocks/react";
150
- *
151
- * const updateMyPresence = useUpdateMyPresence();
152
- * updateMyPresence({ x: 0 });
153
- * updateMyPresence({ y: 0 });
154
- *
155
- * // At the next render, the presence of the current user will be equal to "{ x: 0, y: 0 }"
156
- */
157
- function useUpdateMyPresence() {
158
- const room = useRoom();
159
- return React.useCallback((overrides, options) => {
160
- room.updatePresence(overrides, options);
161
- }, [room]);
162
- }
163
- /**
164
- * Returns an object that lets you get information about all the the users currently connected in the room.
165
- *
166
- * @example
167
- * import { useOthers } from "@liveblocks/react";
168
- *
169
- * const others = useOthers();
170
- *
171
- * // Example to map all cursors in jsx
172
- * {
173
- * others.map(({ connectionId, presence }) => {
174
- * if(presence == null || presence.cursor == null) {
175
- * return null;
176
- * }
177
- * return <Cursor key={connectionId} cursor={presence.cursor} />
178
- * })
179
- * }
180
- */
181
- function useOthers() {
182
- const room = useRoom();
183
- const rerender = useRerender();
184
- React.useEffect(() => {
185
- const unsubscribe = room.subscribe("others", rerender);
186
- return () => {
187
- unsubscribe();
188
- };
189
- }, [room]);
190
- return room.getOthers();
191
- }
192
- /**
193
- * Returns a callback that lets you broadcast custom events to other users in the room
194
- *
195
- * @example
196
- * import { useBroadcastEvent } from "@liveblocks/react";
197
- *
198
- * const broadcast = useBroadcastEvent();
199
- *
200
- * broadcast({ type: "CUSTOM_EVENT", data: { x: 0, y: 0 } });
201
- */
202
- function useBroadcastEvent() {
203
- const room = useRoom();
204
- return React.useCallback((event, options = { shouldQueueEventIfNotReady: false }) => {
205
- room.broadcastEvent(event, options);
206
- }, [room]);
207
- }
208
- /**
209
- * useErrorListener is a react hook that lets you react to potential room connection errors.
210
- *
211
- * @example
212
- * import { useErrorListener } from "@liveblocks/react";
213
- *
214
- * useErrorListener(er => {
215
- * console.error(er);
216
- * })
217
- */
218
- function useErrorListener(callback) {
219
- const room = useRoom();
220
- const savedCallback = React.useRef(callback);
221
- React.useEffect(() => {
222
- savedCallback.current = callback;
223
- });
224
- React.useEffect(() => {
225
- const listener = (e) => savedCallback.current(e);
226
- const unsubscribe = room.subscribe("error", listener);
227
- return () => {
228
- unsubscribe();
229
- };
230
- }, [room]);
231
- }
232
- /**
233
- * useEventListener is a react hook that lets you react to event broadcasted by other users in the room.
234
- *
235
- * @example
236
- * import { useEventListener } from "@liveblocks/react";
237
- *
238
- * useEventListener(({ connectionId, event }) => {
239
- * if (event.type === "CUSTOM_EVENT") {
240
- * // Do something
241
- * }
242
- * });
243
- */
244
- function useEventListener(callback) {
245
- const room = useRoom();
246
- const savedCallback = React.useRef(callback);
247
- React.useEffect(() => {
248
- savedCallback.current = callback;
249
- });
250
- React.useEffect(() => {
251
- const listener = (e) => savedCallback.current(e);
252
- const unsubscribe = room.subscribe("event", listener);
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 };