@liveblocks/react 0.16.4-beta1 → 0.16.5
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 +39 -188
- package/index.js +241 -331
- package/index.mjs +230 -0
- package/package.json +15 -20
- package/esm/index.js +0 -212
- package/esm/index.mjs +0 -212
package/index.mjs
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { useReducer } from "react";
|
|
4
|
+
|
|
5
|
+
import { LiveMap, LiveList, LiveObject } from "@liveblocks/client";
|
|
6
|
+
|
|
7
|
+
import { deprecateIf } from "@liveblocks/client/internal";
|
|
8
|
+
|
|
9
|
+
const ClientContext = React.createContext(null);
|
|
10
|
+
|
|
11
|
+
function LiveblocksProvider(props) {
|
|
12
|
+
return React.createElement(ClientContext.Provider, {
|
|
13
|
+
value: props.client
|
|
14
|
+
}, props.children);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function useClient() {
|
|
18
|
+
const client = React.useContext(ClientContext);
|
|
19
|
+
if (null == client) throw new Error("LiveblocksProvider is missing from the react tree");
|
|
20
|
+
return client;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
24
|
+
return new (P || (P = Promise))((function(resolve, reject) {
|
|
25
|
+
function fulfilled(value) {
|
|
26
|
+
try {
|
|
27
|
+
step(generator.next(value));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
reject(e);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function rejected(value) {
|
|
33
|
+
try {
|
|
34
|
+
step(generator.throw(value));
|
|
35
|
+
} catch (e) {
|
|
36
|
+
reject(e);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function step(result) {
|
|
40
|
+
var value;
|
|
41
|
+
result.done ? resolve(result.value) : (value = result.value, value instanceof P ? value : new P((function(resolve) {
|
|
42
|
+
resolve(value);
|
|
43
|
+
}))).then(fulfilled, rejected);
|
|
44
|
+
}
|
|
45
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function useRerender() {
|
|
50
|
+
const [, update] = useReducer((x => x + 1), 0);
|
|
51
|
+
return update;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const {RoomProvider: RoomProvider, useRoom: useRoom, useMyPresence: useMyPresence, useUpdateMyPresence: useUpdateMyPresence, useOthers: useOthers, useBroadcastEvent: useBroadcastEvent, useErrorListener: useErrorListener, useEventListener: useEventListener, useSelf: useSelf, useStorage: useStorage, useMap: useMap, useList: useList, useObject: useObject, useUndo: useUndo, useRedo: useRedo, useBatch: useBatch, useHistory: useHistory} = function() {
|
|
55
|
+
const RoomContext = React.createContext(null);
|
|
56
|
+
function useRoom() {
|
|
57
|
+
const room = React.useContext(RoomContext);
|
|
58
|
+
if (null == room) throw new Error("RoomProvider is missing from the react tree");
|
|
59
|
+
return room;
|
|
60
|
+
}
|
|
61
|
+
function useStorage() {
|
|
62
|
+
const room = useRoom(), [root, setState] = React.useState(null);
|
|
63
|
+
return React.useEffect((() => {
|
|
64
|
+
let didCancel = !1;
|
|
65
|
+
return function() {
|
|
66
|
+
__awaiter(this, void 0, void 0, (function*() {
|
|
67
|
+
const storage = yield room.getStorage();
|
|
68
|
+
didCancel || setState(storage.root);
|
|
69
|
+
}));
|
|
70
|
+
}(), () => {
|
|
71
|
+
didCancel = !0;
|
|
72
|
+
};
|
|
73
|
+
}), [ room ]), [ root ];
|
|
74
|
+
}
|
|
75
|
+
function useHistory() {
|
|
76
|
+
return useRoom().history;
|
|
77
|
+
}
|
|
78
|
+
function useStorageValue(key, initialValue) {
|
|
79
|
+
const room = useRoom(), [root] = useStorage(), rerender = useRerender();
|
|
80
|
+
if (React.useEffect((() => {
|
|
81
|
+
if (null == root) return;
|
|
82
|
+
let liveValue = root.get(key);
|
|
83
|
+
null == liveValue && (liveValue = initialValue, root.set(key, liveValue));
|
|
84
|
+
let unsubscribeCrdt = room.subscribe(liveValue, rerender);
|
|
85
|
+
const unsubscribeRoot = room.subscribe(root, (function() {
|
|
86
|
+
const newCrdt = root.get(key);
|
|
87
|
+
newCrdt !== liveValue && (unsubscribeCrdt(), liveValue = newCrdt, unsubscribeCrdt = room.subscribe(liveValue, rerender),
|
|
88
|
+
rerender());
|
|
89
|
+
}));
|
|
90
|
+
return rerender(), () => {
|
|
91
|
+
unsubscribeRoot(), unsubscribeCrdt();
|
|
92
|
+
};
|
|
93
|
+
}), [ root, room ]), null == root) return {
|
|
94
|
+
status: "loading"
|
|
95
|
+
};
|
|
96
|
+
{
|
|
97
|
+
const value = root.get(key);
|
|
98
|
+
return null == value ? {
|
|
99
|
+
status: "notfound"
|
|
100
|
+
} : {
|
|
101
|
+
status: "ok",
|
|
102
|
+
value: value
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
RoomProvider: function(props) {
|
|
108
|
+
const {id: roomId, initialPresence: initialPresence, initialStorage: initialStorage, defaultPresence: defaultPresence, defaultStorageRoot: defaultStorageRoot} = props;
|
|
109
|
+
if ("production" !== process.env.NODE_ENV) {
|
|
110
|
+
if (null == roomId) throw new Error("RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required");
|
|
111
|
+
if ("string" != typeof roomId) throw new Error("RoomProvider id property should be a string.");
|
|
112
|
+
}
|
|
113
|
+
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"),
|
|
114
|
+
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");
|
|
115
|
+
const client = useClient(), [room, setRoom] = React.useState((() => client.enter(roomId, {
|
|
116
|
+
initialPresence: initialPresence,
|
|
117
|
+
initialStorage: initialStorage,
|
|
118
|
+
defaultPresence: defaultPresence,
|
|
119
|
+
defaultStorageRoot: defaultStorageRoot,
|
|
120
|
+
DO_NOT_USE_withoutConnecting: "undefined" == typeof window
|
|
121
|
+
})));
|
|
122
|
+
return React.useEffect((() => (setRoom(client.enter(roomId, {
|
|
123
|
+
initialPresence: initialPresence,
|
|
124
|
+
initialStorage: initialStorage,
|
|
125
|
+
defaultPresence: defaultPresence,
|
|
126
|
+
defaultStorageRoot: defaultStorageRoot,
|
|
127
|
+
DO_NOT_USE_withoutConnecting: "undefined" == typeof window
|
|
128
|
+
})), () => {
|
|
129
|
+
client.leave(roomId);
|
|
130
|
+
})), [ client, roomId ]), React.createElement(RoomContext.Provider, {
|
|
131
|
+
value: room
|
|
132
|
+
}, props.children);
|
|
133
|
+
},
|
|
134
|
+
useBatch: function() {
|
|
135
|
+
return useRoom().batch;
|
|
136
|
+
},
|
|
137
|
+
useBroadcastEvent: function() {
|
|
138
|
+
const room = useRoom();
|
|
139
|
+
return React.useCallback(((event, options = {
|
|
140
|
+
shouldQueueEventIfNotReady: !1
|
|
141
|
+
}) => {
|
|
142
|
+
room.broadcastEvent(event, options);
|
|
143
|
+
}), [ room ]);
|
|
144
|
+
},
|
|
145
|
+
useErrorListener: function(callback) {
|
|
146
|
+
const room = useRoom(), savedCallback = React.useRef(callback);
|
|
147
|
+
React.useEffect((() => {
|
|
148
|
+
savedCallback.current = callback;
|
|
149
|
+
})), React.useEffect((() => {
|
|
150
|
+
const unsubscribe = room.subscribe("error", (e => savedCallback.current(e)));
|
|
151
|
+
return () => {
|
|
152
|
+
unsubscribe();
|
|
153
|
+
};
|
|
154
|
+
}), [ room ]);
|
|
155
|
+
},
|
|
156
|
+
useEventListener: function(callback) {
|
|
157
|
+
const room = useRoom(), savedCallback = React.useRef(callback);
|
|
158
|
+
React.useEffect((() => {
|
|
159
|
+
savedCallback.current = callback;
|
|
160
|
+
})), React.useEffect((() => {
|
|
161
|
+
const unsubscribe = room.subscribe("event", (e => savedCallback.current(e)));
|
|
162
|
+
return () => {
|
|
163
|
+
unsubscribe();
|
|
164
|
+
};
|
|
165
|
+
}), [ room ]);
|
|
166
|
+
},
|
|
167
|
+
useHistory: useHistory,
|
|
168
|
+
useList: function(key, items) {
|
|
169
|
+
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.`);
|
|
170
|
+
const value = useStorageValue(key, new LiveList(items));
|
|
171
|
+
return "ok" === value.status ? value.value : (deprecateIf("notfound" === value.status, `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.`),
|
|
172
|
+
null);
|
|
173
|
+
},
|
|
174
|
+
useMap: function(key, entries) {
|
|
175
|
+
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.`);
|
|
176
|
+
const value = useStorageValue(key, new LiveMap(null != entries ? entries : void 0));
|
|
177
|
+
return "ok" === value.status ? value.value : (deprecateIf("notfound" === value.status, `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.`),
|
|
178
|
+
null);
|
|
179
|
+
},
|
|
180
|
+
useMyPresence: function() {
|
|
181
|
+
const room = useRoom(), presence = room.getPresence(), rerender = useRerender();
|
|
182
|
+
return React.useEffect((() => {
|
|
183
|
+
const unsubscribe = room.subscribe("my-presence", rerender);
|
|
184
|
+
return () => {
|
|
185
|
+
unsubscribe();
|
|
186
|
+
};
|
|
187
|
+
}), [ room ]), [ presence, React.useCallback(((overrides, options) => room.updatePresence(overrides, options)), [ room ]) ];
|
|
188
|
+
},
|
|
189
|
+
useObject: function(key, initialData) {
|
|
190
|
+
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.`);
|
|
191
|
+
const value = useStorageValue(key, new LiveObject(initialData));
|
|
192
|
+
return "ok" === value.status ? value.value : (deprecateIf("notfound" === value.status, `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.`),
|
|
193
|
+
null);
|
|
194
|
+
},
|
|
195
|
+
useOthers: function() {
|
|
196
|
+
const room = useRoom(), rerender = useRerender();
|
|
197
|
+
return React.useEffect((() => {
|
|
198
|
+
const unsubscribe = room.subscribe("others", rerender);
|
|
199
|
+
return () => {
|
|
200
|
+
unsubscribe();
|
|
201
|
+
};
|
|
202
|
+
}), [ room ]), room.getOthers();
|
|
203
|
+
},
|
|
204
|
+
useRedo: function() {
|
|
205
|
+
return useHistory().redo;
|
|
206
|
+
},
|
|
207
|
+
useRoom: useRoom,
|
|
208
|
+
useSelf: function() {
|
|
209
|
+
const room = useRoom(), rerender = useRerender();
|
|
210
|
+
return React.useEffect((() => {
|
|
211
|
+
const unsubscribePresence = room.subscribe("my-presence", rerender), unsubscribeConnection = room.subscribe("connection", rerender);
|
|
212
|
+
return () => {
|
|
213
|
+
unsubscribePresence(), unsubscribeConnection();
|
|
214
|
+
};
|
|
215
|
+
}), [ room ]), room.getSelf();
|
|
216
|
+
},
|
|
217
|
+
useStorage: useStorage,
|
|
218
|
+
useUndo: function() {
|
|
219
|
+
return useHistory().undo;
|
|
220
|
+
},
|
|
221
|
+
useUpdateMyPresence: function() {
|
|
222
|
+
const room = useRoom();
|
|
223
|
+
return React.useCallback(((overrides, options) => {
|
|
224
|
+
room.updatePresence(overrides, options);
|
|
225
|
+
}), [ room ]);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}();
|
|
229
|
+
|
|
230
|
+
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,21 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/react",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.5",
|
|
4
4
|
"description": "A set of React hooks and providers to use Liveblocks declaratively.",
|
|
5
5
|
"main": "./index.js",
|
|
6
|
+
"module": "./index.mjs",
|
|
6
7
|
"types": "./index.d.ts",
|
|
7
8
|
"files": [
|
|
8
9
|
"**"
|
|
9
10
|
],
|
|
10
|
-
"exports": {
|
|
11
|
-
"./package.json": "./package.json",
|
|
12
|
-
".": {
|
|
13
|
-
"types": "./index.d.ts",
|
|
14
|
-
"module": "./esm/index.js",
|
|
15
|
-
"import": "./esm/index.mjs",
|
|
16
|
-
"default": "./index.js"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
11
|
"keywords": [
|
|
20
12
|
"react",
|
|
21
13
|
"liveblocks",
|
|
@@ -29,22 +21,24 @@
|
|
|
29
21
|
"scripts": {
|
|
30
22
|
"build": "rollup -c && cp ./package.json ./README.md ./lib",
|
|
31
23
|
"start": "rollup -c -w",
|
|
24
|
+
"format": "eslint --fix src/ && prettier --write src/",
|
|
32
25
|
"lint": "eslint src/",
|
|
33
26
|
"test": "jest --watch",
|
|
34
27
|
"test-ci": "jest"
|
|
35
28
|
},
|
|
36
29
|
"license": "Apache-2.0",
|
|
37
30
|
"peerDependencies": {
|
|
38
|
-
"@liveblocks/client": "0.16.
|
|
31
|
+
"@liveblocks/client": "0.16.5",
|
|
39
32
|
"react": "^16.14.0 || ^17 || ^18"
|
|
40
33
|
},
|
|
41
34
|
"devDependencies": {
|
|
42
|
-
"@babel/core": "^7.
|
|
43
|
-
"@babel/preset-env": "^7.
|
|
44
|
-
"@babel/preset-react": "^7.
|
|
45
|
-
"@babel/preset-typescript": "^7.
|
|
35
|
+
"@babel/core": "^7.17.10",
|
|
36
|
+
"@babel/preset-env": "^7.17.10",
|
|
37
|
+
"@babel/preset-react": "^7.16.7",
|
|
38
|
+
"@babel/preset-typescript": "^7.16.7",
|
|
46
39
|
"@rollup/plugin-babel": "^5.3.1",
|
|
47
40
|
"@rollup/plugin-node-resolve": "^11.2.1",
|
|
41
|
+
"@rollup/plugin-replace": "^4.0.0",
|
|
48
42
|
"@rollup/plugin-typescript": "^8.3.1",
|
|
49
43
|
"@testing-library/jest-dom": "^5.11.9",
|
|
50
44
|
"@testing-library/react": "^13.1.1",
|
|
@@ -53,16 +47,16 @@
|
|
|
53
47
|
"@types/react-dom": "^17.0.14",
|
|
54
48
|
"@typescript-eslint/eslint-plugin": "^5.18.0",
|
|
55
49
|
"@typescript-eslint/parser": "^5.18.0",
|
|
56
|
-
"babel-core": "^6.26.3",
|
|
57
|
-
"babel-jest": "^26.6.3",
|
|
58
|
-
"babel-runtime": "^6.26.0",
|
|
59
50
|
"eslint": "^8.12.0",
|
|
51
|
+
"eslint-plugin-import": "^2.26.0",
|
|
52
|
+
"eslint-plugin-simple-import-sort": "^7.0.0",
|
|
60
53
|
"jest": "^26.6.3",
|
|
61
54
|
"msw": "^0.27.1",
|
|
62
55
|
"react-error-boundary": "^3.1.1",
|
|
63
56
|
"rollup": "^2.39.0",
|
|
57
|
+
"rollup-plugin-command": "^1.1.3",
|
|
64
58
|
"rollup-plugin-dts": "^4.2.0",
|
|
65
|
-
"rollup-plugin-
|
|
59
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
66
60
|
"typescript": "^4.1.5",
|
|
67
61
|
"whatwg-fetch": "^3.6.2"
|
|
68
62
|
},
|
|
@@ -70,5 +64,6 @@
|
|
|
70
64
|
"type": "git",
|
|
71
65
|
"url": "https://github.com/liveblocks/liveblocks.git",
|
|
72
66
|
"directory": "packages/liveblocks-react"
|
|
73
|
-
}
|
|
67
|
+
},
|
|
68
|
+
"sideEffects": false
|
|
74
69
|
}
|
package/esm/index.js
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { useReducer } from 'react';
|
|
3
|
-
import { LiveMap, LiveList, LiveObject } from '@liveblocks/client';
|
|
4
|
-
|
|
5
|
-
const ClientContext = React.createContext(null);
|
|
6
|
-
function LiveblocksProvider(props) {
|
|
7
|
-
return /* @__PURE__ */ React.createElement(ClientContext.Provider, {
|
|
8
|
-
value: props.client
|
|
9
|
-
}, props.children);
|
|
10
|
-
}
|
|
11
|
-
function useClient() {
|
|
12
|
-
const client = React.useContext(ClientContext);
|
|
13
|
-
if (client == null) {
|
|
14
|
-
throw new Error("LiveblocksProvider is missing from the react tree");
|
|
15
|
-
}
|
|
16
|
-
return client;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function useRerender() {
|
|
20
|
-
const [, update] = useReducer((x) => x + 1, 0);
|
|
21
|
-
return update;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const RoomContext = React.createContext(null);
|
|
25
|
-
function RoomProvider(props) {
|
|
26
|
-
const { id: roomId, defaultPresence, defaultStorageRoot } = props;
|
|
27
|
-
if (process.env.NODE_ENV !== "production") {
|
|
28
|
-
if (roomId == null) {
|
|
29
|
-
throw new Error("RoomProvider id property is required. For more information: https://liveblocks.io/docs/errors/liveblocks-react/RoomProvider-id-property-is-required");
|
|
30
|
-
}
|
|
31
|
-
if (typeof roomId !== "string") {
|
|
32
|
-
throw new Error("RoomProvider id property should be a string.");
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
const client = useClient();
|
|
36
|
-
const [room, setRoom] = React.useState(() => client.enter(roomId, {
|
|
37
|
-
defaultPresence: defaultPresence ? defaultPresence() : void 0,
|
|
38
|
-
defaultStorageRoot,
|
|
39
|
-
DO_NOT_USE_withoutConnecting: typeof window === "undefined"
|
|
40
|
-
}));
|
|
41
|
-
React.useEffect(() => {
|
|
42
|
-
setRoom(client.enter(roomId, {
|
|
43
|
-
defaultPresence: defaultPresence ? defaultPresence() : void 0,
|
|
44
|
-
defaultStorageRoot,
|
|
45
|
-
DO_NOT_USE_withoutConnecting: typeof window === "undefined"
|
|
46
|
-
}));
|
|
47
|
-
return () => {
|
|
48
|
-
client.leave(roomId);
|
|
49
|
-
};
|
|
50
|
-
}, [client, roomId]);
|
|
51
|
-
return /* @__PURE__ */ React.createElement(RoomContext.Provider, {
|
|
52
|
-
value: room
|
|
53
|
-
}, props.children);
|
|
54
|
-
}
|
|
55
|
-
function useRoom() {
|
|
56
|
-
const room = React.useContext(RoomContext);
|
|
57
|
-
if (room == null) {
|
|
58
|
-
throw new Error("RoomProvider is missing from the react tree");
|
|
59
|
-
}
|
|
60
|
-
return room;
|
|
61
|
-
}
|
|
62
|
-
function useMyPresence() {
|
|
63
|
-
const room = useRoom();
|
|
64
|
-
const presence = room.getPresence();
|
|
65
|
-
const rerender = useRerender();
|
|
66
|
-
React.useEffect(() => {
|
|
67
|
-
const unsubscribe = room.subscribe("my-presence", rerender);
|
|
68
|
-
return () => {
|
|
69
|
-
unsubscribe();
|
|
70
|
-
};
|
|
71
|
-
}, [room]);
|
|
72
|
-
const setPresence = React.useCallback((overrides, options) => room.updatePresence(overrides, options), [room]);
|
|
73
|
-
return [presence, setPresence];
|
|
74
|
-
}
|
|
75
|
-
function useUpdateMyPresence() {
|
|
76
|
-
const room = useRoom();
|
|
77
|
-
return React.useCallback((overrides, options) => {
|
|
78
|
-
room.updatePresence(overrides, options);
|
|
79
|
-
}, [room]);
|
|
80
|
-
}
|
|
81
|
-
function useOthers() {
|
|
82
|
-
const room = useRoom();
|
|
83
|
-
const rerender = useRerender();
|
|
84
|
-
React.useEffect(() => {
|
|
85
|
-
const unsubscribe = room.subscribe("others", rerender);
|
|
86
|
-
return () => {
|
|
87
|
-
unsubscribe();
|
|
88
|
-
};
|
|
89
|
-
}, [room]);
|
|
90
|
-
return room.getOthers();
|
|
91
|
-
}
|
|
92
|
-
function useBroadcastEvent() {
|
|
93
|
-
const room = useRoom();
|
|
94
|
-
return React.useCallback((event, options = { shouldQueueEventIfNotReady: false }) => {
|
|
95
|
-
room.broadcastEvent(event, options);
|
|
96
|
-
}, [room]);
|
|
97
|
-
}
|
|
98
|
-
function useErrorListener(callback) {
|
|
99
|
-
const room = useRoom();
|
|
100
|
-
const savedCallback = React.useRef(callback);
|
|
101
|
-
React.useEffect(() => {
|
|
102
|
-
savedCallback.current = callback;
|
|
103
|
-
});
|
|
104
|
-
React.useEffect(() => {
|
|
105
|
-
const listener = (e) => savedCallback.current(e);
|
|
106
|
-
const unsubscribe = room.subscribe("error", listener);
|
|
107
|
-
return () => {
|
|
108
|
-
unsubscribe();
|
|
109
|
-
};
|
|
110
|
-
}, [room]);
|
|
111
|
-
}
|
|
112
|
-
function useEventListener(callback) {
|
|
113
|
-
const room = useRoom();
|
|
114
|
-
const savedCallback = React.useRef(callback);
|
|
115
|
-
React.useEffect(() => {
|
|
116
|
-
savedCallback.current = callback;
|
|
117
|
-
});
|
|
118
|
-
React.useEffect(() => {
|
|
119
|
-
const listener = (e) => savedCallback.current(e);
|
|
120
|
-
const unsubscribe = room.subscribe("event", listener);
|
|
121
|
-
return () => {
|
|
122
|
-
unsubscribe();
|
|
123
|
-
};
|
|
124
|
-
}, [room]);
|
|
125
|
-
}
|
|
126
|
-
function useSelf() {
|
|
127
|
-
const room = useRoom();
|
|
128
|
-
const rerender = useRerender();
|
|
129
|
-
React.useEffect(() => {
|
|
130
|
-
const unsubscribePresence = room.subscribe("my-presence", rerender);
|
|
131
|
-
const unsubscribeConnection = room.subscribe("connection", rerender);
|
|
132
|
-
return () => {
|
|
133
|
-
unsubscribePresence();
|
|
134
|
-
unsubscribeConnection();
|
|
135
|
-
};
|
|
136
|
-
}, [room]);
|
|
137
|
-
return room.getSelf();
|
|
138
|
-
}
|
|
139
|
-
function useStorage() {
|
|
140
|
-
const room = useRoom();
|
|
141
|
-
const [root, setState] = React.useState(null);
|
|
142
|
-
React.useEffect(() => {
|
|
143
|
-
let didCancel = false;
|
|
144
|
-
async function fetchStorage() {
|
|
145
|
-
const storage = await room.getStorage();
|
|
146
|
-
if (!didCancel) {
|
|
147
|
-
setState(storage.root);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
fetchStorage();
|
|
151
|
-
return () => {
|
|
152
|
-
didCancel = true;
|
|
153
|
-
};
|
|
154
|
-
}, [room]);
|
|
155
|
-
return [root];
|
|
156
|
-
}
|
|
157
|
-
function useMap(key, entries) {
|
|
158
|
-
return useCrdt(key, new LiveMap(entries));
|
|
159
|
-
}
|
|
160
|
-
function useList(key, items) {
|
|
161
|
-
return useCrdt(key, new LiveList(items));
|
|
162
|
-
}
|
|
163
|
-
function useObject(key, initialData) {
|
|
164
|
-
return useCrdt(key, new LiveObject(initialData));
|
|
165
|
-
}
|
|
166
|
-
function useUndo() {
|
|
167
|
-
return useRoom().history.undo;
|
|
168
|
-
}
|
|
169
|
-
function useRedo() {
|
|
170
|
-
return useRoom().history.redo;
|
|
171
|
-
}
|
|
172
|
-
function useBatch() {
|
|
173
|
-
return useRoom().batch;
|
|
174
|
-
}
|
|
175
|
-
function useHistory() {
|
|
176
|
-
return useRoom().history;
|
|
177
|
-
}
|
|
178
|
-
function useCrdt(key, initialCrdt) {
|
|
179
|
-
var _a;
|
|
180
|
-
const room = useRoom();
|
|
181
|
-
const [root] = useStorage();
|
|
182
|
-
const rerender = useRerender();
|
|
183
|
-
React.useEffect(() => {
|
|
184
|
-
if (root == null) {
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
let crdt = root.get(key);
|
|
188
|
-
if (crdt == null) {
|
|
189
|
-
crdt = initialCrdt;
|
|
190
|
-
root.set(key, crdt);
|
|
191
|
-
}
|
|
192
|
-
function onRootChange() {
|
|
193
|
-
const newCrdt = root.get(key);
|
|
194
|
-
if (newCrdt !== crdt) {
|
|
195
|
-
unsubscribeCrdt();
|
|
196
|
-
crdt = newCrdt;
|
|
197
|
-
unsubscribeCrdt = room.subscribe(crdt, rerender);
|
|
198
|
-
rerender();
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
let unsubscribeCrdt = room.subscribe(crdt, rerender);
|
|
202
|
-
const unsubscribeRoot = room.subscribe(root, onRootChange);
|
|
203
|
-
rerender();
|
|
204
|
-
return () => {
|
|
205
|
-
unsubscribeRoot();
|
|
206
|
-
unsubscribeCrdt();
|
|
207
|
-
};
|
|
208
|
-
}, [root, room]);
|
|
209
|
-
return (_a = root == null ? void 0 : root.get(key)) != null ? _a : null;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
export { LiveblocksProvider, RoomProvider, useBatch, useBroadcastEvent, useClient, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
|