@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/esm/index.mjs 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 };