@liveblocks/react 0.15.11-test.1 → 0.16.1

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
@@ -1,12 +1,24 @@
1
1
  <p align="center">
2
2
  <a href="https://liveblocks.io">
3
- <img src="https://liveblocks.io/images/blog/introducing-liveblocks.png">
3
+ <img src="https://raw.githubusercontent.com/liveblocks/liveblocks/main/.github/assets/header.svg" alt="Liveblocks" />
4
4
  </a>
5
5
  </p>
6
6
 
7
- # Liveblocks · [![Twitter Follow](https://shields.io/twitter/follow/liveblocks?label=Follow)](https://twitter.com/liveblocks)
7
+ # `@liveblocks/react`
8
8
 
9
- **At [Liveblocks](https://liveblocks.io), we’re building tools to help companies create world-class collaborative products that attract, engage and retain users.** This repository is a set of open-source packages for building performant and reliable multiplayer experiences.
9
+ <p>
10
+ <a href="https://npmjs.org/package/@liveblocks/react">
11
+ <img src="https://img.shields.io/npm/v/@liveblocks/react?style=flat&label=npm&color=c33" alt="NPM" />
12
+ </a>
13
+ <a href="https://bundlephobia.com/package/@liveblocks/react">
14
+ <img src="https://img.shields.io/bundlephobia/minzip/@liveblocks/react?style=flat&label=size&color=09f" alt="Size" />
15
+ </a>
16
+ <a href="https://github.com/liveblocks/liveblocks/blob/main/LICENSE">
17
+ <img src="https://img.shields.io/github/license/liveblocks/liveblocks?style=flat&label=license&color=f80" alt="License" />
18
+ </a>
19
+ </p>
20
+
21
+ A set of [React](https://reactjs.org/) hooks and providers to use [Liveblocks](https://liveblocks.io) declaratively.
10
22
 
11
23
  ## Installation
12
24
 
package/lib/esm/index.js CHANGED
@@ -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);
@@ -60,12 +66,9 @@ function useRoom() {
60
66
  function useMyPresence() {
61
67
  const room = useRoom();
62
68
  const presence = room.getPresence();
63
- const [, update] = React.useState(0);
69
+ const rerender = useRerender();
64
70
  React.useEffect(() => {
65
- function onMyPresenceChange() {
66
- update((x) => x + 1);
67
- }
68
- const unsubscribe = room.subscribe("my-presence", onMyPresenceChange);
71
+ const unsubscribe = room.subscribe("my-presence", rerender);
69
72
  return () => {
70
73
  unsubscribe();
71
74
  };
@@ -81,12 +84,9 @@ function useUpdateMyPresence() {
81
84
  }
82
85
  function useOthers() {
83
86
  const room = useRoom();
84
- const [, update] = React.useState(0);
87
+ const rerender = useRerender();
85
88
  React.useEffect(() => {
86
- function onOthersChange() {
87
- update((x) => x + 1);
88
- }
89
- const unsubscribe = room.subscribe("others", onOthersChange);
89
+ const unsubscribe = room.subscribe("others", rerender);
90
90
  return () => {
91
91
  unsubscribe();
92
92
  };
@@ -213,4 +213,4 @@ function useCrdt(key, initialCrdt) {
213
213
  return (_a = root == null ? void 0 : root.get(key)) != null ? _a : null;
214
214
  }
215
215
 
216
- 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 };
package/lib/esm/index.mjs CHANGED
@@ -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);
@@ -60,12 +66,9 @@ function useRoom() {
60
66
  function useMyPresence() {
61
67
  const room = useRoom();
62
68
  const presence = room.getPresence();
63
- const [, update] = React.useState(0);
69
+ const rerender = useRerender();
64
70
  React.useEffect(() => {
65
- function onMyPresenceChange() {
66
- update((x) => x + 1);
67
- }
68
- const unsubscribe = room.subscribe("my-presence", onMyPresenceChange);
71
+ const unsubscribe = room.subscribe("my-presence", rerender);
69
72
  return () => {
70
73
  unsubscribe();
71
74
  };
@@ -81,12 +84,9 @@ function useUpdateMyPresence() {
81
84
  }
82
85
  function useOthers() {
83
86
  const room = useRoom();
84
- const [, update] = React.useState(0);
87
+ const rerender = useRerender();
85
88
  React.useEffect(() => {
86
- function onOthersChange() {
87
- update((x) => x + 1);
88
- }
89
- const unsubscribe = room.subscribe("others", onOthersChange);
89
+ const unsubscribe = room.subscribe("others", rerender);
90
90
  return () => {
91
91
  unsubscribe();
92
92
  };
@@ -213,4 +213,4 @@ function useCrdt(key, initialCrdt) {
213
213
  return (_a = root == null ? void 0 : root.get(key)) != null ? _a : null;
214
214
  }
215
215
 
216
- 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 };
package/lib/index.d.ts CHANGED
@@ -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 };
package/lib/index.js CHANGED
@@ -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,25 @@ function RoomProvider(_ref) {
105
104
  }
106
105
 
107
106
  var client = useClient();
107
+
108
+ var _React$useState = React__namespace.useState(client.enter(id, {
109
+ defaultPresence: defaultPresence ? defaultPresence() : undefined,
110
+ defaultStorageRoot: defaultStorageRoot,
111
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
112
+ })),
113
+ room = _React$useState[0],
114
+ setRoom = _React$useState[1];
115
+
108
116
  React__namespace.useEffect(function () {
117
+ setRoom(client.enter(id, {
118
+ defaultPresence: defaultPresence ? defaultPresence() : undefined,
119
+ defaultStorageRoot: defaultStorageRoot,
120
+ DO_NOT_USE_withoutConnecting: typeof window === "undefined"
121
+ }));
109
122
  return function () {
110
123
  client.leave(id);
111
124
  };
112
125
  }, [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
126
  return React__namespace.createElement(RoomContext.Provider, {
119
127
  value: room
120
128
  }, children);
@@ -131,18 +139,9 @@ function useRoom() {
131
139
  function useMyPresence() {
132
140
  var room = useRoom();
133
141
  var presence = room.getPresence();
134
-
135
- var _React$useState = React__namespace.useState(0),
136
- update = _React$useState[1];
137
-
142
+ var rerender = useRerender();
138
143
  React__namespace.useEffect(function () {
139
- function onMyPresenceChange() {
140
- update(function (x) {
141
- return x + 1;
142
- });
143
- }
144
-
145
- var unsubscribe = room.subscribe("my-presence", onMyPresenceChange);
144
+ var unsubscribe = room.subscribe("my-presence", rerender);
146
145
  return function () {
147
146
  unsubscribe();
148
147
  };
@@ -160,18 +159,9 @@ function useUpdateMyPresence() {
160
159
  }
161
160
  function useOthers() {
162
161
  var room = useRoom();
163
-
164
- var _React$useState2 = React__namespace.useState(0),
165
- update = _React$useState2[1];
166
-
162
+ var rerender = useRerender();
167
163
  React__namespace.useEffect(function () {
168
- function onOthersChange() {
169
- update(function (x) {
170
- return x + 1;
171
- });
172
- }
173
-
174
- var unsubscribe = room.subscribe("others", onOthersChange);
164
+ var unsubscribe = room.subscribe("others", rerender);
175
165
  return function () {
176
166
  unsubscribe();
177
167
  };
@@ -240,9 +230,9 @@ function useSelf() {
240
230
  function useStorage() {
241
231
  var room = useRoom();
242
232
 
243
- var _React$useState3 = React__namespace.useState(null),
244
- root = _React$useState3[0],
245
- setState = _React$useState3[1];
233
+ var _React$useState2 = React__namespace.useState(null),
234
+ root = _React$useState2[0],
235
+ setState = _React$useState2[1];
246
236
 
247
237
  React__namespace.useEffect(function () {
248
238
  var didCancel = false;
@@ -354,6 +344,7 @@ exports.LiveblocksProvider = LiveblocksProvider;
354
344
  exports.RoomProvider = RoomProvider;
355
345
  exports.useBatch = useBatch;
356
346
  exports.useBroadcastEvent = useBroadcastEvent;
347
+ exports.useClient = useClient;
357
348
  exports.useErrorListener = useErrorListener;
358
349
  exports.useEventListener = useEventListener;
359
350
  exports.useHistory = useHistory;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@liveblocks/react",
3
- "version": "0.15.11-test.1",
4
- "description": "",
3
+ "version": "0.16.1",
4
+ "description": "A set of React hooks and providers to use Liveblocks declaratively.",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
7
7
  "files": [
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "license": "Apache-2.0",
35
35
  "peerDependencies": {
36
- "@liveblocks/client": "0.15.11-test.1",
37
- "react": "^16.14.0 || ^17"
36
+ "@liveblocks/client": "0.16.1",
37
+ "react": "^16.14.0 || ^17 || ^18"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@babel/core": "^7.12.16",
@@ -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",