@liveblocks/react 0.15.11 → 0.16.2

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/README.md CHANGED
@@ -18,6 +18,8 @@
18
18
  </a>
19
19
  </p>
20
20
 
21
+ A set of [React](https://reactjs.org/) hooks and providers to use [Liveblocks](https://liveblocks.io) declaratively.
22
+
21
23
  ## Installation
22
24
 
23
25
  ```
@@ -1,14 +1,8 @@
1
- import { LiveMap, LiveList, LiveObject } from '@liveblocks/client';
2
1
  import * as React from 'react';
3
2
  import { useReducer } from 'react';
4
-
5
- function useRerender() {
6
- const [, update] = useReducer((x) => x + 1, 0);
7
- return update;
8
- }
3
+ import { LiveMap, LiveList, LiveObject } from '@liveblocks/client';
9
4
 
10
5
  const ClientContext = React.createContext(null);
11
- const RoomContext = React.createContext(null);
12
6
  function LiveblocksProvider(props) {
13
7
  return /* @__PURE__ */ React.createElement(ClientContext.Provider, {
14
8
  value: props.client
@@ -21,6 +15,13 @@ function useClient() {
21
15
  }
22
16
  return client;
23
17
  }
18
+
19
+ function useRerender() {
20
+ const [, update] = useReducer((x) => x + 1, 0);
21
+ return update;
22
+ }
23
+
24
+ const RoomContext = React.createContext(null);
24
25
  function RoomProvider({
25
26
  id,
26
27
  children,
@@ -36,16 +37,21 @@ function RoomProvider({
36
37
  }
37
38
  }
38
39
  const client = useClient();
40
+ const [room, setRoom] = React.useState(() => client.enter(id, {
41
+ defaultPresence: defaultPresence ? defaultPresence() : void 0,
42
+ defaultStorageRoot,
43
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
44
+ }));
39
45
  React.useEffect(() => {
46
+ setRoom(client.enter(id, {
47
+ defaultPresence: defaultPresence ? defaultPresence() : void 0,
48
+ defaultStorageRoot,
49
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
50
+ }));
40
51
  return () => {
41
52
  client.leave(id);
42
53
  };
43
54
  }, [client, id]);
44
- const room = client.getRoom(id) || client.enter(id, {
45
- defaultPresence: defaultPresence ? defaultPresence() : void 0,
46
- defaultStorageRoot,
47
- DO_NOT_USE_withoutConnecting: typeof window === "undefined"
48
- });
49
55
  return /* @__PURE__ */ React.createElement(RoomContext.Provider, {
50
56
  value: room
51
57
  }, children);
@@ -207,4 +213,4 @@ function useCrdt(key, initialCrdt) {
207
213
  return (_a = root == null ? void 0 : root.get(key)) != null ? _a : null;
208
214
  }
209
215
 
210
- export { LiveblocksProvider, RoomProvider, useBatch, useBroadcastEvent, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
216
+ export { LiveblocksProvider, RoomProvider, useBatch, useBroadcastEvent, useClient, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
@@ -1,14 +1,8 @@
1
- import { LiveMap, LiveList, LiveObject } from '@liveblocks/client';
2
1
  import * as React from 'react';
3
2
  import { useReducer } from 'react';
4
-
5
- function useRerender() {
6
- const [, update] = useReducer((x) => x + 1, 0);
7
- return update;
8
- }
3
+ import { LiveMap, LiveList, LiveObject } from '@liveblocks/client';
9
4
 
10
5
  const ClientContext = React.createContext(null);
11
- const RoomContext = React.createContext(null);
12
6
  function LiveblocksProvider(props) {
13
7
  return /* @__PURE__ */ React.createElement(ClientContext.Provider, {
14
8
  value: props.client
@@ -21,6 +15,13 @@ function useClient() {
21
15
  }
22
16
  return client;
23
17
  }
18
+
19
+ function useRerender() {
20
+ const [, update] = useReducer((x) => x + 1, 0);
21
+ return update;
22
+ }
23
+
24
+ const RoomContext = React.createContext(null);
24
25
  function RoomProvider({
25
26
  id,
26
27
  children,
@@ -36,16 +37,21 @@ function RoomProvider({
36
37
  }
37
38
  }
38
39
  const client = useClient();
40
+ const [room, setRoom] = React.useState(() => client.enter(id, {
41
+ defaultPresence: defaultPresence ? defaultPresence() : void 0,
42
+ defaultStorageRoot,
43
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
44
+ }));
39
45
  React.useEffect(() => {
46
+ setRoom(client.enter(id, {
47
+ defaultPresence: defaultPresence ? defaultPresence() : void 0,
48
+ defaultStorageRoot,
49
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
50
+ }));
40
51
  return () => {
41
52
  client.leave(id);
42
53
  };
43
54
  }, [client, id]);
44
- const room = client.getRoom(id) || client.enter(id, {
45
- defaultPresence: defaultPresence ? defaultPresence() : void 0,
46
- defaultStorageRoot,
47
- DO_NOT_USE_withoutConnecting: typeof window === "undefined"
48
- });
49
55
  return /* @__PURE__ */ React.createElement(RoomContext.Provider, {
50
56
  value: room
51
57
  }, children);
@@ -207,4 +213,4 @@ function useCrdt(key, initialCrdt) {
207
213
  return (_a = root == null ? void 0 : root.get(key)) != null ? _a : null;
208
214
  }
209
215
 
210
- export { LiveblocksProvider, RoomProvider, useBatch, useBroadcastEvent, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
216
+ export { LiveblocksProvider, RoomProvider, useBatch, useBroadcastEvent, useClient, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
@@ -1,5 +1,6 @@
1
- import { Room, Presence, Others, BroadcastOptions, User, LiveObject, LiveMap, LiveList, Client } from '@liveblocks/client';
2
1
  import * as React from 'react';
2
+ import { Client, Room, Presence, Others, BroadcastOptions, Json, User, LiveObject, Lson, LiveMap, LiveList, LsonObject, History } from '@liveblocks/client';
3
+ export { Json, JsonObject } from '@liveblocks/client';
3
4
 
4
5
  declare type LiveblocksProviderProps = {
5
6
  children: React.ReactNode;
@@ -9,6 +10,12 @@ declare type LiveblocksProviderProps = {
9
10
  * Makes the Liveblocks client available in the component hierarchy below.
10
11
  */
11
12
  declare function LiveblocksProvider(props: LiveblocksProviderProps): JSX.Element;
13
+ /**
14
+ * Returns the Client of the nearest LiveblocksProvider above in the React
15
+ * component tree.
16
+ */
17
+ declare function useClient(): Client;
18
+
12
19
  declare type RoomProviderProps<TStorageRoot> = {
13
20
  /**
14
21
  * The id of the room you want to connect to
@@ -29,7 +36,8 @@ declare type RoomProviderProps<TStorageRoot> = {
29
36
  */
30
37
  declare function RoomProvider<TStorageRoot>({ id, children, defaultPresence, defaultStorageRoot, }: RoomProviderProps<TStorageRoot>): JSX.Element;
31
38
  /**
32
- * Returns the room of the nearest RoomProvider above in the react component tree
39
+ * Returns the Room of the nearest RoomProvider above in the React component
40
+ * tree.
33
41
  */
34
42
  declare function useRoom(): Room;
35
43
  /**
@@ -108,7 +116,7 @@ declare function useBroadcastEvent(): (event: any, options?: BroadcastOptions) =
108
116
  * console.error(er);
109
117
  * })
110
118
  */
111
- declare function useErrorListener(callback: (er: Error) => void): void;
119
+ declare function useErrorListener(callback: (err: Error) => void): void;
112
120
  /**
113
121
  * useEventListener is a react hook that lets you react to event broadcasted by other users in the room.
114
122
  *
@@ -121,7 +129,7 @@ declare function useErrorListener(callback: (er: Error) => void): void;
121
129
  * }
122
130
  * });
123
131
  */
124
- declare function useEventListener<TEvent>(callback: ({ connectionId, event, }: {
132
+ declare function useEventListener<TEvent extends Json>(callback: ({ connectionId, event, }: {
125
133
  connectionId: number;
126
134
  event: TEvent;
127
135
  }) => void): void;
@@ -149,7 +157,7 @@ declare function useStorage<TRoot extends Record<string, any>>(): [
149
157
  * const emptyMap = useMap("mapA");
150
158
  * const mapWithItems = useMap("mapB", [["keyA", "valueA"], ["keyB", "valueB"]]);
151
159
  */
152
- declare function useMap<TKey extends string, TValue>(key: string, entries?: readonly (readonly [TKey, TValue])[] | null | undefined): LiveMap<TKey, TValue> | null;
160
+ declare function useMap<TKey extends string, TValue extends Lson>(key: string, entries?: readonly (readonly [TKey, TValue])[] | null | undefined): LiveMap<TKey, TValue> | null;
153
161
  /**
154
162
  * Returns the LiveList associated with the provided key. If the LiveList does not exist, a new LiveList will be created.
155
163
  * 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.
@@ -162,7 +170,7 @@ declare function useMap<TKey extends string, TValue>(key: string, entries?: read
162
170
  * const emptyList = useList("listA");
163
171
  * const listWithItems = useList("listB", ["a", "b", "c"]);
164
172
  */
165
- declare function useList<TValue>(key: string, items?: TValue[] | undefined): LiveList<TValue> | null;
173
+ declare function useList<TValue extends Lson>(key: string, items?: TValue[] | undefined): LiveList<TValue> | null;
166
174
  /**
167
175
  * Returns the LiveObject associated with the provided key. If the LiveObject does not exist, it will be created with the initialData parameter.
168
176
  * 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,7 +185,7 @@ declare function useList<TValue>(key: string, items?: TValue[] | undefined): Liv
177
185
  * website: "https://liveblocks.io"
178
186
  * });
179
187
  */
180
- declare function useObject<TData>(key: string, initialData?: TData): LiveObject<TData> | null;
188
+ declare function useObject<TData extends LsonObject>(key: string, initialData?: TData): LiveObject<TData> | null;
181
189
  /**
182
190
  * Returns a function that undoes the last operation executed by the current client.
183
191
  * It does not impact operations made by other clients.
@@ -194,15 +202,10 @@ declare function useRedo(): () => void;
194
202
  * All the modifications are merged in a single history item (undo/redo).
195
203
  * All the subscribers are called only after the batch is over.
196
204
  */
197
- declare function useBatch(): (fn: () => void) => void;
205
+ declare function useBatch(): (callback: () => void) => void;
198
206
  /**
199
207
  * Returns the room.history
200
208
  */
201
- declare function useHistory(): {
202
- undo: () => void;
203
- redo: () => void;
204
- pause: () => void;
205
- resume: () => void;
206
- };
209
+ declare function useHistory(): History;
207
210
 
208
- export { LiveblocksProvider, RoomProvider, useBatch, useBroadcastEvent, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
211
+ export { LiveblocksProvider, RoomProvider, useBatch, useBroadcastEvent, useClient, useErrorListener, useEventListener, useHistory, useList, useMap, useMyPresence, useObject, useOthers, useRedo, useRoom, useSelf, useStorage, useUndo, useUpdateMyPresence };
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var client = require('@liveblocks/client');
6
5
  var React = require('react');
6
+ var client = require('@liveblocks/client');
7
7
 
8
8
  function _interopNamespace(e) {
9
9
  if (e && e.__esModule) return e;
@@ -25,6 +25,22 @@ function _interopNamespace(e) {
25
25
 
26
26
  var React__namespace = /*#__PURE__*/_interopNamespace(React);
27
27
 
28
+ var ClientContext = React__namespace.createContext(null);
29
+ function LiveblocksProvider(props) {
30
+ return React__namespace.createElement(ClientContext.Provider, {
31
+ value: props.client
32
+ }, props.children);
33
+ }
34
+ function useClient() {
35
+ var client = React__namespace.useContext(ClientContext);
36
+
37
+ if (client == null) {
38
+ throw new Error("LiveblocksProvider is missing from the react tree");
39
+ }
40
+
41
+ return client;
42
+ }
43
+
28
44
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
29
45
  try {
30
46
  var info = gen[key](arg);
@@ -70,24 +86,7 @@ function useRerender() {
70
86
  return update;
71
87
  }
72
88
 
73
- var ClientContext = React__namespace.createContext(null);
74
89
  var RoomContext = React__namespace.createContext(null);
75
- function LiveblocksProvider(props) {
76
- return React__namespace.createElement(ClientContext.Provider, {
77
- value: props.client
78
- }, props.children);
79
- }
80
-
81
- function useClient() {
82
- var client = React__namespace.useContext(ClientContext);
83
-
84
- if (client == null) {
85
- throw new Error("LiveblocksProvider is missing from the react tree");
86
- }
87
-
88
- return client;
89
- }
90
-
91
90
  function RoomProvider(_ref) {
92
91
  var id = _ref.id,
93
92
  children = _ref.children,
@@ -105,16 +104,27 @@ function RoomProvider(_ref) {
105
104
  }
106
105
 
107
106
  var client = useClient();
107
+
108
+ var _React$useState = React__namespace.useState(function () {
109
+ return client.enter(id, {
110
+ defaultPresence: defaultPresence ? defaultPresence() : undefined,
111
+ defaultStorageRoot: defaultStorageRoot,
112
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
113
+ });
114
+ }),
115
+ room = _React$useState[0],
116
+ setRoom = _React$useState[1];
117
+
108
118
  React__namespace.useEffect(function () {
119
+ setRoom(client.enter(id, {
120
+ defaultPresence: defaultPresence ? defaultPresence() : undefined,
121
+ defaultStorageRoot: defaultStorageRoot,
122
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
123
+ }));
109
124
  return function () {
110
125
  client.leave(id);
111
126
  };
112
127
  }, [client, id]);
113
- var room = client.getRoom(id) || client.enter(id, {
114
- defaultPresence: defaultPresence ? defaultPresence() : undefined,
115
- defaultStorageRoot: defaultStorageRoot,
116
- DO_NOT_USE_withoutConnecting: typeof window === "undefined"
117
- });
118
128
  return React__namespace.createElement(RoomContext.Provider, {
119
129
  value: room
120
130
  }, children);
@@ -222,9 +232,9 @@ function useSelf() {
222
232
  function useStorage() {
223
233
  var room = useRoom();
224
234
 
225
- var _React$useState = React__namespace.useState(null),
226
- root = _React$useState[0],
227
- setState = _React$useState[1];
235
+ var _React$useState2 = React__namespace.useState(null),
236
+ root = _React$useState2[0],
237
+ setState = _React$useState2[1];
228
238
 
229
239
  React__namespace.useEffect(function () {
230
240
  var didCancel = false;
@@ -336,6 +346,7 @@ exports.LiveblocksProvider = LiveblocksProvider;
336
346
  exports.RoomProvider = RoomProvider;
337
347
  exports.useBatch = useBatch;
338
348
  exports.useBroadcastEvent = useBroadcastEvent;
349
+ exports.useClient = useClient;
339
350
  exports.useErrorListener = useErrorListener;
340
351
  exports.useEventListener = useEventListener;
341
352
  exports.useHistory = useHistory;
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@liveblocks/react",
3
- "version": "0.15.11",
4
- "description": "",
5
- "main": "./lib/index.js",
6
- "types": "./lib/index.d.ts",
3
+ "version": "0.16.2",
4
+ "description": "A set of React hooks and providers to use Liveblocks declaratively.",
5
+ "main": "./index.js",
6
+ "types": "./index.d.ts",
7
7
  "files": [
8
- "lib/**"
8
+ "**"
9
9
  ],
10
10
  "exports": {
11
11
  "./package.json": "./package.json",
12
12
  ".": {
13
- "types": "./lib/index.d.ts",
14
- "module": "./lib/esm/index.js",
15
- "import": "./lib/esm/index.mjs",
16
- "default": "./lib/index.js"
13
+ "types": "./index.d.ts",
14
+ "module": "./esm/index.js",
15
+ "import": "./esm/index.mjs",
16
+ "default": "./index.js"
17
17
  }
18
18
  },
19
19
  "keywords": [
@@ -27,13 +27,13 @@
27
27
  "url": "https://github.com/liveblocks/liveblocks/issues"
28
28
  },
29
29
  "scripts": {
30
- "build": "rollup -c",
30
+ "build": "rollup -c && cp ./package.json ./README.md ./lib",
31
31
  "start": "rollup -c -w",
32
32
  "test": "jest --watch"
33
33
  },
34
34
  "license": "Apache-2.0",
35
35
  "peerDependencies": {
36
- "@liveblocks/client": "0.15.11",
36
+ "@liveblocks/client": "0.16.2",
37
37
  "react": "^16.14.0 || ^17 || ^18"
38
38
  },
39
39
  "devDependencies": {
@@ -45,7 +45,7 @@
45
45
  "@rollup/plugin-node-resolve": "^11.2.1",
46
46
  "@rollup/plugin-typescript": "^8.3.1",
47
47
  "@testing-library/jest-dom": "^5.11.9",
48
- "@testing-library/react": "^11.2.5",
48
+ "@testing-library/react": "^13.1.1",
49
49
  "@types/jest": "^26.0.20",
50
50
  "@types/react": "^16.14.0",
51
51
  "@types/react-dom": "^17.0.14",