@liveblocks/redux 0.16.4-beta2 → 0.16.6

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 -358
  2. package/index.mjs +160 -254
  3. package/package.json +9 -5
package/index.js CHANGED
@@ -1,397 +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 client = require('@liveblocks/client');
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 patchImmutableObject = client.internals.patchImmutableObject,
81
- patchLiveObjectKey = client.internals.patchLiveObjectKey,
82
- lsonToJson = client.internals.lsonToJson;
83
- var ACTION_TYPES = {
84
- ENTER: "@@LIVEBLOCKS/ENTER",
85
- LEAVE: "@@LIVEBLOCKS/LEAVE",
86
- START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
87
- INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
88
- PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
89
- UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
90
- 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
+ }
91
77
  };
92
78
 
93
- var internalEnhancer = function internalEnhancer(options) {
94
- if (process.env.NODE_ENV !== "production" && options.client == null) {
95
- throw missingClient();
96
- }
97
-
98
- var client = options.client;
99
- var mapping = validateMapping(options.storageMapping || {}, "storageMapping");
100
- var presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
101
-
102
- if (process.env.NODE_ENV !== "production") {
103
- validateNoDuplicateKeys(mapping, presenceMapping);
104
- }
105
-
106
- return function (createStore) {
107
- return function (reducer, initialState, enhancer) {
108
- var room = null;
109
- var isPatching = false;
110
- var storageRoot = null;
111
- var unsubscribeCallbacks = [];
112
-
113
- var newReducer = function newReducer(state, action) {
114
- switch (action.type) {
115
- case ACTION_TYPES.PATCH_REDUX_STATE:
116
- return _extends({}, state, action.state);
117
-
118
- case ACTION_TYPES.INIT_STORAGE:
119
- return _extends({}, state, action.state, {
120
- liveblocks: _extends({}, state.liveblocks, {
121
- isStorageLoading: false
122
- })
123
- });
124
-
125
- case ACTION_TYPES.START_LOADING_STORAGE:
126
- return _extends({}, state, {
127
- liveblocks: _extends({}, state.liveblocks, {
128
- isStorageLoading: true
129
- })
130
- });
131
-
132
- case ACTION_TYPES.UPDATE_CONNECTION:
133
- {
134
- return _extends({}, state, {
135
- liveblocks: _extends({}, state.liveblocks, {
136
- connection: action.connection
137
- })
138
- });
139
- }
140
-
141
- case ACTION_TYPES.UPDATE_OTHERS:
142
- {
143
- return _extends({}, state, {
144
- liveblocks: _extends({}, state.liveblocks, {
145
- others: action.others
146
- })
147
- });
148
- }
149
-
150
- default:
151
- {
152
- var newState = reducer(state, action);
153
-
154
- if (room) {
155
- isPatching = true;
156
- updatePresence(room, state, newState, presenceMapping);
157
- room.batch(function () {
158
- if (storageRoot) {
159
- patchLiveblocksStorage(storageRoot, state, newState, mapping);
160
- }
161
- });
162
- isPatching = false;
163
- }
164
-
165
- if (newState.liveblocks == null) {
166
- return _extends({}, newState, {
167
- liveblocks: {
168
- others: [],
169
- isStorageLoading: false,
170
- connection: "closed"
171
- }
172
- });
173
- }
174
-
175
- return newState;
176
- }
177
- }
178
- };
179
-
180
- var store = createStore(newReducer, initialState, enhancer);
181
-
182
- function enterRoom(roomId, storageInitialState, reduxState) {
183
- if (storageInitialState === void 0) {
184
- storageInitialState = {};
185
- }
186
-
187
- if (storageRoot) {
188
- return;
189
- }
190
-
191
- room = client.enter(roomId);
192
- broadcastInitialPresence(room, reduxState, presenceMapping);
193
- unsubscribeCallbacks.push(room.subscribe("connection", function () {
194
- store.dispatch({
195
- type: ACTION_TYPES.UPDATE_CONNECTION,
196
- connection: room.getConnectionState()
197
- });
198
- }));
199
- unsubscribeCallbacks.push(room.subscribe("others", function (others) {
200
- store.dispatch({
201
- type: ACTION_TYPES.UPDATE_OTHERS,
202
- others: others.toArray()
203
- });
204
- }));
205
- unsubscribeCallbacks.push(room.subscribe("my-presence", function () {
206
- if (isPatching === false) {
207
- store.dispatch({
208
- type: ACTION_TYPES.PATCH_REDUX_STATE,
209
- state: patchPresenceState(room.getPresence(), presenceMapping)
210
- });
211
- }
212
- }));
213
- store.dispatch({
214
- type: ACTION_TYPES.START_LOADING_STORAGE
215
- });
216
- room.getStorage().then(function (_ref) {
217
- var root = _ref.root;
218
- var updates = {};
219
- room.batch(function () {
220
- for (var _key in mapping) {
221
- var liveblocksStatePart = root.get(_key);
222
-
223
- if (liveblocksStatePart == null) {
224
- updates[_key] = storageInitialState[_key];
225
- patchLiveObjectKey(root, _key, undefined, storageInitialState[_key]);
226
- } else {
227
- updates[_key] = lsonToJson(liveblocksStatePart);
228
- }
229
- }
230
- });
231
- store.dispatch({
232
- type: ACTION_TYPES.INIT_STORAGE,
233
- state: updates
234
- });
235
- storageRoot = root;
236
- unsubscribeCallbacks.push(room.subscribe(root, function (updates) {
237
- if (isPatching === false) {
238
- store.dispatch({
239
- type: ACTION_TYPES.PATCH_REDUX_STATE,
240
- state: patchState(store.getState(), updates, mapping)
241
- });
242
- }
243
- }, {
244
- isDeep: true
245
- }));
246
- });
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));
247
127
  }
248
-
249
- function leaveRoom(roomId) {
250
- for (var _iterator = _createForOfIteratorHelperLoose(unsubscribeCallbacks), _step; !(_step = _iterator()).done;) {
251
- var unsubscribe = _step.value;
252
- unsubscribe();
253
- }
254
-
255
- storageRoot = null;
256
- room = null;
257
- isPatching = false;
258
- unsubscribeCallbacks = [];
259
- 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"
260
140
  }
261
-
262
- function newDispatch(action, state) {
263
- if (action.type === ACTION_TYPES.ENTER) {
264
- enterRoom(action.roomId, action.initialState, store.getState());
265
- } else if (action.type === ACTION_TYPES.LEAVE) {
266
- leaveRoom(action.roomId);
267
- } else {
268
- store.dispatch(action, state);
269
- }
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));
270
154
  }
271
-
272
- return _extends({}, store, {
273
- 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()
274
164
  });
275
- };
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
+ });
276
197
  };
198
+ };
277
199
  };
278
200
 
279
- var actions = {
280
- enterRoom: enterRoom,
281
- leaveRoom: leaveRoom
282
- };
283
-
284
- function enterRoom(roomId, initialState) {
285
- return {
286
- type: ACTION_TYPES.ENTER,
287
- roomId: roomId,
288
- initialState: initialState
289
- };
290
- }
291
-
292
- function leaveRoom(roomId) {
293
- return {
294
- type: ACTION_TYPES.LEAVE,
295
- roomId: roomId
296
- };
297
- }
298
-
299
- var enhancer = internalEnhancer;
300
-
301
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
302
- for (var _key2 in mapping) {
303
- if (process.env.NODE_ENV !== "production" && typeof newState[_key2] === "function") {
304
- throw mappingToFunctionIsNotAllowed("value");
305
- }
306
-
307
- if (oldState[_key2] !== newState[_key2]) {
308
- patchLiveObjectKey(root, _key2, oldState[_key2], newState[_key2]);
309
- }
310
- }
311
- }
312
-
313
- function broadcastInitialPresence(room, state, mapping) {
314
- for (var _key3 in mapping) {
315
- var _room$updatePresence;
316
-
317
- room == null ? void 0 : room.updatePresence((_room$updatePresence = {}, _room$updatePresence[_key3] = state[_key3], _room$updatePresence));
318
- }
319
- }
320
-
321
- function updatePresence(room, oldState, newState, presenceMapping) {
322
- for (var _key4 in presenceMapping) {
323
- if (typeof newState[_key4] === "function") {
324
- throw mappingToFunctionIsNotAllowed("value");
325
- }
326
-
327
- if (oldState[_key4] !== newState[_key4]) {
328
- var _room$updatePresence2;
329
-
330
- room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[_key4] = newState[_key4], _room$updatePresence2));
331
- }
332
- }
333
- }
334
-
335
- function isObject(value) {
336
- return Object.prototype.toString.call(value) === "[object Object]";
337
- }
338
-
339
- function validateNoDuplicateKeys(storageMapping, presenceMapping) {
340
- for (var _key5 in storageMapping) {
341
- if (presenceMapping[_key5] !== undefined) {
342
- throw mappingShouldNotHaveTheSameKeys(_key5);
343
- }
344
- }
345
- }
346
-
347
201
  function patchPresenceState(presence, mapping) {
348
- var partialState = {};
349
-
350
- for (var _key6 in mapping) {
351
- partialState[_key6] = presence[_key6];
352
- }
353
-
354
- return partialState;
202
+ var partialState = {};
203
+ for (var key in mapping) partialState[key] = presence[key];
204
+ return partialState;
355
205
  }
356
206
 
357
207
  function patchState(state, updates, mapping) {
358
- var partialState = {};
359
-
360
- for (var _key7 in mapping) {
361
- partialState[_key7] = state[_key7];
362
- }
363
-
364
- var patched = patchImmutableObject(partialState, updates);
365
- var result = {};
366
-
367
- for (var _key8 in mapping) {
368
- result[_key8] = patched[_key8];
369
- }
370
-
371
- 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;
372
213
  }
373
214
 
374
215
  function validateMapping(mapping, mappingType) {
375
- if (process.env.NODE_ENV !== "production") {
376
- if (!isObject(mapping)) {
377
- throw mappingShouldBeAnObject(mappingType);
378
- }
379
- }
380
-
381
- var result = {};
382
-
383
- for (var _key9 in mapping) {
384
- if (process.env.NODE_ENV !== "production" && typeof mapping[_key9] !== "boolean") {
385
- throw mappingValueShouldBeABoolean(mappingType, _key9);
386
- }
387
-
388
- if (mapping[_key9] === true) {
389
- result[_key9] = true;
390
- }
391
- }
392
-
393
- 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;
394
225
  }
395
226
 
396
- exports.actions = actions;
397
- exports.enhancer = enhancer;
227
+ exports.actions = actions, exports.enhancer = enhancer;
package/index.mjs CHANGED
@@ -1,273 +1,179 @@
1
- import { internals } from '@liveblocks/client';
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 { patchImmutableObject, patchLiveObjectKey, lsonToJson } = internals;
21
- const ACTION_TYPES = {
22
- ENTER: "@@LIVEBLOCKS/ENTER",
23
- LEAVE: "@@LIVEBLOCKS/LEAVE",
24
- START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
25
- INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
26
- PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
27
- UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
28
- 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
+ }
29
31
  };
30
- const internalEnhancer = (options) => {
31
- if (process.env.NODE_ENV !== "production" && options.client == null) {
32
- throw missingClient();
33
- }
34
- const client = options.client;
35
- const mapping = validateMapping(options.storageMapping || {}, "storageMapping");
36
- const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
37
- if (process.env.NODE_ENV !== "production") {
38
- 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;
39
97
  }
40
- return (createStore) => (reducer, initialState, enhancer) => {
41
- let room = null;
42
- let isPatching = false;
43
- let storageRoot = null;
44
- let unsubscribeCallbacks = [];
45
- const newReducer = (state, action) => {
46
- switch (action.type) {
47
- case ACTION_TYPES.PATCH_REDUX_STATE:
48
- return Object.assign(Object.assign({}, state), action.state);
49
- case ACTION_TYPES.INIT_STORAGE:
50
- return Object.assign(Object.assign(Object.assign({}, state), action.state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { isStorageLoading: false }) });
51
- case ACTION_TYPES.START_LOADING_STORAGE:
52
- return Object.assign(Object.assign({}, state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { isStorageLoading: true }) });
53
- case ACTION_TYPES.UPDATE_CONNECTION: {
54
- return Object.assign(Object.assign({}, state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { connection: action.connection }) });
55
- }
56
- case ACTION_TYPES.UPDATE_OTHERS: {
57
- return Object.assign(Object.assign({}, state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { others: action.others }) });
58
- }
59
- default: {
60
- const newState = reducer(state, action);
61
- if (room) {
62
- isPatching = true;
63
- updatePresence(room, state, newState, presenceMapping);
64
- room.batch(() => {
65
- if (storageRoot) {
66
- patchLiveblocksStorage(storageRoot, state, newState, mapping);
67
- }
68
- });
69
- isPatching = false;
70
- }
71
- if (newState.liveblocks == null) {
72
- return Object.assign(Object.assign({}, newState), { liveblocks: {
73
- others: [],
74
- isStorageLoading: false,
75
- connection: "closed",
76
- } });
77
- }
78
- return newState;
79
- }
80
- }
81
- };
82
- const store = createStore(newReducer, initialState, enhancer);
83
- function enterRoom(roomId, storageInitialState = {}, reduxState) {
84
- if (storageRoot) {
85
- return;
86
- }
87
- room = client.enter(roomId);
88
- broadcastInitialPresence(room, reduxState, presenceMapping);
89
- unsubscribeCallbacks.push(room.subscribe("connection", () => {
90
- store.dispatch({
91
- type: ACTION_TYPES.UPDATE_CONNECTION,
92
- connection: room.getConnectionState(),
93
- });
94
- }));
95
- unsubscribeCallbacks.push(room.subscribe("others", (others) => {
96
- store.dispatch({
97
- type: ACTION_TYPES.UPDATE_OTHERS,
98
- others: others.toArray(),
99
- });
100
- }));
101
- unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
102
- if (isPatching === false) {
103
- store.dispatch({
104
- type: ACTION_TYPES.PATCH_REDUX_STATE,
105
- state: patchPresenceState(room.getPresence(), presenceMapping),
106
- });
107
- }
108
- }));
109
- store.dispatch({
110
- type: ACTION_TYPES.START_LOADING_STORAGE,
111
- });
112
- room.getStorage().then(({ root }) => {
113
- const updates = {};
114
- room.batch(() => {
115
- for (const key in mapping) {
116
- const liveblocksStatePart = root.get(key);
117
- if (liveblocksStatePart == null) {
118
- updates[key] = storageInitialState[key];
119
- patchLiveObjectKey(root, key, undefined, storageInitialState[key]);
120
- }
121
- else {
122
- updates[key] = lsonToJson(liveblocksStatePart);
123
- }
124
- }
125
- });
126
- store.dispatch({
127
- type: ACTION_TYPES.INIT_STORAGE,
128
- state: updates,
129
- });
130
- storageRoot = root;
131
- unsubscribeCallbacks.push(room.subscribe(root, (updates) => {
132
- if (isPatching === false) {
133
- store.dispatch({
134
- type: ACTION_TYPES.PATCH_REDUX_STATE,
135
- state: patchState(store.getState(), updates, mapping),
136
- });
137
- }
138
- }, { isDeep: true }));
139
- });
140
- }
141
- function leaveRoom(roomId) {
142
- for (const unsubscribe of unsubscribeCallbacks) {
143
- unsubscribe();
144
- }
145
- storageRoot = null;
146
- room = null;
147
- isPatching = false;
148
- unsubscribeCallbacks = [];
149
- client.leave(roomId);
150
- }
151
- function newDispatch(action, state) {
152
- if (action.type === ACTION_TYPES.ENTER) {
153
- enterRoom(action.roomId, action.initialState, store.getState());
154
- }
155
- else if (action.type === ACTION_TYPES.LEAVE) {
156
- leaveRoom(action.roomId);
157
- }
158
- else {
159
- store.dispatch(action, state);
160
- }
161
- }
162
- return Object.assign(Object.assign({}, store), { dispatch: newDispatch });
163
- };
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
+ };
164
150
  };
165
- /**
166
- * Actions used to interact with Liveblocks
167
- */
168
- const actions = {
169
- /**
170
- * Enters a room and starts sync it with zustand state
171
- * @param roomId The id of the room
172
- * @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.
173
- */
174
- enterRoom,
175
- /**
176
- * Leaves a room and stops sync it with zustand state.
177
- * @param roomId The id of the room
178
- */
179
- leaveRoom,
180
- };
181
- function enterRoom(roomId, initialState) {
182
- return {
183
- type: ACTION_TYPES.ENTER,
184
- roomId,
185
- initialState,
186
- };
187
- }
188
- function leaveRoom(roomId) {
189
- return {
190
- type: ACTION_TYPES.LEAVE,
191
- roomId,
192
- };
193
- }
194
- const enhancer = internalEnhancer;
195
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
196
- for (const key in mapping) {
197
- if (process.env.NODE_ENV !== "production" &&
198
- typeof newState[key] === "function") {
199
- throw mappingToFunctionIsNotAllowed("value");
200
- }
201
- if (oldState[key] !== newState[key]) {
202
- patchLiveObjectKey(root, key, oldState[key], newState[key]);
203
- }
204
- }
205
- }
206
- function broadcastInitialPresence(room, state, mapping) {
207
- for (const key in mapping) {
208
- room === null || room === void 0 ? void 0 : room.updatePresence({ [key]: state[key] });
209
- }
210
- }
211
- function updatePresence(room, oldState, newState, presenceMapping) {
212
- for (const key in presenceMapping) {
213
- if (typeof newState[key] === "function") {
214
- throw mappingToFunctionIsNotAllowed("value");
215
- }
216
- if (oldState[key] !== newState[key]) {
217
- room.updatePresence({ [key]: newState[key] });
218
- }
219
- }
220
- }
221
- function isObject(value) {
222
- return Object.prototype.toString.call(value) === "[object Object]";
223
- }
224
- function validateNoDuplicateKeys(storageMapping, presenceMapping) {
225
- for (const key in storageMapping) {
226
- if (presenceMapping[key] !== undefined) {
227
- throw mappingShouldNotHaveTheSameKeys(key);
228
- }
229
- }
230
- }
151
+
231
152
  function patchPresenceState(presence, mapping) {
232
- const partialState = {};
233
- for (const key in mapping) {
234
- partialState[key] = presence[key];
235
- }
236
- return partialState;
153
+ const partialState = {};
154
+ for (const key in mapping) partialState[key] = presence[key];
155
+ return partialState;
237
156
  }
238
- function patchState(state, updates, // StorageUpdate
239
- mapping) {
240
- const partialState = {};
241
- for (const key in mapping) {
242
- partialState[key] = state[key];
243
- }
244
- const patched = patchImmutableObject(partialState, updates);
245
- const result = {};
246
- for (const key in mapping) {
247
- result[key] = patched[key];
248
- }
249
- 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;
250
164
  }
251
- /**
252
- * Remove false keys from mapping and generate to a new object to avoid potential mutation from outside the middleware
253
- */
165
+
254
166
  function validateMapping(mapping, mappingType) {
255
- if (process.env.NODE_ENV !== "production") {
256
- if (!isObject(mapping)) {
257
- throw mappingShouldBeAnObject(mappingType);
258
- }
259
- }
260
- const result = {};
261
- for (const key in mapping) {
262
- if (process.env.NODE_ENV !== "production" &&
263
- typeof mapping[key] !== "boolean") {
264
- throw mappingValueShouldBeABoolean(mappingType, key);
265
- }
266
- if (mapping[key] === true) {
267
- result[key] = true;
268
- }
269
- }
270
- 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;
271
177
  }
272
178
 
273
179
  export { actions, enhancer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/redux",
3
- "version": "0.16.4-beta2",
3
+ "version": "0.16.6",
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-beta2",
34
+ "@liveblocks/client": "0.16.6",
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