@liveblocks/redux 0.16.4 → 0.16.7

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.
Files changed (3) hide show
  1. package/index.js +188 -355
  2. package/index.mjs +160 -253
  3. package/package.json +9 -5
package/index.js CHANGED
@@ -1,394 +1,227 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: !0
5
+ });
4
6
 
5
- var internal = require('@liveblocks/client/internal');
7
+ var internal = require("@liveblocks/client/internal");
6
8
 
7
9
  function _extends() {
8
- _extends = Object.assign || function (target) {
9
- for (var i = 1; i < arguments.length; i++) {
10
- var source = arguments[i];
11
-
12
- for (var key in source) {
13
- if (Object.prototype.hasOwnProperty.call(source, key)) {
14
- target[key] = source[key];
15
- }
16
- }
17
- }
18
-
19
- return target;
20
- };
21
-
22
- return _extends.apply(this, arguments);
23
- }
24
-
25
- function _unsupportedIterableToArray(o, minLen) {
26
- if (!o) return;
27
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
28
- var n = Object.prototype.toString.call(o).slice(8, -1);
29
- if (n === "Object" && o.constructor) n = o.constructor.name;
30
- if (n === "Map" || n === "Set") return Array.from(o);
31
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
10
+ return _extends = Object.assign || function(target) {
11
+ for (var i = 1; i < arguments.length; i++) {
12
+ var source = arguments[i];
13
+ for (var key in source) Object.prototype.hasOwnProperty.call(source, key) && (target[key] = source[key]);
14
+ }
15
+ return target;
16
+ }, _extends.apply(this, arguments);
32
17
  }
33
18
 
34
19
  function _arrayLikeToArray(arr, len) {
35
- if (len == null || len > arr.length) len = arr.length;
36
-
37
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
38
-
39
- return arr2;
20
+ (null == len || len > arr.length) && (len = arr.length);
21
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
22
+ return arr2;
40
23
  }
41
24
 
42
25
  function _createForOfIteratorHelperLoose(o, allowArrayLike) {
43
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
44
- if (it) return (it = it.call(o)).next.bind(it);
45
-
46
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
47
- if (it) o = it;
48
- var i = 0;
49
- return function () {
50
- if (i >= o.length) return {
51
- done: true
52
- };
53
- return {
54
- done: false,
55
- value: o[i++]
56
- };
57
- };
26
+ var it = "undefined" != typeof Symbol && o[Symbol.iterator] || o["@@iterator"];
27
+ if (it) return (it = it.call(o)).next.bind(it);
28
+ if (Array.isArray(o) || (it = function(o, minLen) {
29
+ if (o) {
30
+ if ("string" == typeof o) return _arrayLikeToArray(o, minLen);
31
+ var n = Object.prototype.toString.call(o).slice(8, -1);
32
+ return "Object" === n && o.constructor && (n = o.constructor.name), "Map" === n || "Set" === n ? Array.from(o) : "Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n) ? _arrayLikeToArray(o, minLen) : void 0;
58
33
  }
59
-
60
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
34
+ }(o)) || allowArrayLike && o && "number" == typeof o.length) {
35
+ it && (o = it);
36
+ var i = 0;
37
+ return function() {
38
+ return i >= o.length ? {
39
+ done: !0
40
+ } : {
41
+ done: !1,
42
+ value: o[i++]
43
+ };
44
+ };
45
+ }
46
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
61
47
  }
62
48
 
63
49
  var ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
64
- function missingClient() {
65
- return new Error(ERROR_PREFIX + " client is missing");
66
- }
67
- function mappingShouldBeAnObject(mappingType) {
68
- return new Error(ERROR_PREFIX + " " + mappingType + " should be an object where the values are boolean.");
69
- }
50
+
70
51
  function mappingValueShouldBeABoolean(mappingType, key) {
71
- return new Error(ERROR_PREFIX + " " + mappingType + "." + key + " value should be a boolean");
52
+ return new Error(ERROR_PREFIX + " " + mappingType + "." + key + " value should be a boolean");
72
53
  }
54
+
73
55
  function mappingShouldNotHaveTheSameKeys(key) {
74
- return new Error(ERROR_PREFIX + " \"" + key + "\" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.");
56
+ return new Error(ERROR_PREFIX + ' "' + key + "\" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.");
75
57
  }
58
+
76
59
  function mappingToFunctionIsNotAllowed(key) {
77
- return new Error(ERROR_PREFIX + " mapping." + key + " is invalid. Mapping to a function is not allowed.");
60
+ return new Error(ERROR_PREFIX + " mapping." + key + " is invalid. Mapping to a function is not allowed.");
78
61
  }
79
62
 
80
- var ACTION_TYPES = {
81
- ENTER: "@@LIVEBLOCKS/ENTER",
82
- LEAVE: "@@LIVEBLOCKS/LEAVE",
83
- START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
84
- INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
85
- PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
86
- UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
87
- UPDATE_OTHERS: "@@LIVEBLOCKS/UPDATE_OTHERS"
63
+ var 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 = {
64
+ enterRoom: function(roomId, initialState) {
65
+ return {
66
+ type: ACTION_TYPES_ENTER,
67
+ roomId: roomId,
68
+ initialState: initialState
69
+ };
70
+ },
71
+ leaveRoom: function(roomId) {
72
+ return {
73
+ type: ACTION_TYPES_LEAVE,
74
+ roomId: roomId
75
+ };
76
+ }
88
77
  };
89
78
 
90
- var internalEnhancer = function internalEnhancer(options) {
91
- if (process.env.NODE_ENV !== "production" && options.client == null) {
92
- throw missingClient();
93
- }
94
-
95
- var client = options.client;
96
- var mapping = validateMapping(options.storageMapping || {}, "storageMapping");
97
- var presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
98
-
99
- if (process.env.NODE_ENV !== "production") {
100
- validateNoDuplicateKeys(mapping, presenceMapping);
101
- }
102
-
103
- return function (createStore) {
104
- return function (reducer, initialState, enhancer) {
105
- var room = null;
106
- var isPatching = false;
107
- var storageRoot = null;
108
- var unsubscribeCallbacks = [];
109
-
110
- var newReducer = function newReducer(state, action) {
111
- switch (action.type) {
112
- case ACTION_TYPES.PATCH_REDUX_STATE:
113
- return _extends({}, state, action.state);
114
-
115
- case ACTION_TYPES.INIT_STORAGE:
116
- return _extends({}, state, action.state, {
117
- liveblocks: _extends({}, state.liveblocks, {
118
- isStorageLoading: false
119
- })
120
- });
121
-
122
- case ACTION_TYPES.START_LOADING_STORAGE:
123
- return _extends({}, state, {
124
- liveblocks: _extends({}, state.liveblocks, {
125
- isStorageLoading: true
126
- })
127
- });
128
-
129
- case ACTION_TYPES.UPDATE_CONNECTION:
130
- {
131
- return _extends({}, state, {
132
- liveblocks: _extends({}, state.liveblocks, {
133
- connection: action.connection
134
- })
135
- });
136
- }
137
-
138
- case ACTION_TYPES.UPDATE_OTHERS:
139
- {
140
- return _extends({}, state, {
141
- liveblocks: _extends({}, state.liveblocks, {
142
- others: action.others
143
- })
144
- });
145
- }
146
-
147
- default:
148
- {
149
- var newState = reducer(state, action);
150
-
151
- if (room) {
152
- isPatching = true;
153
- updatePresence(room, state, newState, presenceMapping);
154
- room.batch(function () {
155
- if (storageRoot) {
156
- patchLiveblocksStorage(storageRoot, state, newState, mapping);
157
- }
158
- });
159
- isPatching = false;
160
- }
161
-
162
- if (newState.liveblocks == null) {
163
- return _extends({}, newState, {
164
- liveblocks: {
165
- others: [],
166
- isStorageLoading: false,
167
- connection: "closed"
168
- }
169
- });
170
- }
171
-
172
- return newState;
173
- }
174
- }
175
- };
176
-
177
- var store = createStore(newReducer, initialState, enhancer);
178
-
179
- function enterRoom(roomId, storageInitialState, reduxState) {
180
- if (storageInitialState === void 0) {
181
- storageInitialState = {};
182
- }
183
-
184
- if (storageRoot) {
185
- return;
186
- }
187
-
188
- room = client.enter(roomId);
189
- broadcastInitialPresence(room, reduxState, presenceMapping);
190
- unsubscribeCallbacks.push(room.subscribe("connection", function () {
191
- store.dispatch({
192
- type: ACTION_TYPES.UPDATE_CONNECTION,
193
- connection: room.getConnectionState()
194
- });
195
- }));
196
- unsubscribeCallbacks.push(room.subscribe("others", function (others) {
197
- store.dispatch({
198
- type: ACTION_TYPES.UPDATE_OTHERS,
199
- others: others.toArray()
200
- });
201
- }));
202
- unsubscribeCallbacks.push(room.subscribe("my-presence", function () {
203
- if (isPatching === false) {
204
- store.dispatch({
205
- type: ACTION_TYPES.PATCH_REDUX_STATE,
206
- state: patchPresenceState(room.getPresence(), presenceMapping)
207
- });
208
- }
209
- }));
210
- store.dispatch({
211
- type: ACTION_TYPES.START_LOADING_STORAGE
212
- });
213
- room.getStorage().then(function (_ref) {
214
- var root = _ref.root;
215
- var updates = {};
216
- room.batch(function () {
217
- for (var key in mapping) {
218
- var liveblocksStatePart = root.get(key);
219
-
220
- if (liveblocksStatePart == null) {
221
- updates[key] = storageInitialState[key];
222
- internal.patchLiveObjectKey(root, key, undefined, storageInitialState[key]);
223
- } else {
224
- updates[key] = internal.lsonToJson(liveblocksStatePart);
225
- }
226
- }
227
- });
228
- store.dispatch({
229
- type: ACTION_TYPES.INIT_STORAGE,
230
- state: updates
231
- });
232
- storageRoot = root;
233
- unsubscribeCallbacks.push(room.subscribe(root, function (updates) {
234
- if (isPatching === false) {
235
- store.dispatch({
236
- type: ACTION_TYPES.PATCH_REDUX_STATE,
237
- state: patchState(store.getState(), updates, mapping)
238
- });
239
- }
240
- }, {
241
- isDeep: true
242
- }));
243
- });
79
+ var enhancer = function(options) {
80
+ if ("production" !== process.env.NODE_ENV && null == options.client) throw new Error(ERROR_PREFIX + " client is missing");
81
+ var client = options.client, mapping = validateMapping(options.storageMapping || {}, "storageMapping"), presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
82
+ return "production" !== process.env.NODE_ENV && function(storageMapping, presenceMapping) {
83
+ for (var key in storageMapping) if (void 0 !== presenceMapping[key]) throw mappingShouldNotHaveTheSameKeys(key);
84
+ }(mapping, presenceMapping), function(createStore) {
85
+ return function(reducer, initialState, enhancer) {
86
+ var room = null, isPatching = !1, storageRoot = null, unsubscribeCallbacks = [], store = createStore((function(state, action) {
87
+ switch (action.type) {
88
+ case ACTION_TYPES_PATCH_REDUX_STATE:
89
+ return _extends({}, state, action.state);
90
+
91
+ case ACTION_TYPES_INIT_STORAGE:
92
+ return _extends({}, state, action.state, {
93
+ liveblocks: _extends({}, state.liveblocks, {
94
+ isStorageLoading: !1
95
+ })
96
+ });
97
+
98
+ case ACTION_TYPES_START_LOADING_STORAGE:
99
+ return _extends({}, state, {
100
+ liveblocks: _extends({}, state.liveblocks, {
101
+ isStorageLoading: !0
102
+ })
103
+ });
104
+
105
+ case ACTION_TYPES_UPDATE_CONNECTION:
106
+ return _extends({}, state, {
107
+ liveblocks: _extends({}, state.liveblocks, {
108
+ connection: action.connection
109
+ })
110
+ });
111
+
112
+ case ACTION_TYPES_UPDATE_OTHERS:
113
+ return _extends({}, state, {
114
+ liveblocks: _extends({}, state.liveblocks, {
115
+ others: action.others
116
+ })
117
+ });
118
+
119
+ default:
120
+ var newState = reducer(state, action);
121
+ return room && (isPatching = !0, function(room, oldState, newState, presenceMapping) {
122
+ for (var key in presenceMapping) {
123
+ if ("function" == typeof newState[key]) throw mappingToFunctionIsNotAllowed("value");
124
+ var _room$updatePresence2;
125
+ if (oldState[key] !== newState[key]) room.updatePresence(((_room$updatePresence2 = {})[key] = newState[key],
126
+ _room$updatePresence2));
244
127
  }
245
-
246
- function leaveRoom(roomId) {
247
- for (var _iterator = _createForOfIteratorHelperLoose(unsubscribeCallbacks), _step; !(_step = _iterator()).done;) {
248
- var unsubscribe = _step.value;
249
- unsubscribe();
250
- }
251
-
252
- storageRoot = null;
253
- room = null;
254
- isPatching = false;
255
- unsubscribeCallbacks = [];
256
- client.leave(roomId);
128
+ }(room, state, newState, presenceMapping), room.batch((function() {
129
+ storageRoot && function(root, oldState, newState, mapping) {
130
+ for (var key in mapping) {
131
+ if ("production" !== process.env.NODE_ENV && "function" == typeof newState[key]) throw mappingToFunctionIsNotAllowed("value");
132
+ oldState[key] !== newState[key] && internal.patchLiveObjectKey(root, key, oldState[key], newState[key]);
133
+ }
134
+ }(storageRoot, state, newState, mapping);
135
+ })), isPatching = !1), null == newState.liveblocks ? _extends({}, newState, {
136
+ liveblocks: {
137
+ others: [],
138
+ isStorageLoading: !1,
139
+ connection: "closed"
257
140
  }
258
-
259
- function newDispatch(action, state) {
260
- if (action.type === ACTION_TYPES.ENTER) {
261
- enterRoom(action.roomId, action.initialState, store.getState());
262
- } else if (action.type === ACTION_TYPES.LEAVE) {
263
- leaveRoom(action.roomId);
264
- } else {
265
- store.dispatch(action, state);
266
- }
141
+ }) : newState;
142
+ }
143
+ }), initialState, enhancer);
144
+ return _extends({}, store, {
145
+ dispatch: function(action, state) {
146
+ var roomId, storageInitialState, reduxState;
147
+ action.type === ACTION_TYPES_ENTER ? (roomId = action.roomId, storageInitialState = action.initialState,
148
+ reduxState = store.getState(), void 0 === storageInitialState && (storageInitialState = {}),
149
+ storageRoot || (function(room, state, mapping) {
150
+ for (var key in mapping) {
151
+ var _room$updatePresence;
152
+ null == room || room.updatePresence(((_room$updatePresence = {})[key] = state[key],
153
+ _room$updatePresence));
267
154
  }
268
-
269
- return _extends({}, store, {
270
- dispatch: newDispatch
155
+ }(room = client.enter(roomId), reduxState, presenceMapping), unsubscribeCallbacks.push(room.subscribe("connection", (function() {
156
+ store.dispatch({
157
+ type: ACTION_TYPES_UPDATE_CONNECTION,
158
+ connection: room.getConnectionState()
159
+ });
160
+ }))), unsubscribeCallbacks.push(room.subscribe("others", (function(others) {
161
+ store.dispatch({
162
+ type: ACTION_TYPES_UPDATE_OTHERS,
163
+ others: others.toArray()
271
164
  });
272
- };
165
+ }))), unsubscribeCallbacks.push(room.subscribe("my-presence", (function() {
166
+ !1 === isPatching && store.dispatch({
167
+ type: ACTION_TYPES_PATCH_REDUX_STATE,
168
+ state: patchPresenceState(room.getPresence(), presenceMapping)
169
+ });
170
+ }))), store.dispatch({
171
+ type: ACTION_TYPES_START_LOADING_STORAGE
172
+ }), room.getStorage().then((function(_ref) {
173
+ var root = _ref.root, updates = {};
174
+ room.batch((function() {
175
+ for (var key in mapping) {
176
+ var liveblocksStatePart = root.get(key);
177
+ null == liveblocksStatePart ? (updates[key] = storageInitialState[key], internal.patchLiveObjectKey(root, key, void 0, storageInitialState[key])) : updates[key] = internal.lsonToJson(liveblocksStatePart);
178
+ }
179
+ })), store.dispatch({
180
+ type: ACTION_TYPES_INIT_STORAGE,
181
+ state: updates
182
+ }), storageRoot = root, unsubscribeCallbacks.push(room.subscribe(root, (function(updates) {
183
+ !1 === isPatching && store.dispatch({
184
+ type: ACTION_TYPES_PATCH_REDUX_STATE,
185
+ state: patchState(store.getState(), updates, mapping)
186
+ });
187
+ }), {
188
+ isDeep: !0
189
+ }));
190
+ })))) : action.type === ACTION_TYPES_LEAVE ? function(roomId) {
191
+ for (var _step, _iterator = _createForOfIteratorHelperLoose(unsubscribeCallbacks); !(_step = _iterator()).done; ) (0,
192
+ _step.value)();
193
+ storageRoot = null, room = null, isPatching = !1, unsubscribeCallbacks = [], client.leave(roomId);
194
+ }(action.roomId) : store.dispatch(action, state);
195
+ }
196
+ });
273
197
  };
198
+ };
274
199
  };
275
200
 
276
- var actions = {
277
- enterRoom: enterRoom,
278
- leaveRoom: leaveRoom
279
- };
280
-
281
- function enterRoom(roomId, initialState) {
282
- return {
283
- type: ACTION_TYPES.ENTER,
284
- roomId: roomId,
285
- initialState: initialState
286
- };
287
- }
288
-
289
- function leaveRoom(roomId) {
290
- return {
291
- type: ACTION_TYPES.LEAVE,
292
- roomId: roomId
293
- };
294
- }
295
-
296
- var enhancer = internalEnhancer;
297
-
298
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
299
- for (var key in mapping) {
300
- if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
301
- throw mappingToFunctionIsNotAllowed("value");
302
- }
303
-
304
- if (oldState[key] !== newState[key]) {
305
- internal.patchLiveObjectKey(root, key, oldState[key], newState[key]);
306
- }
307
- }
308
- }
309
-
310
- function broadcastInitialPresence(room, state, mapping) {
311
- for (var key in mapping) {
312
- var _room$updatePresence;
313
-
314
- room == null ? void 0 : room.updatePresence((_room$updatePresence = {}, _room$updatePresence[key] = state[key], _room$updatePresence));
315
- }
316
- }
317
-
318
- function updatePresence(room, oldState, newState, presenceMapping) {
319
- for (var key in presenceMapping) {
320
- if (typeof newState[key] === "function") {
321
- throw mappingToFunctionIsNotAllowed("value");
322
- }
323
-
324
- if (oldState[key] !== newState[key]) {
325
- var _room$updatePresence2;
326
-
327
- room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[key] = newState[key], _room$updatePresence2));
328
- }
329
- }
330
- }
331
-
332
- function isObject(value) {
333
- return Object.prototype.toString.call(value) === "[object Object]";
334
- }
335
-
336
- function validateNoDuplicateKeys(storageMapping, presenceMapping) {
337
- for (var key in storageMapping) {
338
- if (presenceMapping[key] !== undefined) {
339
- throw mappingShouldNotHaveTheSameKeys(key);
340
- }
341
- }
342
- }
343
-
344
201
  function patchPresenceState(presence, mapping) {
345
- var partialState = {};
346
-
347
- for (var key in mapping) {
348
- partialState[key] = presence[key];
349
- }
350
-
351
- return partialState;
202
+ var partialState = {};
203
+ for (var key in mapping) partialState[key] = presence[key];
204
+ return partialState;
352
205
  }
353
206
 
354
207
  function patchState(state, updates, mapping) {
355
- var partialState = {};
356
-
357
- for (var key in mapping) {
358
- partialState[key] = state[key];
359
- }
360
-
361
- var patched = internal.patchImmutableObject(partialState, updates);
362
- var result = {};
363
-
364
- for (var _key in mapping) {
365
- result[_key] = patched[_key];
366
- }
367
-
368
- return result;
208
+ var partialState = {};
209
+ for (var key in mapping) partialState[key] = state[key];
210
+ var patched = internal.patchImmutableObject(partialState, updates), result = {};
211
+ for (var _key in mapping) result[_key] = patched[_key];
212
+ return result;
369
213
  }
370
214
 
371
215
  function validateMapping(mapping, mappingType) {
372
- if (process.env.NODE_ENV !== "production") {
373
- if (!isObject(mapping)) {
374
- throw mappingShouldBeAnObject(mappingType);
375
- }
376
- }
377
-
378
- var result = {};
379
-
380
- for (var key in mapping) {
381
- if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
382
- throw mappingValueShouldBeABoolean(mappingType, key);
383
- }
384
-
385
- if (mapping[key] === true) {
386
- result[key] = true;
387
- }
388
- }
389
-
390
- return result;
216
+ if ("production" !== process.env.NODE_ENV && (value = mapping, "[object Object]" !== Object.prototype.toString.call(value))) throw function(mappingType) {
217
+ return new Error(ERROR_PREFIX + " " + mappingType + " should be an object where the values are boolean.");
218
+ }(mappingType);
219
+ var value, result = {};
220
+ for (var key in mapping) {
221
+ if ("production" !== process.env.NODE_ENV && "boolean" != typeof mapping[key]) throw mappingValueShouldBeABoolean(mappingType, key);
222
+ !0 === mapping[key] && (result[key] = !0);
223
+ }
224
+ return result;
391
225
  }
392
226
 
393
- exports.actions = actions;
394
- exports.enhancer = enhancer;
227
+ exports.actions = actions, exports.enhancer = enhancer;
package/index.mjs CHANGED
@@ -1,272 +1,179 @@
1
- import { patchLiveObjectKey, lsonToJson, patchImmutableObject } from '@liveblocks/client/internal';
1
+ import { patchLiveObjectKey, lsonToJson, patchImmutableObject } from "@liveblocks/client/internal";
2
2
 
3
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
- }
4
+
10
5
  function mappingValueShouldBeABoolean(mappingType, key) {
11
- return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
6
+ return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
12
7
  }
8
+
13
9
  function mappingShouldNotHaveTheSameKeys(key) {
14
- return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
10
+ return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
15
11
  }
12
+
16
13
  function mappingToFunctionIsNotAllowed(key) {
17
- return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
14
+ return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
18
15
  }
19
16
 
20
- const ACTION_TYPES = {
21
- ENTER: "@@LIVEBLOCKS/ENTER",
22
- LEAVE: "@@LIVEBLOCKS/LEAVE",
23
- START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
24
- INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
25
- PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
26
- UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
27
- UPDATE_OTHERS: "@@LIVEBLOCKS/UPDATE_OTHERS",
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
+ }
28
31
  };
29
- const internalEnhancer = (options) => {
30
- if (process.env.NODE_ENV !== "production" && options.client == null) {
31
- throw missingClient();
32
- }
33
- const client = options.client;
34
- const mapping = validateMapping(options.storageMapping || {}, "storageMapping");
35
- const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
36
- if (process.env.NODE_ENV !== "production") {
37
- validateNoDuplicateKeys(mapping, presenceMapping);
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;
38
97
  }
39
- return (createStore) => (reducer, initialState, enhancer) => {
40
- let room = null;
41
- let isPatching = false;
42
- let storageRoot = null;
43
- let unsubscribeCallbacks = [];
44
- const newReducer = (state, action) => {
45
- switch (action.type) {
46
- case ACTION_TYPES.PATCH_REDUX_STATE:
47
- return Object.assign(Object.assign({}, state), action.state);
48
- case ACTION_TYPES.INIT_STORAGE:
49
- return Object.assign(Object.assign(Object.assign({}, state), action.state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { isStorageLoading: false }) });
50
- case ACTION_TYPES.START_LOADING_STORAGE:
51
- return Object.assign(Object.assign({}, state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { isStorageLoading: true }) });
52
- case ACTION_TYPES.UPDATE_CONNECTION: {
53
- return Object.assign(Object.assign({}, state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { connection: action.connection }) });
54
- }
55
- case ACTION_TYPES.UPDATE_OTHERS: {
56
- return Object.assign(Object.assign({}, state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { others: action.others }) });
57
- }
58
- default: {
59
- const newState = reducer(state, action);
60
- if (room) {
61
- isPatching = true;
62
- updatePresence(room, state, newState, presenceMapping);
63
- room.batch(() => {
64
- if (storageRoot) {
65
- patchLiveblocksStorage(storageRoot, state, newState, mapping);
66
- }
67
- });
68
- isPatching = false;
69
- }
70
- if (newState.liveblocks == null) {
71
- return Object.assign(Object.assign({}, newState), { liveblocks: {
72
- others: [],
73
- isStorageLoading: false,
74
- connection: "closed",
75
- } });
76
- }
77
- return newState;
78
- }
79
- }
80
- };
81
- const store = createStore(newReducer, initialState, enhancer);
82
- function enterRoom(roomId, storageInitialState = {}, reduxState) {
83
- if (storageRoot) {
84
- return;
85
- }
86
- room = client.enter(roomId);
87
- broadcastInitialPresence(room, reduxState, presenceMapping);
88
- unsubscribeCallbacks.push(room.subscribe("connection", () => {
89
- store.dispatch({
90
- type: ACTION_TYPES.UPDATE_CONNECTION,
91
- connection: room.getConnectionState(),
92
- });
93
- }));
94
- unsubscribeCallbacks.push(room.subscribe("others", (others) => {
95
- store.dispatch({
96
- type: ACTION_TYPES.UPDATE_OTHERS,
97
- others: others.toArray(),
98
- });
99
- }));
100
- unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
101
- if (isPatching === false) {
102
- store.dispatch({
103
- type: ACTION_TYPES.PATCH_REDUX_STATE,
104
- state: patchPresenceState(room.getPresence(), presenceMapping),
105
- });
106
- }
107
- }));
108
- store.dispatch({
109
- type: ACTION_TYPES.START_LOADING_STORAGE,
110
- });
111
- room.getStorage().then(({ root }) => {
112
- const updates = {};
113
- room.batch(() => {
114
- for (const key in mapping) {
115
- const liveblocksStatePart = root.get(key);
116
- if (liveblocksStatePart == null) {
117
- updates[key] = storageInitialState[key];
118
- patchLiveObjectKey(root, key, undefined, storageInitialState[key]);
119
- }
120
- else {
121
- updates[key] = lsonToJson(liveblocksStatePart);
122
- }
123
- }
124
- });
125
- store.dispatch({
126
- type: ACTION_TYPES.INIT_STORAGE,
127
- state: updates,
128
- });
129
- storageRoot = root;
130
- unsubscribeCallbacks.push(room.subscribe(root, (updates) => {
131
- if (isPatching === false) {
132
- store.dispatch({
133
- type: ACTION_TYPES.PATCH_REDUX_STATE,
134
- state: patchState(store.getState(), updates, mapping),
135
- });
136
- }
137
- }, { isDeep: true }));
138
- });
139
- }
140
- function leaveRoom(roomId) {
141
- for (const unsubscribe of unsubscribeCallbacks) {
142
- unsubscribe();
143
- }
144
- storageRoot = null;
145
- room = null;
146
- isPatching = false;
147
- unsubscribeCallbacks = [];
148
- client.leave(roomId);
149
- }
150
- function newDispatch(action, state) {
151
- if (action.type === ACTION_TYPES.ENTER) {
152
- enterRoom(action.roomId, action.initialState, store.getState());
153
- }
154
- else if (action.type === ACTION_TYPES.LEAVE) {
155
- leaveRoom(action.roomId);
156
- }
157
- else {
158
- store.dispatch(action, state);
159
- }
160
- }
161
- return Object.assign(Object.assign({}, store), { dispatch: newDispatch });
162
- };
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
+ };
163
150
  };
164
- /**
165
- * Actions used to interact with Liveblocks
166
- */
167
- const actions = {
168
- /**
169
- * Enters a room and starts sync it with zustand state
170
- * @param roomId The id of the room
171
- * @param initialState The initial state of the room storage. If a key does not exist if your room storage root, initialState[key] will be used.
172
- */
173
- enterRoom,
174
- /**
175
- * Leaves a room and stops sync it with zustand state.
176
- * @param roomId The id of the room
177
- */
178
- leaveRoom,
179
- };
180
- function enterRoom(roomId, initialState) {
181
- return {
182
- type: ACTION_TYPES.ENTER,
183
- roomId,
184
- initialState,
185
- };
186
- }
187
- function leaveRoom(roomId) {
188
- return {
189
- type: ACTION_TYPES.LEAVE,
190
- roomId,
191
- };
192
- }
193
- const enhancer = internalEnhancer;
194
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
195
- for (const key in mapping) {
196
- if (process.env.NODE_ENV !== "production" &&
197
- typeof newState[key] === "function") {
198
- throw mappingToFunctionIsNotAllowed("value");
199
- }
200
- if (oldState[key] !== newState[key]) {
201
- patchLiveObjectKey(root, key, oldState[key], newState[key]);
202
- }
203
- }
204
- }
205
- function broadcastInitialPresence(room, state, mapping) {
206
- for (const key in mapping) {
207
- room === null || room === void 0 ? void 0 : room.updatePresence({ [key]: state[key] });
208
- }
209
- }
210
- function updatePresence(room, oldState, newState, presenceMapping) {
211
- for (const key in presenceMapping) {
212
- if (typeof newState[key] === "function") {
213
- throw mappingToFunctionIsNotAllowed("value");
214
- }
215
- if (oldState[key] !== newState[key]) {
216
- room.updatePresence({ [key]: newState[key] });
217
- }
218
- }
219
- }
220
- function isObject(value) {
221
- return Object.prototype.toString.call(value) === "[object Object]";
222
- }
223
- function validateNoDuplicateKeys(storageMapping, presenceMapping) {
224
- for (const key in storageMapping) {
225
- if (presenceMapping[key] !== undefined) {
226
- throw mappingShouldNotHaveTheSameKeys(key);
227
- }
228
- }
229
- }
151
+
230
152
  function patchPresenceState(presence, mapping) {
231
- const partialState = {};
232
- for (const key in mapping) {
233
- partialState[key] = presence[key];
234
- }
235
- return partialState;
153
+ const partialState = {};
154
+ for (const key in mapping) partialState[key] = presence[key];
155
+ return partialState;
236
156
  }
237
- function patchState(state, updates, // StorageUpdate
238
- mapping) {
239
- const partialState = {};
240
- for (const key in mapping) {
241
- partialState[key] = state[key];
242
- }
243
- const patched = patchImmutableObject(partialState, updates);
244
- const result = {};
245
- for (const key in mapping) {
246
- result[key] = patched[key];
247
- }
248
- return result;
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;
249
164
  }
250
- /**
251
- * Remove false keys from mapping and generate to a new object to avoid potential mutation from outside the middleware
252
- */
165
+
253
166
  function validateMapping(mapping, mappingType) {
254
- if (process.env.NODE_ENV !== "production") {
255
- if (!isObject(mapping)) {
256
- throw mappingShouldBeAnObject(mappingType);
257
- }
258
- }
259
- const result = {};
260
- for (const key in mapping) {
261
- if (process.env.NODE_ENV !== "production" &&
262
- typeof mapping[key] !== "boolean") {
263
- throw mappingValueShouldBeABoolean(mappingType, key);
264
- }
265
- if (mapping[key] === true) {
266
- result[key] = true;
267
- }
268
- }
269
- return result;
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;
270
177
  }
271
178
 
272
179
  export { actions, enhancer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/redux",
3
- "version": "0.16.4",
3
+ "version": "0.16.7",
4
4
  "description": "A store enhancer to integrate Liveblocks into Redux stores.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
@@ -18,6 +18,7 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "build": "rollup -c && cp ./package.json ./README.md ./lib",
21
+ "format": "eslint --fix src/ test/ && prettier --write src/ test/",
21
22
  "lint": "eslint src/ test/",
22
23
  "test": "jest --watch",
23
24
  "test-ci": "jest",
@@ -30,13 +31,13 @@
30
31
  "directory": "packages/liveblocks-redux"
31
32
  },
32
33
  "peerDependencies": {
33
- "@liveblocks/client": "0.16.4",
34
+ "@liveblocks/client": "0.16.7",
34
35
  "redux": "^4"
35
36
  },
36
37
  "devDependencies": {
37
- "@babel/core": "^7.16.7",
38
- "@babel/plugin-transform-typescript": "^7.16.8",
39
- "@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",
40
41
  "@definitelytyped/dtslint": "^0.0.103",
41
42
  "@reduxjs/toolkit": "^1.7.2",
42
43
  "@rollup/plugin-babel": "^5.3.0",
@@ -50,12 +51,15 @@
50
51
  "@typescript-eslint/eslint-plugin": "^5.18.0",
51
52
  "@typescript-eslint/parser": "^5.18.0",
52
53
  "eslint": "^8.12.0",
54
+ "eslint-plugin-import": "^2.26.0",
55
+ "eslint-plugin-simple-import-sort": "^7.0.0",
53
56
  "jest": "^27.4.7",
54
57
  "msw": "^0.36.4",
55
58
  "redux": "^4.1.2",
56
59
  "rollup": "^2.64.0",
57
60
  "rollup-plugin-command": "^1.1.3",
58
61
  "rollup-plugin-dts": "^4.2.1",
62
+ "rollup-plugin-terser": "^7.0.2",
59
63
  "whatwg-fetch": "^3.6.2"
60
64
  },
61
65
  "sideEffects": false