@liveblocks/redux 1.1.0 → 1.1.1-dual2

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.
@@ -0,0 +1,90 @@
1
+ import { JsonObject, BaseUserMeta, Client, User, Status } from '@liveblocks/client';
2
+ import { LegacyConnectionStatus } from '@liveblocks/core';
3
+ import { StoreEnhancer } from 'redux';
4
+
5
+ declare type Mapping<T> = {
6
+ [K in keyof T]?: boolean;
7
+ };
8
+ declare type LiveblocksContext<TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = {
9
+ /**
10
+ * Other users in the room. Empty no room is currently synced
11
+ */
12
+ readonly others: readonly User<TPresence, TUserMeta>[];
13
+ /**
14
+ * Whether or not the room storage is currently loading
15
+ */
16
+ readonly isStorageLoading: boolean;
17
+ /**
18
+ * Legacy connection status of the room.
19
+ *
20
+ * @deprecated This API will be removed in a future version of Liveblocks.
21
+ * Prefer using the newer `.status` property.
22
+ *
23
+ * We recommend making the following changes if you use these APIs:
24
+ *
25
+ * OLD STATUSES NEW STATUSES
26
+ * closed --> initial
27
+ * authenticating --> connecting
28
+ * connecting --> connecting
29
+ * open --> connected
30
+ * unavailable --> reconnecting
31
+ * failed --> disconnected
32
+ */
33
+ readonly connection: LegacyConnectionStatus;
34
+ /**
35
+ * Connection status of the room.
36
+ */
37
+ readonly status: Status;
38
+ };
39
+ /**
40
+ * @deprecated Please rename to WithLiveblocks<...>
41
+ */
42
+ declare type LiveblocksState<TState, TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = WithLiveblocks<TState, TPresence, TUserMeta>;
43
+ /**
44
+ * Adds the `liveblocks` property to your custom Redux state.
45
+ */
46
+ declare type WithLiveblocks<TState, TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = TState & {
47
+ readonly liveblocks: LiveblocksContext<TPresence, TUserMeta>;
48
+ };
49
+ /**
50
+ * Actions used to interact with Liveblocks
51
+ */
52
+ declare const actions: {
53
+ /**
54
+ * Enters a room and starts sync it with Redux state
55
+ * @param roomId The id of the room
56
+ */
57
+ enterRoom: typeof enterRoom;
58
+ /**
59
+ * Leaves a room and stops sync it with Redux state.
60
+ * @param roomId The id of the room
61
+ */
62
+ leaveRoom: typeof leaveRoom;
63
+ };
64
+ declare function enterRoom(roomId: string): {
65
+ type: string;
66
+ roomId: string;
67
+ };
68
+ declare function leaveRoom(roomId: string): {
69
+ type: string;
70
+ roomId: string;
71
+ };
72
+ /**
73
+ * Redux store enhancer that will make the `liveblocks` key available on your
74
+ * Redux store.
75
+ */
76
+ declare const liveblocksEnhancer: <TState>(options: {
77
+ client: Client;
78
+ storageMapping?: Mapping<TState> | undefined;
79
+ presenceMapping?: Mapping<TState> | undefined;
80
+ }) => StoreEnhancer;
81
+ /**
82
+ * @deprecated Renamed to `liveblocksEnhancer`.
83
+ */
84
+ declare const enhancer: <TState>(options: {
85
+ client: Client;
86
+ storageMapping?: Mapping<TState> | undefined;
87
+ presenceMapping?: Mapping<TState> | undefined;
88
+ }) => StoreEnhancer;
89
+
90
+ export { LiveblocksState, Mapping, WithLiveblocks, actions, enhancer, liveblocksEnhancer };
package/dist/index.mjs ADDED
@@ -0,0 +1,349 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+
21
+ // src/index.ts
22
+ import {
23
+ legacy_patchImmutableObject,
24
+ lsonToJson,
25
+ patchLiveObjectKey
26
+ } from "@liveblocks/core";
27
+
28
+ // src/errors.ts
29
+ var ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
30
+ function missingClient() {
31
+ return new Error(`${ERROR_PREFIX} client is missing`);
32
+ }
33
+ function mappingShouldBeAnObject(mappingType) {
34
+ return new Error(
35
+ `${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`
36
+ );
37
+ }
38
+ function mappingValueShouldBeABoolean(mappingType, key) {
39
+ return new Error(
40
+ `${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`
41
+ );
42
+ }
43
+ function mappingShouldNotHaveTheSameKeys(key) {
44
+ return new Error(
45
+ `${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`
46
+ );
47
+ }
48
+ function mappingToFunctionIsNotAllowed(key) {
49
+ return new Error(
50
+ `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`
51
+ );
52
+ }
53
+
54
+ // src/index.ts
55
+ var ACTION_TYPES = {
56
+ ENTER: "@@LIVEBLOCKS/ENTER",
57
+ LEAVE: "@@LIVEBLOCKS/LEAVE",
58
+ START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
59
+ INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
60
+ PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
61
+ UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
62
+ UPDATE_OTHERS: "@@LIVEBLOCKS/UPDATE_OTHERS"
63
+ };
64
+ var internalEnhancer = (options) => {
65
+ if (process.env.NODE_ENV !== "production" && options.client == null) {
66
+ throw missingClient();
67
+ }
68
+ const client = options.client;
69
+ const mapping = validateMapping(
70
+ options.storageMapping || {},
71
+ "storageMapping"
72
+ );
73
+ const presenceMapping = validateMapping(
74
+ options.presenceMapping || {},
75
+ "presenceMapping"
76
+ );
77
+ if (process.env.NODE_ENV !== "production") {
78
+ validateNoDuplicateKeys(mapping, presenceMapping);
79
+ }
80
+ return (createStore) => {
81
+ return (reducer, initialState, enhancer2) => {
82
+ let room = null;
83
+ let isPatching = false;
84
+ let storageRoot = null;
85
+ let unsubscribeCallbacks = [];
86
+ const newReducer = (state, action) => {
87
+ switch (action.type) {
88
+ case ACTION_TYPES.PATCH_REDUX_STATE:
89
+ return __spreadValues(__spreadValues({}, state), action.state);
90
+ case ACTION_TYPES.INIT_STORAGE:
91
+ return __spreadProps(__spreadValues(__spreadValues({}, state), action.state), {
92
+ liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
93
+ isStorageLoading: false
94
+ })
95
+ });
96
+ case ACTION_TYPES.START_LOADING_STORAGE:
97
+ return __spreadProps(__spreadValues({}, state), {
98
+ liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
99
+ isStorageLoading: true
100
+ })
101
+ });
102
+ case ACTION_TYPES.UPDATE_CONNECTION: {
103
+ return __spreadProps(__spreadValues({}, state), {
104
+ liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
105
+ connection: action.connection,
106
+ status: action.status
107
+ })
108
+ });
109
+ }
110
+ case ACTION_TYPES.UPDATE_OTHERS: {
111
+ return __spreadProps(__spreadValues({}, state), {
112
+ liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
113
+ others: action.others
114
+ })
115
+ });
116
+ }
117
+ default: {
118
+ const newState = reducer(state, action);
119
+ if (room) {
120
+ isPatching = true;
121
+ updatePresence(room, state, newState, presenceMapping);
122
+ room.batch(() => {
123
+ if (storageRoot) {
124
+ patchLiveblocksStorage(
125
+ storageRoot,
126
+ state,
127
+ newState,
128
+ mapping
129
+ );
130
+ }
131
+ });
132
+ isPatching = false;
133
+ }
134
+ if (newState.liveblocks == null) {
135
+ return __spreadProps(__spreadValues({}, newState), {
136
+ liveblocks: {
137
+ others: [],
138
+ isStorageLoading: false,
139
+ connection: "closed",
140
+ status: "initial"
141
+ }
142
+ });
143
+ }
144
+ return newState;
145
+ }
146
+ }
147
+ };
148
+ const store = createStore(newReducer, initialState, enhancer2);
149
+ function enterRoom2(roomId) {
150
+ if (storageRoot) {
151
+ return;
152
+ }
153
+ const initialPresence = selectFields(
154
+ store.getState(),
155
+ presenceMapping
156
+ );
157
+ room = client.enter(roomId, { initialPresence });
158
+ unsubscribeCallbacks.push(
159
+ room.events.connection.subscribe(() => {
160
+ store.dispatch({
161
+ type: ACTION_TYPES.UPDATE_CONNECTION,
162
+ connection: room.getConnectionState(),
163
+ status: room.getStatus()
164
+ });
165
+ })
166
+ );
167
+ unsubscribeCallbacks.push(
168
+ room.events.others.subscribe(({ others }) => {
169
+ store.dispatch({
170
+ type: ACTION_TYPES.UPDATE_OTHERS,
171
+ others
172
+ });
173
+ })
174
+ );
175
+ unsubscribeCallbacks.push(
176
+ room.events.me.subscribe(() => {
177
+ if (isPatching === false) {
178
+ store.dispatch({
179
+ type: ACTION_TYPES.PATCH_REDUX_STATE,
180
+ state: selectFields(room.getPresence(), presenceMapping)
181
+ });
182
+ }
183
+ })
184
+ );
185
+ store.dispatch({
186
+ type: ACTION_TYPES.START_LOADING_STORAGE
187
+ });
188
+ void room.getStorage().then(({ root }) => {
189
+ const updates = {};
190
+ room.batch(() => {
191
+ for (const key in mapping) {
192
+ const liveblocksStatePart = root.get(key);
193
+ if (liveblocksStatePart == null) {
194
+ updates[key] = store.getState()[key];
195
+ patchLiveObjectKey(root, key, void 0, store.getState()[key]);
196
+ } else {
197
+ updates[key] = lsonToJson(liveblocksStatePart);
198
+ }
199
+ }
200
+ });
201
+ store.dispatch({
202
+ type: ACTION_TYPES.INIT_STORAGE,
203
+ state: updates
204
+ });
205
+ storageRoot = root;
206
+ unsubscribeCallbacks.push(
207
+ room.subscribe(
208
+ root,
209
+ (updates2) => {
210
+ if (isPatching === false) {
211
+ store.dispatch({
212
+ type: ACTION_TYPES.PATCH_REDUX_STATE,
213
+ state: patchState(
214
+ store.getState(),
215
+ updates2,
216
+ mapping
217
+ )
218
+ });
219
+ }
220
+ },
221
+ { isDeep: true }
222
+ )
223
+ );
224
+ });
225
+ }
226
+ function leaveRoom2(roomId) {
227
+ for (const unsubscribe of unsubscribeCallbacks) {
228
+ unsubscribe();
229
+ }
230
+ storageRoot = null;
231
+ room = null;
232
+ isPatching = false;
233
+ unsubscribeCallbacks = [];
234
+ client.leave(roomId);
235
+ }
236
+ function newDispatch(action) {
237
+ if (action.type === ACTION_TYPES.ENTER) {
238
+ enterRoom2(action.roomId);
239
+ } else if (action.type === ACTION_TYPES.LEAVE) {
240
+ leaveRoom2(action.roomId);
241
+ } else {
242
+ store.dispatch(action);
243
+ }
244
+ }
245
+ return __spreadProps(__spreadValues({}, store), {
246
+ dispatch: newDispatch
247
+ });
248
+ };
249
+ };
250
+ };
251
+ var actions = {
252
+ /**
253
+ * Enters a room and starts sync it with Redux state
254
+ * @param roomId The id of the room
255
+ */
256
+ enterRoom,
257
+ /**
258
+ * Leaves a room and stops sync it with Redux state.
259
+ * @param roomId The id of the room
260
+ */
261
+ leaveRoom
262
+ };
263
+ function enterRoom(roomId) {
264
+ return {
265
+ type: ACTION_TYPES.ENTER,
266
+ roomId
267
+ };
268
+ }
269
+ function leaveRoom(roomId) {
270
+ return {
271
+ type: ACTION_TYPES.LEAVE,
272
+ roomId
273
+ };
274
+ }
275
+ var liveblocksEnhancer = internalEnhancer;
276
+ var enhancer = liveblocksEnhancer;
277
+ function patchLiveblocksStorage(root, oldState, newState, mapping) {
278
+ for (const key in mapping) {
279
+ if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
280
+ throw mappingToFunctionIsNotAllowed("value");
281
+ }
282
+ if (oldState[key] !== newState[key]) {
283
+ const oldVal = oldState[key];
284
+ const newVal = newState[key];
285
+ patchLiveObjectKey(root, key, oldVal, newVal);
286
+ }
287
+ }
288
+ }
289
+ function updatePresence(room, oldState, newState, presenceMapping) {
290
+ for (const key in presenceMapping) {
291
+ if (typeof newState[key] === "function") {
292
+ throw mappingToFunctionIsNotAllowed("value");
293
+ }
294
+ if (oldState[key] !== newState[key]) {
295
+ room.updatePresence({ [key]: newState[key] });
296
+ }
297
+ }
298
+ }
299
+ function isObject(value) {
300
+ return Object.prototype.toString.call(value) === "[object Object]";
301
+ }
302
+ function validateNoDuplicateKeys(storageMapping, presenceMapping) {
303
+ for (const key in storageMapping) {
304
+ if (presenceMapping[key] !== void 0) {
305
+ throw mappingShouldNotHaveTheSameKeys(key);
306
+ }
307
+ }
308
+ }
309
+ function selectFields(presence, mapping) {
310
+ const partialState = {};
311
+ for (const key in mapping) {
312
+ partialState[key] = presence[key];
313
+ }
314
+ return partialState;
315
+ }
316
+ function patchState(state, updates, mapping) {
317
+ const partialState = {};
318
+ for (const key in mapping) {
319
+ partialState[key] = state[key];
320
+ }
321
+ const patched = legacy_patchImmutableObject(partialState, updates);
322
+ const result = {};
323
+ for (const key in mapping) {
324
+ result[key] = patched[key];
325
+ }
326
+ return result;
327
+ }
328
+ function validateMapping(mapping, mappingType) {
329
+ if (process.env.NODE_ENV !== "production") {
330
+ if (!isObject(mapping)) {
331
+ throw mappingShouldBeAnObject(mappingType);
332
+ }
333
+ }
334
+ const result = {};
335
+ for (const key in mapping) {
336
+ if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
337
+ throw mappingValueShouldBeABoolean(mappingType, key);
338
+ }
339
+ if (mapping[key] === true) {
340
+ result[key] = true;
341
+ }
342
+ }
343
+ return result;
344
+ }
345
+ export {
346
+ actions,
347
+ enhancer,
348
+ liveblocksEnhancer
349
+ };
package/package.json CHANGED
@@ -1,10 +1,23 @@
1
1
  {
2
2
  "name": "@liveblocks/redux",
3
- "version": "1.1.0",
3
+ "version": "1.1.1-dual2",
4
4
  "description": "A store enhancer to integrate Liveblocks into Redux stores. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.mts",
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.ts",
16
+ "module": "./dist/index.mjs",
17
+ "default": "./dist/index.js"
18
+ }
19
+ }
20
+ },
8
21
  "files": [
9
22
  "dist/**",
10
23
  "README.md"
@@ -12,14 +25,15 @@
12
25
  "scripts": {
13
26
  "dev": "tsup --watch",
14
27
  "build": "tsup",
15
- "format": "eslint --fix src/; prettier --write src/",
28
+ "format": "(eslint --fix src/ || true) && prettier --write src/",
16
29
  "lint": "eslint src/",
30
+ "lint:package": "publint --strict && attw --pack",
17
31
  "test": "jest --silent --verbose --color=always",
18
32
  "test:watch": "jest --silent --verbose --color=always --watch"
19
33
  },
20
34
  "dependencies": {
21
- "@liveblocks/client": "1.1.0",
22
- "@liveblocks/core": "1.1.0"
35
+ "@liveblocks/client": "1.1.1-dual2",
36
+ "@liveblocks/core": "1.1.1-dual2"
23
37
  },
24
38
  "peerDependencies": {
25
39
  "redux": "^4"