@liveblocks/react 0.16.4-beta2 → 0.16.4
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.d.ts +26 -24
- package/index.js +57 -8
- package/index.mjs +144 -46
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { Client, Room, Presence, Others, BroadcastOptions, Json, User, LiveObject, Lson, LiveMap, LiveList, LsonObject, History } from '@liveblocks/client';
|
|
3
3
|
export { Json, JsonObject } from '@liveblocks/client';
|
|
4
|
+
import { Resolve, RoomInitializers } from '@liveblocks/client/internal';
|
|
4
5
|
|
|
5
6
|
declare type LiveblocksProviderProps = {
|
|
6
7
|
children: React.ReactNode;
|
|
@@ -16,19 +17,13 @@ declare function LiveblocksProvider(props: LiveblocksProviderProps): JSX.Element
|
|
|
16
17
|
*/
|
|
17
18
|
declare function useClient(): Client;
|
|
18
19
|
|
|
19
|
-
declare type RoomProviderProps<TStorage> = {
|
|
20
|
+
declare type RoomProviderProps<TStorage> = Resolve<{
|
|
20
21
|
/**
|
|
21
22
|
* The id of the room you want to connect to
|
|
22
23
|
*/
|
|
23
24
|
id: string;
|
|
24
|
-
/**
|
|
25
|
-
* A callback that let you initialize the default presence when entering the room.
|
|
26
|
-
* If ommited, the default presence will be an empty object
|
|
27
|
-
*/
|
|
28
|
-
defaultPresence?: () => Presence;
|
|
29
|
-
defaultStorageRoot?: TStorage;
|
|
30
25
|
children: React.ReactNode;
|
|
31
|
-
}
|
|
26
|
+
} & RoomInitializers<Presence, TStorage>>;
|
|
32
27
|
/**
|
|
33
28
|
* Makes a Room available in the component hierarchy below.
|
|
34
29
|
* When this component is unmounted, the current user leave the room.
|
|
@@ -150,42 +145,49 @@ declare function useStorage<TStorage extends Record<string, any>>(): [
|
|
|
150
145
|
* 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.
|
|
151
146
|
*
|
|
152
147
|
* @param key The storage key associated with the LiveMap
|
|
153
|
-
* @param entries Optional entries that are used to create the LiveMap for the first time
|
|
154
148
|
* @returns null while the storage is loading, otherwise, returns the LiveMap associated to the storage
|
|
155
149
|
*
|
|
156
150
|
* @example
|
|
157
|
-
* const
|
|
158
|
-
|
|
151
|
+
* const shapesById = useMap<string, Shape>("shapes");
|
|
152
|
+
*/
|
|
153
|
+
declare function useMap<TKey extends string, TValue extends Lson>(key: string): LiveMap<TKey, TValue> | null;
|
|
154
|
+
/**
|
|
155
|
+
* @deprecated We no longer recommend initializing the
|
|
156
|
+
* entries from the useMap() hook. For details, see https://bit.ly/3Niy5aP.
|
|
159
157
|
*/
|
|
160
|
-
declare function useMap<TKey extends string, TValue extends Lson>(key: string, entries
|
|
158
|
+
declare function useMap<TKey extends string, TValue extends Lson>(key: string, entries: readonly (readonly [TKey, TValue])[] | null): LiveMap<TKey, TValue> | null;
|
|
161
159
|
/**
|
|
162
|
-
* Returns the LiveList associated with the provided key.
|
|
160
|
+
* Returns the LiveList associated with the provided key.
|
|
163
161
|
* 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.
|
|
164
162
|
*
|
|
165
163
|
* @param key The storage key associated with the LiveList
|
|
166
|
-
* @param items Optional items that are used to create the LiveList for the first time
|
|
167
164
|
* @returns null while the storage is loading, otherwise, returns the LiveList associated to the storage
|
|
168
165
|
*
|
|
169
166
|
* @example
|
|
170
|
-
* const
|
|
171
|
-
* const listWithItems = useList("listB", ["a", "b", "c"]);
|
|
167
|
+
* const animals = useList("animals"); // e.g. [] or ["🦁", "🐍", "🦍"]
|
|
172
168
|
*/
|
|
173
|
-
declare function useList<TValue extends Lson>(key: string
|
|
169
|
+
declare function useList<TValue extends Lson>(key: string): LiveList<TValue> | null;
|
|
174
170
|
/**
|
|
175
|
-
*
|
|
171
|
+
* @deprecated We no longer recommend initializing the
|
|
172
|
+
* items from the useList() hook. For details, see https://bit.ly/3Niy5aP.
|
|
173
|
+
*/
|
|
174
|
+
declare function useList<TValue extends Lson>(key: string, items: TValue[]): LiveList<TValue> | null;
|
|
175
|
+
/**
|
|
176
|
+
* Returns the LiveObject associated with the provided key.
|
|
176
177
|
* 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.
|
|
177
178
|
*
|
|
178
179
|
* @param key The storage key associated with the LiveObject
|
|
179
|
-
* @param initialData Optional data that is used to create the LiveObject for the first time
|
|
180
180
|
* @returns null while the storage is loading, otherwise, returns the LveObject associated to the storage
|
|
181
181
|
*
|
|
182
182
|
* @example
|
|
183
|
-
* const object = useObject("obj"
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
183
|
+
* const object = useObject("obj");
|
|
184
|
+
*/
|
|
185
|
+
declare function useObject<TData extends LsonObject>(key: string): LiveObject<TData> | null;
|
|
186
|
+
/**
|
|
187
|
+
* @deprecated We no longer recommend initializing the fields from the
|
|
188
|
+
* useObject() hook. For details, see https://bit.ly/3Niy5aP.
|
|
187
189
|
*/
|
|
188
|
-
declare function useObject<TData extends LsonObject>(key: string, initialData
|
|
190
|
+
declare function useObject<TData extends LsonObject>(key: string, initialData: TData): LiveObject<TData> | null;
|
|
189
191
|
/**
|
|
190
192
|
* Returns a function that undoes the last operation executed by the current client.
|
|
191
193
|
* It does not impact operations made by other clients.
|
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
var client = require('@liveblocks/client');
|
|
7
|
+
var internal = require('@liveblocks/client/internal');
|
|
7
8
|
|
|
8
9
|
function _interopNamespace(e) {
|
|
9
10
|
if (e && e.__esModule) return e;
|
|
@@ -89,6 +90,8 @@ function useRerender() {
|
|
|
89
90
|
var RoomContext = React__namespace.createContext(null);
|
|
90
91
|
function RoomProvider(props) {
|
|
91
92
|
var roomId = props.id,
|
|
93
|
+
initialPresence = props.initialPresence,
|
|
94
|
+
initialStorage = props.initialStorage,
|
|
92
95
|
defaultPresence = props.defaultPresence,
|
|
93
96
|
defaultStorageRoot = props.defaultStorageRoot;
|
|
94
97
|
|
|
@@ -102,11 +105,15 @@ function RoomProvider(props) {
|
|
|
102
105
|
}
|
|
103
106
|
}
|
|
104
107
|
|
|
108
|
+
internal.deprecateIf(defaultPresence, "RoomProvider's `defaultPresence` prop will be removed in @liveblocks/react 0.18. Please use `initialPresence` instead. For more info, see https://bit.ly/3Niy5aP", "defaultPresence");
|
|
109
|
+
internal.deprecateIf(defaultStorageRoot, "RoomProvider's `defaultStorageRoot` prop will be removed in @liveblocks/react 0.18. Please use `initialStorage` instead. For more info, see https://bit.ly/3Niy5aP", "defaultStorageRoot");
|
|
105
110
|
var client = useClient();
|
|
106
111
|
|
|
107
112
|
var _React$useState = React__namespace.useState(function () {
|
|
108
113
|
return client.enter(roomId, {
|
|
109
|
-
|
|
114
|
+
initialPresence: initialPresence,
|
|
115
|
+
initialStorage: initialStorage,
|
|
116
|
+
defaultPresence: defaultPresence,
|
|
110
117
|
defaultStorageRoot: defaultStorageRoot,
|
|
111
118
|
DO_NOT_USE_withoutConnecting: typeof window === "undefined"
|
|
112
119
|
});
|
|
@@ -116,7 +123,9 @@ function RoomProvider(props) {
|
|
|
116
123
|
|
|
117
124
|
React__namespace.useEffect(function () {
|
|
118
125
|
setRoom(client.enter(roomId, {
|
|
119
|
-
|
|
126
|
+
initialPresence: initialPresence,
|
|
127
|
+
initialStorage: initialStorage,
|
|
128
|
+
defaultPresence: defaultPresence,
|
|
120
129
|
defaultStorageRoot: defaultStorageRoot,
|
|
121
130
|
DO_NOT_USE_withoutConnecting: typeof window === "undefined"
|
|
122
131
|
}));
|
|
@@ -277,13 +286,37 @@ function useStorage() {
|
|
|
277
286
|
return [root];
|
|
278
287
|
}
|
|
279
288
|
function useMap(key, entries) {
|
|
280
|
-
|
|
289
|
+
internal.deprecateIf(entries, "Support for initializing entries in useMap() directly will be removed in @liveblocks/react 0.18.\n\nInstead, please initialize this data where you set up your RoomProvider:\n\n const initialStorage = () => {\n " + JSON.stringify(key) + ": new LiveMap(...),\n ...\n };\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.");
|
|
290
|
+
var value = useCrdt(key, new client.LiveMap(entries));
|
|
291
|
+
|
|
292
|
+
if (value.status === "ok") {
|
|
293
|
+
return value.value;
|
|
294
|
+
} else {
|
|
295
|
+
internal.deprecateIf(value.status === "notfound", "Key " + JSON.stringify(key) + " was not found in Storage. Starting with 0.18, useMap() will no longer automatically create this key.\n\nInstead, please initialize your storage where you set up your RoomProvider:\n\n import { LiveMap } from \"@liveblocks/client\";\n\n const initialStorage = () => {\n " + JSON.stringify(key) + ": new LiveMap(...),\n ...\n };\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.");
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
281
298
|
}
|
|
282
299
|
function useList(key, items) {
|
|
283
|
-
|
|
300
|
+
internal.deprecateIf(items, "Support for initializing items in useList() directly will be removed in @liveblocks/react 0.18.\n\nInstead, please initialize this data where you set up your RoomProvider:\n\n import { LiveList } from \"@liveblocks/client\";\n\n const initialStorage = () => {\n " + JSON.stringify(key) + ": new LiveList(...),\n ...\n };\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.");
|
|
301
|
+
var value = useCrdt(key, new client.LiveList(items));
|
|
302
|
+
|
|
303
|
+
if (value.status === "ok") {
|
|
304
|
+
return value.value;
|
|
305
|
+
} else {
|
|
306
|
+
internal.deprecateIf(value.status === "notfound", "Key " + JSON.stringify(key) + " was not found in Storage. Starting with 0.18, useList() will no longer automatically create this key.\n\nInstead, please initialize your storage where you set up your RoomProvider:\n\n import { LiveList } from \"@liveblocks/client\";\n\n const initialStorage = () => {\n " + JSON.stringify(key) + ": new LiveList(...),\n ...\n };\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.");
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
284
309
|
}
|
|
285
310
|
function useObject(key, initialData) {
|
|
286
|
-
|
|
311
|
+
internal.deprecateIf(initialData, "Support for initializing data in useObject() directly will be removed in @liveblocks/react 0.18.\n\nInstead, please initialize this data where you set up your RoomProvider:\n\n import { LiveObject } from \"@liveblocks/client\";\n\n const initialStorage = () => {\n " + JSON.stringify(key) + ": new LiveObject(...),\n ...\n };\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.");
|
|
312
|
+
var value = useCrdt(key, new client.LiveObject(initialData));
|
|
313
|
+
|
|
314
|
+
if (value.status === "ok") {
|
|
315
|
+
return value.value;
|
|
316
|
+
} else {
|
|
317
|
+
internal.deprecateIf(value.status === "notfound", "Key " + JSON.stringify(key) + " was not found in Storage. Starting with 0.18, useObject() will no longer automatically create this key.\n\nInstead, please initialize your storage where you set up your RoomProvider:\n\n import { LiveObject } from \"@liveblocks/client\";\n\n const initialStorage = () => {\n " + JSON.stringify(key) + ": new LiveObject(...),\n ...\n };\n\n <RoomProvider initialStorage={initialStorage}>\n ...\n </RoomProvider>\n\nPlease see https://bit.ly/3Niy5aP for details.");
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
287
320
|
}
|
|
288
321
|
function useUndo() {
|
|
289
322
|
return useRoom().history.undo;
|
|
@@ -299,8 +332,6 @@ function useHistory() {
|
|
|
299
332
|
}
|
|
300
333
|
|
|
301
334
|
function useCrdt(key, initialCrdt) {
|
|
302
|
-
var _root$get;
|
|
303
|
-
|
|
304
335
|
var room = useRoom();
|
|
305
336
|
|
|
306
337
|
var _useStorage = useStorage(),
|
|
@@ -338,7 +369,25 @@ function useCrdt(key, initialCrdt) {
|
|
|
338
369
|
unsubscribeCrdt();
|
|
339
370
|
};
|
|
340
371
|
}, [root, room]);
|
|
341
|
-
|
|
372
|
+
|
|
373
|
+
if (root == null) {
|
|
374
|
+
return {
|
|
375
|
+
status: "loading"
|
|
376
|
+
};
|
|
377
|
+
} else {
|
|
378
|
+
var value = root.get(key);
|
|
379
|
+
|
|
380
|
+
if (value == null) {
|
|
381
|
+
return {
|
|
382
|
+
status: "notfound"
|
|
383
|
+
};
|
|
384
|
+
} else {
|
|
385
|
+
return {
|
|
386
|
+
status: "ok",
|
|
387
|
+
value: value
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
}
|
|
342
391
|
}
|
|
343
392
|
|
|
344
393
|
exports.LiveblocksProvider = LiveblocksProvider;
|
package/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { useReducer } from 'react';
|
|
3
3
|
import { LiveMap, LiveList, LiveObject } from '@liveblocks/client';
|
|
4
|
+
import { deprecateIf } from '@liveblocks/client/internal';
|
|
4
5
|
|
|
5
6
|
const ClientContext = React.createContext(null);
|
|
6
7
|
/**
|
|
@@ -76,7 +77,8 @@ const RoomContext = React.createContext(null);
|
|
|
76
77
|
* That means that you can't have 2 RoomProvider with the same room id in your react tree.
|
|
77
78
|
*/
|
|
78
79
|
function RoomProvider(props) {
|
|
79
|
-
const { id: roomId, defaultPresence,
|
|
80
|
+
const { id: roomId, initialPresence, initialStorage, defaultPresence, // Will get removed in 0.18
|
|
81
|
+
defaultStorageRoot, } = props;
|
|
80
82
|
if (process.env.NODE_ENV !== "production") {
|
|
81
83
|
if (roomId == null) {
|
|
82
84
|
throw new Error("RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required");
|
|
@@ -85,15 +87,21 @@ function RoomProvider(props) {
|
|
|
85
87
|
throw new Error("RoomProvider id property should be a string.");
|
|
86
88
|
}
|
|
87
89
|
}
|
|
90
|
+
deprecateIf(defaultPresence, "RoomProvider's `defaultPresence` prop will be removed in @liveblocks/react 0.18. Please use `initialPresence` instead. For more info, see https://bit.ly/3Niy5aP", "defaultPresence");
|
|
91
|
+
deprecateIf(defaultStorageRoot, "RoomProvider's `defaultStorageRoot` prop will be removed in @liveblocks/react 0.18. Please use `initialStorage` instead. For more info, see https://bit.ly/3Niy5aP", "defaultStorageRoot");
|
|
88
92
|
const client = useClient();
|
|
89
93
|
const [room, setRoom] = React.useState(() => client.enter(roomId, {
|
|
90
|
-
|
|
94
|
+
initialPresence,
|
|
95
|
+
initialStorage,
|
|
96
|
+
defaultPresence,
|
|
91
97
|
defaultStorageRoot,
|
|
92
98
|
DO_NOT_USE_withoutConnecting: typeof window === "undefined",
|
|
93
99
|
}));
|
|
94
100
|
React.useEffect(() => {
|
|
95
101
|
setRoom(client.enter(roomId, {
|
|
96
|
-
|
|
102
|
+
initialPresence,
|
|
103
|
+
initialStorage,
|
|
104
|
+
defaultPresence,
|
|
97
105
|
defaultStorageRoot,
|
|
98
106
|
DO_NOT_USE_withoutConnecting: typeof window === "undefined",
|
|
99
107
|
}));
|
|
@@ -296,52 +304,132 @@ function useStorage() {
|
|
|
296
304
|
}, [room]);
|
|
297
305
|
return [root];
|
|
298
306
|
}
|
|
299
|
-
/**
|
|
300
|
-
* Returns the LiveMap associated with the provided key. If the LiveMap does not exist, a new empty LiveMap will be created.
|
|
301
|
-
* The hook triggers a re-render if the LiveMap is updated, however it does not triggers a re-render if a nested CRDT is updated.
|
|
302
|
-
*
|
|
303
|
-
* @param key The storage key associated with the LiveMap
|
|
304
|
-
* @param entries Optional entries that are used to create the LiveMap for the first time
|
|
305
|
-
* @returns null while the storage is loading, otherwise, returns the LiveMap associated to the storage
|
|
306
|
-
*
|
|
307
|
-
* @example
|
|
308
|
-
* const emptyMap = useMap("mapA");
|
|
309
|
-
* const mapWithItems = useMap("mapB", [["keyA", "valueA"], ["keyB", "valueB"]]);
|
|
310
|
-
*/
|
|
311
307
|
function useMap(key, entries) {
|
|
312
|
-
|
|
308
|
+
deprecateIf(entries, `Support for initializing entries in useMap() directly will be removed in @liveblocks/react 0.18.
|
|
309
|
+
|
|
310
|
+
Instead, please initialize this data where you set up your RoomProvider:
|
|
311
|
+
|
|
312
|
+
const initialStorage = () => {
|
|
313
|
+
${JSON.stringify(key)}: new LiveMap(...),
|
|
314
|
+
...
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
318
|
+
...
|
|
319
|
+
</RoomProvider>
|
|
320
|
+
|
|
321
|
+
Please see https://bit.ly/3Niy5aP for details.`);
|
|
322
|
+
const value = useCrdt(key, new LiveMap(entries));
|
|
323
|
+
// ^^^^^^^^^^^^^^^^^^^^
|
|
324
|
+
// NOTE: This param is scheduled for removal in 0.18
|
|
325
|
+
if (value.status === "ok") {
|
|
326
|
+
return value.value;
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
deprecateIf(value.status === "notfound", `Key ${JSON.stringify(key)} was not found in Storage. Starting with 0.18, useMap() will no longer automatically create this key.
|
|
330
|
+
|
|
331
|
+
Instead, please initialize your storage where you set up your RoomProvider:
|
|
332
|
+
|
|
333
|
+
import { LiveMap } from "@liveblocks/client";
|
|
334
|
+
|
|
335
|
+
const initialStorage = () => {
|
|
336
|
+
${JSON.stringify(key)}: new LiveMap(...),
|
|
337
|
+
...
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
341
|
+
...
|
|
342
|
+
</RoomProvider>
|
|
343
|
+
|
|
344
|
+
Please see https://bit.ly/3Niy5aP for details.`);
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
313
347
|
}
|
|
314
|
-
/**
|
|
315
|
-
* Returns the LiveList associated with the provided key. If the LiveList does not exist, a new LiveList will be created.
|
|
316
|
-
* The hook triggers a re-render if the LiveList is updated, however it does not triggers a re-render if a nested CRDT is updated.
|
|
317
|
-
*
|
|
318
|
-
* @param key The storage key associated with the LiveList
|
|
319
|
-
* @param items Optional items that are used to create the LiveList for the first time
|
|
320
|
-
* @returns null while the storage is loading, otherwise, returns the LiveList associated to the storage
|
|
321
|
-
*
|
|
322
|
-
* @example
|
|
323
|
-
* const emptyList = useList("listA");
|
|
324
|
-
* const listWithItems = useList("listB", ["a", "b", "c"]);
|
|
325
|
-
*/
|
|
326
348
|
function useList(key, items) {
|
|
327
|
-
|
|
349
|
+
deprecateIf(items, `Support for initializing items in useList() directly will be removed in @liveblocks/react 0.18.
|
|
350
|
+
|
|
351
|
+
Instead, please initialize this data where you set up your RoomProvider:
|
|
352
|
+
|
|
353
|
+
import { LiveList } from "@liveblocks/client";
|
|
354
|
+
|
|
355
|
+
const initialStorage = () => {
|
|
356
|
+
${JSON.stringify(key)}: new LiveList(...),
|
|
357
|
+
...
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
361
|
+
...
|
|
362
|
+
</RoomProvider>
|
|
363
|
+
|
|
364
|
+
Please see https://bit.ly/3Niy5aP for details.`);
|
|
365
|
+
const value = useCrdt(key, new LiveList(items));
|
|
366
|
+
// ^^^^^^^^^^^^^^^^^^^
|
|
367
|
+
// NOTE: This param is scheduled for removal in 0.18
|
|
368
|
+
if (value.status === "ok") {
|
|
369
|
+
return value.value;
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
deprecateIf(value.status === "notfound", `Key ${JSON.stringify(key)} was not found in Storage. Starting with 0.18, useList() will no longer automatically create this key.
|
|
373
|
+
|
|
374
|
+
Instead, please initialize your storage where you set up your RoomProvider:
|
|
375
|
+
|
|
376
|
+
import { LiveList } from "@liveblocks/client";
|
|
377
|
+
|
|
378
|
+
const initialStorage = () => {
|
|
379
|
+
${JSON.stringify(key)}: new LiveList(...),
|
|
380
|
+
...
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
384
|
+
...
|
|
385
|
+
</RoomProvider>
|
|
386
|
+
|
|
387
|
+
Please see https://bit.ly/3Niy5aP for details.`);
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
328
390
|
}
|
|
329
|
-
/**
|
|
330
|
-
* Returns the LiveObject associated with the provided key. If the LiveObject does not exist, it will be created with the initialData parameter.
|
|
331
|
-
* The hook triggers a re-render if the LiveObject is updated, however it does not triggers a re-render if a nested CRDT is updated.
|
|
332
|
-
*
|
|
333
|
-
* @param key The storage key associated with the LiveObject
|
|
334
|
-
* @param initialData Optional data that is used to create the LiveObject for the first time
|
|
335
|
-
* @returns null while the storage is loading, otherwise, returns the LveObject associated to the storage
|
|
336
|
-
*
|
|
337
|
-
* @example
|
|
338
|
-
* const object = useObject("obj", {
|
|
339
|
-
* company: "Liveblocks",
|
|
340
|
-
* website: "https://liveblocks.io"
|
|
341
|
-
* });
|
|
342
|
-
*/
|
|
343
391
|
function useObject(key, initialData) {
|
|
344
|
-
|
|
392
|
+
deprecateIf(initialData, `Support for initializing data in useObject() directly will be removed in @liveblocks/react 0.18.
|
|
393
|
+
|
|
394
|
+
Instead, please initialize this data where you set up your RoomProvider:
|
|
395
|
+
|
|
396
|
+
import { LiveObject } from "@liveblocks/client";
|
|
397
|
+
|
|
398
|
+
const initialStorage = () => {
|
|
399
|
+
${JSON.stringify(key)}: new LiveObject(...),
|
|
400
|
+
...
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
404
|
+
...
|
|
405
|
+
</RoomProvider>
|
|
406
|
+
|
|
407
|
+
Please see https://bit.ly/3Niy5aP for details.`);
|
|
408
|
+
const value = useCrdt(key, new LiveObject(initialData));
|
|
409
|
+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
410
|
+
// NOTE: This param is scheduled for removal in 0.18
|
|
411
|
+
if (value.status === "ok") {
|
|
412
|
+
return value.value;
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
deprecateIf(value.status === "notfound", `Key ${JSON.stringify(key)} was not found in Storage. Starting with 0.18, useObject() will no longer automatically create this key.
|
|
416
|
+
|
|
417
|
+
Instead, please initialize your storage where you set up your RoomProvider:
|
|
418
|
+
|
|
419
|
+
import { LiveObject } from "@liveblocks/client";
|
|
420
|
+
|
|
421
|
+
const initialStorage = () => {
|
|
422
|
+
${JSON.stringify(key)}: new LiveObject(...),
|
|
423
|
+
...
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
<RoomProvider initialStorage={initialStorage}>
|
|
427
|
+
...
|
|
428
|
+
</RoomProvider>
|
|
429
|
+
|
|
430
|
+
Please see https://bit.ly/3Niy5aP for details.`);
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
345
433
|
}
|
|
346
434
|
/**
|
|
347
435
|
* Returns a function that undoes the last operation executed by the current client.
|
|
@@ -373,7 +461,6 @@ function useHistory() {
|
|
|
373
461
|
return useRoom().history;
|
|
374
462
|
}
|
|
375
463
|
function useCrdt(key, initialCrdt) {
|
|
376
|
-
var _a;
|
|
377
464
|
const room = useRoom();
|
|
378
465
|
const [root] = useStorage();
|
|
379
466
|
const rerender = useRerender();
|
|
@@ -403,7 +490,18 @@ function useCrdt(key, initialCrdt) {
|
|
|
403
490
|
unsubscribeCrdt();
|
|
404
491
|
};
|
|
405
492
|
}, [root, room]);
|
|
406
|
-
|
|
493
|
+
if (root == null) {
|
|
494
|
+
return { status: "loading" };
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
const value = root.get(key);
|
|
498
|
+
if (value == null) {
|
|
499
|
+
return { status: "notfound" };
|
|
500
|
+
}
|
|
501
|
+
else {
|
|
502
|
+
return { status: "ok", value };
|
|
503
|
+
}
|
|
504
|
+
}
|
|
407
505
|
}
|
|
408
506
|
|
|
409
507
|
export { LiveblocksProvider, RoomProvider, useBatch, useBroadcastEvent, useClient, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/react",
|
|
3
|
-
"version": "0.16.4
|
|
3
|
+
"version": "0.16.4",
|
|
4
4
|
"description": "A set of React hooks and providers to use Liveblocks declaratively.",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.mjs",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"license": "Apache-2.0",
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@liveblocks/client": "0.16.4
|
|
30
|
+
"@liveblocks/client": "0.16.4",
|
|
31
31
|
"react": "^16.14.0 || ^17 || ^18"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|