@liveblocks/react 1.1.1-dual5 → 1.1.1-dual6
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/dist/index.js +568 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +568 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,11 +1,575 @@
|
|
|
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; } }
|
|
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; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/ClientSideSuspense.tsx
|
|
2
|
+
var _react = require('react'); var React = _interopRequireWildcard(_react); var React2 = _interopRequireWildcard(_react);
|
|
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, { fallback: props.fallback }, mounted ? props.children() : props.fallback);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// src/factory.tsx
|
|
12
|
+
var _client = require('@liveblocks/client');
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
var _core = require('@liveblocks/core');
|
|
18
|
+
|
|
19
|
+
var _withselectorjs = require('use-sync-external-store/shim/with-selector.js');
|
|
20
|
+
|
|
21
|
+
// src/hooks.ts
|
|
22
|
+
|
|
23
|
+
function useRerender() {
|
|
24
|
+
const [, update] = _react.useReducer.call(void 0,
|
|
25
|
+
// This implementation works by incrementing a hidden counter value that is
|
|
26
|
+
// never consumed. Simply incrementing the counter changes the component's
|
|
27
|
+
// state and, thus, trigger a re-render.
|
|
28
|
+
(x) => x + 1,
|
|
29
|
+
0
|
|
30
|
+
);
|
|
31
|
+
return update;
|
|
32
|
+
}
|
|
33
|
+
function useInitial(value) {
|
|
34
|
+
return _react.useRef.call(void 0, value).current;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/factory.tsx
|
|
38
|
+
var noop = () => {
|
|
39
|
+
};
|
|
40
|
+
var identity = (x) => x;
|
|
41
|
+
var missing_unstable_batchedUpdates = (reactVersion, roomId) => `We noticed you\u2019re using React ${reactVersion}. Please pass unstable_batchedUpdates at the RoomProvider level until you\u2019re ready to upgrade to React 18:
|
|
2
42
|
|
|
3
43
|
import { unstable_batchedUpdates } from "react-dom"; // or "react-native"
|
|
4
44
|
|
|
5
|
-
<RoomProvider id=${JSON.stringify(
|
|
45
|
+
<RoomProvider id=${JSON.stringify(
|
|
46
|
+
roomId
|
|
47
|
+
)} ... unstable_batchedUpdates={unstable_batchedUpdates}>
|
|
6
48
|
...
|
|
7
49
|
</RoomProvider>
|
|
8
50
|
|
|
9
|
-
Why? Please see https://liveblocks.io/docs/guides/troubleshooting#stale-props-zombie-child for more information
|
|
10
|
-
|
|
51
|
+
Why? Please see https://liveblocks.io/docs/guides/troubleshooting#stale-props-zombie-child for more information`;
|
|
52
|
+
var superfluous_unstable_batchedUpdates = "You don\u2019t need to pass unstable_batchedUpdates to RoomProvider anymore, since you\u2019re on React 18+ already.";
|
|
53
|
+
function useSyncExternalStore(s, gs, gss) {
|
|
54
|
+
return _withselectorjs.useSyncExternalStoreWithSelector.call(void 0, s, gs, gss, identity);
|
|
55
|
+
}
|
|
56
|
+
var EMPTY_OTHERS = (
|
|
57
|
+
// NOTE: asArrayWithLegacyMethods() wrapping should no longer be necessary in 0.19
|
|
58
|
+
_core.asArrayWithLegacyMethods.call(void 0, [])
|
|
59
|
+
);
|
|
60
|
+
function getEmptyOthers() {
|
|
61
|
+
return EMPTY_OTHERS;
|
|
62
|
+
}
|
|
63
|
+
function makeMutationContext(room) {
|
|
64
|
+
const errmsg = "This mutation cannot be used until connected to the Liveblocks room";
|
|
65
|
+
return {
|
|
66
|
+
get storage() {
|
|
67
|
+
const mutableRoot = room.getStorageSnapshot();
|
|
68
|
+
if (mutableRoot === null) {
|
|
69
|
+
throw new Error(errmsg);
|
|
70
|
+
}
|
|
71
|
+
return mutableRoot;
|
|
72
|
+
},
|
|
73
|
+
get self() {
|
|
74
|
+
const self = room.getSelf();
|
|
75
|
+
if (self === null) {
|
|
76
|
+
throw new Error(errmsg);
|
|
77
|
+
}
|
|
78
|
+
return self;
|
|
79
|
+
},
|
|
80
|
+
get others() {
|
|
81
|
+
const others = room.getOthers();
|
|
82
|
+
if (!room.isSelfAware()) {
|
|
83
|
+
throw new Error(errmsg);
|
|
84
|
+
}
|
|
85
|
+
return others;
|
|
86
|
+
},
|
|
87
|
+
setMyPresence: room.updatePresence
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function createRoomContext(client) {
|
|
91
|
+
const RoomContext = React2.createContext(null);
|
|
92
|
+
function RoomProvider(props) {
|
|
93
|
+
const {
|
|
94
|
+
id: roomId,
|
|
95
|
+
initialPresence,
|
|
96
|
+
initialStorage,
|
|
97
|
+
unstable_batchedUpdates,
|
|
98
|
+
shouldInitiallyConnect
|
|
99
|
+
} = props;
|
|
100
|
+
if (process.env.NODE_ENV !== "production") {
|
|
101
|
+
if (!roomId) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
"RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required"
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (typeof roomId !== "string") {
|
|
107
|
+
throw new Error("RoomProvider id property should be a string.");
|
|
108
|
+
}
|
|
109
|
+
const majorReactVersion = parseInt(React2.version) || 1;
|
|
110
|
+
const oldReactVersion = majorReactVersion < 18;
|
|
111
|
+
_core.errorIf.call(void 0,
|
|
112
|
+
oldReactVersion && props.unstable_batchedUpdates === void 0,
|
|
113
|
+
missing_unstable_batchedUpdates(majorReactVersion, roomId)
|
|
114
|
+
);
|
|
115
|
+
_core.deprecateIf.call(void 0,
|
|
116
|
+
!oldReactVersion && props.unstable_batchedUpdates !== void 0,
|
|
117
|
+
superfluous_unstable_batchedUpdates
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const frozen = useInitial({
|
|
121
|
+
initialPresence,
|
|
122
|
+
initialStorage,
|
|
123
|
+
unstable_batchedUpdates,
|
|
124
|
+
shouldInitiallyConnect: shouldInitiallyConnect === void 0 ? typeof window !== "undefined" : shouldInitiallyConnect
|
|
125
|
+
});
|
|
126
|
+
const [room, setRoom] = React2.useState(
|
|
127
|
+
() => client.enter(roomId, {
|
|
128
|
+
initialPresence: frozen.initialPresence,
|
|
129
|
+
initialStorage: frozen.initialStorage,
|
|
130
|
+
shouldInitiallyConnect: frozen.shouldInitiallyConnect,
|
|
131
|
+
unstable_batchedUpdates: frozen.unstable_batchedUpdates
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
React2.useEffect(() => {
|
|
135
|
+
setRoom(
|
|
136
|
+
client.enter(roomId, {
|
|
137
|
+
initialPresence: frozen.initialPresence,
|
|
138
|
+
initialStorage: frozen.initialStorage,
|
|
139
|
+
shouldInitiallyConnect: frozen.shouldInitiallyConnect,
|
|
140
|
+
unstable_batchedUpdates: frozen.unstable_batchedUpdates
|
|
141
|
+
})
|
|
142
|
+
);
|
|
143
|
+
return () => {
|
|
144
|
+
client.leave(roomId);
|
|
145
|
+
};
|
|
146
|
+
}, [roomId, frozen]);
|
|
147
|
+
return /* @__PURE__ */ React2.createElement(RoomContext.Provider, { value: room }, props.children);
|
|
148
|
+
}
|
|
149
|
+
function connectionIdSelector(others) {
|
|
150
|
+
return others.map((user) => user.connectionId);
|
|
151
|
+
}
|
|
152
|
+
function useRoom() {
|
|
153
|
+
const room = React2.useContext(RoomContext);
|
|
154
|
+
if (room === null) {
|
|
155
|
+
throw new Error("RoomProvider is missing from the react tree");
|
|
156
|
+
}
|
|
157
|
+
return room;
|
|
158
|
+
}
|
|
159
|
+
function useStatus() {
|
|
160
|
+
const room = useRoom();
|
|
161
|
+
const subscribe = room.events.status.subscribe;
|
|
162
|
+
const getSnapshot = room.getStatus;
|
|
163
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
164
|
+
}
|
|
165
|
+
function useMyPresence() {
|
|
166
|
+
const room = useRoom();
|
|
167
|
+
const subscribe = room.events.me.subscribe;
|
|
168
|
+
const getSnapshot = room.getPresence;
|
|
169
|
+
const presence = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
170
|
+
const setPresence = room.updatePresence;
|
|
171
|
+
return [presence, setPresence];
|
|
172
|
+
}
|
|
173
|
+
function useUpdateMyPresence() {
|
|
174
|
+
return useRoom().updatePresence;
|
|
175
|
+
}
|
|
176
|
+
function useOthers(selector, isEqual) {
|
|
177
|
+
const room = useRoom();
|
|
178
|
+
const subscribe = room.events.others.subscribe;
|
|
179
|
+
const getSnapshot = room.getOthers;
|
|
180
|
+
const getServerSnapshot = getEmptyOthers;
|
|
181
|
+
return _withselectorjs.useSyncExternalStoreWithSelector.call(void 0,
|
|
182
|
+
subscribe,
|
|
183
|
+
getSnapshot,
|
|
184
|
+
getServerSnapshot,
|
|
185
|
+
_nullishCoalesce(selector, () => ( identity)),
|
|
186
|
+
isEqual
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
function useOthersConnectionIds() {
|
|
190
|
+
return useOthers(connectionIdSelector, _client.shallow);
|
|
191
|
+
}
|
|
192
|
+
function useOthersMapped(itemSelector, itemIsEqual) {
|
|
193
|
+
const wrappedSelector = React2.useCallback(
|
|
194
|
+
(others) => others.map(
|
|
195
|
+
(other) => [other.connectionId, itemSelector(other)]
|
|
196
|
+
),
|
|
197
|
+
[itemSelector]
|
|
198
|
+
);
|
|
199
|
+
const wrappedIsEqual = React2.useCallback(
|
|
200
|
+
(a, b) => {
|
|
201
|
+
const eq = _nullishCoalesce(itemIsEqual, () => ( Object.is));
|
|
202
|
+
return a.length === b.length && a.every((atuple, index) => {
|
|
203
|
+
const btuple = b[index];
|
|
204
|
+
return atuple[0] === btuple[0] && eq(atuple[1], btuple[1]);
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
[itemIsEqual]
|
|
208
|
+
);
|
|
209
|
+
return useOthers(wrappedSelector, wrappedIsEqual);
|
|
210
|
+
}
|
|
211
|
+
const NOT_FOUND = Symbol();
|
|
212
|
+
function useOther(connectionId, selector, isEqual) {
|
|
213
|
+
const wrappedSelector = React2.useCallback(
|
|
214
|
+
(others) => {
|
|
215
|
+
const other2 = others.find(
|
|
216
|
+
(other3) => other3.connectionId === connectionId
|
|
217
|
+
);
|
|
218
|
+
return other2 !== void 0 ? selector(other2) : NOT_FOUND;
|
|
219
|
+
},
|
|
220
|
+
[connectionId, selector]
|
|
221
|
+
);
|
|
222
|
+
const wrappedIsEqual = React2.useCallback(
|
|
223
|
+
(prev, curr) => {
|
|
224
|
+
if (prev === NOT_FOUND || curr === NOT_FOUND) {
|
|
225
|
+
return prev === curr;
|
|
226
|
+
}
|
|
227
|
+
const eq = _nullishCoalesce(isEqual, () => ( Object.is));
|
|
228
|
+
return eq(prev, curr);
|
|
229
|
+
},
|
|
230
|
+
[isEqual]
|
|
231
|
+
);
|
|
232
|
+
const other = useOthers(wrappedSelector, wrappedIsEqual);
|
|
233
|
+
if (other === NOT_FOUND) {
|
|
234
|
+
throw new Error(
|
|
235
|
+
`No such other user with connection id ${connectionId} exists`
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
return other;
|
|
239
|
+
}
|
|
240
|
+
function useBroadcastEvent() {
|
|
241
|
+
const room = useRoom();
|
|
242
|
+
return React2.useCallback(
|
|
243
|
+
(event, options = { shouldQueueEventIfNotReady: false }) => {
|
|
244
|
+
room.broadcastEvent(event, options);
|
|
245
|
+
},
|
|
246
|
+
[room]
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
function useLostConnectionListener(callback) {
|
|
250
|
+
const room = useRoom();
|
|
251
|
+
const savedCallback = React2.useRef(callback);
|
|
252
|
+
React2.useEffect(() => {
|
|
253
|
+
savedCallback.current = callback;
|
|
254
|
+
});
|
|
255
|
+
React2.useEffect(
|
|
256
|
+
() => room.events.lostConnection.subscribe(
|
|
257
|
+
(event) => savedCallback.current(event)
|
|
258
|
+
),
|
|
259
|
+
[room]
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
function useErrorListener(callback) {
|
|
263
|
+
const room = useRoom();
|
|
264
|
+
const savedCallback = React2.useRef(callback);
|
|
265
|
+
React2.useEffect(() => {
|
|
266
|
+
savedCallback.current = callback;
|
|
267
|
+
});
|
|
268
|
+
React2.useEffect(
|
|
269
|
+
() => room.events.error.subscribe((e) => savedCallback.current(e)),
|
|
270
|
+
[room]
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
function useEventListener(callback) {
|
|
274
|
+
const room = useRoom();
|
|
275
|
+
const savedCallback = React2.useRef(callback);
|
|
276
|
+
React2.useEffect(() => {
|
|
277
|
+
savedCallback.current = callback;
|
|
278
|
+
});
|
|
279
|
+
React2.useEffect(() => {
|
|
280
|
+
const listener = (eventData) => {
|
|
281
|
+
savedCallback.current(eventData);
|
|
282
|
+
};
|
|
283
|
+
return room.events.customEvent.subscribe(listener);
|
|
284
|
+
}, [room]);
|
|
285
|
+
}
|
|
286
|
+
function useSelf(maybeSelector, isEqual) {
|
|
287
|
+
const room = useRoom();
|
|
288
|
+
const subscribe = React2.useCallback(
|
|
289
|
+
(onChange) => {
|
|
290
|
+
const unsub1 = room.events.me.subscribe(onChange);
|
|
291
|
+
const unsub2 = room.events.connection.subscribe(onChange);
|
|
292
|
+
return () => {
|
|
293
|
+
unsub1();
|
|
294
|
+
unsub2();
|
|
295
|
+
};
|
|
296
|
+
},
|
|
297
|
+
[room]
|
|
298
|
+
);
|
|
299
|
+
const getSnapshot = room.getSelf;
|
|
300
|
+
const selector = _nullishCoalesce(maybeSelector, () => ( identity));
|
|
301
|
+
const wrappedSelector = React2.useCallback(
|
|
302
|
+
(me) => me !== null ? selector(me) : null,
|
|
303
|
+
[selector]
|
|
304
|
+
);
|
|
305
|
+
const getServerSnapshot = React2.useCallback(() => null, []);
|
|
306
|
+
return _withselectorjs.useSyncExternalStoreWithSelector.call(void 0,
|
|
307
|
+
subscribe,
|
|
308
|
+
getSnapshot,
|
|
309
|
+
getServerSnapshot,
|
|
310
|
+
wrappedSelector,
|
|
311
|
+
isEqual
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
function useMutableStorageRoot() {
|
|
315
|
+
const room = useRoom();
|
|
316
|
+
const subscribe = room.events.storageDidLoad.subscribeOnce;
|
|
317
|
+
const getSnapshot = room.getStorageSnapshot;
|
|
318
|
+
const getServerSnapshot = React2.useCallback(() => null, []);
|
|
319
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
320
|
+
}
|
|
321
|
+
function useStorageRoot() {
|
|
322
|
+
return [useMutableStorageRoot()];
|
|
323
|
+
}
|
|
324
|
+
function useHistory() {
|
|
325
|
+
return useRoom().history;
|
|
326
|
+
}
|
|
327
|
+
function useUndo() {
|
|
328
|
+
return useHistory().undo;
|
|
329
|
+
}
|
|
330
|
+
function useRedo() {
|
|
331
|
+
return useHistory().redo;
|
|
332
|
+
}
|
|
333
|
+
function useCanUndo() {
|
|
334
|
+
const room = useRoom();
|
|
335
|
+
const subscribe = room.events.history.subscribe;
|
|
336
|
+
const canUndo = room.history.canUndo;
|
|
337
|
+
return useSyncExternalStore(subscribe, canUndo, canUndo);
|
|
338
|
+
}
|
|
339
|
+
function useCanRedo() {
|
|
340
|
+
const room = useRoom();
|
|
341
|
+
const subscribe = room.events.history.subscribe;
|
|
342
|
+
const canRedo = room.history.canRedo;
|
|
343
|
+
return useSyncExternalStore(subscribe, canRedo, canRedo);
|
|
344
|
+
}
|
|
345
|
+
function useBatch() {
|
|
346
|
+
return useRoom().batch;
|
|
347
|
+
}
|
|
348
|
+
function useLegacyKey(key) {
|
|
349
|
+
const room = useRoom();
|
|
350
|
+
const root = useMutableStorageRoot();
|
|
351
|
+
const rerender = useRerender();
|
|
352
|
+
React2.useEffect(() => {
|
|
353
|
+
if (root === null) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
let liveValue = root.get(key);
|
|
357
|
+
function onRootChange() {
|
|
358
|
+
const newCrdt = root.get(key);
|
|
359
|
+
if (newCrdt !== liveValue) {
|
|
360
|
+
unsubscribeCrdt();
|
|
361
|
+
liveValue = newCrdt;
|
|
362
|
+
unsubscribeCrdt = room.subscribe(
|
|
363
|
+
liveValue,
|
|
364
|
+
// TODO: This is hiding a bug! If `liveValue` happens to be the string `"event"` this actually subscribes an event handler!
|
|
365
|
+
rerender
|
|
366
|
+
);
|
|
367
|
+
rerender();
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
let unsubscribeCrdt = room.subscribe(
|
|
371
|
+
liveValue,
|
|
372
|
+
// TODO: This is hiding a bug! If `liveValue` happens to be the string `"event"` this actually subscribes an event handler!
|
|
373
|
+
rerender
|
|
374
|
+
);
|
|
375
|
+
const unsubscribeRoot = room.subscribe(
|
|
376
|
+
root,
|
|
377
|
+
// TODO: This is hiding a bug! If `liveValue` happens to be the string `"event"` this actually subscribes an event handler!
|
|
378
|
+
onRootChange
|
|
379
|
+
);
|
|
380
|
+
rerender();
|
|
381
|
+
return () => {
|
|
382
|
+
unsubscribeRoot();
|
|
383
|
+
unsubscribeCrdt();
|
|
384
|
+
};
|
|
385
|
+
}, [root, room, key, rerender]);
|
|
386
|
+
if (root === null) {
|
|
387
|
+
return null;
|
|
388
|
+
} else {
|
|
389
|
+
return root.get(key);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
function useStorage(selector, isEqual) {
|
|
393
|
+
const room = useRoom();
|
|
394
|
+
const rootOrNull = useMutableStorageRoot();
|
|
395
|
+
const wrappedSelector = React2.useCallback(
|
|
396
|
+
(rootOrNull2) => rootOrNull2 !== null ? selector(rootOrNull2) : null,
|
|
397
|
+
[selector]
|
|
398
|
+
);
|
|
399
|
+
const subscribe = React2.useCallback(
|
|
400
|
+
(onStoreChange) => rootOrNull !== null ? room.subscribe(rootOrNull, onStoreChange, { isDeep: true }) : noop,
|
|
401
|
+
[room, rootOrNull]
|
|
402
|
+
);
|
|
403
|
+
const getSnapshot = React2.useCallback(() => {
|
|
404
|
+
if (rootOrNull === null) {
|
|
405
|
+
return null;
|
|
406
|
+
} else {
|
|
407
|
+
const root = rootOrNull;
|
|
408
|
+
const imm = root.toImmutable();
|
|
409
|
+
return imm;
|
|
410
|
+
}
|
|
411
|
+
}, [rootOrNull]);
|
|
412
|
+
const getServerSnapshot = React2.useCallback(() => null, []);
|
|
413
|
+
return _withselectorjs.useSyncExternalStoreWithSelector.call(void 0,
|
|
414
|
+
subscribe,
|
|
415
|
+
getSnapshot,
|
|
416
|
+
getServerSnapshot,
|
|
417
|
+
wrappedSelector,
|
|
418
|
+
isEqual
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
function ensureNotServerSide() {
|
|
422
|
+
if (typeof window === "undefined") {
|
|
423
|
+
throw new Error(
|
|
424
|
+
"You cannot use the Suspense version of this hook on the server side. Make sure to only call them on the client side.\nFor tips, see https://liveblocks.io/docs/api-reference/liveblocks-react#suspense-avoid-ssr"
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
function useSuspendUntilStorageLoaded() {
|
|
429
|
+
const room = useRoom();
|
|
430
|
+
if (room.getStorageSnapshot() !== null) {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
ensureNotServerSide();
|
|
434
|
+
throw new Promise((res) => {
|
|
435
|
+
room.events.storageDidLoad.subscribeOnce(() => res());
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
function useSuspendUntilPresenceLoaded() {
|
|
439
|
+
const room = useRoom();
|
|
440
|
+
if (room.isSelfAware()) {
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
ensureNotServerSide();
|
|
444
|
+
throw new Promise((res) => {
|
|
445
|
+
room.events.connection.subscribeOnce(() => res());
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
function useMutation(callback, deps) {
|
|
449
|
+
const room = useRoom();
|
|
450
|
+
return React2.useMemo(
|
|
451
|
+
() => {
|
|
452
|
+
return (...args) => (
|
|
453
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
454
|
+
room.batch(
|
|
455
|
+
() => (
|
|
456
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
457
|
+
callback(
|
|
458
|
+
makeMutationContext(room),
|
|
459
|
+
...args
|
|
460
|
+
)
|
|
461
|
+
)
|
|
462
|
+
)
|
|
463
|
+
);
|
|
464
|
+
},
|
|
465
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
466
|
+
[room, ...deps]
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
function useStorageSuspense(selector, isEqual) {
|
|
470
|
+
useSuspendUntilStorageLoaded();
|
|
471
|
+
return useStorage(
|
|
472
|
+
selector,
|
|
473
|
+
isEqual
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
function useSelfSuspense(selector, isEqual) {
|
|
477
|
+
useSuspendUntilPresenceLoaded();
|
|
478
|
+
return useSelf(
|
|
479
|
+
selector,
|
|
480
|
+
isEqual
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
function useOthersSuspense(selector, isEqual) {
|
|
484
|
+
useSuspendUntilPresenceLoaded();
|
|
485
|
+
return useOthers(
|
|
486
|
+
selector,
|
|
487
|
+
isEqual
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
function useOthersConnectionIdsSuspense() {
|
|
491
|
+
useSuspendUntilPresenceLoaded();
|
|
492
|
+
return useOthersConnectionIds();
|
|
493
|
+
}
|
|
494
|
+
function useOthersMappedSuspense(itemSelector, itemIsEqual) {
|
|
495
|
+
useSuspendUntilPresenceLoaded();
|
|
496
|
+
return useOthersMapped(itemSelector, itemIsEqual);
|
|
497
|
+
}
|
|
498
|
+
function useOtherSuspense(connectionId, selector, isEqual) {
|
|
499
|
+
useSuspendUntilPresenceLoaded();
|
|
500
|
+
return useOther(connectionId, selector, isEqual);
|
|
501
|
+
}
|
|
502
|
+
function useLegacyKeySuspense(key) {
|
|
503
|
+
useSuspendUntilStorageLoaded();
|
|
504
|
+
return useLegacyKey(key);
|
|
505
|
+
}
|
|
506
|
+
return {
|
|
507
|
+
RoomContext,
|
|
508
|
+
RoomProvider,
|
|
509
|
+
useRoom,
|
|
510
|
+
useStatus,
|
|
511
|
+
useBatch,
|
|
512
|
+
useBroadcastEvent,
|
|
513
|
+
useLostConnectionListener,
|
|
514
|
+
useErrorListener,
|
|
515
|
+
useEventListener,
|
|
516
|
+
useHistory,
|
|
517
|
+
useUndo,
|
|
518
|
+
useRedo,
|
|
519
|
+
useCanRedo,
|
|
520
|
+
useCanUndo,
|
|
521
|
+
// These are just aliases. The passed-in key will define their return values.
|
|
522
|
+
useList: useLegacyKey,
|
|
523
|
+
useMap: useLegacyKey,
|
|
524
|
+
useObject: useLegacyKey,
|
|
525
|
+
useStorageRoot,
|
|
526
|
+
useStorage,
|
|
527
|
+
useSelf,
|
|
528
|
+
useMyPresence,
|
|
529
|
+
useUpdateMyPresence,
|
|
530
|
+
useOthers,
|
|
531
|
+
useOthersMapped,
|
|
532
|
+
useOthersConnectionIds,
|
|
533
|
+
useOther,
|
|
534
|
+
useMutation,
|
|
535
|
+
suspense: {
|
|
536
|
+
RoomContext,
|
|
537
|
+
RoomProvider,
|
|
538
|
+
useRoom,
|
|
539
|
+
useStatus,
|
|
540
|
+
useBatch,
|
|
541
|
+
useBroadcastEvent,
|
|
542
|
+
useLostConnectionListener,
|
|
543
|
+
useErrorListener,
|
|
544
|
+
useEventListener,
|
|
545
|
+
useHistory,
|
|
546
|
+
useUndo,
|
|
547
|
+
useRedo,
|
|
548
|
+
useCanRedo,
|
|
549
|
+
useCanUndo,
|
|
550
|
+
// Legacy hooks
|
|
551
|
+
useList: useLegacyKeySuspense,
|
|
552
|
+
useMap: useLegacyKeySuspense,
|
|
553
|
+
useObject: useLegacyKeySuspense,
|
|
554
|
+
useStorageRoot,
|
|
555
|
+
useStorage: useStorageSuspense,
|
|
556
|
+
useSelf: useSelfSuspense,
|
|
557
|
+
useMyPresence,
|
|
558
|
+
useUpdateMyPresence,
|
|
559
|
+
useOthers: useOthersSuspense,
|
|
560
|
+
useOthersMapped: useOthersMappedSuspense,
|
|
561
|
+
useOthersConnectionIds: useOthersConnectionIdsSuspense,
|
|
562
|
+
useOther: useOtherSuspense,
|
|
563
|
+
useMutation
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// src/index.ts
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
exports.ClientSideSuspense = ClientSideSuspense; exports.createRoomContext = createRoomContext; exports.shallow = _client.shallow;
|
|
11
575
|
//# sourceMappingURL=index.js.map
|