@liveblocks/redux 0.17.8 → 0.17.10-debug2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ 2d963544ab794518e83521361a7388453b6eebc9
package/index.d.ts CHANGED
@@ -1,70 +1,57 @@
1
- import { JsonObject, BaseUserMeta, User, Client } from "@liveblocks/client";
2
- import { StoreEnhancer } from "redux";
1
+ import { JsonObject, BaseUserMeta, User, Client } from '@liveblocks/client';
2
+ import { StoreEnhancer } from 'redux';
3
3
 
4
4
  declare type Mapping<T> = {
5
- [K in keyof T]?: boolean;
5
+ [K in keyof T]?: boolean;
6
6
  };
7
- declare type LiveblocksState<
8
- TState,
9
- TPresence extends JsonObject,
10
- TUserMeta extends BaseUserMeta
11
- > = TState & {
12
- /**
13
- * Liveblocks extra state attached by the enhancer
14
- */
15
- readonly liveblocks: {
7
+ declare type LiveblocksState<TState, TPresence extends JsonObject, TUserMeta extends BaseUserMeta> = TState & {
16
8
  /**
17
- * Other users in the room. Empty no room is currently synced
9
+ * Liveblocks extra state attached by the enhancer
18
10
  */
19
- readonly others: Array<User<TPresence, TUserMeta>>;
20
- /**
21
- * Whether or not the room storage is currently loading
22
- */
23
- readonly isStorageLoading: boolean;
24
- /**
25
- * Connection state of the room
26
- */
27
- readonly connection:
28
- | "closed"
29
- | "authenticating"
30
- | "unavailable"
31
- | "failed"
32
- | "open"
33
- | "connecting";
34
- };
11
+ readonly liveblocks: {
12
+ /**
13
+ * Other users in the room. Empty no room is currently synced
14
+ */
15
+ readonly others: Array<User<TPresence, TUserMeta>>;
16
+ /**
17
+ * Whether or not the room storage is currently loading
18
+ */
19
+ readonly isStorageLoading: boolean;
20
+ /**
21
+ * Connection state of the room
22
+ */
23
+ readonly connection: "closed" | "authenticating" | "unavailable" | "failed" | "open" | "connecting";
24
+ };
35
25
  };
36
26
  /**
37
27
  * Actions used to interact with Liveblocks
38
28
  */
39
29
  declare const actions: {
40
- /**
41
- * Enters a room and starts sync it with zustand state
42
- * @param roomId The id of the room
43
- * @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.
44
- */
45
- enterRoom: typeof enterRoom;
46
- /**
47
- * Leaves a room and stops sync it with zustand state.
48
- * @param roomId The id of the room
49
- */
50
- leaveRoom: typeof leaveRoom;
30
+ /**
31
+ * Enters a room and starts sync it with zustand state
32
+ * @param roomId The id of the room
33
+ * @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.
34
+ */
35
+ enterRoom: typeof enterRoom;
36
+ /**
37
+ * Leaves a room and stops sync it with zustand state.
38
+ * @param roomId The id of the room
39
+ */
40
+ leaveRoom: typeof leaveRoom;
51
41
  };
52
- declare function enterRoom<T>(
53
- roomId: string,
54
- initialState?: T
55
- ): {
56
- type: string;
57
- roomId: string;
58
- initialState?: T;
42
+ declare function enterRoom<T>(roomId: string, initialState?: T): {
43
+ type: string;
44
+ roomId: string;
45
+ initialState?: T;
59
46
  };
60
47
  declare function leaveRoom(roomId: string): {
61
- type: string;
62
- roomId: string;
48
+ type: string;
49
+ roomId: string;
63
50
  };
64
51
  declare const enhancer: <T>(options: {
65
- client: Client;
66
- storageMapping?: Mapping<T> | undefined;
67
- presenceMapping?: Mapping<T> | undefined;
52
+ client: Client;
53
+ storageMapping?: Mapping<T> | undefined;
54
+ presenceMapping?: Mapping<T> | undefined;
68
55
  }) => StoreEnhancer;
69
56
 
70
57
  export { LiveblocksState, Mapping, actions, enhancer };
package/index.js CHANGED
@@ -1,358 +1,343 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var internal = require("@liveblocks/client/internal");
4
- function _extends() {
5
- return (
6
- (_extends =
7
- Object.assign ||
8
- function (target) {
9
- for (var i = 1; i < arguments.length; i++) {
10
- var source = arguments[i];
11
- for (var key in source)
12
- Object.prototype.hasOwnProperty.call(source, key) &&
13
- (target[key] = source[key]);
14
- }
15
- return target;
16
- }),
17
- _extends.apply(this, arguments)
18
- );
19
- }
20
- function _arrayLikeToArray(arr, len) {
21
- (null == len || len > arr.length) && (len = arr.length);
22
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
23
- return arr2;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+
21
+ // src/index.ts
22
+
23
+
24
+
25
+
26
+ var _internal = require('@liveblocks/client/internal');
27
+
28
+ // src/errors.ts
29
+ var ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
30
+ function missingClient() {
31
+ return new Error(`${ERROR_PREFIX} client is missing`);
24
32
  }
25
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
26
- var it =
27
- ("undefined" != typeof Symbol && o[Symbol.iterator]) || o["@@iterator"];
28
- if (it) return (it = it.call(o)).next.bind(it);
29
- if (
30
- Array.isArray(o) ||
31
- (it = (function (o, minLen) {
32
- if (o) {
33
- if ("string" == typeof o) return _arrayLikeToArray(o, minLen);
34
- var n = Object.prototype.toString.call(o).slice(8, -1);
35
- return (
36
- "Object" === n && o.constructor && (n = o.constructor.name),
37
- "Map" === n || "Set" === n
38
- ? Array.from(o)
39
- : "Arguments" === n ||
40
- /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)
41
- ? _arrayLikeToArray(o, minLen)
42
- : void 0
43
- );
44
- }
45
- })(o)) ||
46
- (allowArrayLike && o && "number" == typeof o.length)
47
- ) {
48
- it && (o = it);
49
- var i = 0;
50
- return function () {
51
- return i >= o.length ? { done: !0 } : { done: !1, value: o[i++] };
52
- };
53
- }
54
- throw new TypeError(
55
- "Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."
33
+ function mappingShouldBeAnObject(mappingType) {
34
+ return new Error(
35
+ `${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`
56
36
  );
57
37
  }
58
- var ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
59
38
  function mappingValueShouldBeABoolean(mappingType, key) {
60
39
  return new Error(
61
- ERROR_PREFIX + " " + mappingType + "." + key + " value should be a boolean"
40
+ `${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`
62
41
  );
63
42
  }
64
43
  function mappingShouldNotHaveTheSameKeys(key) {
65
44
  return new Error(
66
- ERROR_PREFIX +
67
- ' "' +
68
- key +
69
- "\" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping."
45
+ `${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`
70
46
  );
71
47
  }
72
48
  function mappingToFunctionIsNotAllowed(key) {
73
49
  return new Error(
74
- ERROR_PREFIX +
75
- " mapping." +
76
- key +
77
- " is invalid. Mapping to a function is not allowed."
50
+ `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`
78
51
  );
79
52
  }
80
- var ACTION_TYPES_ENTER = "@@LIVEBLOCKS/ENTER",
81
- ACTION_TYPES_LEAVE = "@@LIVEBLOCKS/LEAVE",
82
- ACTION_TYPES_START_LOADING_STORAGE = "@@LIVEBLOCKS/START_LOADING_STORAGE",
83
- ACTION_TYPES_INIT_STORAGE = "@@LIVEBLOCKS/INIT_STORAGE",
84
- ACTION_TYPES_PATCH_REDUX_STATE = "@@LIVEBLOCKS/PATCH_REDUX_STATE",
85
- ACTION_TYPES_UPDATE_CONNECTION = "@@LIVEBLOCKS/UPDATE_CONNECTION",
86
- ACTION_TYPES_UPDATE_OTHERS = "@@LIVEBLOCKS/UPDATE_OTHERS",
87
- actions = {
88
- enterRoom: function (roomId, initialState) {
89
- return {
90
- type: ACTION_TYPES_ENTER,
91
- roomId: roomId,
92
- initialState: initialState,
93
- };
94
- },
95
- leaveRoom: function (roomId) {
96
- return { type: ACTION_TYPES_LEAVE, roomId: roomId };
97
- },
98
- };
99
- var enhancer = function (options) {
100
- if ("production" !== process.env.NODE_ENV && null == options.client)
101
- throw new Error(ERROR_PREFIX + " client is missing");
102
- var client = options.client,
103
- mapping = validateMapping(options.storageMapping || {}, "storageMapping"),
104
- presenceMapping = validateMapping(
105
- options.presenceMapping || {},
106
- "presenceMapping"
107
- );
108
- return (
109
- "production" !== process.env.NODE_ENV &&
110
- (function (storageMapping, presenceMapping) {
111
- for (var key in storageMapping)
112
- if (void 0 !== presenceMapping[key])
113
- throw mappingShouldNotHaveTheSameKeys(key);
114
- })(mapping, presenceMapping),
115
- function (createStore) {
116
- return function (reducer, initialState, enhancer) {
117
- var room = null,
118
- isPatching = !1,
119
- storageRoot = null,
120
- unsubscribeCallbacks = [],
121
- store = createStore(
122
- function (state, action) {
123
- switch (action.type) {
124
- case ACTION_TYPES_PATCH_REDUX_STATE:
125
- return _extends({}, state, action.state);
126
- case ACTION_TYPES_INIT_STORAGE:
127
- return _extends({}, state, action.state, {
128
- liveblocks: _extends({}, state.liveblocks, {
129
- isStorageLoading: !1,
130
- }),
131
- });
132
- case ACTION_TYPES_START_LOADING_STORAGE:
133
- return _extends({}, state, {
134
- liveblocks: _extends({}, state.liveblocks, {
135
- isStorageLoading: !0,
136
- }),
137
- });
138
- case ACTION_TYPES_UPDATE_CONNECTION:
139
- return _extends({}, state, {
140
- liveblocks: _extends({}, state.liveblocks, {
141
- connection: action.connection,
142
- }),
143
- });
144
- case ACTION_TYPES_UPDATE_OTHERS:
145
- return _extends({}, state, {
146
- liveblocks: _extends({}, state.liveblocks, {
147
- others: action.others,
148
- }),
149
- });
150
- default:
151
- var newState = reducer(state, action);
152
- return (
153
- room &&
154
- ((isPatching = !0),
155
- (function (room, oldState, newState, presenceMapping) {
156
- for (var key in presenceMapping) {
157
- if ("function" == typeof newState[key])
158
- throw mappingToFunctionIsNotAllowed("value");
159
- var _room$updatePresence2;
160
- if (oldState[key] !== newState[key])
161
- room.updatePresence(
162
- (((_room$updatePresence2 = {})[key] =
163
- newState[key]),
164
- _room$updatePresence2)
165
- );
166
- }
167
- })(room, state, newState, presenceMapping),
168
- room.batch(function () {
169
- storageRoot &&
170
- (function (root, oldState, newState, mapping) {
171
- for (var key in mapping) {
172
- if (
173
- "production" !== process.env.NODE_ENV &&
174
- "function" == typeof newState[key]
175
- )
176
- throw mappingToFunctionIsNotAllowed("value");
177
- oldState[key] !== newState[key] &&
178
- internal.patchLiveObjectKey(
179
- root,
180
- key,
181
- oldState[key],
182
- newState[key]
183
- );
184
- }
185
- })(storageRoot, state, newState, mapping);
186
- }),
187
- (isPatching = !1)),
188
- null == newState.liveblocks
189
- ? _extends({}, newState, {
190
- liveblocks: {
191
- others: [],
192
- isStorageLoading: !1,
193
- connection: "closed",
194
- },
195
- })
196
- : newState
197
- );
198
- }
199
- },
200
- initialState,
201
- enhancer
202
- );
203
- return _extends({}, store, {
204
- dispatch: function (action, state) {
205
- var roomId, storageInitialState, reduxState;
206
- action.type === ACTION_TYPES_ENTER
207
- ? ((roomId = action.roomId),
208
- (storageInitialState = action.initialState),
209
- (reduxState = store.getState()),
210
- void 0 === storageInitialState && (storageInitialState = {}),
211
- storageRoot ||
212
- ((function (room, state, mapping) {
213
- for (var key in mapping) {
214
- var _room$updatePresence;
215
- null == room ||
216
- room.updatePresence(
217
- (((_room$updatePresence = {})[key] = state[key]),
218
- _room$updatePresence)
219
- );
220
- }
221
- })(
222
- (room = client.enter(roomId)),
223
- reduxState,
224
- presenceMapping
225
- ),
226
- unsubscribeCallbacks.push(
227
- room.subscribe("connection", function () {
228
- store.dispatch({
229
- type: ACTION_TYPES_UPDATE_CONNECTION,
230
- connection: room.getConnectionState(),
231
- });
232
- })
233
- ),
234
- unsubscribeCallbacks.push(
235
- room.subscribe("others", function (others) {
236
- store.dispatch({
237
- type: ACTION_TYPES_UPDATE_OTHERS,
238
- others: others.toArray(),
239
- });
240
- })
241
- ),
242
- unsubscribeCallbacks.push(
243
- room.subscribe("my-presence", function () {
244
- !1 === isPatching &&
245
- store.dispatch({
246
- type: ACTION_TYPES_PATCH_REDUX_STATE,
247
- state: patchPresenceState(
248
- room.getPresence(),
249
- presenceMapping
250
- ),
251
- });
252
- })
253
- ),
254
- store.dispatch({ type: ACTION_TYPES_START_LOADING_STORAGE }),
255
- room.getStorage().then(function (_ref) {
256
- var root = _ref.root,
257
- updates = {};
258
- room.batch(function () {
259
- for (var key in mapping) {
260
- var liveblocksStatePart = root.get(key);
261
- null == liveblocksStatePart
262
- ? ((updates[key] = storageInitialState[key]),
263
- internal.patchLiveObjectKey(
264
- root,
265
- key,
266
- void 0,
267
- storageInitialState[key]
268
- ))
269
- : (updates[key] =
270
- internal.lsonToJson(liveblocksStatePart));
271
- }
272
- }),
273
- store.dispatch({
274
- type: ACTION_TYPES_INIT_STORAGE,
275
- state: updates,
276
- }),
277
- (storageRoot = root),
278
- unsubscribeCallbacks.push(
279
- room.subscribe(
280
- root,
281
- function (updates) {
282
- !1 === isPatching &&
283
- store.dispatch({
284
- type: ACTION_TYPES_PATCH_REDUX_STATE,
285
- state: patchState(
286
- store.getState(),
287
- updates,
288
- mapping
289
- ),
290
- });
291
- },
292
- { isDeep: !0 }
293
- )
294
- );
295
- })))
296
- : action.type === ACTION_TYPES_LEAVE
297
- ? (function (roomId) {
298
- for (
299
- var _step,
300
- _iterator =
301
- _createForOfIteratorHelperLoose(unsubscribeCallbacks);
302
- !(_step = _iterator()).done;
303
53
 
304
- )
305
- (0, _step.value)();
306
- (storageRoot = null),
307
- (room = null),
308
- (isPatching = !1),
309
- (unsubscribeCallbacks = []),
310
- client.leave(roomId);
311
- })(action.roomId)
312
- : store.dispatch(action, state);
313
- },
54
+ // src/index.ts
55
+ var ACTION_TYPES = {
56
+ ENTER: "@@LIVEBLOCKS/ENTER",
57
+ LEAVE: "@@LIVEBLOCKS/LEAVE",
58
+ START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
59
+ INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
60
+ PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
61
+ UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
62
+ UPDATE_OTHERS: "@@LIVEBLOCKS/UPDATE_OTHERS"
63
+ };
64
+ var internalEnhancer = (options) => {
65
+ if (process.env.NODE_ENV !== "production" && options.client == null) {
66
+ throw missingClient();
67
+ }
68
+ const client = options.client;
69
+ const mapping = validateMapping(
70
+ options.storageMapping || {},
71
+ "storageMapping"
72
+ );
73
+ const presenceMapping = validateMapping(
74
+ options.presenceMapping || {},
75
+ "presenceMapping"
76
+ );
77
+ if (process.env.NODE_ENV !== "production") {
78
+ validateNoDuplicateKeys(mapping, presenceMapping);
79
+ }
80
+ return (createStore) => (reducer, initialState, enhancer2) => {
81
+ let room = null;
82
+ let isPatching = false;
83
+ let storageRoot = null;
84
+ let unsubscribeCallbacks = [];
85
+ const newReducer = (state, action) => {
86
+ switch (action.type) {
87
+ case ACTION_TYPES.PATCH_REDUX_STATE:
88
+ return __spreadValues(__spreadValues({}, state), action.state);
89
+ case ACTION_TYPES.INIT_STORAGE:
90
+ return __spreadProps(__spreadValues(__spreadValues({}, state), action.state), {
91
+ liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
92
+ isStorageLoading: false
93
+ })
94
+ });
95
+ case ACTION_TYPES.START_LOADING_STORAGE:
96
+ return __spreadProps(__spreadValues({}, state), {
97
+ liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
98
+ isStorageLoading: true
99
+ })
100
+ });
101
+ case ACTION_TYPES.UPDATE_CONNECTION: {
102
+ return __spreadProps(__spreadValues({}, state), {
103
+ liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
104
+ connection: action.connection
105
+ })
106
+ });
107
+ }
108
+ case ACTION_TYPES.UPDATE_OTHERS: {
109
+ return __spreadProps(__spreadValues({}, state), {
110
+ liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
111
+ others: action.others
112
+ })
113
+ });
114
+ }
115
+ default: {
116
+ const newState = reducer(state, action);
117
+ if (room) {
118
+ isPatching = true;
119
+ updatePresence(room, state, newState, presenceMapping);
120
+ room.batch(() => {
121
+ if (storageRoot) {
122
+ patchLiveblocksStorage(
123
+ storageRoot,
124
+ state,
125
+ newState,
126
+ mapping
127
+ );
128
+ }
129
+ });
130
+ isPatching = false;
131
+ }
132
+ if (newState.liveblocks == null) {
133
+ return __spreadProps(__spreadValues({}, newState), {
134
+ liveblocks: {
135
+ others: [],
136
+ isStorageLoading: false,
137
+ connection: "closed"
138
+ }
139
+ });
140
+ }
141
+ return newState;
142
+ }
143
+ }
144
+ };
145
+ const store = createStore(newReducer, initialState, enhancer2);
146
+ function enterRoom2(roomId, storageInitialState = {}, reduxState) {
147
+ if (storageRoot) {
148
+ return;
149
+ }
150
+ room = client.enter(roomId);
151
+ broadcastInitialPresence(room, reduxState, presenceMapping);
152
+ unsubscribeCallbacks.push(
153
+ room.subscribe("connection", () => {
154
+ store.dispatch({
155
+ type: ACTION_TYPES.UPDATE_CONNECTION,
156
+ connection: room.getConnectionState()
157
+ });
158
+ })
159
+ );
160
+ unsubscribeCallbacks.push(
161
+ room.subscribe("others", (others) => {
162
+ store.dispatch({
163
+ type: ACTION_TYPES.UPDATE_OTHERS,
164
+ others: others.toArray()
165
+ });
166
+ })
167
+ );
168
+ unsubscribeCallbacks.push(
169
+ room.subscribe("my-presence", () => {
170
+ if (isPatching === false) {
171
+ store.dispatch({
172
+ type: ACTION_TYPES.PATCH_REDUX_STATE,
173
+ state: patchPresenceState(
174
+ room.getPresence(),
175
+ presenceMapping
176
+ )
177
+ });
178
+ }
179
+ })
180
+ );
181
+ store.dispatch({
182
+ type: ACTION_TYPES.START_LOADING_STORAGE
183
+ });
184
+ room.getStorage().then(({ root }) => {
185
+ const updates = {};
186
+ room.batch(() => {
187
+ for (const key in mapping) {
188
+ const liveblocksStatePart = root.get(key);
189
+ if (liveblocksStatePart == null) {
190
+ updates[key] = storageInitialState[key];
191
+ _internal.patchLiveObjectKey.call(void 0,
192
+ root,
193
+ key,
194
+ void 0,
195
+ storageInitialState[key]
196
+ );
197
+ } else {
198
+ updates[key] = _internal.lsonToJson.call(void 0, liveblocksStatePart);
199
+ }
200
+ }
314
201
  });
315
- };
202
+ store.dispatch({
203
+ type: ACTION_TYPES.INIT_STORAGE,
204
+ state: updates
205
+ });
206
+ storageRoot = root;
207
+ unsubscribeCallbacks.push(
208
+ room.subscribe(
209
+ root,
210
+ (updates2) => {
211
+ if (isPatching === false) {
212
+ store.dispatch({
213
+ type: ACTION_TYPES.PATCH_REDUX_STATE,
214
+ state: patchState(
215
+ store.getState(),
216
+ updates2,
217
+ mapping
218
+ )
219
+ });
220
+ }
221
+ },
222
+ { isDeep: true }
223
+ )
224
+ );
225
+ });
316
226
  }
317
- );
227
+ function leaveRoom2(roomId) {
228
+ for (const unsubscribe of unsubscribeCallbacks) {
229
+ unsubscribe();
230
+ }
231
+ storageRoot = null;
232
+ room = null;
233
+ isPatching = false;
234
+ unsubscribeCallbacks = [];
235
+ client.leave(roomId);
236
+ }
237
+ function newDispatch(action, state) {
238
+ if (action.type === ACTION_TYPES.ENTER) {
239
+ enterRoom2(action.roomId, action.initialState, store.getState());
240
+ } else if (action.type === ACTION_TYPES.LEAVE) {
241
+ leaveRoom2(action.roomId);
242
+ } else {
243
+ store.dispatch(action, state);
244
+ }
245
+ }
246
+ return __spreadProps(__spreadValues({}, store), {
247
+ dispatch: newDispatch
248
+ });
249
+ };
250
+ };
251
+ var actions = {
252
+ enterRoom,
253
+ leaveRoom
318
254
  };
255
+ function enterRoom(roomId, initialState) {
256
+ return {
257
+ type: ACTION_TYPES.ENTER,
258
+ roomId,
259
+ initialState
260
+ };
261
+ }
262
+ function leaveRoom(roomId) {
263
+ return {
264
+ type: ACTION_TYPES.LEAVE,
265
+ roomId
266
+ };
267
+ }
268
+ var enhancer = internalEnhancer;
269
+ function patchLiveblocksStorage(root, oldState, newState, mapping) {
270
+ for (const key in mapping) {
271
+ if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
272
+ throw mappingToFunctionIsNotAllowed("value");
273
+ }
274
+ if (oldState[key] !== newState[key]) {
275
+ _internal.patchLiveObjectKey.call(void 0, root, key, oldState[key], newState[key]);
276
+ }
277
+ }
278
+ }
279
+ function broadcastInitialPresence(room, state, mapping) {
280
+ for (const key in mapping) {
281
+ room == null ? void 0 : room.updatePresence({ [key]: state[key] });
282
+ }
283
+ }
284
+ function updatePresence(room, oldState, newState, presenceMapping) {
285
+ for (const key in presenceMapping) {
286
+ if (typeof newState[key] === "function") {
287
+ throw mappingToFunctionIsNotAllowed("value");
288
+ }
289
+ if (oldState[key] !== newState[key]) {
290
+ room.updatePresence({ [key]: newState[key] });
291
+ }
292
+ }
293
+ }
294
+ function isObject(value) {
295
+ return Object.prototype.toString.call(value) === "[object Object]";
296
+ }
297
+ function validateNoDuplicateKeys(storageMapping, presenceMapping) {
298
+ for (const key in storageMapping) {
299
+ if (presenceMapping[key] !== void 0) {
300
+ throw mappingShouldNotHaveTheSameKeys(key);
301
+ }
302
+ }
303
+ }
319
304
  function patchPresenceState(presence, mapping) {
320
- var partialState = {};
321
- for (var key in mapping) partialState[key] = presence[key];
305
+ const partialState = {};
306
+ for (const key in mapping) {
307
+ partialState[key] = presence[key];
308
+ }
322
309
  return partialState;
323
310
  }
324
311
  function patchState(state, updates, mapping) {
325
- var partialState = {};
326
- for (var key in mapping) partialState[key] = state[key];
327
- var patched = internal.patchImmutableObject(partialState, updates),
328
- result = {};
329
- for (var _key in mapping) result[_key] = patched[_key];
312
+ const partialState = {};
313
+ for (const key in mapping) {
314
+ partialState[key] = state[key];
315
+ }
316
+ const patched = _internal.patchImmutableObject.call(void 0, partialState, updates);
317
+ const result = {};
318
+ for (const key in mapping) {
319
+ result[key] = patched[key];
320
+ }
330
321
  return result;
331
322
  }
332
323
  function validateMapping(mapping, mappingType) {
333
- if (
334
- "production" !== process.env.NODE_ENV &&
335
- ((value = mapping),
336
- "[object Object]" !== Object.prototype.toString.call(value))
337
- )
338
- throw (function (mappingType) {
339
- return new Error(
340
- ERROR_PREFIX +
341
- " " +
342
- mappingType +
343
- " should be an object where the values are boolean."
344
- );
345
- })(mappingType);
346
- var value,
347
- result = {};
348
- for (var key in mapping) {
349
- if (
350
- "production" !== process.env.NODE_ENV &&
351
- "boolean" != typeof mapping[key]
352
- )
324
+ if (process.env.NODE_ENV !== "production") {
325
+ if (!isObject(mapping)) {
326
+ throw mappingShouldBeAnObject(mappingType);
327
+ }
328
+ }
329
+ const result = {};
330
+ for (const key in mapping) {
331
+ if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
353
332
  throw mappingValueShouldBeABoolean(mappingType, key);
354
- !0 === mapping[key] && (result[key] = !0);
333
+ }
334
+ if (mapping[key] === true) {
335
+ result[key] = true;
336
+ }
355
337
  }
356
338
  return result;
357
339
  }
358
- (exports.actions = actions), (exports.enhancer = enhancer);
340
+
341
+
342
+
343
+ exports.actions = actions; exports.enhancer = enhancer;
package/index.mjs CHANGED
@@ -1,275 +1 @@
1
- import {
2
- patchLiveObjectKey,
3
- lsonToJson,
4
- patchImmutableObject,
5
- } from "@liveblocks/client/internal";
6
- const ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
7
- function mappingValueShouldBeABoolean(mappingType, key) {
8
- return new Error(
9
- `${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`
10
- );
11
- }
12
- function mappingShouldNotHaveTheSameKeys(key) {
13
- return new Error(
14
- `${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`
15
- );
16
- }
17
- function mappingToFunctionIsNotAllowed(key) {
18
- return new Error(
19
- `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`
20
- );
21
- }
22
- const ACTION_TYPES_ENTER = "@@LIVEBLOCKS/ENTER",
23
- ACTION_TYPES_LEAVE = "@@LIVEBLOCKS/LEAVE",
24
- ACTION_TYPES_START_LOADING_STORAGE = "@@LIVEBLOCKS/START_LOADING_STORAGE",
25
- ACTION_TYPES_INIT_STORAGE = "@@LIVEBLOCKS/INIT_STORAGE",
26
- ACTION_TYPES_PATCH_REDUX_STATE = "@@LIVEBLOCKS/PATCH_REDUX_STATE",
27
- ACTION_TYPES_UPDATE_CONNECTION = "@@LIVEBLOCKS/UPDATE_CONNECTION",
28
- ACTION_TYPES_UPDATE_OTHERS = "@@LIVEBLOCKS/UPDATE_OTHERS",
29
- actions = {
30
- enterRoom: function (roomId, initialState) {
31
- return {
32
- type: ACTION_TYPES_ENTER,
33
- roomId: roomId,
34
- initialState: initialState,
35
- };
36
- },
37
- leaveRoom: function (roomId) {
38
- return { type: ACTION_TYPES_LEAVE, roomId: roomId };
39
- },
40
- };
41
- const enhancer = (options) => {
42
- if ("production" !== process.env.NODE_ENV && null == options.client)
43
- throw new Error(`${ERROR_PREFIX} client is missing`);
44
- const client = options.client,
45
- mapping = validateMapping(options.storageMapping || {}, "storageMapping"),
46
- presenceMapping = validateMapping(
47
- options.presenceMapping || {},
48
- "presenceMapping"
49
- );
50
- return (
51
- "production" !== process.env.NODE_ENV &&
52
- (function (storageMapping, presenceMapping) {
53
- for (const key in storageMapping)
54
- if (void 0 !== presenceMapping[key])
55
- throw mappingShouldNotHaveTheSameKeys(key);
56
- })(mapping, presenceMapping),
57
- (createStore) => (reducer, initialState, enhancer) => {
58
- let room = null,
59
- isPatching = !1,
60
- storageRoot = null,
61
- unsubscribeCallbacks = [];
62
- const store = createStore(
63
- (state, action) => {
64
- switch (action.type) {
65
- case ACTION_TYPES_PATCH_REDUX_STATE:
66
- return Object.assign(Object.assign({}, state), action.state);
67
- case ACTION_TYPES_INIT_STORAGE:
68
- return Object.assign(
69
- Object.assign(Object.assign({}, state), action.state),
70
- {
71
- liveblocks: Object.assign(
72
- Object.assign({}, state.liveblocks),
73
- { isStorageLoading: !1 }
74
- ),
75
- }
76
- );
77
- case ACTION_TYPES_START_LOADING_STORAGE:
78
- return Object.assign(Object.assign({}, state), {
79
- liveblocks: Object.assign(Object.assign({}, state.liveblocks), {
80
- isStorageLoading: !0,
81
- }),
82
- });
83
- case ACTION_TYPES_UPDATE_CONNECTION:
84
- return Object.assign(Object.assign({}, state), {
85
- liveblocks: Object.assign(Object.assign({}, state.liveblocks), {
86
- connection: action.connection,
87
- }),
88
- });
89
- case ACTION_TYPES_UPDATE_OTHERS:
90
- return Object.assign(Object.assign({}, state), {
91
- liveblocks: Object.assign(Object.assign({}, state.liveblocks), {
92
- others: action.others,
93
- }),
94
- });
95
- default: {
96
- const newState = reducer(state, action);
97
- return (
98
- room &&
99
- ((isPatching = !0),
100
- (function (room, oldState, newState, presenceMapping) {
101
- for (const key in presenceMapping) {
102
- if ("function" == typeof newState[key])
103
- throw mappingToFunctionIsNotAllowed("value");
104
- oldState[key] !== newState[key] &&
105
- room.updatePresence({ [key]: newState[key] });
106
- }
107
- })(room, state, newState, presenceMapping),
108
- room.batch(() => {
109
- storageRoot &&
110
- (function (root, oldState, newState, mapping) {
111
- for (const key in mapping) {
112
- if (
113
- "production" !== process.env.NODE_ENV &&
114
- "function" == typeof newState[key]
115
- )
116
- throw mappingToFunctionIsNotAllowed("value");
117
- oldState[key] !== newState[key] &&
118
- patchLiveObjectKey(
119
- root,
120
- key,
121
- oldState[key],
122
- newState[key]
123
- );
124
- }
125
- })(storageRoot, state, newState, mapping);
126
- }),
127
- (isPatching = !1)),
128
- null == newState.liveblocks
129
- ? Object.assign(Object.assign({}, newState), {
130
- liveblocks: {
131
- others: [],
132
- isStorageLoading: !1,
133
- connection: "closed",
134
- },
135
- })
136
- : newState
137
- );
138
- }
139
- }
140
- },
141
- initialState,
142
- enhancer
143
- );
144
- return Object.assign(Object.assign({}, store), {
145
- dispatch: function (action, state) {
146
- action.type === ACTION_TYPES_ENTER
147
- ? (function (roomId, storageInitialState = {}, reduxState) {
148
- storageRoot ||
149
- ((room = client.enter(roomId)),
150
- (function (room, state, mapping) {
151
- for (const key in mapping)
152
- null == room ||
153
- room.updatePresence({ [key]: state[key] });
154
- })(room, reduxState, presenceMapping),
155
- unsubscribeCallbacks.push(
156
- room.subscribe("connection", () => {
157
- store.dispatch({
158
- type: ACTION_TYPES_UPDATE_CONNECTION,
159
- connection: room.getConnectionState(),
160
- });
161
- })
162
- ),
163
- unsubscribeCallbacks.push(
164
- room.subscribe("others", (others) => {
165
- store.dispatch({
166
- type: ACTION_TYPES_UPDATE_OTHERS,
167
- others: others.toArray(),
168
- });
169
- })
170
- ),
171
- unsubscribeCallbacks.push(
172
- room.subscribe("my-presence", () => {
173
- !1 === isPatching &&
174
- store.dispatch({
175
- type: ACTION_TYPES_PATCH_REDUX_STATE,
176
- state: patchPresenceState(
177
- room.getPresence(),
178
- presenceMapping
179
- ),
180
- });
181
- })
182
- ),
183
- store.dispatch({ type: ACTION_TYPES_START_LOADING_STORAGE }),
184
- room.getStorage().then(({ root: root }) => {
185
- const updates = {};
186
- room.batch(() => {
187
- for (const key in mapping) {
188
- const liveblocksStatePart = root.get(key);
189
- null == liveblocksStatePart
190
- ? ((updates[key] = storageInitialState[key]),
191
- patchLiveObjectKey(
192
- root,
193
- key,
194
- void 0,
195
- storageInitialState[key]
196
- ))
197
- : (updates[key] = lsonToJson(liveblocksStatePart));
198
- }
199
- }),
200
- store.dispatch({
201
- type: ACTION_TYPES_INIT_STORAGE,
202
- state: updates,
203
- }),
204
- (storageRoot = root),
205
- unsubscribeCallbacks.push(
206
- room.subscribe(
207
- root,
208
- (updates) => {
209
- !1 === isPatching &&
210
- store.dispatch({
211
- type: ACTION_TYPES_PATCH_REDUX_STATE,
212
- state: patchState(
213
- store.getState(),
214
- updates,
215
- mapping
216
- ),
217
- });
218
- },
219
- { isDeep: !0 }
220
- )
221
- );
222
- }));
223
- })(action.roomId, action.initialState, store.getState())
224
- : action.type === ACTION_TYPES_LEAVE
225
- ? (function (roomId) {
226
- for (const unsubscribe of unsubscribeCallbacks) unsubscribe();
227
- (storageRoot = null),
228
- (room = null),
229
- (isPatching = !1),
230
- (unsubscribeCallbacks = []),
231
- client.leave(roomId);
232
- })(action.roomId)
233
- : store.dispatch(action, state);
234
- },
235
- });
236
- }
237
- );
238
- };
239
- function patchPresenceState(presence, mapping) {
240
- const partialState = {};
241
- for (const key in mapping) partialState[key] = presence[key];
242
- return partialState;
243
- }
244
- function patchState(state, updates, mapping) {
245
- const partialState = {};
246
- for (const key in mapping) partialState[key] = state[key];
247
- const patched = patchImmutableObject(partialState, updates),
248
- result = {};
249
- for (const key in mapping) result[key] = patched[key];
250
- return result;
251
- }
252
- function validateMapping(mapping, mappingType) {
253
- if (
254
- "production" !== process.env.NODE_ENV &&
255
- ((value = mapping),
256
- "[object Object]" !== Object.prototype.toString.call(value))
257
- )
258
- throw (function (mappingType) {
259
- return new Error(
260
- `${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`
261
- );
262
- })(mappingType);
263
- var value;
264
- const result = {};
265
- for (const key in mapping) {
266
- if (
267
- "production" !== process.env.NODE_ENV &&
268
- "boolean" != typeof mapping[key]
269
- )
270
- throw mappingValueShouldBeABoolean(mappingType, key);
271
- !0 === mapping[key] && (result[key] = !0);
272
- }
273
- return result;
274
- }
275
- export { actions, enhancer };
1
+ export { actions, enhancer } from "./index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/redux",
3
- "version": "0.17.8",
3
+ "version": "0.17.10-debug2",
4
4
  "description": "A store enhancer to integrate Liveblocks into Redux stores.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
@@ -20,7 +20,7 @@
20
20
  "directory": "packages/liveblocks-redux"
21
21
  },
22
22
  "peerDependencies": {
23
- "@liveblocks/client": "0.17.8",
23
+ "@liveblocks/client": "0.17.10-debug2",
24
24
  "redux": "^4"
25
25
  },
26
26
  "sideEffects": false