@liveblocks/react 0.17.10-debug2 → 0.17.11-debug2
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/.built-by-link-script +1 -1
- package/index.d.ts +15 -2
- package/index.js +17 -3
- package/index.mjs +633 -2
- package/package.json +9 -2
package/.built-by-link-script
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
7a145c07616c7ce057d49178c31d7436fb2ab297
|
package/index.d.ts
CHANGED
|
@@ -183,7 +183,12 @@ declare type RoomContextBundle<TPresence extends JsonObject, TStorage extends Ls
|
|
|
183
183
|
* Storage.
|
|
184
184
|
*
|
|
185
185
|
* @example
|
|
186
|
-
* const [root] =
|
|
186
|
+
* const [root] = useStorageRoot();
|
|
187
|
+
*/
|
|
188
|
+
useStorageRoot(): [root: LiveObject<TStorage> | null];
|
|
189
|
+
/**
|
|
190
|
+
* @deprecated In the next major version, we're changing the meaning of `useStorage()`.
|
|
191
|
+
* Please use `useStorageRoot()` instead for the current behavior.
|
|
187
192
|
*/
|
|
188
193
|
useStorage(): [root: LiveObject<TStorage> | null];
|
|
189
194
|
/**
|
|
@@ -332,6 +337,14 @@ declare function useRoom<TPresence extends JsonObject, TStorage extends LsonObje
|
|
|
332
337
|
* https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details.
|
|
333
338
|
*/
|
|
334
339
|
declare function useSelf<TPresence extends JsonObject, TUserMeta extends BaseUserMeta>(): User<TPresence, TUserMeta> | null;
|
|
340
|
+
/**
|
|
341
|
+
* @deprecated Please use `createRoomContext()` instead of importing
|
|
342
|
+
* `useStorageRoot` from `@liveblocks/react` directly. See
|
|
343
|
+
* https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details.
|
|
344
|
+
*/
|
|
345
|
+
declare function useStorageRoot<TStorage extends LsonObject>(): [
|
|
346
|
+
root: LiveObject<TStorage> | null
|
|
347
|
+
];
|
|
335
348
|
/**
|
|
336
349
|
* @deprecated Please use `createRoomContext()` instead of importing
|
|
337
350
|
* `useStorage` from `@liveblocks/react` directly. See
|
|
@@ -391,4 +404,4 @@ declare function useObject<TData extends LsonObject>(key: string): LiveObject<TD
|
|
|
391
404
|
*/
|
|
392
405
|
declare function useObject<TData extends LsonObject>(key: string, initialData: TData): LiveObject<TData> | null;
|
|
393
406
|
|
|
394
|
-
export { LiveblocksProvider, RoomProvider, createRoomContext, useBatch, useBroadcastEvent, useClient, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
|
|
407
|
+
export { LiveblocksProvider, RoomProvider, createRoomContext, useBatch, useBroadcastEvent, useClient, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useStorageRoot, useUndo, useUpdateMyPresence };
|
package/index.js
CHANGED
|
@@ -221,7 +221,7 @@ function createRoomContext(client) {
|
|
|
221
221
|
}, [room, rerender]);
|
|
222
222
|
return room.getSelf();
|
|
223
223
|
}
|
|
224
|
-
function
|
|
224
|
+
function useStorageRoot2() {
|
|
225
225
|
const room = useRoom2();
|
|
226
226
|
const [root, setState] = React2.useState(null);
|
|
227
227
|
React2.useEffect(() => {
|
|
@@ -241,6 +241,12 @@ function createRoomContext(client) {
|
|
|
241
241
|
}, [room]);
|
|
242
242
|
return [root];
|
|
243
243
|
}
|
|
244
|
+
function useStorage2() {
|
|
245
|
+
_internal.deprecate.call(void 0,
|
|
246
|
+
"In the upcoming 0.18 version, the name `useStorage()` is going to be repurposed for a new hook. Please use `useStorageRoot()` instead to keep the current behavior."
|
|
247
|
+
);
|
|
248
|
+
return useStorageRoot2();
|
|
249
|
+
}
|
|
244
250
|
function useMap_deprecated(key, entries) {
|
|
245
251
|
_internal.errorIf.call(void 0,
|
|
246
252
|
entries,
|
|
@@ -434,7 +440,7 @@ Please see https://bit.ly/3Niy5aP for details.`
|
|
|
434
440
|
}
|
|
435
441
|
function useStorageValue(key, initialValue) {
|
|
436
442
|
const room = useRoom2();
|
|
437
|
-
const [root] =
|
|
443
|
+
const [root] = useStorageRoot2();
|
|
438
444
|
const rerender = useRerender();
|
|
439
445
|
const frozenInitialValue = useInitial(initialValue);
|
|
440
446
|
React2.useEffect(() => {
|
|
@@ -500,6 +506,7 @@ Please see https://bit.ly/3Niy5aP for details.`
|
|
|
500
506
|
useRedo: useRedo2,
|
|
501
507
|
useRoom: useRoom2,
|
|
502
508
|
useSelf: useSelf2,
|
|
509
|
+
useStorageRoot: useStorageRoot2,
|
|
503
510
|
useStorage: useStorage2,
|
|
504
511
|
useUndo: useUndo2,
|
|
505
512
|
useUpdateMyPresence: useUpdateMyPresence2,
|
|
@@ -579,6 +586,12 @@ function useSelf() {
|
|
|
579
586
|
);
|
|
580
587
|
return _hooks.useSelf();
|
|
581
588
|
}
|
|
589
|
+
function useStorageRoot() {
|
|
590
|
+
_internal.deprecate.call(void 0,
|
|
591
|
+
"Please use `createRoomContext()` instead of importing `useStorageRoot` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
|
|
592
|
+
);
|
|
593
|
+
return _hooks.useStorageRoot();
|
|
594
|
+
}
|
|
582
595
|
function useStorage() {
|
|
583
596
|
_internal.deprecate.call(void 0,
|
|
584
597
|
"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."
|
|
@@ -636,4 +649,5 @@ function useObject(key, initialData) {
|
|
|
636
649
|
|
|
637
650
|
|
|
638
651
|
|
|
639
|
-
|
|
652
|
+
|
|
653
|
+
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.useStorageRoot = useStorageRoot; exports.useUndo = useUndo; exports.useUpdateMyPresence = useUpdateMyPresence;
|
package/index.mjs
CHANGED
|
@@ -1,3 +1,633 @@
|
|
|
1
|
+
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
|
+
import { deprecate } from "@liveblocks/client/internal";
|
|
24
|
+
import * as React from "react";
|
|
25
|
+
var ClientContext = React.createContext(null);
|
|
26
|
+
function LiveblocksProvider(props) {
|
|
27
|
+
deprecate(
|
|
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."
|
|
29
|
+
);
|
|
30
|
+
return /* @__PURE__ */ React.createElement(ClientContext.Provider, {
|
|
31
|
+
value: props.client
|
|
32
|
+
}, props.children);
|
|
33
|
+
}
|
|
34
|
+
function useClient() {
|
|
35
|
+
const client = React.useContext(ClientContext);
|
|
36
|
+
if (client == null) {
|
|
37
|
+
throw new Error("LiveblocksProvider is missing from the react tree");
|
|
38
|
+
}
|
|
39
|
+
return client;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/factory.tsx
|
|
43
|
+
import { LiveList, LiveMap, LiveObject } from "@liveblocks/client";
|
|
44
|
+
import { deprecate as deprecate2, errorIf } from "@liveblocks/client/internal";
|
|
45
|
+
import * as React2 from "react";
|
|
46
|
+
|
|
47
|
+
// src/hooks.ts
|
|
48
|
+
import { useReducer } from "react";
|
|
49
|
+
function useRerender() {
|
|
50
|
+
const [, update] = useReducer(
|
|
51
|
+
(x) => x + 1,
|
|
52
|
+
0
|
|
53
|
+
);
|
|
54
|
+
return update;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/factory.tsx
|
|
58
|
+
function useInitial(value) {
|
|
59
|
+
return React2.useRef(value).current;
|
|
60
|
+
}
|
|
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
|
+
errorIf(
|
|
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
|
+
errorIf(
|
|
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) {
|
|
132
|
+
throw new Error("RoomProvider is missing from the react tree");
|
|
133
|
+
}
|
|
134
|
+
return room;
|
|
135
|
+
}
|
|
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]
|
|
159
|
+
);
|
|
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 useStorageRoot2() {
|
|
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
|
+
}
|
|
244
|
+
function useStorage2() {
|
|
245
|
+
deprecate2(
|
|
246
|
+
"In the upcoming 0.18 version, the name `useStorage()` is going to be repurposed for a new hook. Please use `useStorageRoot()` instead to keep the current behavior."
|
|
247
|
+
);
|
|
248
|
+
return useStorageRoot2();
|
|
249
|
+
}
|
|
250
|
+
function useMap_deprecated(key, entries) {
|
|
251
|
+
errorIf(
|
|
252
|
+
entries,
|
|
253
|
+
`Support for initializing entries in useMap() directly will be removed in @liveblocks/react 0.18.
|
|
254
|
+
|
|
255
|
+
Instead, please initialize this data where you set up your RoomProvider:
|
|
256
|
+
|
|
257
|
+
const initialStorage = () => ({
|
|
258
|
+
${JSON.stringify(key)}: new LiveMap(...),
|
|
259
|
+
...
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
263
|
+
...
|
|
264
|
+
</RoomProvider>
|
|
265
|
+
|
|
266
|
+
Please see https://bit.ly/3Niy5aP for details.`
|
|
267
|
+
);
|
|
268
|
+
const value = useStorageValue(key, new LiveMap(entries != null ? entries : void 0));
|
|
269
|
+
if (value.status === "ok") {
|
|
270
|
+
return value.value;
|
|
271
|
+
} else {
|
|
272
|
+
errorIf(
|
|
273
|
+
value.status === "notfound",
|
|
274
|
+
`Key ${JSON.stringify(
|
|
275
|
+
key
|
|
276
|
+
)} was not found in Storage. Starting with 0.18, useMap() will no longer automatically create this key.
|
|
277
|
+
|
|
278
|
+
Instead, please initialize your storage where you set up your RoomProvider:
|
|
279
|
+
|
|
280
|
+
import { LiveMap } from "@liveblocks/client";
|
|
281
|
+
|
|
282
|
+
const initialStorage = () => ({
|
|
283
|
+
${JSON.stringify(key)}: new LiveMap(...),
|
|
284
|
+
...
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
288
|
+
...
|
|
289
|
+
</RoomProvider>
|
|
290
|
+
|
|
291
|
+
Please see https://bit.ly/3Niy5aP for details.`
|
|
292
|
+
);
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function useList_deprecated(key, items) {
|
|
297
|
+
errorIf(
|
|
298
|
+
items,
|
|
299
|
+
`Support for initializing items in useList() directly will be removed in @liveblocks/react 0.18.
|
|
300
|
+
|
|
301
|
+
Instead, please initialize this data where you set up your RoomProvider:
|
|
302
|
+
|
|
303
|
+
import { LiveList } from "@liveblocks/client";
|
|
304
|
+
|
|
305
|
+
const initialStorage = () => ({
|
|
306
|
+
${JSON.stringify(key)}: new LiveList(...),
|
|
307
|
+
...
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
311
|
+
...
|
|
312
|
+
</RoomProvider>
|
|
313
|
+
|
|
314
|
+
Please see https://bit.ly/3Niy5aP for details.`
|
|
315
|
+
);
|
|
316
|
+
const value = useStorageValue(key, new LiveList(items));
|
|
317
|
+
if (value.status === "ok") {
|
|
318
|
+
return value.value;
|
|
319
|
+
} else {
|
|
320
|
+
errorIf(
|
|
321
|
+
value.status === "notfound",
|
|
322
|
+
`Key ${JSON.stringify(
|
|
323
|
+
key
|
|
324
|
+
)} was not found in Storage. Starting with 0.18, useList() will no longer automatically create this key.
|
|
325
|
+
|
|
326
|
+
Instead, please initialize your storage where you set up your RoomProvider:
|
|
327
|
+
|
|
328
|
+
import { LiveList } from "@liveblocks/client";
|
|
329
|
+
|
|
330
|
+
const initialStorage = () => ({
|
|
331
|
+
${JSON.stringify(key)}: new LiveList(...),
|
|
332
|
+
...
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
336
|
+
...
|
|
337
|
+
</RoomProvider>
|
|
338
|
+
|
|
339
|
+
Please see https://bit.ly/3Niy5aP for details.`
|
|
340
|
+
);
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function useObject_deprecated(key, initialData) {
|
|
345
|
+
errorIf(
|
|
346
|
+
initialData,
|
|
347
|
+
`Support for initializing data in useObject() directly will be removed in @liveblocks/react 0.18.
|
|
348
|
+
|
|
349
|
+
Instead, please initialize this data where you set up your RoomProvider:
|
|
350
|
+
|
|
351
|
+
import { LiveObject } from "@liveblocks/client";
|
|
352
|
+
|
|
353
|
+
const initialStorage = () => ({
|
|
354
|
+
${JSON.stringify(key)}: new LiveObject(...),
|
|
355
|
+
...
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
359
|
+
...
|
|
360
|
+
</RoomProvider>
|
|
361
|
+
|
|
362
|
+
Please see https://bit.ly/3Niy5aP for details.`
|
|
363
|
+
);
|
|
364
|
+
const value = useStorageValue(key, new LiveObject(initialData));
|
|
365
|
+
if (value.status === "ok") {
|
|
366
|
+
return value.value;
|
|
367
|
+
} else {
|
|
368
|
+
errorIf(
|
|
369
|
+
value.status === "notfound",
|
|
370
|
+
`Key ${JSON.stringify(
|
|
371
|
+
key
|
|
372
|
+
)} was not found in Storage. Starting with 0.18, useObject() will no longer automatically create this key.
|
|
373
|
+
|
|
374
|
+
Instead, please initialize your storage where you set up your RoomProvider:
|
|
375
|
+
|
|
376
|
+
import { LiveObject } from "@liveblocks/client";
|
|
377
|
+
|
|
378
|
+
const initialStorage = () => ({
|
|
379
|
+
${JSON.stringify(key)}: new LiveObject(...),
|
|
380
|
+
...
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
384
|
+
...
|
|
385
|
+
</RoomProvider>
|
|
386
|
+
|
|
387
|
+
Please see https://bit.ly/3Niy5aP for details.`
|
|
388
|
+
);
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
function useList2(key) {
|
|
393
|
+
return useList_deprecated(key);
|
|
394
|
+
}
|
|
395
|
+
function useMap2(key) {
|
|
396
|
+
return useMap_deprecated(key);
|
|
397
|
+
}
|
|
398
|
+
function useObject2(key) {
|
|
399
|
+
return useObject_deprecated(key);
|
|
400
|
+
}
|
|
401
|
+
function useHistory2() {
|
|
402
|
+
return useRoom2().history;
|
|
403
|
+
}
|
|
404
|
+
function useUndo2() {
|
|
405
|
+
return useHistory2().undo;
|
|
406
|
+
}
|
|
407
|
+
function useRedo2() {
|
|
408
|
+
return useHistory2().redo;
|
|
409
|
+
}
|
|
410
|
+
function useCanUndo() {
|
|
411
|
+
const room = useRoom2();
|
|
412
|
+
const [canUndo, setCanUndo] = React2.useState(room.history.canUndo);
|
|
413
|
+
React2.useEffect(() => {
|
|
414
|
+
const unsubscribe = room.subscribe(
|
|
415
|
+
"history",
|
|
416
|
+
({ canUndo: canUndo2 }) => setCanUndo(canUndo2)
|
|
417
|
+
);
|
|
418
|
+
return () => {
|
|
419
|
+
unsubscribe();
|
|
420
|
+
};
|
|
421
|
+
}, [room]);
|
|
422
|
+
return canUndo;
|
|
423
|
+
}
|
|
424
|
+
function useCanRedo() {
|
|
425
|
+
const room = useRoom2();
|
|
426
|
+
const [canRedo, setCanRedo] = React2.useState(room.history.canRedo);
|
|
427
|
+
React2.useEffect(() => {
|
|
428
|
+
const unsubscribe = room.subscribe(
|
|
429
|
+
"history",
|
|
430
|
+
({ canRedo: canRedo2 }) => setCanRedo(canRedo2)
|
|
431
|
+
);
|
|
432
|
+
return () => {
|
|
433
|
+
unsubscribe();
|
|
434
|
+
};
|
|
435
|
+
}, [room]);
|
|
436
|
+
return canRedo;
|
|
437
|
+
}
|
|
438
|
+
function useBatch2() {
|
|
439
|
+
return useRoom2().batch;
|
|
440
|
+
}
|
|
441
|
+
function useStorageValue(key, initialValue) {
|
|
442
|
+
const room = useRoom2();
|
|
443
|
+
const [root] = useStorageRoot2();
|
|
444
|
+
const rerender = useRerender();
|
|
445
|
+
const frozenInitialValue = useInitial(initialValue);
|
|
446
|
+
React2.useEffect(() => {
|
|
447
|
+
if (root == null) {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
let liveValue = root.get(key);
|
|
451
|
+
if (liveValue == null) {
|
|
452
|
+
liveValue = frozenInitialValue;
|
|
453
|
+
root.set(key, liveValue);
|
|
454
|
+
}
|
|
455
|
+
function onRootChange() {
|
|
456
|
+
const newCrdt = root.get(key);
|
|
457
|
+
if (newCrdt !== liveValue) {
|
|
458
|
+
unsubscribeCrdt();
|
|
459
|
+
liveValue = newCrdt;
|
|
460
|
+
unsubscribeCrdt = room.subscribe(
|
|
461
|
+
liveValue,
|
|
462
|
+
rerender
|
|
463
|
+
);
|
|
464
|
+
rerender();
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
let unsubscribeCrdt = room.subscribe(
|
|
468
|
+
liveValue,
|
|
469
|
+
rerender
|
|
470
|
+
);
|
|
471
|
+
const unsubscribeRoot = room.subscribe(
|
|
472
|
+
root,
|
|
473
|
+
onRootChange
|
|
474
|
+
);
|
|
475
|
+
rerender();
|
|
476
|
+
return () => {
|
|
477
|
+
unsubscribeRoot();
|
|
478
|
+
unsubscribeCrdt();
|
|
479
|
+
};
|
|
480
|
+
}, [root, room, key, frozenInitialValue, rerender]);
|
|
481
|
+
if (root == null) {
|
|
482
|
+
return { status: "loading" };
|
|
483
|
+
} else {
|
|
484
|
+
const value = root.get(key);
|
|
485
|
+
if (value == null) {
|
|
486
|
+
return { status: "notfound" };
|
|
487
|
+
} else {
|
|
488
|
+
return { status: "ok", value };
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return {
|
|
493
|
+
RoomProvider: RoomProvider2,
|
|
494
|
+
useBatch: useBatch2,
|
|
495
|
+
useBroadcastEvent: useBroadcastEvent2,
|
|
496
|
+
useCanRedo,
|
|
497
|
+
useCanUndo,
|
|
498
|
+
useErrorListener: useErrorListener2,
|
|
499
|
+
useEventListener: useEventListener2,
|
|
500
|
+
useHistory: useHistory2,
|
|
501
|
+
useList: useList2,
|
|
502
|
+
useMap: useMap2,
|
|
503
|
+
useMyPresence: useMyPresence2,
|
|
504
|
+
useObject: useObject2,
|
|
505
|
+
useOthers: useOthers2,
|
|
506
|
+
useRedo: useRedo2,
|
|
507
|
+
useRoom: useRoom2,
|
|
508
|
+
useSelf: useSelf2,
|
|
509
|
+
useStorageRoot: useStorageRoot2,
|
|
510
|
+
useStorage: useStorage2,
|
|
511
|
+
useUndo: useUndo2,
|
|
512
|
+
useUpdateMyPresence: useUpdateMyPresence2,
|
|
513
|
+
RoomContext,
|
|
514
|
+
useList_deprecated,
|
|
515
|
+
useMap_deprecated,
|
|
516
|
+
useObject_deprecated
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// src/compat.tsx
|
|
521
|
+
import { deprecate as deprecate3 } from "@liveblocks/client/internal";
|
|
522
|
+
var _hooks = createRoomContext("__legacy");
|
|
523
|
+
function RoomProvider(props) {
|
|
524
|
+
deprecate3(
|
|
525
|
+
"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."
|
|
526
|
+
);
|
|
527
|
+
return _hooks.RoomProvider(props);
|
|
528
|
+
}
|
|
529
|
+
function useBatch() {
|
|
530
|
+
deprecate3(
|
|
531
|
+
"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."
|
|
532
|
+
);
|
|
533
|
+
return _hooks.useBatch();
|
|
534
|
+
}
|
|
535
|
+
function useBroadcastEvent() {
|
|
536
|
+
deprecate3(
|
|
537
|
+
"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."
|
|
538
|
+
);
|
|
539
|
+
return _hooks.useBroadcastEvent();
|
|
540
|
+
}
|
|
541
|
+
function useErrorListener(callback) {
|
|
542
|
+
deprecate3(
|
|
543
|
+
"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."
|
|
544
|
+
);
|
|
545
|
+
return _hooks.useErrorListener(callback);
|
|
546
|
+
}
|
|
547
|
+
function useEventListener(callback) {
|
|
548
|
+
deprecate3(
|
|
549
|
+
"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."
|
|
550
|
+
);
|
|
551
|
+
return _hooks.useEventListener(callback);
|
|
552
|
+
}
|
|
553
|
+
function useHistory() {
|
|
554
|
+
deprecate3(
|
|
555
|
+
"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."
|
|
556
|
+
);
|
|
557
|
+
return _hooks.useHistory();
|
|
558
|
+
}
|
|
559
|
+
function useMyPresence() {
|
|
560
|
+
deprecate3(
|
|
561
|
+
"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."
|
|
562
|
+
);
|
|
563
|
+
return _hooks.useMyPresence();
|
|
564
|
+
}
|
|
565
|
+
function useOthers() {
|
|
566
|
+
deprecate3(
|
|
567
|
+
"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."
|
|
568
|
+
);
|
|
569
|
+
return _hooks.useOthers();
|
|
570
|
+
}
|
|
571
|
+
function useRedo() {
|
|
572
|
+
deprecate3(
|
|
573
|
+
"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."
|
|
574
|
+
);
|
|
575
|
+
return _hooks.useRedo();
|
|
576
|
+
}
|
|
577
|
+
function useRoom() {
|
|
578
|
+
deprecate3(
|
|
579
|
+
"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."
|
|
580
|
+
);
|
|
581
|
+
return _hooks.useRoom();
|
|
582
|
+
}
|
|
583
|
+
function useSelf() {
|
|
584
|
+
deprecate3(
|
|
585
|
+
"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."
|
|
586
|
+
);
|
|
587
|
+
return _hooks.useSelf();
|
|
588
|
+
}
|
|
589
|
+
function useStorageRoot() {
|
|
590
|
+
deprecate3(
|
|
591
|
+
"Please use `createRoomContext()` instead of importing `useStorageRoot` from `@liveblocks/react` directly. See https://liveblocks.io/docs/guides/upgrading#upgrading-from-0-16-to-0-17 for details."
|
|
592
|
+
);
|
|
593
|
+
return _hooks.useStorageRoot();
|
|
594
|
+
}
|
|
595
|
+
function useStorage() {
|
|
596
|
+
deprecate3(
|
|
597
|
+
"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."
|
|
598
|
+
);
|
|
599
|
+
return _hooks.useStorage();
|
|
600
|
+
}
|
|
601
|
+
function useUndo() {
|
|
602
|
+
deprecate3(
|
|
603
|
+
"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."
|
|
604
|
+
);
|
|
605
|
+
return _hooks.useUndo();
|
|
606
|
+
}
|
|
607
|
+
function useUpdateMyPresence() {
|
|
608
|
+
deprecate3(
|
|
609
|
+
"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."
|
|
610
|
+
);
|
|
611
|
+
return _hooks.useUpdateMyPresence();
|
|
612
|
+
}
|
|
613
|
+
function useList(key, items) {
|
|
614
|
+
deprecate3(
|
|
615
|
+
"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."
|
|
616
|
+
);
|
|
617
|
+
return _hooks.useList_deprecated(key, items);
|
|
618
|
+
}
|
|
619
|
+
function useMap(key, entries) {
|
|
620
|
+
deprecate3(
|
|
621
|
+
"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."
|
|
622
|
+
);
|
|
623
|
+
return _hooks.useMap_deprecated(key, entries);
|
|
624
|
+
}
|
|
625
|
+
function useObject(key, initialData) {
|
|
626
|
+
deprecate3(
|
|
627
|
+
"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."
|
|
628
|
+
);
|
|
629
|
+
return _hooks.useObject_deprecated(key, initialData);
|
|
630
|
+
}
|
|
1
631
|
export {
|
|
2
632
|
LiveblocksProvider,
|
|
3
633
|
RoomProvider,
|
|
@@ -17,6 +647,7 @@ export {
|
|
|
17
647
|
useRoom,
|
|
18
648
|
useSelf,
|
|
19
649
|
useStorage,
|
|
650
|
+
useStorageRoot,
|
|
20
651
|
useUndo,
|
|
21
|
-
useUpdateMyPresence
|
|
22
|
-
}
|
|
652
|
+
useUpdateMyPresence
|
|
653
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/react",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.11-debug2",
|
|
4
4
|
"description": "A set of React hooks and providers to use Liveblocks declaratively.",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.mjs",
|
|
7
7
|
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./index.js",
|
|
11
|
+
"import": "./index.mjs",
|
|
12
|
+
"types": "./index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
8
15
|
"keywords": [
|
|
9
16
|
"react",
|
|
10
17
|
"liveblocks",
|
|
@@ -17,7 +24,7 @@
|
|
|
17
24
|
},
|
|
18
25
|
"license": "Apache-2.0",
|
|
19
26
|
"peerDependencies": {
|
|
20
|
-
"@liveblocks/client": "0.17.
|
|
27
|
+
"@liveblocks/client": "0.17.11-debug2",
|
|
21
28
|
"react": "^16.14.0 || ^17 || ^18"
|
|
22
29
|
},
|
|
23
30
|
"repository": {
|