@liveblocks/react 0.15.5 → 0.15.6-beta.1

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/lib/index.js CHANGED
@@ -1,433 +1,365 @@
1
+ 'use strict';
2
+
1
3
  Object.defineProperty(exports, '__esModule', { value: true });
2
4
 
3
5
  var client = require('@liveblocks/client');
4
6
  var React = require('react');
5
7
 
6
- /*! *****************************************************************************
7
- Copyright (c) Microsoft Corporation.
8
-
9
- Permission to use, copy, modify, and/or distribute this software for any
10
- purpose with or without fee is hereby granted.
11
-
12
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
- PERFORMANCE OF THIS SOFTWARE.
19
- ***************************************************************************** */
20
-
21
- function __awaiter(thisArg, _arguments, P, generator) {
22
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
- return new (P || (P = Promise))(function (resolve, reject) {
24
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
- step((generator = generator.apply(thisArg, _arguments || [])).next());
28
- });
29
- }
30
-
31
- function __generator(thisArg, body) {
32
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
33
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
34
- function verb(n) { return function (v) { return step([n, v]); }; }
35
- function step(op) {
36
- if (f) throw new TypeError("Generator is already executing.");
37
- while (_) try {
38
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
39
- if (y = 0, t) op = [op[0] & 2, t.value];
40
- switch (op[0]) {
41
- case 0: case 1: t = op; break;
42
- case 4: _.label++; return { value: op[1], done: false };
43
- case 5: _.label++; y = op[1]; op = [0]; continue;
44
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
45
- default:
46
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
47
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
48
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
49
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
50
- if (t[2]) _.ops.pop();
51
- _.trys.pop(); continue;
52
- }
53
- op = body.call(thisArg, _);
54
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
55
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
56
- }
8
+ function _interopNamespace(e) {
9
+ if (e && e.__esModule) return e;
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n["default"] = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
27
+
28
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
29
+ try {
30
+ var info = gen[key](arg);
31
+ var value = info.value;
32
+ } catch (error) {
33
+ reject(error);
34
+ return;
35
+ }
36
+
37
+ if (info.done) {
38
+ resolve(value);
39
+ } else {
40
+ Promise.resolve(value).then(_next, _throw);
41
+ }
42
+ }
43
+
44
+ function _asyncToGenerator(fn) {
45
+ return function () {
46
+ var self = this,
47
+ args = arguments;
48
+ return new Promise(function (resolve, reject) {
49
+ var gen = fn.apply(self, args);
50
+
51
+ function _next(value) {
52
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
53
+ }
54
+
55
+ function _throw(err) {
56
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
57
+ }
58
+
59
+ _next(undefined);
60
+ });
61
+ };
62
+ }
63
+
64
+ var ClientContext = React__namespace.createContext(null);
65
+ var RoomContext = React__namespace.createContext(null);
66
+ function LiveblocksProvider(props) {
67
+ return React__namespace.createElement(ClientContext.Provider, {
68
+ value: props.client
69
+ }, props.children);
70
+ }
71
+
72
+ function useClient() {
73
+ var client = React__namespace.useContext(ClientContext);
74
+
75
+ if (client == null) {
76
+ throw new Error("LiveblocksProvider is missing from the react tree");
77
+ }
78
+
79
+ return client;
80
+ }
81
+
82
+ function RoomProvider(_ref) {
83
+ var id = _ref.id,
84
+ children = _ref.children,
85
+ defaultPresence = _ref.defaultPresence,
86
+ defaultStorageRoot = _ref.defaultStorageRoot;
87
+
88
+ if (process.env.NODE_ENV !== "production") {
89
+ if (id == null) {
90
+ throw new Error("RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required");
91
+ }
92
+
93
+ if (typeof id !== "string") {
94
+ throw new Error("RoomProvider id property should be a string.");
95
+ }
96
+ }
97
+
98
+ var client = useClient();
99
+ React__namespace.useEffect(function () {
100
+ return function () {
101
+ client.leave(id);
102
+ };
103
+ }, [client, id]);
104
+ var room = client.getRoom(id) || client.enter(id, {
105
+ defaultPresence: defaultPresence ? defaultPresence() : undefined,
106
+ defaultStorageRoot: defaultStorageRoot,
107
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
108
+ });
109
+ return React__namespace.createElement(RoomContext.Provider, {
110
+ value: room
111
+ }, children);
112
+ }
113
+ function useRoom() {
114
+ var room = React__namespace.useContext(RoomContext);
115
+
116
+ if (room == null) {
117
+ throw new Error("RoomProvider is missing from the react tree");
118
+ }
119
+
120
+ return room;
121
+ }
122
+ function useMyPresence() {
123
+ var room = useRoom();
124
+ var presence = room.getPresence();
125
+
126
+ var _React$useState = React__namespace.useState(0),
127
+ update = _React$useState[1];
128
+
129
+ React__namespace.useEffect(function () {
130
+ function onMyPresenceChange() {
131
+ update(function (x) {
132
+ return x + 1;
133
+ });
134
+ }
135
+
136
+ var unsubscribe = room.subscribe("my-presence", onMyPresenceChange);
137
+ return function () {
138
+ unsubscribe();
139
+ };
140
+ }, [room]);
141
+ var setPresence = React__namespace.useCallback(function (overrides, options) {
142
+ return room.updatePresence(overrides, options);
143
+ }, [room]);
144
+ return [presence, setPresence];
145
+ }
146
+ function useUpdateMyPresence() {
147
+ var room = useRoom();
148
+ return React__namespace.useCallback(function (overrides, options) {
149
+ room.updatePresence(overrides, options);
150
+ }, [room]);
151
+ }
152
+ function useOthers() {
153
+ var room = useRoom();
154
+
155
+ var _React$useState2 = React__namespace.useState(0),
156
+ update = _React$useState2[1];
157
+
158
+ React__namespace.useEffect(function () {
159
+ function onOthersChange() {
160
+ update(function (x) {
161
+ return x + 1;
162
+ });
163
+ }
164
+
165
+ var unsubscribe = room.subscribe("others", onOthersChange);
166
+ return function () {
167
+ unsubscribe();
168
+ };
169
+ }, [room]);
170
+ return room.getOthers();
171
+ }
172
+ function useBroadcastEvent() {
173
+ var room = useRoom();
174
+ return React__namespace.useCallback(function (event, options) {
175
+ if (options === void 0) {
176
+ options = {
177
+ shouldQueueEventIfNotReady: false
178
+ };
179
+ }
180
+
181
+ room.broadcastEvent(event, options);
182
+ }, [room]);
57
183
  }
184
+ function useErrorListener(callback) {
185
+ var room = useRoom();
186
+ var savedCallback = React__namespace.useRef(callback);
187
+ React__namespace.useEffect(function () {
188
+ savedCallback.current = callback;
189
+ });
190
+ React__namespace.useEffect(function () {
191
+ var listener = function listener(e) {
192
+ return savedCallback.current(e);
193
+ };
194
+
195
+ var unsubscribe = room.subscribe("error", listener);
196
+ return function () {
197
+ unsubscribe();
198
+ };
199
+ }, [room]);
200
+ }
201
+ function useEventListener(callback) {
202
+ var room = useRoom();
203
+ var savedCallback = React__namespace.useRef(callback);
204
+ React__namespace.useEffect(function () {
205
+ savedCallback.current = callback;
206
+ });
207
+ React__namespace.useEffect(function () {
208
+ var listener = function listener(e) {
209
+ return savedCallback.current(e);
210
+ };
211
+
212
+ var unsubscribe = room.subscribe("event", listener);
213
+ return function () {
214
+ unsubscribe();
215
+ };
216
+ }, [room]);
217
+ }
218
+ function useSelf() {
219
+ var room = useRoom();
220
+
221
+ var _React$useState3 = React__namespace.useState(0),
222
+ update = _React$useState3[1];
223
+
224
+ React__namespace.useEffect(function () {
225
+ function onChange() {
226
+ update(function (x) {
227
+ return x + 1;
228
+ });
229
+ }
230
+
231
+ var unsubscribePresence = room.subscribe("my-presence", onChange);
232
+ var unsubscribeConnection = room.subscribe("connection", onChange);
233
+ return function () {
234
+ unsubscribePresence();
235
+ unsubscribeConnection();
236
+ };
237
+ }, [room]);
238
+ return room.getSelf();
239
+ }
240
+ function useStorage() {
241
+ var room = useRoom();
242
+
243
+ var _React$useState4 = React__namespace.useState(null),
244
+ root = _React$useState4[0],
245
+ setState = _React$useState4[1];
246
+
247
+ React__namespace.useEffect(function () {
248
+ var didCancel = false;
249
+
250
+ function fetchStorage() {
251
+ return _fetchStorage.apply(this, arguments);
252
+ }
253
+
254
+ function _fetchStorage() {
255
+ _fetchStorage = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
256
+ var storage;
257
+ return regeneratorRuntime.wrap(function _callee$(_context) {
258
+ while (1) {
259
+ switch (_context.prev = _context.next) {
260
+ case 0:
261
+ _context.next = 2;
262
+ return room.getStorage();
263
+
264
+ case 2:
265
+ storage = _context.sent;
266
+
267
+ if (!didCancel) {
268
+ setState(storage.root);
269
+ }
270
+
271
+ case 4:
272
+ case "end":
273
+ return _context.stop();
274
+ }
275
+ }
276
+ }, _callee);
277
+ }));
278
+ return _fetchStorage.apply(this, arguments);
279
+ }
280
+
281
+ fetchStorage();
282
+ return function () {
283
+ didCancel = true;
284
+ };
285
+ }, [room]);
286
+ return [root];
287
+ }
288
+ function useMap(key, entries) {
289
+ return useCrdt(key, new client.LiveMap(entries));
290
+ }
291
+ function useList(key, items) {
292
+ return useCrdt(key, new client.LiveList(items));
293
+ }
294
+ function useObject(key, initialData) {
295
+ return useCrdt(key, new client.LiveObject(initialData));
296
+ }
297
+ function useUndo() {
298
+ return useRoom().history.undo;
299
+ }
300
+ function useRedo() {
301
+ return useRoom().history.redo;
302
+ }
303
+ function useBatch() {
304
+ return useRoom().batch;
305
+ }
306
+ function useHistory() {
307
+ return useRoom().history;
308
+ }
309
+
310
+ function useCrdt(key, initialCrdt) {
311
+ var _root$get;
312
+
313
+ var room = useRoom();
314
+
315
+ var _useStorage = useStorage(),
316
+ root = _useStorage[0];
317
+
318
+ var _React$useState5 = React__namespace.useState(0),
319
+ setCount = _React$useState5[1];
320
+
321
+ React__namespace.useEffect(function () {
322
+ if (root == null) {
323
+ return;
324
+ }
325
+
326
+ var crdt = root.get(key);
327
+
328
+ if (crdt == null) {
329
+ crdt = initialCrdt;
330
+ root.set(key, crdt);
331
+ }
332
+
333
+ function onChange() {
334
+ setCount(function (x) {
335
+ return x + 1;
336
+ });
337
+ }
338
+
339
+ function onRootChange() {
340
+ var newCrdt = root.get(key);
341
+
342
+ if (newCrdt !== crdt) {
343
+ unsubscribeCrdt();
344
+ crdt = newCrdt;
345
+ unsubscribeCrdt = room.subscribe(crdt, onChange);
346
+ setCount(function (x) {
347
+ return x + 1;
348
+ });
349
+ }
350
+ }
58
351
 
59
- var ClientContext = React.createContext(null);
60
- var RoomContext = React.createContext(null);
61
- /**
62
- * Makes the Liveblocks client available in the component hierarchy below.
63
- */
64
- function LiveblocksProvider(props) {
65
- return (React.createElement(ClientContext.Provider, { value: props.client }, props.children));
66
- }
67
- /**
68
- * Returns the client of the nearest LiveblocksProvider above in the react component tree
69
- */
70
- function useClient() {
71
- var client = React.useContext(ClientContext);
72
- if (client == null) {
73
- throw new Error("LiveblocksProvider is missing from the react tree");
74
- }
75
- return client;
76
- }
77
- /**
78
- * Makes a Room available in the component hierarchy below.
79
- * When this component is unmounted, the current user leave the room.
80
- * That means that you can't have 2 RoomProvider with the same room id in your react tree.
81
- */
82
- function RoomProvider(_a) {
83
- var id = _a.id, children = _a.children, defaultPresence = _a.defaultPresence, defaultStorageRoot = _a.defaultStorageRoot;
84
- if (process.env.NODE_ENV !== "production") {
85
- if (id == null) {
86
- throw new Error("RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required");
87
- }
88
- if (typeof id !== "string") {
89
- throw new Error("RoomProvider id property should be a string.");
90
- }
91
- }
92
- var client = useClient();
93
- React.useEffect(function () {
94
- return function () {
95
- client.leave(id);
96
- };
97
- }, [client, id]);
98
- var room = client.getRoom(id) ||
99
- client.enter(id, {
100
- defaultPresence: defaultPresence ? defaultPresence() : undefined,
101
- defaultStorageRoot: defaultStorageRoot,
102
- DO_NOT_USE_withoutConnecting: typeof window === "undefined",
103
- });
104
- return React.createElement(RoomContext.Provider, { value: room }, children);
105
- }
106
- /**
107
- * Returns the room of the nearest RoomProvider above in the react component tree
108
- */
109
- function useRoom() {
110
- var room = React.useContext(RoomContext);
111
- if (room == null) {
112
- throw new Error("RoomProvider is missing from the react tree");
113
- }
114
- return room;
115
- }
116
- /**
117
- * Returns the presence of the current user of the current room, and a function to update it.
118
- * It is different from the setState function returned by the useState hook from React.
119
- * You don't need to pass the full presence object to update it.
120
- *
121
- * @example
122
- * import { useMyPresence } from "@liveblocks/react";
123
- *
124
- * const [myPresence, updateMyPresence] = useMyPresence();
125
- * updateMyPresence({ x: 0 });
126
- * updateMyPresence({ y: 0 });
127
- *
128
- * // At the next render, "myPresence" will be equal to "{ x: 0, y: 0 }"
129
- */
130
- function useMyPresence() {
131
- var room = useRoom();
132
- var presence = room.getPresence();
133
- var _a = React.useState(0), update = _a[1];
134
- React.useEffect(function () {
135
- function onMyPresenceChange() {
136
- update(function (x) { return x + 1; });
137
- }
138
- var unsubscribe = room.subscribe("my-presence", onMyPresenceChange);
139
- return function () {
140
- unsubscribe();
141
- };
142
- }, [room]);
143
- var setPresence = React.useCallback(function (overrides, options) {
144
- return room.updatePresence(overrides, options);
145
- }, [room]);
146
- return [presence, setPresence];
147
- }
148
- /**
149
- * useUpdateMyPresence is similar to useMyPresence but it only returns the function to update the current user presence.
150
- * 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.
151
- *
152
- * @example
153
- * import { useUpdateMyPresence } from "@liveblocks/react";
154
- *
155
- * const updateMyPresence = useUpdateMyPresence();
156
- * updateMyPresence({ x: 0 });
157
- * updateMyPresence({ y: 0 });
158
- *
159
- * // At the next render, the presence of the current user will be equal to "{ x: 0, y: 0 }"
160
- */
161
- function useUpdateMyPresence() {
162
- var room = useRoom();
163
- return React.useCallback(function (overrides, options) {
164
- room.updatePresence(overrides, options);
165
- }, [room]);
166
- }
167
- /**
168
- * Returns an object that lets you get information about all the the users currently connected in the room.
169
- *
170
- * @example
171
- * import { useOthers } from "@liveblocks/react";
172
- *
173
- * const others = useOthers();
174
- *
175
- * // Example to map all cursors in jsx
176
- * {
177
- * others.map(({ connectionId, presence }) => {
178
- * if(presence == null || presence.cursor == null) {
179
- * return null;
180
- * }
181
- * return <Cursor key={connectionId} cursor={presence.cursor} />
182
- * })
183
- * }
184
- */
185
- function useOthers() {
186
- var room = useRoom();
187
- var _a = React.useState(0), update = _a[1];
188
- React.useEffect(function () {
189
- function onOthersChange() {
190
- update(function (x) { return x + 1; });
191
- }
192
- var unsubscribe = room.subscribe("others", onOthersChange);
193
- return function () {
194
- unsubscribe();
195
- };
196
- }, [room]);
197
- return room.getOthers();
198
- }
199
- /**
200
- * Returns a callback that lets you broadcast custom events to other users in the room
201
- *
202
- * @example
203
- * import { useBroadcastEvent } from "@liveblocks/react";
204
- *
205
- * const broadcast = useBroadcastEvent();
206
- *
207
- * broadcast({ type: "CUSTOM_EVENT", data: { x: 0, y: 0 } });
208
- */
209
- function useBroadcastEvent() {
210
- var room = useRoom();
211
- return React.useCallback(function (event, options) {
212
- if (options === void 0) { options = { shouldQueueEventIfNotReady: false }; }
213
- room.broadcastEvent(event, options);
214
- }, [room]);
215
- }
216
- /**
217
- * useErrorListener is a react hook that lets you react to potential room connection errors.
218
- *
219
- * @example
220
- * import { useErrorListener } from "@liveblocks/react";
221
- *
222
- * useErrorListener(er => {
223
- * console.error(er);
224
- * })
225
- */
226
- function useErrorListener(callback) {
227
- var room = useRoom();
228
- var savedCallback = React.useRef(callback);
229
- React.useEffect(function () {
230
- savedCallback.current = callback;
231
- });
232
- React.useEffect(function () {
233
- var listener = function (e) { return savedCallback.current(e); };
234
- var unsubscribe = room.subscribe("error", listener);
235
- return function () {
236
- unsubscribe();
237
- };
238
- }, [room]);
239
- }
240
- /**
241
- * useEventListener is a react hook that lets you react to event broadcasted by other users in the room.
242
- *
243
- * @example
244
- * import { useEventListener } from "@liveblocks/react";
245
- *
246
- * useEventListener(({ connectionId, event }) => {
247
- * if (event.type === "CUSTOM_EVENT") {
248
- * // Do something
249
- * }
250
- * });
251
- */
252
- function useEventListener(callback) {
253
- var room = useRoom();
254
- var savedCallback = React.useRef(callback);
255
- React.useEffect(function () {
256
- savedCallback.current = callback;
257
- });
258
- React.useEffect(function () {
259
- var listener = function (e) {
260
- return savedCallback.current(e);
261
- };
262
- var unsubscribe = room.subscribe("event", listener);
263
- return function () {
264
- unsubscribe();
265
- };
266
- }, [room]);
267
- }
268
- /**
269
- * Gets the current user once it is connected to the room.
270
- *
271
- * @example
272
- * import { useSelf } from "@liveblocks/react";
273
- *
274
- * const user = useSelf();
275
- */
276
- function useSelf() {
277
- var room = useRoom();
278
- var _a = React.useState(0), update = _a[1];
279
- React.useEffect(function () {
280
- function onChange() {
281
- update(function (x) { return x + 1; });
282
- }
283
- var unsubscribePresence = room.subscribe("my-presence", onChange);
284
- var unsubscribeConnection = room.subscribe("connection", onChange);
285
- return function () {
286
- unsubscribePresence();
287
- unsubscribeConnection();
288
- };
289
- }, [room]);
290
- return room.getSelf();
291
- }
292
- function useStorage() {
293
- var room = useRoom();
294
- var _a = React.useState(null), root = _a[0], setState = _a[1];
295
- React.useEffect(function () {
296
- var didCancel = false;
297
- function fetchStorage() {
298
- return __awaiter(this, void 0, void 0, function () {
299
- var storage;
300
- return __generator(this, function (_a) {
301
- switch (_a.label) {
302
- case 0: return [4 /*yield*/, room.getStorage()];
303
- case 1:
304
- storage = _a.sent();
305
- if (!didCancel) {
306
- setState(storage.root);
307
- }
308
- return [2 /*return*/];
309
- }
310
- });
311
- });
312
- }
313
- fetchStorage();
314
- return function () {
315
- didCancel = true;
316
- };
317
- }, [room]);
318
- return [root];
319
- }
320
- /**
321
- * Returns the LiveMap associated with the provided key. If the LiveMap does not exist, a new empty LiveMap will be created.
322
- * 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.
323
- *
324
- * @param key The storage key associated with the LiveMap
325
- * @param entries Optional entries that are used to create the LiveMap for the first time
326
- * @returns null while the storage is loading, otherwise, returns the LiveMap associated to the storage
327
- *
328
- * @example
329
- * const emptyMap = useMap("mapA");
330
- * const mapWithItems = useMap("mapB", [["keyA", "valueA"], ["keyB", "valueB"]]);
331
- */
332
- function useMap(key, entries) {
333
- return useCrdt(key, new client.LiveMap(entries));
334
- }
335
- /**
336
- * Returns the LiveList associated with the provided key. If the LiveList does not exist, a new LiveList will be created.
337
- * 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.
338
- *
339
- * @param key The storage key associated with the LiveList
340
- * @param items Optional items that are used to create the LiveList for the first time
341
- * @returns null while the storage is loading, otherwise, returns the LiveList associated to the storage
342
- *
343
- * @example
344
- * const emptyList = useList("listA");
345
- * const listWithItems = useList("listB", ["a", "b", "c"]);
346
- */
347
- function useList(key, items) {
348
- return useCrdt(key, new client.LiveList(items));
349
- }
350
- /**
351
- * Returns the LiveObject associated with the provided key. If the LiveObject does not exist, it will be created with the initialData parameter.
352
- * 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.
353
- *
354
- * @param key The storage key associated with the LiveObject
355
- * @param initialData Optional data that is used to create the LiveObject for the first time
356
- * @returns null while the storage is loading, otherwise, returns the LveObject associated to the storage
357
- *
358
- * @example
359
- * const object = useObject("obj", {
360
- * company: "Liveblocks",
361
- * website: "https://liveblocks.io"
362
- * });
363
- */
364
- function useObject(key, initialData) {
365
- return useCrdt(key, new client.LiveObject(initialData));
366
- }
367
- /**
368
- * Returns a function that undoes the last operation executed by the current client.
369
- * It does not impact operations made by other clients.
370
- */
371
- function useUndo() {
372
- return useRoom().history.undo;
373
- }
374
- /**
375
- * Returns a function that redoes the last operation executed by the current client.
376
- * It does not impact operations made by other clients.
377
- */
378
- function useRedo() {
379
- return useRoom().history.redo;
380
- }
381
- /**
382
- * Returns a function that batches modifications made during the given function.
383
- * All the modifications are sent to other clients in a single message.
384
- * All the modifications are merged in a single history item (undo/redo).
385
- * All the subscribers are called only after the batch is over.
386
- */
387
- function useBatch() {
388
- return useRoom().batch;
389
- }
390
- /**
391
- * Returns the room.history
392
- */
393
- function useHistory() {
394
- return useRoom().history;
395
- }
396
- function useCrdt(key, initialCrdt) {
397
- var _a;
398
- var room = useRoom();
399
- var root = useStorage()[0];
400
- var _b = React.useState(0), setCount = _b[1];
401
- React.useEffect(function () {
402
- if (root == null) {
403
- return;
404
- }
405
- var crdt = root.get(key);
406
- if (crdt == null) {
407
- crdt = initialCrdt;
408
- root.set(key, crdt);
409
- }
410
- function onChange() {
411
- setCount(function (x) { return x + 1; });
412
- }
413
- function onRootChange() {
414
- var newCrdt = root.get(key);
415
- if (newCrdt !== crdt) {
416
- unsubscribeCrdt();
417
- crdt = newCrdt;
418
- unsubscribeCrdt = room.subscribe(crdt /* AbstractCrdt */, onChange);
419
- setCount(function (x) { return x + 1; });
420
- }
421
- }
422
- var unsubscribeCrdt = room.subscribe(crdt /* AbstractCrdt */, onChange);
423
- var unsubscribeRoot = room.subscribe(root /* AbstractCrdt */, onRootChange);
424
- setCount(function (x) { return x + 1; });
425
- return function () {
426
- unsubscribeRoot();
427
- unsubscribeCrdt();
428
- };
429
- }, [root, room]);
430
- return (_a = root === null || root === void 0 ? void 0 : root.get(key)) !== null && _a !== void 0 ? _a : null;
352
+ var unsubscribeCrdt = room.subscribe(crdt, onChange);
353
+ var unsubscribeRoot = room.subscribe(root, onRootChange);
354
+ setCount(function (x) {
355
+ return x + 1;
356
+ });
357
+ return function () {
358
+ unsubscribeRoot();
359
+ unsubscribeCrdt();
360
+ };
361
+ }, [root, room]);
362
+ return (_root$get = root == null ? void 0 : root.get(key)) != null ? _root$get : null;
431
363
  }
432
364
 
433
365
  exports.LiveblocksProvider = LiveblocksProvider;
@@ -448,4 +380,3 @@ exports.useSelf = useSelf;
448
380
  exports.useStorage = useStorage;
449
381
  exports.useUndo = useUndo;
450
382
  exports.useUpdateMyPresence = useUpdateMyPresence;
451
- //# sourceMappingURL=index.js.map