@liveblocks/redux 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.mjs ADDED
@@ -0,0 +1,179 @@
1
+ import { patchLiveObjectKey, lsonToJson, patchImmutableObject } from "@liveblocks/client/internal";
2
+
3
+ const ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
4
+
5
+ function mappingValueShouldBeABoolean(mappingType, key) {
6
+ return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
7
+ }
8
+
9
+ function mappingShouldNotHaveTheSameKeys(key) {
10
+ return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
11
+ }
12
+
13
+ function mappingToFunctionIsNotAllowed(key) {
14
+ return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
15
+ }
16
+
17
+ const ACTION_TYPES_ENTER = "@@LIVEBLOCKS/ENTER", ACTION_TYPES_LEAVE = "@@LIVEBLOCKS/LEAVE", ACTION_TYPES_START_LOADING_STORAGE = "@@LIVEBLOCKS/START_LOADING_STORAGE", ACTION_TYPES_INIT_STORAGE = "@@LIVEBLOCKS/INIT_STORAGE", ACTION_TYPES_PATCH_REDUX_STATE = "@@LIVEBLOCKS/PATCH_REDUX_STATE", ACTION_TYPES_UPDATE_CONNECTION = "@@LIVEBLOCKS/UPDATE_CONNECTION", ACTION_TYPES_UPDATE_OTHERS = "@@LIVEBLOCKS/UPDATE_OTHERS", actions = {
18
+ enterRoom: function(roomId, initialState) {
19
+ return {
20
+ type: ACTION_TYPES_ENTER,
21
+ roomId: roomId,
22
+ initialState: initialState
23
+ };
24
+ },
25
+ leaveRoom: function(roomId) {
26
+ return {
27
+ type: ACTION_TYPES_LEAVE,
28
+ roomId: roomId
29
+ };
30
+ }
31
+ };
32
+
33
+ const enhancer = options => {
34
+ if ("production" !== process.env.NODE_ENV && null == options.client) throw new Error(`${ERROR_PREFIX} client is missing`);
35
+ const client = options.client, mapping = validateMapping(options.storageMapping || {}, "storageMapping"), presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
36
+ return "production" !== process.env.NODE_ENV && function(storageMapping, presenceMapping) {
37
+ for (const key in storageMapping) if (void 0 !== presenceMapping[key]) throw mappingShouldNotHaveTheSameKeys(key);
38
+ }(mapping, presenceMapping), createStore => (reducer, initialState, enhancer) => {
39
+ let room = null, isPatching = !1, storageRoot = null, unsubscribeCallbacks = [];
40
+ const store = createStore(((state, action) => {
41
+ switch (action.type) {
42
+ case ACTION_TYPES_PATCH_REDUX_STATE:
43
+ return Object.assign(Object.assign({}, state), action.state);
44
+
45
+ case ACTION_TYPES_INIT_STORAGE:
46
+ return Object.assign(Object.assign(Object.assign({}, state), action.state), {
47
+ liveblocks: Object.assign(Object.assign({}, state.liveblocks), {
48
+ isStorageLoading: !1
49
+ })
50
+ });
51
+
52
+ case ACTION_TYPES_START_LOADING_STORAGE:
53
+ return Object.assign(Object.assign({}, state), {
54
+ liveblocks: Object.assign(Object.assign({}, state.liveblocks), {
55
+ isStorageLoading: !0
56
+ })
57
+ });
58
+
59
+ case ACTION_TYPES_UPDATE_CONNECTION:
60
+ return Object.assign(Object.assign({}, state), {
61
+ liveblocks: Object.assign(Object.assign({}, state.liveblocks), {
62
+ connection: action.connection
63
+ })
64
+ });
65
+
66
+ case ACTION_TYPES_UPDATE_OTHERS:
67
+ return Object.assign(Object.assign({}, state), {
68
+ liveblocks: Object.assign(Object.assign({}, state.liveblocks), {
69
+ others: action.others
70
+ })
71
+ });
72
+
73
+ default:
74
+ {
75
+ const newState = reducer(state, action);
76
+ return room && (isPatching = !0, function(room, oldState, newState, presenceMapping) {
77
+ for (const key in presenceMapping) {
78
+ if ("function" == typeof newState[key]) throw mappingToFunctionIsNotAllowed("value");
79
+ oldState[key] !== newState[key] && room.updatePresence({
80
+ [key]: newState[key]
81
+ });
82
+ }
83
+ }(room, state, newState, presenceMapping), room.batch((() => {
84
+ storageRoot && function(root, oldState, newState, mapping) {
85
+ for (const key in mapping) {
86
+ if ("production" !== process.env.NODE_ENV && "function" == typeof newState[key]) throw mappingToFunctionIsNotAllowed("value");
87
+ oldState[key] !== newState[key] && patchLiveObjectKey(root, key, oldState[key], newState[key]);
88
+ }
89
+ }(storageRoot, state, newState, mapping);
90
+ })), isPatching = !1), null == newState.liveblocks ? Object.assign(Object.assign({}, newState), {
91
+ liveblocks: {
92
+ others: [],
93
+ isStorageLoading: !1,
94
+ connection: "closed"
95
+ }
96
+ }) : newState;
97
+ }
98
+ }
99
+ }), initialState, enhancer);
100
+ return Object.assign(Object.assign({}, store), {
101
+ dispatch: function(action, state) {
102
+ action.type === ACTION_TYPES_ENTER ? function(roomId, storageInitialState = {}, reduxState) {
103
+ storageRoot || (room = client.enter(roomId), function(room, state, mapping) {
104
+ for (const key in mapping) null == room || room.updatePresence({
105
+ [key]: state[key]
106
+ });
107
+ }(room, reduxState, presenceMapping), unsubscribeCallbacks.push(room.subscribe("connection", (() => {
108
+ store.dispatch({
109
+ type: ACTION_TYPES_UPDATE_CONNECTION,
110
+ connection: room.getConnectionState()
111
+ });
112
+ }))), unsubscribeCallbacks.push(room.subscribe("others", (others => {
113
+ store.dispatch({
114
+ type: ACTION_TYPES_UPDATE_OTHERS,
115
+ others: others.toArray()
116
+ });
117
+ }))), unsubscribeCallbacks.push(room.subscribe("my-presence", (() => {
118
+ !1 === isPatching && store.dispatch({
119
+ type: ACTION_TYPES_PATCH_REDUX_STATE,
120
+ state: patchPresenceState(room.getPresence(), presenceMapping)
121
+ });
122
+ }))), store.dispatch({
123
+ type: ACTION_TYPES_START_LOADING_STORAGE
124
+ }), room.getStorage().then((({root: root}) => {
125
+ const updates = {};
126
+ room.batch((() => {
127
+ for (const key in mapping) {
128
+ const liveblocksStatePart = root.get(key);
129
+ null == liveblocksStatePart ? (updates[key] = storageInitialState[key], patchLiveObjectKey(root, key, void 0, storageInitialState[key])) : updates[key] = lsonToJson(liveblocksStatePart);
130
+ }
131
+ })), store.dispatch({
132
+ type: ACTION_TYPES_INIT_STORAGE,
133
+ state: updates
134
+ }), storageRoot = root, unsubscribeCallbacks.push(room.subscribe(root, (updates => {
135
+ !1 === isPatching && store.dispatch({
136
+ type: ACTION_TYPES_PATCH_REDUX_STATE,
137
+ state: patchState(store.getState(), updates, mapping)
138
+ });
139
+ }), {
140
+ isDeep: !0
141
+ }));
142
+ })));
143
+ }(action.roomId, action.initialState, store.getState()) : action.type === ACTION_TYPES_LEAVE ? function(roomId) {
144
+ for (const unsubscribe of unsubscribeCallbacks) unsubscribe();
145
+ storageRoot = null, room = null, isPatching = !1, unsubscribeCallbacks = [], client.leave(roomId);
146
+ }(action.roomId) : store.dispatch(action, state);
147
+ }
148
+ });
149
+ };
150
+ };
151
+
152
+ function patchPresenceState(presence, mapping) {
153
+ const partialState = {};
154
+ for (const key in mapping) partialState[key] = presence[key];
155
+ return partialState;
156
+ }
157
+
158
+ function patchState(state, updates, mapping) {
159
+ const partialState = {};
160
+ for (const key in mapping) partialState[key] = state[key];
161
+ const patched = patchImmutableObject(partialState, updates), result = {};
162
+ for (const key in mapping) result[key] = patched[key];
163
+ return result;
164
+ }
165
+
166
+ function validateMapping(mapping, mappingType) {
167
+ if ("production" !== process.env.NODE_ENV && (value = mapping, "[object Object]" !== Object.prototype.toString.call(value))) throw function(mappingType) {
168
+ return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
169
+ }(mappingType);
170
+ var value;
171
+ const result = {};
172
+ for (const key in mapping) {
173
+ if ("production" !== process.env.NODE_ENV && "boolean" != typeof mapping[key]) throw mappingValueShouldBeABoolean(mappingType, key);
174
+ !0 === mapping[key] && (result[key] = !0);
175
+ }
176
+ return result;
177
+ }
178
+
179
+ export { actions, enhancer };
package/package.json CHANGED
@@ -1,21 +1,13 @@
1
1
  {
2
2
  "name": "@liveblocks/redux",
3
- "version": "0.16.4-beta1",
4
- "sideEffects": false,
3
+ "version": "0.16.5",
5
4
  "description": "A store enhancer to integrate Liveblocks into Redux stores.",
6
5
  "main": "./index.js",
6
+ "module": "./index.mjs",
7
7
  "types": "./index.d.ts",
8
8
  "files": [
9
9
  "**"
10
10
  ],
11
- "exports": {
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
  "redux",
21
13
  "react",
@@ -26,6 +18,7 @@
26
18
  ],
27
19
  "scripts": {
28
20
  "build": "rollup -c && cp ./package.json ./README.md ./lib",
21
+ "format": "eslint --fix src/ test/ && prettier --write src/ test/",
29
22
  "lint": "eslint src/ test/",
30
23
  "test": "jest --watch",
31
24
  "test-ci": "jest",
@@ -38,17 +31,18 @@
38
31
  "directory": "packages/liveblocks-redux"
39
32
  },
40
33
  "peerDependencies": {
41
- "@liveblocks/client": "0.16.4-beta1",
34
+ "@liveblocks/client": "0.16.5",
42
35
  "redux": "^4"
43
36
  },
44
37
  "devDependencies": {
45
- "@babel/core": "^7.16.7",
46
- "@babel/plugin-transform-typescript": "^7.16.8",
47
- "@babel/preset-env": "^7.16.8",
38
+ "@babel/core": "^7.17.10",
39
+ "@babel/preset-env": "^7.17.10",
40
+ "@babel/preset-typescript": "^7.16.7",
48
41
  "@definitelytyped/dtslint": "^0.0.103",
49
42
  "@reduxjs/toolkit": "^1.7.2",
50
43
  "@rollup/plugin-babel": "^5.3.0",
51
44
  "@rollup/plugin-node-resolve": "^13.1.3",
45
+ "@rollup/plugin-replace": "^4.0.0",
52
46
  "@rollup/plugin-typescript": "^8.3.0",
53
47
  "@testing-library/jest-dom": "^5.16.1",
54
48
  "@testing-library/react": "^12.1.2",
@@ -56,13 +50,17 @@
56
50
  "@types/jest": "^27.4.1",
57
51
  "@typescript-eslint/eslint-plugin": "^5.18.0",
58
52
  "@typescript-eslint/parser": "^5.18.0",
59
- "esbuild": "0.14.11",
60
53
  "eslint": "^8.12.0",
54
+ "eslint-plugin-import": "^2.26.0",
55
+ "eslint-plugin-simple-import-sort": "^7.0.0",
61
56
  "jest": "^27.4.7",
62
57
  "msw": "^0.36.4",
63
58
  "redux": "^4.1.2",
64
59
  "rollup": "^2.64.0",
65
- "rollup-plugin-esbuild": "^4.8.2",
60
+ "rollup-plugin-command": "^1.1.3",
61
+ "rollup-plugin-dts": "^4.2.1",
62
+ "rollup-plugin-terser": "^7.0.2",
66
63
  "whatwg-fetch": "^3.6.2"
67
- }
64
+ },
65
+ "sideEffects": false
68
66
  }
package/esm/index.js DELETED
@@ -1,291 +0,0 @@
1
- import { internals } from '@liveblocks/client';
2
-
3
- const ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
4
- function missingClient() {
5
- return new Error(`${ERROR_PREFIX} client is missing`);
6
- }
7
- function mappingShouldBeAnObject(mappingType) {
8
- return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
9
- }
10
- function mappingValueShouldBeABoolean(mappingType, key) {
11
- return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
12
- }
13
- function mappingShouldNotHaveTheSameKeys(key) {
14
- return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
15
- }
16
- function mappingToFunctionIsNotAllowed(key) {
17
- return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
18
- }
19
-
20
- var __defProp = Object.defineProperty;
21
- var __defProps = Object.defineProperties;
22
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
23
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
24
- var __hasOwnProp = Object.prototype.hasOwnProperty;
25
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
26
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
27
- var __spreadValues = (a, b) => {
28
- for (var prop in b || (b = {}))
29
- if (__hasOwnProp.call(b, prop))
30
- __defNormalProp(a, prop, b[prop]);
31
- if (__getOwnPropSymbols)
32
- for (var prop of __getOwnPropSymbols(b)) {
33
- if (__propIsEnum.call(b, prop))
34
- __defNormalProp(a, prop, b[prop]);
35
- }
36
- return a;
37
- };
38
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
39
- const { patchImmutableObject, patchLiveObjectKey, lsonToJson } = internals;
40
- const ACTION_TYPES = {
41
- ENTER: "@@LIVEBLOCKS/ENTER",
42
- LEAVE: "@@LIVEBLOCKS/LEAVE",
43
- START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
44
- INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
45
- PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
46
- UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
47
- UPDATE_OTHERS: "@@LIVEBLOCKS/UPDATE_OTHERS"
48
- };
49
- const internalEnhancer = (options) => {
50
- if (process.env.NODE_ENV !== "production" && options.client == null) {
51
- throw missingClient();
52
- }
53
- const client = options.client;
54
- const mapping = validateMapping(options.storageMapping || {}, "storageMapping");
55
- const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
56
- if (process.env.NODE_ENV !== "production") {
57
- validateNoDuplicateKeys(mapping, presenceMapping);
58
- }
59
- return (createStore) => (reducer, initialState, enhancer2) => {
60
- let room = null;
61
- let isPatching = false;
62
- let storageRoot = null;
63
- let unsubscribeCallbacks = [];
64
- const newReducer = (state, action) => {
65
- switch (action.type) {
66
- case ACTION_TYPES.PATCH_REDUX_STATE:
67
- return __spreadValues(__spreadValues({}, state), action.state);
68
- case ACTION_TYPES.INIT_STORAGE:
69
- return __spreadProps(__spreadValues(__spreadValues({}, state), action.state), {
70
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
71
- isStorageLoading: false
72
- })
73
- });
74
- case ACTION_TYPES.START_LOADING_STORAGE:
75
- return __spreadProps(__spreadValues({}, state), {
76
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
77
- isStorageLoading: true
78
- })
79
- });
80
- case ACTION_TYPES.UPDATE_CONNECTION: {
81
- return __spreadProps(__spreadValues({}, state), {
82
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
83
- connection: action.connection
84
- })
85
- });
86
- }
87
- case ACTION_TYPES.UPDATE_OTHERS: {
88
- return __spreadProps(__spreadValues({}, state), {
89
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
90
- others: action.others
91
- })
92
- });
93
- }
94
- default: {
95
- const newState = reducer(state, action);
96
- if (room) {
97
- isPatching = true;
98
- updatePresence(room, state, newState, presenceMapping);
99
- room.batch(() => {
100
- if (storageRoot) {
101
- patchLiveblocksStorage(storageRoot, state, newState, mapping);
102
- }
103
- });
104
- isPatching = false;
105
- }
106
- if (newState.liveblocks == null) {
107
- return __spreadProps(__spreadValues({}, newState), {
108
- liveblocks: {
109
- others: [],
110
- isStorageLoading: false,
111
- connection: "closed"
112
- }
113
- });
114
- }
115
- return newState;
116
- }
117
- }
118
- };
119
- const store = createStore(newReducer, initialState, enhancer2);
120
- function enterRoom2(roomId, storageInitialState = {}, reduxState) {
121
- if (storageRoot) {
122
- return;
123
- }
124
- room = client.enter(roomId);
125
- broadcastInitialPresence(room, reduxState, presenceMapping);
126
- unsubscribeCallbacks.push(room.subscribe("connection", () => {
127
- store.dispatch({
128
- type: ACTION_TYPES.UPDATE_CONNECTION,
129
- connection: room.getConnectionState()
130
- });
131
- }));
132
- unsubscribeCallbacks.push(room.subscribe("others", (others) => {
133
- store.dispatch({
134
- type: ACTION_TYPES.UPDATE_OTHERS,
135
- others: others.toArray()
136
- });
137
- }));
138
- unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
139
- if (isPatching === false) {
140
- store.dispatch({
141
- type: ACTION_TYPES.PATCH_REDUX_STATE,
142
- state: patchPresenceState(room.getPresence(), presenceMapping)
143
- });
144
- }
145
- }));
146
- store.dispatch({
147
- type: ACTION_TYPES.START_LOADING_STORAGE
148
- });
149
- room.getStorage().then(({ root }) => {
150
- const updates = {};
151
- room.batch(() => {
152
- for (const key in mapping) {
153
- const liveblocksStatePart = root.get(key);
154
- if (liveblocksStatePart == null) {
155
- updates[key] = storageInitialState[key];
156
- patchLiveObjectKey(root, key, void 0, storageInitialState[key]);
157
- } else {
158
- updates[key] = lsonToJson(liveblocksStatePart);
159
- }
160
- }
161
- });
162
- store.dispatch({
163
- type: ACTION_TYPES.INIT_STORAGE,
164
- state: updates
165
- });
166
- storageRoot = root;
167
- unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
168
- if (isPatching === false) {
169
- store.dispatch({
170
- type: ACTION_TYPES.PATCH_REDUX_STATE,
171
- state: patchState(store.getState(), updates2, mapping)
172
- });
173
- }
174
- }, { isDeep: true }));
175
- });
176
- }
177
- function leaveRoom2(roomId) {
178
- for (const unsubscribe of unsubscribeCallbacks) {
179
- unsubscribe();
180
- }
181
- storageRoot = null;
182
- room = null;
183
- isPatching = false;
184
- unsubscribeCallbacks = [];
185
- client.leave(roomId);
186
- }
187
- function newDispatch(action, state) {
188
- if (action.type === ACTION_TYPES.ENTER) {
189
- enterRoom2(action.roomId, action.initialState, store.getState());
190
- } else if (action.type === ACTION_TYPES.LEAVE) {
191
- leaveRoom2(action.roomId);
192
- } else {
193
- store.dispatch(action, state);
194
- }
195
- }
196
- return __spreadProps(__spreadValues({}, store), {
197
- dispatch: newDispatch
198
- });
199
- };
200
- };
201
- const actions = {
202
- enterRoom,
203
- leaveRoom
204
- };
205
- function enterRoom(roomId, initialState) {
206
- return {
207
- type: ACTION_TYPES.ENTER,
208
- roomId,
209
- initialState
210
- };
211
- }
212
- function leaveRoom(roomId) {
213
- return {
214
- type: ACTION_TYPES.LEAVE,
215
- roomId
216
- };
217
- }
218
- const enhancer = internalEnhancer;
219
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
220
- for (const key in mapping) {
221
- if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
222
- throw mappingToFunctionIsNotAllowed("value");
223
- }
224
- if (oldState[key] !== newState[key]) {
225
- patchLiveObjectKey(root, key, oldState[key], newState[key]);
226
- }
227
- }
228
- }
229
- function broadcastInitialPresence(room, state, mapping) {
230
- for (const key in mapping) {
231
- room == null ? void 0 : room.updatePresence({ [key]: state[key] });
232
- }
233
- }
234
- function updatePresence(room, oldState, newState, presenceMapping) {
235
- for (const key in presenceMapping) {
236
- if (typeof newState[key] === "function") {
237
- throw mappingToFunctionIsNotAllowed("value");
238
- }
239
- if (oldState[key] !== newState[key]) {
240
- room.updatePresence({ [key]: newState[key] });
241
- }
242
- }
243
- }
244
- function isObject(value) {
245
- return Object.prototype.toString.call(value) === "[object Object]";
246
- }
247
- function validateNoDuplicateKeys(storageMapping, presenceMapping) {
248
- for (const key in storageMapping) {
249
- if (presenceMapping[key] !== void 0) {
250
- throw mappingShouldNotHaveTheSameKeys(key);
251
- }
252
- }
253
- }
254
- function patchPresenceState(presence, mapping) {
255
- const partialState = {};
256
- for (const key in mapping) {
257
- partialState[key] = presence[key];
258
- }
259
- return partialState;
260
- }
261
- function patchState(state, updates, mapping) {
262
- const partialState = {};
263
- for (const key in mapping) {
264
- partialState[key] = state[key];
265
- }
266
- const patched = patchImmutableObject(partialState, updates);
267
- const result = {};
268
- for (const key in mapping) {
269
- result[key] = patched[key];
270
- }
271
- return result;
272
- }
273
- function validateMapping(mapping, mappingType) {
274
- if (process.env.NODE_ENV !== "production") {
275
- if (!isObject(mapping)) {
276
- throw mappingShouldBeAnObject(mappingType);
277
- }
278
- }
279
- const result = {};
280
- for (const key in mapping) {
281
- if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
282
- throw mappingValueShouldBeABoolean(mappingType, key);
283
- }
284
- if (mapping[key] === true) {
285
- result[key] = true;
286
- }
287
- }
288
- return result;
289
- }
290
-
291
- export { actions, enhancer };