@liveblocks/redux 0.17.11-debug1 → 0.17.11-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.
Files changed (2) hide show
  1. package/index.mjs +342 -4
  2. package/package.json +2 -2
package/index.mjs CHANGED
@@ -1,5 +1,343 @@
1
- import mod from "./index.js";
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
2
20
 
3
- export default mod;
4
- export const actions = mod.actions;
5
- export const enhancer = mod.enhancer;
21
+ // src/index.ts
22
+ import {
23
+ lsonToJson,
24
+ patchImmutableObject,
25
+ patchLiveObjectKey
26
+ } from "@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`);
32
+ }
33
+ function mappingShouldBeAnObject(mappingType) {
34
+ return new Error(
35
+ `${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`
36
+ );
37
+ }
38
+ function mappingValueShouldBeABoolean(mappingType, key) {
39
+ return new Error(
40
+ `${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`
41
+ );
42
+ }
43
+ function mappingShouldNotHaveTheSameKeys(key) {
44
+ return new Error(
45
+ `${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`
46
+ );
47
+ }
48
+ function mappingToFunctionIsNotAllowed(key) {
49
+ return new Error(
50
+ `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`
51
+ );
52
+ }
53
+
54
+ // src/index.ts
55
+ var ACTION_TYPES = {
56
+ ENTER: "@@LIVEBLOCKS/ENTER",
57
+ LEAVE: "@@LIVEBLOCKS/LEAVE",
58
+ START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
59
+ INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
60
+ PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
61
+ UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
62
+ UPDATE_OTHERS: "@@LIVEBLOCKS/UPDATE_OTHERS"
63
+ };
64
+ var internalEnhancer = (options) => {
65
+ if (process.env.NODE_ENV !== "production" && options.client == null) {
66
+ throw missingClient();
67
+ }
68
+ const client = options.client;
69
+ const mapping = validateMapping(
70
+ options.storageMapping || {},
71
+ "storageMapping"
72
+ );
73
+ const presenceMapping = validateMapping(
74
+ options.presenceMapping || {},
75
+ "presenceMapping"
76
+ );
77
+ if (process.env.NODE_ENV !== "production") {
78
+ validateNoDuplicateKeys(mapping, presenceMapping);
79
+ }
80
+ return (createStore) => (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
+ patchLiveObjectKey(
192
+ root,
193
+ key,
194
+ void 0,
195
+ storageInitialState[key]
196
+ );
197
+ } else {
198
+ updates[key] = lsonToJson(liveblocksStatePart);
199
+ }
200
+ }
201
+ });
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
+ });
226
+ }
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
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
+ patchLiveObjectKey(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
+ }
304
+ function patchPresenceState(presence, mapping) {
305
+ const partialState = {};
306
+ for (const key in mapping) {
307
+ partialState[key] = presence[key];
308
+ }
309
+ return partialState;
310
+ }
311
+ function patchState(state, updates, mapping) {
312
+ const partialState = {};
313
+ for (const key in mapping) {
314
+ partialState[key] = state[key];
315
+ }
316
+ const patched = patchImmutableObject(partialState, updates);
317
+ const result = {};
318
+ for (const key in mapping) {
319
+ result[key] = patched[key];
320
+ }
321
+ return result;
322
+ }
323
+ function validateMapping(mapping, mappingType) {
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") {
332
+ throw mappingValueShouldBeABoolean(mappingType, key);
333
+ }
334
+ if (mapping[key] === true) {
335
+ result[key] = true;
336
+ }
337
+ }
338
+ return result;
339
+ }
340
+ export {
341
+ actions,
342
+ enhancer
343
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/redux",
3
- "version": "0.17.11-debug1",
3
+ "version": "0.17.11-debug2",
4
4
  "description": "A store enhancer to integrate Liveblocks into Redux stores.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
@@ -27,7 +27,7 @@
27
27
  "directory": "packages/liveblocks-redux"
28
28
  },
29
29
  "peerDependencies": {
30
- "@liveblocks/client": "0.17.11-debug1",
30
+ "@liveblocks/client": "0.17.11-debug2",
31
31
  "redux": "^4"
32
32
  },
33
33
  "sideEffects": false