@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.js CHANGED
@@ -1,636 +1,639 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var internal = require("@liveblocks/client/internal"),
4
- React = require("react"),
5
- client = require("@liveblocks/client");
6
- function _interopNamespace(e) {
7
- if (e && e.__esModule) return e;
8
- var n = Object.create(null);
9
- return (
10
- e &&
11
- Object.keys(e).forEach(function (k) {
12
- if ("default" !== k) {
13
- var d = Object.getOwnPropertyDescriptor(e, k);
14
- Object.defineProperty(
15
- n,
16
- k,
17
- d.get
18
- ? d
19
- : {
20
- enumerable: !0,
21
- get: function () {
22
- return e[k];
23
- },
24
- }
25
- );
26
- }
27
- }),
28
- (n.default = e),
29
- Object.freeze(n)
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+
22
+ // src/client.tsx
23
+ var _internal = require('@liveblocks/client/internal');
24
+ var _react = require('react'); var React = _interopRequireWildcard(_react); var React2 = _interopRequireWildcard(_react);
25
+ var ClientContext = React.createContext(null);
26
+ function LiveblocksProvider(props) {
27
+ _internal.deprecate.call(void 0,
28
+ "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."
30
29
  );
30
+ return /* @__PURE__ */ React.createElement(ClientContext.Provider, {
31
+ value: props.client
32
+ }, props.children);
31
33
  }
32
- var React__namespace = _interopNamespace(React),
33
- ClientContext = React__namespace.createContext(null);
34
34
  function useClient() {
35
- var client = React__namespace.useContext(ClientContext);
36
- if (null == client)
35
+ const client = React.useContext(ClientContext);
36
+ if (client == null) {
37
37
  throw new Error("LiveblocksProvider is missing from the react tree");
38
- return client;
39
- }
40
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
41
- try {
42
- var info = gen[key](arg),
43
- value = info.value;
44
- } catch (error) {
45
- return void reject(error);
46
38
  }
47
- info.done ? resolve(value) : Promise.resolve(value).then(_next, _throw);
48
- }
49
- function _asyncToGenerator(fn) {
50
- return function () {
51
- var self = this,
52
- args = arguments;
53
- return new Promise(function (resolve, reject) {
54
- var gen = fn.apply(self, args);
55
- function _next(value) {
56
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
57
- }
58
- function _throw(err) {
59
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
60
- }
61
- _next(void 0);
62
- });
63
- };
39
+ return client;
64
40
  }
41
+
42
+ // src/factory.tsx
43
+ var _client2 = require('@liveblocks/client');
44
+
45
+
46
+
47
+ // src/hooks.ts
48
+
65
49
  function useRerender() {
66
- return React.useReducer(function (x) {
67
- return x + 1;
68
- }, 0)[1];
50
+ const [, update] = _react.useReducer.call(void 0,
51
+ (x) => x + 1,
52
+ 0
53
+ );
54
+ return update;
69
55
  }
56
+
57
+ // src/factory.tsx
70
58
  function useInitial(value) {
71
- return React__namespace.useRef(value).current;
59
+ return React2.useRef(value).current;
72
60
  }
73
- function createRoomContext(client$1) {
74
- var useClient$1;
75
- useClient$1 =
76
- "__legacy" !== client$1
77
- ? function () {
78
- return client$1;
79
- }
80
- : useClient;
81
- var RoomContext = React__namespace.createContext(null);
82
- function useRoom() {
83
- var room = React__namespace.useContext(RoomContext);
84
- if (null == room)
61
+ function createRoomContext(client) {
62
+ let useClient2;
63
+ if (client !== "__legacy") {
64
+ useClient2 = () => client;
65
+ } else {
66
+ useClient2 = useClient;
67
+ }
68
+ const RoomContext = React2.createContext(null);
69
+ function RoomProvider2(props) {
70
+ const {
71
+ id: roomId,
72
+ initialPresence,
73
+ initialStorage,
74
+ defaultPresence,
75
+ defaultStorageRoot
76
+ } = props;
77
+ if (process.env.NODE_ENV !== "production") {
78
+ if (roomId == null) {
79
+ throw new Error(
80
+ "RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required"
81
+ );
82
+ }
83
+ if (typeof roomId !== "string") {
84
+ throw new Error("RoomProvider id property should be a string.");
85
+ }
86
+ }
87
+ _internal.errorIf.call(void 0,
88
+ defaultPresence,
89
+ "RoomProvider's `defaultPresence` prop will be removed in @liveblocks/react 0.18. Please use `initialPresence` instead. For more info, see https://bit.ly/3Niy5aP"
90
+ );
91
+ _internal.errorIf.call(void 0,
92
+ defaultStorageRoot,
93
+ "RoomProvider's `defaultStorageRoot` prop will be removed in @liveblocks/react 0.18. Please use `initialStorage` instead. For more info, see https://bit.ly/3Niy5aP"
94
+ );
95
+ const _client = useClient2();
96
+ const frozen = useInitial({
97
+ initialPresence,
98
+ initialStorage,
99
+ defaultPresence,
100
+ defaultStorageRoot
101
+ });
102
+ const [room, setRoom] = React2.useState(
103
+ () => _client.enter(roomId, {
104
+ initialPresence,
105
+ initialStorage,
106
+ defaultPresence,
107
+ defaultStorageRoot,
108
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
109
+ })
110
+ );
111
+ React2.useEffect(() => {
112
+ setRoom(
113
+ _client.enter(roomId, {
114
+ initialPresence: frozen.initialPresence,
115
+ initialStorage: frozen.initialStorage,
116
+ defaultPresence: frozen.defaultPresence,
117
+ defaultStorageRoot: frozen.defaultStorageRoot,
118
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
119
+ })
120
+ );
121
+ return () => {
122
+ _client.leave(roomId);
123
+ };
124
+ }, [_client, roomId, frozen]);
125
+ return /* @__PURE__ */ React2.createElement(RoomContext.Provider, {
126
+ value: room
127
+ }, props.children);
128
+ }
129
+ function useRoom2() {
130
+ const room = React2.useContext(RoomContext);
131
+ if (room == null) {
85
132
  throw new Error("RoomProvider is missing from the react tree");
133
+ }
86
134
  return room;
87
135
  }
88
- function useStorage() {
89
- var room = useRoom(),
90
- _React$useState2 = React__namespace.useState(null),
91
- root = _React$useState2[0],
92
- setState = _React$useState2[1];
93
- return (
94
- React__namespace.useEffect(
95
- function () {
96
- var didCancel = !1;
97
- function _fetchStorage() {
98
- return (_fetchStorage = _asyncToGenerator(
99
- regeneratorRuntime.mark(function _callee() {
100
- var storage;
101
- return regeneratorRuntime.wrap(function (_context) {
102
- for (;;)
103
- switch ((_context.prev = _context.next)) {
104
- case 0:
105
- return (_context.next = 2), room.getStorage();
106
- case 2:
107
- (storage = _context.sent),
108
- didCancel || setState(storage.root);
109
- case 4:
110
- case "end":
111
- return _context.stop();
112
- }
113
- }, _callee);
114
- })
115
- )).apply(this, arguments);
116
- }
117
- return (
118
- (function () {
119
- _fetchStorage.apply(this, arguments);
120
- })(),
121
- function () {
122
- didCancel = !0;
123
- }
124
- );
125
- },
126
- [room]
127
- ),
128
- [root]
136
+ function useMyPresence2() {
137
+ const room = useRoom2();
138
+ const presence = room.getPresence();
139
+ const rerender = useRerender();
140
+ React2.useEffect(() => {
141
+ const unsubscribe = room.subscribe("my-presence", rerender);
142
+ return () => {
143
+ unsubscribe();
144
+ };
145
+ }, [room, rerender]);
146
+ const setPresence = React2.useCallback(
147
+ (overrides, options) => room.updatePresence(overrides, options),
148
+ [room]
149
+ );
150
+ return [presence, setPresence];
151
+ }
152
+ function useUpdateMyPresence2() {
153
+ const room = useRoom2();
154
+ return React2.useCallback(
155
+ (overrides, options) => {
156
+ room.updatePresence(overrides, options);
157
+ },
158
+ [room]
129
159
  );
130
160
  }
161
+ function useOthers2() {
162
+ const room = useRoom2();
163
+ const rerender = useRerender();
164
+ React2.useEffect(() => {
165
+ const unsubscribe = room.subscribe("others", rerender);
166
+ return () => {
167
+ unsubscribe();
168
+ };
169
+ }, [room, rerender]);
170
+ return room.getOthers();
171
+ }
172
+ function useBroadcastEvent2() {
173
+ const room = useRoom2();
174
+ return React2.useCallback(
175
+ (event, options = { shouldQueueEventIfNotReady: false }) => {
176
+ room.broadcastEvent(event, options);
177
+ },
178
+ [room]
179
+ );
180
+ }
181
+ function useErrorListener2(callback) {
182
+ const room = useRoom2();
183
+ const savedCallback = React2.useRef(callback);
184
+ React2.useEffect(() => {
185
+ savedCallback.current = callback;
186
+ });
187
+ React2.useEffect(() => {
188
+ const listener = (e) => savedCallback.current(e);
189
+ const unsubscribe = room.subscribe("error", listener);
190
+ return () => {
191
+ unsubscribe();
192
+ };
193
+ }, [room]);
194
+ }
195
+ function useEventListener2(callback) {
196
+ const room = useRoom2();
197
+ const savedCallback = React2.useRef(callback);
198
+ React2.useEffect(() => {
199
+ savedCallback.current = callback;
200
+ });
201
+ React2.useEffect(() => {
202
+ const listener = (eventData) => {
203
+ savedCallback.current(eventData);
204
+ };
205
+ const unsubscribe = room.subscribe("event", listener);
206
+ return () => {
207
+ unsubscribe();
208
+ };
209
+ }, [room]);
210
+ }
211
+ function useSelf2() {
212
+ const room = useRoom2();
213
+ const rerender = useRerender();
214
+ React2.useEffect(() => {
215
+ const unsubscribePresence = room.subscribe("my-presence", rerender);
216
+ const unsubscribeConnection = room.subscribe("connection", rerender);
217
+ return () => {
218
+ unsubscribePresence();
219
+ unsubscribeConnection();
220
+ };
221
+ }, [room, rerender]);
222
+ return room.getSelf();
223
+ }
224
+ function useStorage2() {
225
+ const room = useRoom2();
226
+ const [root, setState] = React2.useState(null);
227
+ React2.useEffect(() => {
228
+ let didCancel = false;
229
+ function fetchStorage() {
230
+ return __async(this, null, function* () {
231
+ const storage = yield room.getStorage();
232
+ if (!didCancel) {
233
+ setState(storage.root);
234
+ }
235
+ });
236
+ }
237
+ fetchStorage();
238
+ return () => {
239
+ didCancel = true;
240
+ };
241
+ }, [room]);
242
+ return [root];
243
+ }
131
244
  function useMap_deprecated(key, entries) {
132
- internal.errorIf(
245
+ _internal.errorIf.call(void 0,
133
246
  entries,
134
- "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 " +
135
- JSON.stringify(key) +
136
- ": new LiveMap(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details."
137
- );
138
- var value = useStorageValue(
139
- key,
140
- new client.LiveMap(null != entries ? entries : void 0)
247
+ `Support for initializing entries in useMap() directly will be removed in @liveblocks/react 0.18.
248
+
249
+ Instead, please initialize this data where you set up your RoomProvider:
250
+
251
+ const initialStorage = () => ({
252
+ ${JSON.stringify(key)}: new LiveMap(...),
253
+ ...
254
+ });
255
+
256
+ <RoomProvider initialStorage={initialStorage}>
257
+ ...
258
+ </RoomProvider>
259
+
260
+ Please see https://bit.ly/3Niy5aP for details.`
141
261
  );
142
- return "ok" === value.status
143
- ? value.value
144
- : (internal.errorIf(
145
- "notfound" === value.status,
146
- "Key " +
147
- JSON.stringify(key) +
148
- ' 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 ' +
149
- JSON.stringify(key) +
150
- ": new LiveMap(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details."
151
- ),
152
- null);
262
+ const value = useStorageValue(key, new (0, _client2.LiveMap)(entries != null ? entries : void 0));
263
+ if (value.status === "ok") {
264
+ return value.value;
265
+ } else {
266
+ _internal.errorIf.call(void 0,
267
+ value.status === "notfound",
268
+ `Key ${JSON.stringify(
269
+ key
270
+ )} was not found in Storage. Starting with 0.18, useMap() will no longer automatically create this key.
271
+
272
+ Instead, please initialize your storage where you set up your RoomProvider:
273
+
274
+ import { LiveMap } from "@liveblocks/client";
275
+
276
+ const initialStorage = () => ({
277
+ ${JSON.stringify(key)}: new LiveMap(...),
278
+ ...
279
+ });
280
+
281
+ <RoomProvider initialStorage={initialStorage}>
282
+ ...
283
+ </RoomProvider>
284
+
285
+ Please see https://bit.ly/3Niy5aP for details.`
286
+ );
287
+ return null;
288
+ }
153
289
  }
154
290
  function useList_deprecated(key, items) {
155
- internal.errorIf(
291
+ _internal.errorIf.call(void 0,
156
292
  items,
157
- '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 ' +
158
- JSON.stringify(key) +
159
- ": new LiveList(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details."
293
+ `Support for initializing items in useList() directly will be removed in @liveblocks/react 0.18.
294
+
295
+ Instead, please initialize this data where you set up your RoomProvider:
296
+
297
+ import { LiveList } from "@liveblocks/client";
298
+
299
+ const initialStorage = () => ({
300
+ ${JSON.stringify(key)}: new LiveList(...),
301
+ ...
302
+ });
303
+
304
+ <RoomProvider initialStorage={initialStorage}>
305
+ ...
306
+ </RoomProvider>
307
+
308
+ Please see https://bit.ly/3Niy5aP for details.`
160
309
  );
161
- var value = useStorageValue(key, new client.LiveList(items));
162
- return "ok" === value.status
163
- ? value.value
164
- : (internal.errorIf(
165
- "notfound" === value.status,
166
- "Key " +
167
- JSON.stringify(key) +
168
- ' 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 ' +
169
- JSON.stringify(key) +
170
- ": new LiveList(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details."
171
- ),
172
- null);
310
+ const value = useStorageValue(key, new (0, _client2.LiveList)(items));
311
+ if (value.status === "ok") {
312
+ return value.value;
313
+ } else {
314
+ _internal.errorIf.call(void 0,
315
+ value.status === "notfound",
316
+ `Key ${JSON.stringify(
317
+ key
318
+ )} was not found in Storage. Starting with 0.18, useList() will no longer automatically create this key.
319
+
320
+ Instead, please initialize your storage where you set up your RoomProvider:
321
+
322
+ import { LiveList } from "@liveblocks/client";
323
+
324
+ const initialStorage = () => ({
325
+ ${JSON.stringify(key)}: new LiveList(...),
326
+ ...
327
+ });
328
+
329
+ <RoomProvider initialStorage={initialStorage}>
330
+ ...
331
+ </RoomProvider>
332
+
333
+ Please see https://bit.ly/3Niy5aP for details.`
334
+ );
335
+ return null;
336
+ }
173
337
  }
174
338
  function useObject_deprecated(key, initialData) {
175
- internal.errorIf(
339
+ _internal.errorIf.call(void 0,
176
340
  initialData,
177
- '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 ' +
178
- JSON.stringify(key) +
179
- ": new LiveObject(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details."
341
+ `Support for initializing data in useObject() directly will be removed in @liveblocks/react 0.18.
342
+
343
+ Instead, please initialize this data where you set up your RoomProvider:
344
+
345
+ import { LiveObject } from "@liveblocks/client";
346
+
347
+ const initialStorage = () => ({
348
+ ${JSON.stringify(key)}: new LiveObject(...),
349
+ ...
350
+ });
351
+
352
+ <RoomProvider initialStorage={initialStorage}>
353
+ ...
354
+ </RoomProvider>
355
+
356
+ Please see https://bit.ly/3Niy5aP for details.`
180
357
  );
181
- var value = useStorageValue(key, new client.LiveObject(initialData));
182
- return "ok" === value.status
183
- ? value.value
184
- : (internal.errorIf(
185
- "notfound" === value.status,
186
- "Key " +
187
- JSON.stringify(key) +
188
- ' 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 ' +
189
- JSON.stringify(key) +
190
- ": new LiveObject(...),\n ...\n });\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details."
191
- ),
192
- null);
358
+ const value = useStorageValue(key, new (0, _client2.LiveObject)(initialData));
359
+ if (value.status === "ok") {
360
+ return value.value;
361
+ } else {
362
+ _internal.errorIf.call(void 0,
363
+ value.status === "notfound",
364
+ `Key ${JSON.stringify(
365
+ key
366
+ )} was not found in Storage. Starting with 0.18, useObject() will no longer automatically create this key.
367
+
368
+ Instead, please initialize your storage where you set up your RoomProvider:
369
+
370
+ import { LiveObject } from "@liveblocks/client";
371
+
372
+ const initialStorage = () => ({
373
+ ${JSON.stringify(key)}: new LiveObject(...),
374
+ ...
375
+ });
376
+
377
+ <RoomProvider initialStorage={initialStorage}>
378
+ ...
379
+ </RoomProvider>
380
+
381
+ Please see https://bit.ly/3Niy5aP for details.`
382
+ );
383
+ return null;
384
+ }
193
385
  }
194
- function useHistory() {
195
- return useRoom().history;
386
+ function useList2(key) {
387
+ return useList_deprecated(key);
196
388
  }
197
- function useStorageValue(key, initialValue) {
198
- var room = useRoom(),
199
- root = useStorage()[0],
200
- rerender = useRerender(),
201
- frozenInitialValue = useInitial(initialValue);
202
- if (
203
- (React__namespace.useEffect(
204
- function () {
205
- if (null != root) {
206
- var liveValue = root.get(key);
207
- null == liveValue &&
208
- ((liveValue = frozenInitialValue), root.set(key, liveValue));
209
- var unsubscribeCrdt = room.subscribe(liveValue, rerender),
210
- unsubscribeRoot = room.subscribe(root, function () {
211
- var newCrdt = root.get(key);
212
- newCrdt !== liveValue &&
213
- (unsubscribeCrdt(),
214
- (liveValue = newCrdt),
215
- (unsubscribeCrdt = room.subscribe(liveValue, rerender)),
216
- rerender());
217
- });
218
- return (
219
- rerender(),
220
- function () {
221
- unsubscribeRoot(), unsubscribeCrdt();
222
- }
223
- );
224
- }
225
- },
226
- [root, room, key, frozenInitialValue, rerender]
227
- ),
228
- null == root)
229
- )
230
- return { status: "loading" };
231
- var value = root.get(key);
232
- return null == value
233
- ? { status: "notfound" }
234
- : { status: "ok", value: value };
389
+ function useMap2(key) {
390
+ return useMap_deprecated(key);
235
391
  }
236
- return {
237
- RoomProvider: function (props) {
238
- var roomId = props.id,
239
- initialPresence = props.initialPresence,
240
- initialStorage = props.initialStorage,
241
- defaultPresence = props.defaultPresence,
242
- defaultStorageRoot = props.defaultStorageRoot;
243
- if ("production" !== process.env.NODE_ENV) {
244
- if (null == roomId)
245
- throw new Error(
246
- "RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required"
247
- );
248
- if ("string" != typeof roomId)
249
- throw new Error("RoomProvider id property should be a string.");
250
- }
251
- internal.errorIf(
252
- defaultPresence,
253
- "RoomProvider's `defaultPresence` prop will be removed in @liveblocks/react 0.18. Please use `initialPresence` instead. For more info, see https://bit.ly/3Niy5aP"
254
- ),
255
- internal.errorIf(
256
- defaultStorageRoot,
257
- "RoomProvider's `defaultStorageRoot` prop will be removed in @liveblocks/react 0.18. Please use `initialStorage` instead. For more info, see https://bit.ly/3Niy5aP"
258
- );
259
- var _client = useClient$1(),
260
- frozen = useInitial({
261
- initialPresence: initialPresence,
262
- initialStorage: initialStorage,
263
- defaultPresence: defaultPresence,
264
- defaultStorageRoot: defaultStorageRoot,
265
- }),
266
- _React$useState = React__namespace.useState(function () {
267
- return _client.enter(roomId, {
268
- initialPresence: initialPresence,
269
- initialStorage: initialStorage,
270
- defaultPresence: defaultPresence,
271
- defaultStorageRoot: defaultStorageRoot,
272
- DO_NOT_USE_withoutConnecting: "undefined" == typeof window,
273
- });
274
- }),
275
- room = _React$useState[0],
276
- setRoom = _React$useState[1];
277
- return (
278
- React__namespace.useEffect(
279
- function () {
280
- return (
281
- setRoom(
282
- _client.enter(roomId, {
283
- initialPresence: frozen.initialPresence,
284
- initialStorage: frozen.initialStorage,
285
- defaultPresence: frozen.defaultPresence,
286
- defaultStorageRoot: frozen.defaultStorageRoot,
287
- DO_NOT_USE_withoutConnecting: "undefined" == typeof window,
288
- })
289
- ),
290
- function () {
291
- _client.leave(roomId);
292
- }
293
- );
294
- },
295
- [_client, roomId, frozen]
296
- ),
297
- React__namespace.createElement(
298
- RoomContext.Provider,
299
- { value: room },
300
- props.children
301
- )
302
- );
303
- },
304
- useBatch: function () {
305
- return useRoom().batch;
306
- },
307
- useBroadcastEvent: function () {
308
- var room = useRoom();
309
- return React__namespace.useCallback(
310
- function (event, options) {
311
- void 0 === options && (options = { shouldQueueEventIfNotReady: !1 }),
312
- room.broadcastEvent(event, options);
313
- },
314
- [room]
315
- );
316
- },
317
- useCanRedo: function () {
318
- var room = useRoom(),
319
- _React$useState4 = React__namespace.useState(room.history.canRedo),
320
- canRedo = _React$useState4[0],
321
- setCanRedo = _React$useState4[1];
322
- return (
323
- React__namespace.useEffect(
324
- function () {
325
- var unsubscribe = room.subscribe("history", function (_ref2) {
326
- var canRedo = _ref2.canRedo;
327
- return setCanRedo(canRedo);
328
- });
329
- return function () {
330
- unsubscribe();
331
- };
332
- },
333
- [room]
334
- ),
335
- canRedo
336
- );
337
- },
338
- useCanUndo: function () {
339
- var room = useRoom(),
340
- _React$useState3 = React__namespace.useState(room.history.canUndo),
341
- canUndo = _React$useState3[0],
342
- setCanUndo = _React$useState3[1];
343
- return (
344
- React__namespace.useEffect(
345
- function () {
346
- var unsubscribe = room.subscribe("history", function (_ref) {
347
- var canUndo = _ref.canUndo;
348
- return setCanUndo(canUndo);
349
- });
350
- return function () {
351
- unsubscribe();
352
- };
353
- },
354
- [room]
355
- ),
356
- canUndo
357
- );
358
- },
359
- useErrorListener: function (callback) {
360
- var room = useRoom(),
361
- savedCallback = React__namespace.useRef(callback);
362
- React__namespace.useEffect(function () {
363
- savedCallback.current = callback;
364
- }),
365
- React__namespace.useEffect(
366
- function () {
367
- var unsubscribe = room.subscribe("error", function (e) {
368
- return savedCallback.current(e);
369
- });
370
- return function () {
371
- unsubscribe();
372
- };
373
- },
374
- [room]
375
- );
376
- },
377
- useEventListener: function (callback) {
378
- var room = useRoom(),
379
- savedCallback = React__namespace.useRef(callback);
380
- React__namespace.useEffect(function () {
381
- savedCallback.current = callback;
382
- }),
383
- React__namespace.useEffect(
384
- function () {
385
- var unsubscribe = room.subscribe("event", function (eventData) {
386
- savedCallback.current(eventData);
387
- });
388
- return function () {
389
- unsubscribe();
390
- };
391
- },
392
- [room]
393
- );
394
- },
395
- useHistory: useHistory,
396
- useList: function (key) {
397
- return useList_deprecated(key);
398
- },
399
- useMap: function (key) {
400
- return useMap_deprecated(key);
401
- },
402
- useMyPresence: function () {
403
- var room = useRoom(),
404
- presence = room.getPresence(),
405
- rerender = useRerender();
406
- return (
407
- React__namespace.useEffect(
408
- function () {
409
- var unsubscribe = room.subscribe("my-presence", rerender);
410
- return function () {
411
- unsubscribe();
412
- };
413
- },
414
- [room, rerender]
415
- ),
416
- [
417
- presence,
418
- React__namespace.useCallback(
419
- function (overrides, options) {
420
- return room.updatePresence(overrides, options);
421
- },
422
- [room]
423
- ),
424
- ]
392
+ function useObject2(key) {
393
+ return useObject_deprecated(key);
394
+ }
395
+ function useHistory2() {
396
+ return useRoom2().history;
397
+ }
398
+ function useUndo2() {
399
+ return useHistory2().undo;
400
+ }
401
+ function useRedo2() {
402
+ return useHistory2().redo;
403
+ }
404
+ function useCanUndo() {
405
+ const room = useRoom2();
406
+ const [canUndo, setCanUndo] = React2.useState(room.history.canUndo);
407
+ React2.useEffect(() => {
408
+ const unsubscribe = room.subscribe(
409
+ "history",
410
+ ({ canUndo: canUndo2 }) => setCanUndo(canUndo2)
425
411
  );
426
- },
427
- useObject: function (key) {
428
- return useObject_deprecated(key);
429
- },
430
- useOthers: function () {
431
- var room = useRoom(),
432
- rerender = useRerender();
433
- return (
434
- React__namespace.useEffect(
435
- function () {
436
- var unsubscribe = room.subscribe("others", rerender);
437
- return function () {
438
- unsubscribe();
439
- };
440
- },
441
- [room, rerender]
442
- ),
443
- room.getOthers()
412
+ return () => {
413
+ unsubscribe();
414
+ };
415
+ }, [room]);
416
+ return canUndo;
417
+ }
418
+ function useCanRedo() {
419
+ const room = useRoom2();
420
+ const [canRedo, setCanRedo] = React2.useState(room.history.canRedo);
421
+ React2.useEffect(() => {
422
+ const unsubscribe = room.subscribe(
423
+ "history",
424
+ ({ canRedo: canRedo2 }) => setCanRedo(canRedo2)
444
425
  );
445
- },
446
- useRedo: function () {
447
- return useHistory().redo;
448
- },
449
- useRoom: useRoom,
450
- useSelf: function () {
451
- var room = useRoom(),
452
- rerender = useRerender();
453
- return (
454
- React__namespace.useEffect(
455
- function () {
456
- var unsubscribePresence = room.subscribe("my-presence", rerender),
457
- unsubscribeConnection = room.subscribe("connection", rerender);
458
- return function () {
459
- unsubscribePresence(), unsubscribeConnection();
460
- };
461
- },
462
- [room, rerender]
463
- ),
464
- room.getSelf()
426
+ return () => {
427
+ unsubscribe();
428
+ };
429
+ }, [room]);
430
+ return canRedo;
431
+ }
432
+ function useBatch2() {
433
+ return useRoom2().batch;
434
+ }
435
+ function useStorageValue(key, initialValue) {
436
+ const room = useRoom2();
437
+ const [root] = useStorage2();
438
+ const rerender = useRerender();
439
+ const frozenInitialValue = useInitial(initialValue);
440
+ React2.useEffect(() => {
441
+ if (root == null) {
442
+ return;
443
+ }
444
+ let liveValue = root.get(key);
445
+ if (liveValue == null) {
446
+ liveValue = frozenInitialValue;
447
+ root.set(key, liveValue);
448
+ }
449
+ function onRootChange() {
450
+ const newCrdt = root.get(key);
451
+ if (newCrdt !== liveValue) {
452
+ unsubscribeCrdt();
453
+ liveValue = newCrdt;
454
+ unsubscribeCrdt = room.subscribe(
455
+ liveValue,
456
+ rerender
457
+ );
458
+ rerender();
459
+ }
460
+ }
461
+ let unsubscribeCrdt = room.subscribe(
462
+ liveValue,
463
+ rerender
465
464
  );
466
- },
467
- useStorage: useStorage,
468
- useUndo: function () {
469
- return useHistory().undo;
470
- },
471
- useUpdateMyPresence: function () {
472
- var room = useRoom();
473
- return React__namespace.useCallback(
474
- function (overrides, options) {
475
- room.updatePresence(overrides, options);
476
- },
477
- [room]
465
+ const unsubscribeRoot = room.subscribe(
466
+ root,
467
+ onRootChange
478
468
  );
479
- },
480
- RoomContext: RoomContext,
481
- useList_deprecated: useList_deprecated,
482
- useMap_deprecated: useMap_deprecated,
483
- useObject_deprecated: useObject_deprecated,
469
+ rerender();
470
+ return () => {
471
+ unsubscribeRoot();
472
+ unsubscribeCrdt();
473
+ };
474
+ }, [root, room, key, frozenInitialValue, rerender]);
475
+ if (root == null) {
476
+ return { status: "loading" };
477
+ } else {
478
+ const value = root.get(key);
479
+ if (value == null) {
480
+ return { status: "notfound" };
481
+ } else {
482
+ return { status: "ok", value };
483
+ }
484
+ }
485
+ }
486
+ return {
487
+ RoomProvider: RoomProvider2,
488
+ useBatch: useBatch2,
489
+ useBroadcastEvent: useBroadcastEvent2,
490
+ useCanRedo,
491
+ useCanUndo,
492
+ useErrorListener: useErrorListener2,
493
+ useEventListener: useEventListener2,
494
+ useHistory: useHistory2,
495
+ useList: useList2,
496
+ useMap: useMap2,
497
+ useMyPresence: useMyPresence2,
498
+ useObject: useObject2,
499
+ useOthers: useOthers2,
500
+ useRedo: useRedo2,
501
+ useRoom: useRoom2,
502
+ useSelf: useSelf2,
503
+ useStorage: useStorage2,
504
+ useUndo: useUndo2,
505
+ useUpdateMyPresence: useUpdateMyPresence2,
506
+ RoomContext,
507
+ useList_deprecated,
508
+ useMap_deprecated,
509
+ useObject_deprecated
484
510
  };
485
511
  }
512
+
513
+ // src/compat.tsx
514
+
486
515
  var _hooks = createRoomContext("__legacy");
487
- (exports.LiveblocksProvider = function (props) {
488
- return (
489
- internal.deprecate(
490
- "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."
491
- ),
492
- React__namespace.createElement(
493
- ClientContext.Provider,
494
- { value: props.client },
495
- props.children
496
- )
516
+ function RoomProvider(props) {
517
+ _internal.deprecate.call(void 0,
518
+ "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."
497
519
  );
498
- }),
499
- (exports.RoomProvider = function (props) {
500
- return (
501
- internal.deprecate(
502
- "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."
503
- ),
504
- _hooks.RoomProvider(props)
505
- );
506
- }),
507
- (exports.createRoomContext = createRoomContext),
508
- (exports.useBatch = function () {
509
- return (
510
- internal.deprecate(
511
- "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."
512
- ),
513
- _hooks.useBatch()
514
- );
515
- }),
516
- (exports.useBroadcastEvent = function () {
517
- return (
518
- internal.deprecate(
519
- "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."
520
- ),
521
- _hooks.useBroadcastEvent()
522
- );
523
- }),
524
- (exports.useClient = useClient),
525
- (exports.useErrorListener = function (callback) {
526
- return (
527
- internal.deprecate(
528
- "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."
529
- ),
530
- _hooks.useErrorListener(callback)
531
- );
532
- }),
533
- (exports.useEventListener = function (callback) {
534
- return (
535
- internal.deprecate(
536
- "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."
537
- ),
538
- _hooks.useEventListener(callback)
539
- );
540
- }),
541
- (exports.useHistory = function () {
542
- return (
543
- internal.deprecate(
544
- "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."
545
- ),
546
- _hooks.useHistory()
547
- );
548
- }),
549
- (exports.useList = function (key, items) {
550
- return (
551
- internal.deprecate(
552
- "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."
553
- ),
554
- _hooks.useList_deprecated(key, items)
555
- );
556
- }),
557
- (exports.useMap = function (key, entries) {
558
- return (
559
- internal.deprecate(
560
- "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."
561
- ),
562
- _hooks.useMap_deprecated(key, entries)
563
- );
564
- }),
565
- (exports.useMyPresence = function () {
566
- return (
567
- internal.deprecate(
568
- "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."
569
- ),
570
- _hooks.useMyPresence()
571
- );
572
- }),
573
- (exports.useObject = function (key, initialData) {
574
- return (
575
- internal.deprecate(
576
- "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."
577
- ),
578
- _hooks.useObject_deprecated(key, initialData)
579
- );
580
- }),
581
- (exports.useOthers = function () {
582
- return (
583
- internal.deprecate(
584
- "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."
585
- ),
586
- _hooks.useOthers()
587
- );
588
- }),
589
- (exports.useRedo = function () {
590
- return (
591
- internal.deprecate(
592
- "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."
593
- ),
594
- _hooks.useRedo()
595
- );
596
- }),
597
- (exports.useRoom = function () {
598
- return (
599
- internal.deprecate(
600
- "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."
601
- ),
602
- _hooks.useRoom()
603
- );
604
- }),
605
- (exports.useSelf = function () {
606
- return (
607
- internal.deprecate(
608
- "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."
609
- ),
610
- _hooks.useSelf()
611
- );
612
- }),
613
- (exports.useStorage = function () {
614
- return (
615
- internal.deprecate(
616
- "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."
617
- ),
618
- _hooks.useStorage()
619
- );
620
- }),
621
- (exports.useUndo = function () {
622
- return (
623
- internal.deprecate(
624
- "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."
625
- ),
626
- _hooks.useUndo()
627
- );
628
- }),
629
- (exports.useUpdateMyPresence = function () {
630
- return (
631
- internal.deprecate(
632
- "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."
633
- ),
634
- _hooks.useUpdateMyPresence()
635
- );
636
- });
520
+ return _hooks.RoomProvider(props);
521
+ }
522
+ function useBatch() {
523
+ _internal.deprecate.call(void 0,
524
+ "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."
525
+ );
526
+ return _hooks.useBatch();
527
+ }
528
+ function useBroadcastEvent() {
529
+ _internal.deprecate.call(void 0,
530
+ "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."
531
+ );
532
+ return _hooks.useBroadcastEvent();
533
+ }
534
+ function useErrorListener(callback) {
535
+ _internal.deprecate.call(void 0,
536
+ "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."
537
+ );
538
+ return _hooks.useErrorListener(callback);
539
+ }
540
+ function useEventListener(callback) {
541
+ _internal.deprecate.call(void 0,
542
+ "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."
543
+ );
544
+ return _hooks.useEventListener(callback);
545
+ }
546
+ function useHistory() {
547
+ _internal.deprecate.call(void 0,
548
+ "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."
549
+ );
550
+ return _hooks.useHistory();
551
+ }
552
+ function useMyPresence() {
553
+ _internal.deprecate.call(void 0,
554
+ "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."
555
+ );
556
+ return _hooks.useMyPresence();
557
+ }
558
+ function useOthers() {
559
+ _internal.deprecate.call(void 0,
560
+ "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."
561
+ );
562
+ return _hooks.useOthers();
563
+ }
564
+ function useRedo() {
565
+ _internal.deprecate.call(void 0,
566
+ "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."
567
+ );
568
+ return _hooks.useRedo();
569
+ }
570
+ function useRoom() {
571
+ _internal.deprecate.call(void 0,
572
+ "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."
573
+ );
574
+ return _hooks.useRoom();
575
+ }
576
+ function useSelf() {
577
+ _internal.deprecate.call(void 0,
578
+ "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."
579
+ );
580
+ return _hooks.useSelf();
581
+ }
582
+ function useStorage() {
583
+ _internal.deprecate.call(void 0,
584
+ "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."
585
+ );
586
+ return _hooks.useStorage();
587
+ }
588
+ function useUndo() {
589
+ _internal.deprecate.call(void 0,
590
+ "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."
591
+ );
592
+ return _hooks.useUndo();
593
+ }
594
+ function useUpdateMyPresence() {
595
+ _internal.deprecate.call(void 0,
596
+ "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."
597
+ );
598
+ return _hooks.useUpdateMyPresence();
599
+ }
600
+ function useList(key, items) {
601
+ _internal.deprecate.call(void 0,
602
+ "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."
603
+ );
604
+ return _hooks.useList_deprecated(key, items);
605
+ }
606
+ function useMap(key, entries) {
607
+ _internal.deprecate.call(void 0,
608
+ "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."
609
+ );
610
+ return _hooks.useMap_deprecated(key, entries);
611
+ }
612
+ function useObject(key, initialData) {
613
+ _internal.deprecate.call(void 0,
614
+ "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."
615
+ );
616
+ return _hooks.useObject_deprecated(key, initialData);
617
+ }
618
+
619
+
620
+
621
+
622
+
623
+
624
+
625
+
626
+
627
+
628
+
629
+
630
+
631
+
632
+
633
+
634
+
635
+
636
+
637
+
638
+
639
+ exports.LiveblocksProvider = LiveblocksProvider; exports.RoomProvider = RoomProvider; exports.createRoomContext = createRoomContext; exports.useBatch = useBatch; exports.useBroadcastEvent = useBroadcastEvent; exports.useClient = useClient; exports.useErrorListener = useErrorListener; exports.useEventListener = useEventListener; exports.useHistory = useHistory; exports.useList = useList; exports.useMap = useMap; exports.useMyPresence = useMyPresence; exports.useObject = useObject; exports.useOthers = useOthers; exports.useRedo = useRedo; exports.useRoom = useRoom; exports.useSelf = useSelf; exports.useStorage = useStorage; exports.useUndo = useUndo; exports.useUpdateMyPresence = useUpdateMyPresence;