@liveblocks/react 0.17.11-debug2 → 0.18.0-beta1
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 +205 -230
- package/index.js +349 -498
- package/index.mjs +5 -652
- package/package.json +5 -2
package/index.js
CHANGED
|
@@ -1,48 +1,20 @@
|
|
|
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; } }
|
|
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');
|
|
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; } }// src/ClientSideSuspense.tsx
|
|
24
2
|
var _react = require('react'); var React = _interopRequireWildcard(_react); var React2 = _interopRequireWildcard(_react);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
);
|
|
30
|
-
return /* @__PURE__ */ React.createElement(
|
|
31
|
-
|
|
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;
|
|
3
|
+
function ClientSideSuspense(props) {
|
|
4
|
+
const [mounted, setMounted] = React.useState(false);
|
|
5
|
+
React.useEffect(() => {
|
|
6
|
+
setMounted(true);
|
|
7
|
+
}, []);
|
|
8
|
+
return /* @__PURE__ */ React.createElement(React.Suspense, {
|
|
9
|
+
fallback: props.fallback
|
|
10
|
+
}, mounted ? props.children() : props.fallback);
|
|
40
11
|
}
|
|
41
12
|
|
|
42
13
|
// src/factory.tsx
|
|
43
|
-
var
|
|
44
|
-
|
|
14
|
+
var _client = require('@liveblocks/client');
|
|
15
|
+
var _internal = require('@liveblocks/client/internal');
|
|
45
16
|
|
|
17
|
+
var _withselector = require('use-sync-external-store/shim/with-selector');
|
|
46
18
|
|
|
47
19
|
// src/hooks.ts
|
|
48
20
|
|
|
@@ -53,27 +25,22 @@ function useRerender() {
|
|
|
53
25
|
);
|
|
54
26
|
return update;
|
|
55
27
|
}
|
|
28
|
+
function useInitial(value) {
|
|
29
|
+
return _react.useRef.call(void 0, value).current;
|
|
30
|
+
}
|
|
56
31
|
|
|
57
32
|
// src/factory.tsx
|
|
58
|
-
|
|
59
|
-
|
|
33
|
+
var noop = () => {
|
|
34
|
+
};
|
|
35
|
+
var identity = (x) => x;
|
|
36
|
+
var EMPTY_OTHERS = _internal.asArrayWithLegacyMethods.call(void 0, []);
|
|
37
|
+
function getEmptyOthers() {
|
|
38
|
+
return EMPTY_OTHERS;
|
|
60
39
|
}
|
|
61
40
|
function createRoomContext(client) {
|
|
62
|
-
let useClient2;
|
|
63
|
-
if (client !== "__legacy") {
|
|
64
|
-
useClient2 = () => client;
|
|
65
|
-
} else {
|
|
66
|
-
useClient2 = useClient;
|
|
67
|
-
}
|
|
68
41
|
const RoomContext = React2.createContext(null);
|
|
69
|
-
function
|
|
70
|
-
const {
|
|
71
|
-
id: roomId,
|
|
72
|
-
initialPresence,
|
|
73
|
-
initialStorage,
|
|
74
|
-
defaultPresence,
|
|
75
|
-
defaultStorageRoot
|
|
76
|
-
} = props;
|
|
42
|
+
function RoomProvider(props) {
|
|
43
|
+
const { id: roomId, initialPresence, initialStorage } = props;
|
|
77
44
|
if (process.env.NODE_ENV !== "production") {
|
|
78
45
|
if (roomId == null) {
|
|
79
46
|
throw new Error(
|
|
@@ -84,93 +51,140 @@ function createRoomContext(client) {
|
|
|
84
51
|
throw new Error("RoomProvider id property should be a string.");
|
|
85
52
|
}
|
|
86
53
|
}
|
|
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
54
|
const frozen = useInitial({
|
|
97
55
|
initialPresence,
|
|
98
|
-
initialStorage
|
|
99
|
-
defaultPresence,
|
|
100
|
-
defaultStorageRoot
|
|
56
|
+
initialStorage
|
|
101
57
|
});
|
|
102
58
|
const [room, setRoom] = React2.useState(
|
|
103
|
-
() =>
|
|
59
|
+
() => client.enter(roomId, {
|
|
104
60
|
initialPresence,
|
|
105
61
|
initialStorage,
|
|
106
|
-
defaultPresence,
|
|
107
|
-
defaultStorageRoot,
|
|
108
62
|
DO_NOT_USE_withoutConnecting: typeof window === "undefined"
|
|
109
63
|
})
|
|
110
64
|
);
|
|
111
65
|
React2.useEffect(() => {
|
|
112
66
|
setRoom(
|
|
113
|
-
|
|
67
|
+
client.enter(roomId, {
|
|
114
68
|
initialPresence: frozen.initialPresence,
|
|
115
69
|
initialStorage: frozen.initialStorage,
|
|
116
|
-
defaultPresence: frozen.defaultPresence,
|
|
117
|
-
defaultStorageRoot: frozen.defaultStorageRoot,
|
|
118
70
|
DO_NOT_USE_withoutConnecting: typeof window === "undefined"
|
|
119
71
|
})
|
|
120
72
|
);
|
|
121
73
|
return () => {
|
|
122
|
-
|
|
74
|
+
client.leave(roomId);
|
|
123
75
|
};
|
|
124
|
-
}, [
|
|
76
|
+
}, [roomId, frozen]);
|
|
125
77
|
return /* @__PURE__ */ React2.createElement(RoomContext.Provider, {
|
|
126
78
|
value: room
|
|
127
79
|
}, props.children);
|
|
128
80
|
}
|
|
129
|
-
function
|
|
81
|
+
function connectionIdSelector(others) {
|
|
82
|
+
return others.map((user) => user.connectionId);
|
|
83
|
+
}
|
|
84
|
+
function useRoom() {
|
|
130
85
|
const room = React2.useContext(RoomContext);
|
|
131
86
|
if (room == null) {
|
|
132
87
|
throw new Error("RoomProvider is missing from the react tree");
|
|
133
88
|
}
|
|
134
89
|
return room;
|
|
135
90
|
}
|
|
136
|
-
function
|
|
137
|
-
const room =
|
|
91
|
+
function useMyPresence() {
|
|
92
|
+
const room = useRoom();
|
|
138
93
|
const presence = room.getPresence();
|
|
139
94
|
const rerender = useRerender();
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return () => {
|
|
143
|
-
unsubscribe();
|
|
144
|
-
};
|
|
145
|
-
}, [room, rerender]);
|
|
146
|
-
const setPresence = React2.useCallback(
|
|
147
|
-
(overrides, options) => room.updatePresence(overrides, options),
|
|
148
|
-
[room]
|
|
149
|
-
);
|
|
95
|
+
const setPresence = room.updatePresence;
|
|
96
|
+
React2.useEffect(() => room.events.me.subscribe(rerender), [room, rerender]);
|
|
150
97
|
return [presence, setPresence];
|
|
151
98
|
}
|
|
152
|
-
function
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
99
|
+
function useUpdateMyPresence() {
|
|
100
|
+
return useRoom().updatePresence;
|
|
101
|
+
}
|
|
102
|
+
function useOthers(selector, isEqual) {
|
|
103
|
+
const room = useRoom();
|
|
104
|
+
const subscribe = room.events.others.subscribe;
|
|
105
|
+
const getSnapshot = room.getOthers;
|
|
106
|
+
const getServerSnapshot = getEmptyOthers;
|
|
107
|
+
return _withselector.useSyncExternalStoreWithSelector.call(void 0,
|
|
108
|
+
subscribe,
|
|
109
|
+
getSnapshot,
|
|
110
|
+
getServerSnapshot,
|
|
111
|
+
selector != null ? selector : identity,
|
|
112
|
+
isEqual
|
|
159
113
|
);
|
|
160
114
|
}
|
|
161
|
-
function
|
|
162
|
-
const
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
115
|
+
function useOtherIds(itemSelector, isEqual) {
|
|
116
|
+
const _useCallback = React2.useCallback;
|
|
117
|
+
const _useOthers = useOthers;
|
|
118
|
+
if (itemSelector === void 0) {
|
|
119
|
+
return _useOthers(connectionIdSelector, _client.shallow);
|
|
120
|
+
} else {
|
|
121
|
+
const wrappedSelector = _useCallback(
|
|
122
|
+
(others) => others.map((other) => ({
|
|
123
|
+
connectionId: other.connectionId,
|
|
124
|
+
data: itemSelector(other)
|
|
125
|
+
})),
|
|
126
|
+
[itemSelector]
|
|
127
|
+
);
|
|
128
|
+
const wrappedIsEqual = _useCallback(
|
|
129
|
+
(a, b) => {
|
|
130
|
+
const eq = isEqual != null ? isEqual : Object.is;
|
|
131
|
+
return a.length === b.length && a.every((atuple, index) => {
|
|
132
|
+
const btuple = b[index];
|
|
133
|
+
return atuple.connectionId === btuple.connectionId && eq(atuple.data, btuple.data);
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
[isEqual]
|
|
137
|
+
);
|
|
138
|
+
return _useOthers(wrappedSelector, wrappedIsEqual);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const sentinel = Symbol();
|
|
142
|
+
function useOther(connectionId, selector, isEqual) {
|
|
143
|
+
const _useCallback = React2.useCallback;
|
|
144
|
+
const _useOthers = useOthers;
|
|
145
|
+
if (selector === void 0) {
|
|
146
|
+
const selector2 = _useCallback(
|
|
147
|
+
(others) => others.find((other2) => other2.connectionId === connectionId),
|
|
148
|
+
[connectionId]
|
|
149
|
+
);
|
|
150
|
+
const other = _useOthers(selector2, _client.shallow);
|
|
151
|
+
if (other === void 0) {
|
|
152
|
+
throw new Error(
|
|
153
|
+
`No such other user with connection id ${connectionId} exists`
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
return other;
|
|
157
|
+
} else {
|
|
158
|
+
const wrappedSelector = _useCallback(
|
|
159
|
+
(others) => {
|
|
160
|
+
const other2 = others.find(
|
|
161
|
+
(other3) => other3.connectionId === connectionId
|
|
162
|
+
);
|
|
163
|
+
return other2 !== void 0 ? selector(other2) : sentinel;
|
|
164
|
+
},
|
|
165
|
+
[connectionId, selector]
|
|
166
|
+
);
|
|
167
|
+
const wrappedIsEqual = _useCallback(
|
|
168
|
+
(a, b) => {
|
|
169
|
+
if (a === sentinel || b === sentinel) {
|
|
170
|
+
return a === b;
|
|
171
|
+
}
|
|
172
|
+
const eq = isEqual != null ? isEqual : Object.is;
|
|
173
|
+
return eq(a, b);
|
|
174
|
+
},
|
|
175
|
+
[isEqual]
|
|
176
|
+
);
|
|
177
|
+
const other = _useOthers(wrappedSelector, wrappedIsEqual);
|
|
178
|
+
if (other === sentinel) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
`No such other user with connection id ${connectionId} exists`
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
return other;
|
|
184
|
+
}
|
|
171
185
|
}
|
|
172
|
-
function
|
|
173
|
-
const room =
|
|
186
|
+
function useBroadcastEvent() {
|
|
187
|
+
const room = useRoom();
|
|
174
188
|
return React2.useCallback(
|
|
175
189
|
(event, options = { shouldQueueEventIfNotReady: false }) => {
|
|
176
190
|
room.broadcastEvent(event, options);
|
|
@@ -178,22 +192,19 @@ function createRoomContext(client) {
|
|
|
178
192
|
[room]
|
|
179
193
|
);
|
|
180
194
|
}
|
|
181
|
-
function
|
|
182
|
-
const room =
|
|
195
|
+
function useErrorListener(callback) {
|
|
196
|
+
const room = useRoom();
|
|
183
197
|
const savedCallback = React2.useRef(callback);
|
|
184
198
|
React2.useEffect(() => {
|
|
185
199
|
savedCallback.current = callback;
|
|
186
200
|
});
|
|
187
|
-
React2.useEffect(
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
unsubscribe();
|
|
192
|
-
};
|
|
193
|
-
}, [room]);
|
|
201
|
+
React2.useEffect(
|
|
202
|
+
() => room.events.error.subscribe((e) => savedCallback.current(e)),
|
|
203
|
+
[room]
|
|
204
|
+
);
|
|
194
205
|
}
|
|
195
|
-
function
|
|
196
|
-
const room =
|
|
206
|
+
function useEventListener(callback) {
|
|
207
|
+
const room = useRoom();
|
|
197
208
|
const savedCallback = React2.useRef(callback);
|
|
198
209
|
React2.useEffect(() => {
|
|
199
210
|
savedCallback.current = callback;
|
|
@@ -202,256 +213,92 @@ function createRoomContext(client) {
|
|
|
202
213
|
const listener = (eventData) => {
|
|
203
214
|
savedCallback.current(eventData);
|
|
204
215
|
};
|
|
205
|
-
|
|
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
|
-
};
|
|
216
|
+
return room.events.customEvent.subscribe(listener);
|
|
241
217
|
}, [room]);
|
|
242
|
-
return [root];
|
|
243
218
|
}
|
|
244
|
-
function
|
|
245
|
-
|
|
246
|
-
|
|
219
|
+
function useSelf(maybeSelector, isEqual) {
|
|
220
|
+
const room = useRoom();
|
|
221
|
+
const subscribe = React2.useCallback(
|
|
222
|
+
(onChange) => {
|
|
223
|
+
const unsub1 = room.events.me.subscribe(onChange);
|
|
224
|
+
const unsub2 = room.events.connection.subscribe(onChange);
|
|
225
|
+
return () => {
|
|
226
|
+
unsub1();
|
|
227
|
+
unsub2();
|
|
228
|
+
};
|
|
229
|
+
},
|
|
230
|
+
[room]
|
|
247
231
|
);
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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.`
|
|
232
|
+
const getSnapshot = room.getSelf;
|
|
233
|
+
const selector = maybeSelector != null ? maybeSelector : identity;
|
|
234
|
+
const wrappedSelector = React2.useCallback(
|
|
235
|
+
(me) => me !== null ? selector(me) : null,
|
|
236
|
+
[selector]
|
|
267
237
|
);
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
_internal.errorIf.call(void 0,
|
|
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.`
|
|
238
|
+
const getServerSnapshot = React2.useCallback(() => null, []);
|
|
239
|
+
return _withselector.useSyncExternalStoreWithSelector.call(void 0,
|
|
240
|
+
subscribe,
|
|
241
|
+
getSnapshot,
|
|
242
|
+
getServerSnapshot,
|
|
243
|
+
wrappedSelector,
|
|
244
|
+
isEqual
|
|
315
245
|
);
|
|
316
|
-
const value = useStorageValue(key, new (0, _client2.LiveList)(items));
|
|
317
|
-
if (value.status === "ok") {
|
|
318
|
-
return value.value;
|
|
319
|
-
} else {
|
|
320
|
-
_internal.errorIf.call(void 0,
|
|
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
246
|
}
|
|
344
|
-
function
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
...
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
<RoomProvider initialStorage={initialStorage}>
|
|
359
|
-
...
|
|
360
|
-
</RoomProvider>
|
|
361
|
-
|
|
362
|
-
Please see https://bit.ly/3Niy5aP for details.`
|
|
247
|
+
function useMutableStorageRoot() {
|
|
248
|
+
const room = useRoom();
|
|
249
|
+
const subscribe = room.events.storageDidLoad.subscribeOnce;
|
|
250
|
+
const getSnapshot = room.getStorageSnapshot;
|
|
251
|
+
const getServerSnapshot = React2.useCallback(() => null, []);
|
|
252
|
+
const selector = identity;
|
|
253
|
+
return _withselector.useSyncExternalStoreWithSelector.call(void 0,
|
|
254
|
+
subscribe,
|
|
255
|
+
getSnapshot,
|
|
256
|
+
getServerSnapshot,
|
|
257
|
+
selector
|
|
363
258
|
);
|
|
364
|
-
const value = useStorageValue(key, new (0, _client2.LiveObject)(initialData));
|
|
365
|
-
if (value.status === "ok") {
|
|
366
|
-
return value.value;
|
|
367
|
-
} else {
|
|
368
|
-
_internal.errorIf.call(void 0,
|
|
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
259
|
}
|
|
395
|
-
function
|
|
396
|
-
return
|
|
260
|
+
function useStorageRoot() {
|
|
261
|
+
return [useMutableStorageRoot()];
|
|
397
262
|
}
|
|
398
|
-
function
|
|
399
|
-
return
|
|
263
|
+
function useHistory() {
|
|
264
|
+
return useRoom().history;
|
|
400
265
|
}
|
|
401
|
-
function
|
|
402
|
-
return
|
|
266
|
+
function useUndo() {
|
|
267
|
+
return useHistory().undo;
|
|
403
268
|
}
|
|
404
|
-
function
|
|
405
|
-
return
|
|
406
|
-
}
|
|
407
|
-
function useRedo2() {
|
|
408
|
-
return useHistory2().redo;
|
|
269
|
+
function useRedo() {
|
|
270
|
+
return useHistory().redo;
|
|
409
271
|
}
|
|
410
272
|
function useCanUndo() {
|
|
411
|
-
const room =
|
|
273
|
+
const room = useRoom();
|
|
412
274
|
const [canUndo, setCanUndo] = React2.useState(room.history.canUndo);
|
|
413
|
-
React2.useEffect(
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
);
|
|
418
|
-
return () => {
|
|
419
|
-
unsubscribe();
|
|
420
|
-
};
|
|
421
|
-
}, [room]);
|
|
275
|
+
React2.useEffect(
|
|
276
|
+
() => room.events.history.subscribe(({ canUndo: canUndo2 }) => setCanUndo(canUndo2)),
|
|
277
|
+
[room]
|
|
278
|
+
);
|
|
422
279
|
return canUndo;
|
|
423
280
|
}
|
|
424
281
|
function useCanRedo() {
|
|
425
|
-
const room =
|
|
282
|
+
const room = useRoom();
|
|
426
283
|
const [canRedo, setCanRedo] = React2.useState(room.history.canRedo);
|
|
427
|
-
React2.useEffect(
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
);
|
|
432
|
-
return () => {
|
|
433
|
-
unsubscribe();
|
|
434
|
-
};
|
|
435
|
-
}, [room]);
|
|
284
|
+
React2.useEffect(
|
|
285
|
+
() => room.events.history.subscribe(({ canRedo: canRedo2 }) => setCanRedo(canRedo2)),
|
|
286
|
+
[room]
|
|
287
|
+
);
|
|
436
288
|
return canRedo;
|
|
437
289
|
}
|
|
438
|
-
function
|
|
439
|
-
return
|
|
290
|
+
function useBatch() {
|
|
291
|
+
return useRoom().batch;
|
|
440
292
|
}
|
|
441
|
-
function
|
|
442
|
-
const room =
|
|
443
|
-
const
|
|
293
|
+
function useLegacyKey(key) {
|
|
294
|
+
const room = useRoom();
|
|
295
|
+
const root = useMutableStorageRoot();
|
|
444
296
|
const rerender = useRerender();
|
|
445
|
-
const frozenInitialValue = useInitial(initialValue);
|
|
446
297
|
React2.useEffect(() => {
|
|
447
298
|
if (root == null) {
|
|
448
299
|
return;
|
|
449
300
|
}
|
|
450
301
|
let liveValue = root.get(key);
|
|
451
|
-
if (liveValue == null) {
|
|
452
|
-
liveValue = frozenInitialValue;
|
|
453
|
-
root.set(key, liveValue);
|
|
454
|
-
}
|
|
455
302
|
function onRootChange() {
|
|
456
303
|
const newCrdt = root.get(key);
|
|
457
304
|
if (newCrdt !== liveValue) {
|
|
@@ -477,177 +324,181 @@ Please see https://bit.ly/3Niy5aP for details.`
|
|
|
477
324
|
unsubscribeRoot();
|
|
478
325
|
unsubscribeCrdt();
|
|
479
326
|
};
|
|
480
|
-
}, [root, room, key,
|
|
327
|
+
}, [root, room, key, rerender]);
|
|
481
328
|
if (root == null) {
|
|
482
|
-
return
|
|
329
|
+
return null;
|
|
483
330
|
} else {
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
331
|
+
return root.get(key);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function useStorage(maybeSelector, isEqual) {
|
|
335
|
+
const room = useRoom();
|
|
336
|
+
const rootOrNull = useMutableStorageRoot();
|
|
337
|
+
const selector = maybeSelector != null ? maybeSelector : identity;
|
|
338
|
+
const wrappedSelector = React2.useCallback(
|
|
339
|
+
(rootOrNull2) => rootOrNull2 !== null ? selector(rootOrNull2) : null,
|
|
340
|
+
[selector]
|
|
341
|
+
);
|
|
342
|
+
const subscribe = React2.useCallback(
|
|
343
|
+
(onStoreChange) => rootOrNull !== null ? room.subscribe(rootOrNull, onStoreChange, { isDeep: true }) : noop,
|
|
344
|
+
[room, rootOrNull]
|
|
345
|
+
);
|
|
346
|
+
const getSnapshot = React2.useCallback(() => {
|
|
347
|
+
if (rootOrNull === null) {
|
|
348
|
+
return null;
|
|
487
349
|
} else {
|
|
488
|
-
|
|
350
|
+
const root = rootOrNull;
|
|
351
|
+
const imm = root.toImmutable();
|
|
352
|
+
return imm;
|
|
489
353
|
}
|
|
354
|
+
}, [rootOrNull]);
|
|
355
|
+
const getServerSnapshot = React2.useCallback(() => null, []);
|
|
356
|
+
return _withselector.useSyncExternalStoreWithSelector.call(void 0,
|
|
357
|
+
subscribe,
|
|
358
|
+
getSnapshot,
|
|
359
|
+
getServerSnapshot,
|
|
360
|
+
wrappedSelector,
|
|
361
|
+
isEqual
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
function ensureNotServerSide() {
|
|
365
|
+
if (typeof window === "undefined") {
|
|
366
|
+
throw new Error(
|
|
367
|
+
"You cannot call the Suspense version of this hook on the server side. Make sure to only call them on the client side.\nFor tips for structuring your app, see XXX"
|
|
368
|
+
);
|
|
490
369
|
}
|
|
491
370
|
}
|
|
371
|
+
function useSuspendUntilStorageLoaded() {
|
|
372
|
+
const room = useRoom();
|
|
373
|
+
if (room.getStorageSnapshot() !== null) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
ensureNotServerSide();
|
|
377
|
+
throw new Promise((res) => {
|
|
378
|
+
room.events.storageDidLoad.subscribeOnce(() => res());
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
function useSuspendUntilPresenceLoaded() {
|
|
382
|
+
const room = useRoom();
|
|
383
|
+
if (room.isSelfAware()) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
ensureNotServerSide();
|
|
387
|
+
throw new Promise((res) => {
|
|
388
|
+
room.events.connection.subscribeOnce(() => res());
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
function useMutation(callback, deps) {
|
|
392
|
+
const room = useRoom();
|
|
393
|
+
const root = useMutableStorageRoot();
|
|
394
|
+
const setMyPresence = room.updatePresence;
|
|
395
|
+
return React2.useMemo(
|
|
396
|
+
() => {
|
|
397
|
+
if (root !== null) {
|
|
398
|
+
const mutationCtx = {
|
|
399
|
+
root,
|
|
400
|
+
setMyPresence
|
|
401
|
+
};
|
|
402
|
+
return (...args) => {
|
|
403
|
+
let rv;
|
|
404
|
+
room.batch(() => {
|
|
405
|
+
rv = callback(mutationCtx, ...args);
|
|
406
|
+
});
|
|
407
|
+
return rv;
|
|
408
|
+
};
|
|
409
|
+
} else {
|
|
410
|
+
return () => {
|
|
411
|
+
throw new Error(
|
|
412
|
+
"Mutation cannot be called while Liveblocks Storage has not loaded yet"
|
|
413
|
+
);
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
deps !== void 0 ? [root, room, setMyPresence, ...deps] : [root, room, setMyPresence, callback]
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
function useStorageSuspense(selector, isEqual) {
|
|
421
|
+
useSuspendUntilStorageLoaded();
|
|
422
|
+
return useStorage(
|
|
423
|
+
selector,
|
|
424
|
+
isEqual
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
function useSelfSuspense(selector, isEqual) {
|
|
428
|
+
useSuspendUntilPresenceLoaded();
|
|
429
|
+
return useSelf(
|
|
430
|
+
selector,
|
|
431
|
+
isEqual
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
function useOthersSuspense(selector, isEqual) {
|
|
435
|
+
useSuspendUntilPresenceLoaded();
|
|
436
|
+
return useOthers(
|
|
437
|
+
selector,
|
|
438
|
+
isEqual
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
function useOtherIdsSuspense(itemSelector, isEqual) {
|
|
442
|
+
useSuspendUntilPresenceLoaded();
|
|
443
|
+
return useOtherIds(
|
|
444
|
+
itemSelector,
|
|
445
|
+
isEqual
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
function useOtherSuspense(connectionId, selector, isEqual) {
|
|
449
|
+
useSuspendUntilPresenceLoaded();
|
|
450
|
+
return useOther(
|
|
451
|
+
connectionId,
|
|
452
|
+
selector,
|
|
453
|
+
isEqual
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
function useLegacyKeySuspense(key) {
|
|
457
|
+
useSuspendUntilStorageLoaded();
|
|
458
|
+
return useLegacyKey(key);
|
|
459
|
+
}
|
|
492
460
|
return {
|
|
493
|
-
RoomProvider
|
|
494
|
-
useBatch
|
|
495
|
-
useBroadcastEvent
|
|
461
|
+
RoomProvider,
|
|
462
|
+
useBatch,
|
|
463
|
+
useBroadcastEvent,
|
|
496
464
|
useCanRedo,
|
|
497
465
|
useCanUndo,
|
|
498
|
-
useErrorListener
|
|
499
|
-
useEventListener
|
|
500
|
-
useHistory
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
466
|
+
useErrorListener,
|
|
467
|
+
useEventListener,
|
|
468
|
+
useHistory,
|
|
469
|
+
useMyPresence,
|
|
470
|
+
useOthers,
|
|
471
|
+
useOtherIds,
|
|
472
|
+
useOther,
|
|
473
|
+
useRedo,
|
|
474
|
+
useRoom,
|
|
475
|
+
useSelf,
|
|
476
|
+
useStorageRoot,
|
|
477
|
+
useStorage,
|
|
478
|
+
useUndo,
|
|
479
|
+
useUpdateMyPresence,
|
|
480
|
+
useMutation,
|
|
481
|
+
useList: useLegacyKey,
|
|
482
|
+
useMap: useLegacyKey,
|
|
483
|
+
useObject: useLegacyKey,
|
|
513
484
|
RoomContext,
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
485
|
+
suspense: {
|
|
486
|
+
useStorage: useStorageSuspense,
|
|
487
|
+
useSelf: useSelfSuspense,
|
|
488
|
+
useOthers: useOthersSuspense,
|
|
489
|
+
useOtherIds: useOtherIdsSuspense,
|
|
490
|
+
useOther: useOtherSuspense,
|
|
491
|
+
useList: useLegacyKeySuspense,
|
|
492
|
+
useMap: useLegacyKeySuspense,
|
|
493
|
+
useObject: useLegacyKeySuspense
|
|
494
|
+
}
|
|
517
495
|
};
|
|
518
496
|
}
|
|
519
497
|
|
|
520
|
-
// src/
|
|
521
|
-
|
|
522
|
-
var _hooks = createRoomContext("__legacy");
|
|
523
|
-
function RoomProvider(props) {
|
|
524
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_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
|
-
}
|
|
595
|
-
function useStorage() {
|
|
596
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
_internal.deprecate.call(void 0,
|
|
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
|
-
}
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
498
|
+
// src/index.ts
|
|
648
499
|
|
|
649
500
|
|
|
650
501
|
|
|
651
502
|
|
|
652
503
|
|
|
653
|
-
exports.
|
|
504
|
+
exports.ClientSideSuspense = ClientSideSuspense; exports.createRoomContext = createRoomContext; exports.shallow = _client.shallow;
|