@liveblocks/redux 1.1.1-dual3 → 1.1.1-dual5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,349 +1,2 @@
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 _core = require('@liveblocks/core');
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) => {
81
- return (reducer, initialState, enhancer2) => {
82
- let room = null;
83
- let isPatching = false;
84
- let storageRoot = null;
85
- let unsubscribeCallbacks = [];
86
- const newReducer = (state, action) => {
87
- switch (action.type) {
88
- case ACTION_TYPES.PATCH_REDUX_STATE:
89
- return __spreadValues(__spreadValues({}, state), action.state);
90
- case ACTION_TYPES.INIT_STORAGE:
91
- return __spreadProps(__spreadValues(__spreadValues({}, state), action.state), {
92
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
93
- isStorageLoading: false
94
- })
95
- });
96
- case ACTION_TYPES.START_LOADING_STORAGE:
97
- return __spreadProps(__spreadValues({}, state), {
98
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
99
- isStorageLoading: true
100
- })
101
- });
102
- case ACTION_TYPES.UPDATE_CONNECTION: {
103
- return __spreadProps(__spreadValues({}, state), {
104
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
105
- connection: action.connection,
106
- status: action.status
107
- })
108
- });
109
- }
110
- case ACTION_TYPES.UPDATE_OTHERS: {
111
- return __spreadProps(__spreadValues({}, state), {
112
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
113
- others: action.others
114
- })
115
- });
116
- }
117
- default: {
118
- const newState = reducer(state, action);
119
- if (room) {
120
- isPatching = true;
121
- updatePresence(room, state, newState, presenceMapping);
122
- room.batch(() => {
123
- if (storageRoot) {
124
- patchLiveblocksStorage(
125
- storageRoot,
126
- state,
127
- newState,
128
- mapping
129
- );
130
- }
131
- });
132
- isPatching = false;
133
- }
134
- if (newState.liveblocks == null) {
135
- return __spreadProps(__spreadValues({}, newState), {
136
- liveblocks: {
137
- others: [],
138
- isStorageLoading: false,
139
- connection: "closed",
140
- status: "initial"
141
- }
142
- });
143
- }
144
- return newState;
145
- }
146
- }
147
- };
148
- const store = createStore(newReducer, initialState, enhancer2);
149
- function enterRoom2(roomId) {
150
- if (storageRoot) {
151
- return;
152
- }
153
- const initialPresence = selectFields(
154
- store.getState(),
155
- presenceMapping
156
- );
157
- room = client.enter(roomId, { initialPresence });
158
- unsubscribeCallbacks.push(
159
- room.events.connection.subscribe(() => {
160
- store.dispatch({
161
- type: ACTION_TYPES.UPDATE_CONNECTION,
162
- connection: room.getConnectionState(),
163
- status: room.getStatus()
164
- });
165
- })
166
- );
167
- unsubscribeCallbacks.push(
168
- room.events.others.subscribe(({ others }) => {
169
- store.dispatch({
170
- type: ACTION_TYPES.UPDATE_OTHERS,
171
- others
172
- });
173
- })
174
- );
175
- unsubscribeCallbacks.push(
176
- room.events.me.subscribe(() => {
177
- if (isPatching === false) {
178
- store.dispatch({
179
- type: ACTION_TYPES.PATCH_REDUX_STATE,
180
- state: selectFields(room.getPresence(), presenceMapping)
181
- });
182
- }
183
- })
184
- );
185
- store.dispatch({
186
- type: ACTION_TYPES.START_LOADING_STORAGE
187
- });
188
- void room.getStorage().then(({ root }) => {
189
- const updates = {};
190
- room.batch(() => {
191
- for (const key in mapping) {
192
- const liveblocksStatePart = root.get(key);
193
- if (liveblocksStatePart == null) {
194
- updates[key] = store.getState()[key];
195
- _core.patchLiveObjectKey.call(void 0, root, key, void 0, store.getState()[key]);
196
- } else {
197
- updates[key] = _core.lsonToJson.call(void 0, liveblocksStatePart);
198
- }
199
- }
200
- });
201
- store.dispatch({
202
- type: ACTION_TYPES.INIT_STORAGE,
203
- state: updates
204
- });
205
- storageRoot = root;
206
- unsubscribeCallbacks.push(
207
- room.subscribe(
208
- root,
209
- (updates2) => {
210
- if (isPatching === false) {
211
- store.dispatch({
212
- type: ACTION_TYPES.PATCH_REDUX_STATE,
213
- state: patchState(
214
- store.getState(),
215
- updates2,
216
- mapping
217
- )
218
- });
219
- }
220
- },
221
- { isDeep: true }
222
- )
223
- );
224
- });
225
- }
226
- function leaveRoom2(roomId) {
227
- for (const unsubscribe of unsubscribeCallbacks) {
228
- unsubscribe();
229
- }
230
- storageRoot = null;
231
- room = null;
232
- isPatching = false;
233
- unsubscribeCallbacks = [];
234
- client.leave(roomId);
235
- }
236
- function newDispatch(action) {
237
- if (action.type === ACTION_TYPES.ENTER) {
238
- enterRoom2(action.roomId);
239
- } else if (action.type === ACTION_TYPES.LEAVE) {
240
- leaveRoom2(action.roomId);
241
- } else {
242
- store.dispatch(action);
243
- }
244
- }
245
- return __spreadProps(__spreadValues({}, store), {
246
- dispatch: newDispatch
247
- });
248
- };
249
- };
250
- };
251
- var actions = {
252
- /**
253
- * Enters a room and starts sync it with Redux state
254
- * @param roomId The id of the room
255
- */
256
- enterRoom,
257
- /**
258
- * Leaves a room and stops sync it with Redux state.
259
- * @param roomId The id of the room
260
- */
261
- leaveRoom
262
- };
263
- function enterRoom(roomId) {
264
- return {
265
- type: ACTION_TYPES.ENTER,
266
- roomId
267
- };
268
- }
269
- function leaveRoom(roomId) {
270
- return {
271
- type: ACTION_TYPES.LEAVE,
272
- roomId
273
- };
274
- }
275
- var liveblocksEnhancer = internalEnhancer;
276
- var enhancer = liveblocksEnhancer;
277
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
278
- for (const key in mapping) {
279
- if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
280
- throw mappingToFunctionIsNotAllowed("value");
281
- }
282
- if (oldState[key] !== newState[key]) {
283
- const oldVal = oldState[key];
284
- const newVal = newState[key];
285
- _core.patchLiveObjectKey.call(void 0, root, key, oldVal, newVal);
286
- }
287
- }
288
- }
289
- function updatePresence(room, oldState, newState, presenceMapping) {
290
- for (const key in presenceMapping) {
291
- if (typeof newState[key] === "function") {
292
- throw mappingToFunctionIsNotAllowed("value");
293
- }
294
- if (oldState[key] !== newState[key]) {
295
- room.updatePresence({ [key]: newState[key] });
296
- }
297
- }
298
- }
299
- function isObject(value) {
300
- return Object.prototype.toString.call(value) === "[object Object]";
301
- }
302
- function validateNoDuplicateKeys(storageMapping, presenceMapping) {
303
- for (const key in storageMapping) {
304
- if (presenceMapping[key] !== void 0) {
305
- throw mappingShouldNotHaveTheSameKeys(key);
306
- }
307
- }
308
- }
309
- function selectFields(presence, mapping) {
310
- const partialState = {};
311
- for (const key in mapping) {
312
- partialState[key] = presence[key];
313
- }
314
- return partialState;
315
- }
316
- function patchState(state, updates, mapping) {
317
- const partialState = {};
318
- for (const key in mapping) {
319
- partialState[key] = state[key];
320
- }
321
- const patched = _core.legacy_patchImmutableObject.call(void 0, partialState, updates);
322
- const result = {};
323
- for (const key in mapping) {
324
- result[key] = patched[key];
325
- }
326
- return result;
327
- }
328
- function validateMapping(mapping, mappingType) {
329
- if (process.env.NODE_ENV !== "production") {
330
- if (!isObject(mapping)) {
331
- throw mappingShouldBeAnObject(mappingType);
332
- }
333
- }
334
- const result = {};
335
- for (const key in mapping) {
336
- if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
337
- throw mappingValueShouldBeABoolean(mappingType, key);
338
- }
339
- if (mapping[key] === true) {
340
- result[key] = true;
341
- }
342
- }
343
- return result;
344
- }
345
-
346
-
347
-
348
-
349
- exports.actions = actions; exports.enhancer = enhancer; exports.liveblocksEnhancer = liveblocksEnhancer;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var D=Object.defineProperty,j=Object.defineProperties;var V=Object.getOwnPropertyDescriptors;var A=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,B=Object.prototype.propertyIsEnumerable;var M=(e,n,t)=>n in e?D(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,c=(e,n)=>{for(var t in n||(n={}))w.call(n,t)&&M(e,t,n[t]);if(A)for(var t of A(n))B.call(n,t)&&M(e,t,n[t]);return e},T=(e,n)=>j(e,V(n));var _core = require('@liveblocks/core');var y="Invalid @liveblocks/redux middleware config.";function L(){return new Error(`${y} client is missing`)}function R(e){return new Error(`${y} ${e} should be an object where the values are boolean.`)}function m(e,n){return new Error(`${y} ${e}.${n} value should be a boolean`)}function N(e){return new Error(`${y} "${e}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`)}function h(e){return new Error(`${y} mapping.${e} is invalid. Mapping to a function is not allowed.`)}var a={ENTER:"@@LIVEBLOCKS/ENTER",LEAVE:"@@LIVEBLOCKS/LEAVE",START_LOADING_STORAGE:"@@LIVEBLOCKS/START_LOADING_STORAGE",INIT_STORAGE:"@@LIVEBLOCKS/INIT_STORAGE",PATCH_REDUX_STATE:"@@LIVEBLOCKS/PATCH_REDUX_STATE",UPDATE_CONNECTION:"@@LIVEBLOCKS/UPDATE_CONNECTION",UPDATE_OTHERS:"@@LIVEBLOCKS/UPDATE_OTHERS"},H=e=>{if(process.env.NODE_ENV!=="production"&&e.client==null)throw L();let n=e.client,t=P(e.storageMapping||{},"storageMapping"),s=P(e.presenceMapping||{},"presenceMapping");return process.env.NODE_ENV!=="production"&&Y(t,s),r=>(E,S,I)=>{let i=null,b=!1,f=null,d=[],p=r((o,u)=>{switch(u.type){case a.PATCH_REDUX_STATE:return c(c({},o),u.state);case a.INIT_STORAGE:return T(c(c({},o),u.state),{liveblocks:T(c({},o.liveblocks),{isStorageLoading:!1})});case a.START_LOADING_STORAGE:return T(c({},o),{liveblocks:T(c({},o.liveblocks),{isStorageLoading:!0})});case a.UPDATE_CONNECTION:return T(c({},o),{liveblocks:T(c({},o.liveblocks),{connection:u.connection,status:u.status})});case a.UPDATE_OTHERS:return T(c({},o),{liveblocks:T(c({},o.liveblocks),{others:u.others})});default:{let l=E(o,u);return i&&(b=!0,q(i,o,l,s),i.batch(()=>{f&&F(f,o,l,t)}),b=!1),l.liveblocks==null?T(c({},l),{liveblocks:{others:[],isStorageLoading:!1,connection:"closed",status:"initial"}}):l}}},S,I);function C(o){if(f)return;let u=_(p.getState(),s);i=n.enter(o,{initialPresence:u}),d.push(i.events.connection.subscribe(()=>{p.dispatch({type:a.UPDATE_CONNECTION,connection:i.getConnectionState(),status:i.getStatus()})})),d.push(i.events.others.subscribe(({others:l})=>{p.dispatch({type:a.UPDATE_OTHERS,others:l})})),d.push(i.events.me.subscribe(()=>{b===!1&&p.dispatch({type:a.PATCH_REDUX_STATE,state:_(i.getPresence(),s)})})),p.dispatch({type:a.START_LOADING_STORAGE}),i.getStorage().then(({root:l})=>{let O={};i.batch(()=>{for(let g in t){let v=l.get(g);v==null?(O[g]=p.getState()[g],_core.patchLiveObjectKey.call(void 0, l,g,void 0,p.getState()[g])):O[g]=_core.lsonToJson.call(void 0, v)}}),p.dispatch({type:a.INIT_STORAGE,state:O}),f=l,d.push(i.subscribe(l,g=>{b===!1&&p.dispatch({type:a.PATCH_REDUX_STATE,state:z(p.getState(),g,t)})},{isDeep:!0}))})}function x(o){for(let u of d)u();f=null,i=null,b=!1,d=[],n.leave(o)}function U(o){o.type===a.ENTER?C(o.roomId):o.type===a.LEAVE?x(o.roomId):p.dispatch(o)}return T(c({},p),{dispatch:U})}},oe= exports.actions ={enterRoom:J,leaveRoom:$};function J(e){return{type:a.ENTER,roomId:e}}function $(e){return{type:a.LEAVE,roomId:e}}var X=H,se= exports.enhancer =X;function F(e,n,t,s){for(let r in s){if(process.env.NODE_ENV!=="production"&&typeof t[r]=="function")throw h("value");if(n[r]!==t[r]){let E=n[r],S=t[r];_core.patchLiveObjectKey.call(void 0, e,r,E,S)}}}function q(e,n,t,s){for(let r in s){if(typeof t[r]=="function")throw h("value");n[r]!==t[r]&&e.updatePresence({[r]:t[r]})}}function W(e){return Object.prototype.toString.call(e)==="[object Object]"}function Y(e,n){for(let t in e)if(n[t]!==void 0)throw N(t)}function _(e,n){let t={};for(let s in n)t[s]=e[s];return t}function z(e,n,t){let s={};for(let S in t)s[S]=e[S];let r=_core.legacy_patchImmutableObject.call(void 0, s,n),E={};for(let S in t)E[S]=r[S];return E}function P(e,n){if(process.env.NODE_ENV!=="production"&&!W(e))throw R(n);let t={};for(let s in e){if(process.env.NODE_ENV!=="production"&&typeof e[s]!="boolean")throw m(n,s);e[s]===!0&&(t[s]=!0)}return t}exports.actions = oe; exports.enhancer = se; exports.liveblocksEnhancer = X;
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/errors.ts"],"names":["legacy_patchImmutableObject","lsonToJson","patchLiveObjectKey","ERROR_PREFIX","missingClient","mappingShouldBeAnObject","mappingType","mappingValueShouldBeABoolean","key","mappingShouldNotHaveTheSameKeys","mappingToFunctionIsNotAllowed","ACTION_TYPES","internalEnhancer","options","client","mapping","validateMapping","presenceMapping","validateNoDuplicateKeys","createStore","reducer","initialState","enhancer","room","isPatching","storageRoot","unsubscribeCallbacks","store","state","action","__spreadValues","__spreadProps","newState","updatePresence","patchLiveblocksStorage","enterRoom","roomId","initialPresence","selectFields","others","root","updates","liveblocksStatePart","patchState","leaveRoom","unsubscribe","newDispatch","actions","liveblocksEnhancer","oldState","oldVal","newVal","isObject","value","storageMapping","presence","partialState","patched","result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8C,EAChB,GAAIrC,EAAgBT,CAAG,IAAM,OAC3B,MAAMC,EAAgCD,CAAG,CAG/C,CAEA,SAAS8B,EACPiB,EACAxC,EAEc,CACd,IAAMyC,EAAe,CAAC,EACtB,QAAWhD,KAAOO,EAChByC,EAAahD,CAAG,EAAI+C,EAAS/C,CAAG,EAElC,OAAOgD,CACT,CAEA,SAASb,EACPf,EACAa,EACA1B,EACA,CACA,IAAMyC,EAAgC,CAAC,EAEvC,QAAWhD,KAAOO,EAChByC,EAAahD,CAAG,EAAIoB,EAAMpB,CAAG,EAG/B,IAAMiD,EAAUzD,EAA4BwD,EAAcf,CAAO,EAE3DiB,EAA0B,CAAC,EAEjC,QAAWlD,KAAOO,EAChB2C,EAAOlD,CAAG,EAAIiD,EAAQjD,CAAG,EAG3B,OAAOkD,CACT,CAKA,SAAS1C,EACPD,EACAT,EACiB,CACjB,GAAI,QAAQ,IAAI,WAAa,cACvB,CAAC8C,EAASrC,CAAO,EACnB,MAAMV,EAAwBC,CAAW,EAI7C,IAAMoD,EAA0B,CAAC,EACjC,QAAWlD,KAAOO,EAAS,CACzB,GACE,QAAQ,IAAI,WAAa,cACzB,OAAOA,EAAQP,CAAG,GAAM,UAExB,MAAMD,EAA6BD,EAAaE,CAAG,EAGjDO,EAAQP,CAAG,IAAM,KACnBkD,EAAOlD,CAAG,EAAI,GAElB,CACA,OAAOkD,CACT","sourcesContent":["import type {\n BaseUserMeta,\n Client,\n Json,\n JsonObject,\n LiveObject,\n LsonObject,\n Room,\n Status,\n User,\n} from \"@liveblocks/client\";\nimport type { LegacyConnectionStatus } from \"@liveblocks/core\";\nimport {\n legacy_patchImmutableObject,\n lsonToJson,\n patchLiveObjectKey,\n} from \"@liveblocks/core\";\nimport type { StoreEnhancer } from \"redux\";\n\nimport {\n mappingShouldBeAnObject,\n mappingShouldNotHaveTheSameKeys,\n mappingToFunctionIsNotAllowed,\n mappingValueShouldBeABoolean,\n missingClient,\n} from \"./errors\";\n\nexport type Mapping<T> = {\n [K in keyof T]?: boolean;\n};\n\nconst ACTION_TYPES = {\n ENTER: \"@@LIVEBLOCKS/ENTER\",\n LEAVE: \"@@LIVEBLOCKS/LEAVE\",\n START_LOADING_STORAGE: \"@@LIVEBLOCKS/START_LOADING_STORAGE\",\n INIT_STORAGE: \"@@LIVEBLOCKS/INIT_STORAGE\",\n PATCH_REDUX_STATE: \"@@LIVEBLOCKS/PATCH_REDUX_STATE\",\n UPDATE_CONNECTION: \"@@LIVEBLOCKS/UPDATE_CONNECTION\",\n UPDATE_OTHERS: \"@@LIVEBLOCKS/UPDATE_OTHERS\",\n};\n\ntype LiveblocksContext<\n TPresence extends JsonObject,\n TUserMeta extends BaseUserMeta\n> = {\n /**\n * Other users in the room. Empty no room is currently synced\n */\n readonly others: readonly User<TPresence, TUserMeta>[];\n /**\n * Whether or not the room storage is currently loading\n */\n readonly isStorageLoading: boolean;\n /**\n * Legacy connection status of the room.\n *\n * @deprecated This API will be removed in a future version of Liveblocks.\n * Prefer using the newer `.status` property.\n *\n * We recommend making the following changes if you use these APIs:\n *\n * OLD STATUSES NEW STATUSES\n * closed --> initial\n * authenticating --> connecting\n * connecting --> connecting\n * open --> connected\n * unavailable --> reconnecting\n * failed --> disconnected\n */\n readonly connection: LegacyConnectionStatus;\n /**\n * Connection status of the room.\n */\n readonly status: Status;\n};\n\n/**\n * @deprecated Please rename to WithLiveblocks<...>\n */\nexport type LiveblocksState<\n TState,\n TPresence extends JsonObject,\n TUserMeta extends BaseUserMeta\n> = WithLiveblocks<TState, TPresence, TUserMeta>;\n\n/**\n * Adds the `liveblocks` property to your custom Redux state.\n */\nexport type WithLiveblocks<\n TState,\n TPresence extends JsonObject,\n TUserMeta extends BaseUserMeta\n> = TState & { readonly liveblocks: LiveblocksContext<TPresence, TUserMeta> };\n\nconst internalEnhancer = <TState>(options: {\n client: Client;\n storageMapping?: Mapping<TState>;\n presenceMapping?: Mapping<TState>;\n}) => {\n type OpaqueRoom = Room<JsonObject, LsonObject, BaseUserMeta, Json>;\n\n if (process.env.NODE_ENV !== \"production\" && options.client == null) {\n throw missingClient();\n }\n const client = options.client;\n const mapping = validateMapping(\n options.storageMapping || {},\n \"storageMapping\"\n );\n const presenceMapping = validateMapping(\n options.presenceMapping || {},\n \"presenceMapping\"\n );\n if (process.env.NODE_ENV !== \"production\") {\n validateNoDuplicateKeys(mapping, presenceMapping);\n }\n\n return (createStore: any) => {\n return (reducer: any, initialState: any, enhancer: any) => {\n let room: OpaqueRoom | null = null;\n let isPatching: boolean = false;\n let storageRoot: LiveObject<LsonObject> | null = null;\n let unsubscribeCallbacks: Array<() => void> = [];\n\n const newReducer = (state: any, action: any) => {\n switch (action.type) {\n case ACTION_TYPES.PATCH_REDUX_STATE:\n return {\n ...state,\n ...action.state,\n };\n case ACTION_TYPES.INIT_STORAGE:\n return {\n ...state,\n ...action.state,\n liveblocks: {\n ...state.liveblocks,\n isStorageLoading: false,\n },\n };\n case ACTION_TYPES.START_LOADING_STORAGE:\n return {\n ...state,\n liveblocks: {\n ...state.liveblocks,\n isStorageLoading: true,\n },\n };\n case ACTION_TYPES.UPDATE_CONNECTION: {\n return {\n ...state,\n liveblocks: {\n ...state.liveblocks,\n connection: action.connection,\n status: action.status,\n },\n };\n }\n case ACTION_TYPES.UPDATE_OTHERS: {\n return {\n ...state,\n liveblocks: {\n ...state.liveblocks,\n others: action.others,\n },\n };\n }\n default: {\n const newState = reducer(state, action);\n\n if (room) {\n isPatching = true;\n updatePresence(room, state, newState, presenceMapping as any);\n\n room.batch(() => {\n if (storageRoot) {\n patchLiveblocksStorage(\n storageRoot,\n state,\n newState,\n mapping as any\n );\n }\n });\n isPatching = false;\n }\n\n if (newState.liveblocks == null) {\n return {\n ...newState,\n liveblocks: {\n others: [],\n isStorageLoading: false,\n connection: \"closed\",\n status: \"initial\",\n },\n };\n }\n return newState;\n }\n }\n };\n\n const store = createStore(newReducer, initialState, enhancer);\n\n function enterRoom(roomId: string) {\n if (storageRoot) {\n return;\n }\n\n const initialPresence = selectFields(\n store.getState(),\n presenceMapping\n ) as any;\n\n room = client.enter(roomId, { initialPresence });\n\n unsubscribeCallbacks.push(\n room.events.connection.subscribe(() => {\n store.dispatch({\n type: ACTION_TYPES.UPDATE_CONNECTION,\n connection: room!.getConnectionState(),\n status: room!.getStatus(),\n });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.others.subscribe(({ others }) => {\n store.dispatch({\n type: ACTION_TYPES.UPDATE_OTHERS,\n others,\n });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.me.subscribe(() => {\n if (isPatching === false) {\n store.dispatch({\n type: ACTION_TYPES.PATCH_REDUX_STATE,\n state: selectFields(room!.getPresence(), presenceMapping),\n });\n }\n })\n );\n\n store.dispatch({\n type: ACTION_TYPES.START_LOADING_STORAGE,\n });\n\n void room.getStorage().then(({ root }) => {\n const updates: any = {};\n\n room!.batch(() => {\n for (const key in mapping) {\n const liveblocksStatePart = root.get(key);\n\n if (liveblocksStatePart == null) {\n updates[key] = store.getState()[key];\n patchLiveObjectKey(root, key, undefined, store.getState()[key]);\n } else {\n updates[key] = lsonToJson(liveblocksStatePart);\n }\n }\n });\n\n store.dispatch({\n type: ACTION_TYPES.INIT_STORAGE,\n state: updates,\n });\n\n storageRoot = root;\n unsubscribeCallbacks.push(\n room!.subscribe(\n root,\n (updates) => {\n if (isPatching === false) {\n store.dispatch({\n type: ACTION_TYPES.PATCH_REDUX_STATE,\n state: patchState(\n store.getState(),\n updates,\n mapping as any\n ),\n });\n }\n },\n { isDeep: true }\n )\n );\n });\n }\n\n function leaveRoom(roomId: string) {\n for (const unsubscribe of unsubscribeCallbacks) {\n unsubscribe();\n }\n\n storageRoot = null;\n room = null;\n isPatching = false;\n unsubscribeCallbacks = [];\n\n client.leave(roomId);\n }\n\n function newDispatch(action: any) {\n if (action.type === ACTION_TYPES.ENTER) {\n enterRoom(action.roomId);\n } else if (action.type === ACTION_TYPES.LEAVE) {\n leaveRoom(action.roomId);\n } else {\n store.dispatch(action);\n }\n }\n\n return {\n ...store,\n dispatch: newDispatch,\n };\n };\n };\n};\n\n/**\n * Actions used to interact with Liveblocks\n */\nexport const actions = {\n /**\n * Enters a room and starts sync it with Redux state\n * @param roomId The id of the room\n */\n enterRoom,\n /**\n * Leaves a room and stops sync it with Redux state.\n * @param roomId The id of the room\n */\n leaveRoom,\n};\n\nfunction enterRoom(roomId: string): {\n type: string;\n roomId: string;\n} {\n return {\n type: ACTION_TYPES.ENTER,\n roomId,\n };\n}\n\nfunction leaveRoom(roomId: string): {\n type: string;\n roomId: string;\n} {\n return {\n type: ACTION_TYPES.LEAVE,\n roomId,\n };\n}\n\n/**\n * Redux store enhancer that will make the `liveblocks` key available on your\n * Redux store.\n */\nexport const liveblocksEnhancer = internalEnhancer as <TState>(options: {\n client: Client;\n storageMapping?: Mapping<TState>;\n presenceMapping?: Mapping<TState>;\n}) => StoreEnhancer;\n\n/**\n * @deprecated Renamed to `liveblocksEnhancer`.\n */\nexport const enhancer = liveblocksEnhancer;\n\nfunction patchLiveblocksStorage<O extends LsonObject, TState>(\n root: LiveObject<O>,\n oldState: TState,\n newState: TState,\n mapping: Mapping<TState>\n) {\n for (const key in mapping) {\n if (\n process.env.NODE_ENV !== \"production\" &&\n typeof newState[key] === \"function\"\n ) {\n throw mappingToFunctionIsNotAllowed(\"value\");\n }\n\n if (oldState[key] !== newState[key]) {\n const oldVal = oldState[key];\n const newVal = newState[key];\n patchLiveObjectKey(root, key, oldVal as any, newVal);\n }\n }\n}\n\nfunction updatePresence<TPresence extends JsonObject>(\n room: Room<TPresence, any, any, any>,\n oldState: TPresence,\n newState: TPresence,\n presenceMapping: Mapping<TPresence>\n) {\n for (const key in presenceMapping) {\n if (typeof newState[key] === \"function\") {\n throw mappingToFunctionIsNotAllowed(\"value\");\n }\n\n if (oldState[key] !== newState[key]) {\n room.updatePresence({ [key]: newState[key] } as TPresence);\n }\n }\n}\n\nfunction isObject(value: any): value is object {\n return Object.prototype.toString.call(value) === \"[object Object]\";\n}\n\nfunction validateNoDuplicateKeys<TState>(\n storageMapping: Mapping<TState>,\n presenceMapping: Mapping<TState>\n) {\n for (const key in storageMapping) {\n if (presenceMapping[key] !== undefined) {\n throw mappingShouldNotHaveTheSameKeys(key);\n }\n }\n}\n\nfunction selectFields<TState>(\n presence: TState,\n mapping: Mapping<TState>\n): /* TODO: Actually, Pick<TState, keyof Mapping<TState>> ? */\nPartial<TState> {\n const partialState = {} as Partial<TState>;\n for (const key in mapping) {\n partialState[key] = presence[key];\n }\n return partialState;\n}\n\nfunction patchState<TState extends JsonObject>(\n state: TState,\n updates: any[], // StorageUpdate\n mapping: Mapping<TState>\n) {\n const partialState: Partial<TState> = {};\n\n for (const key in mapping) {\n partialState[key] = state[key];\n }\n\n const patched = legacy_patchImmutableObject(partialState, updates);\n\n const result: Partial<TState> = {};\n\n for (const key in mapping) {\n result[key] = patched[key];\n }\n\n return result;\n}\n\n/**\n * Remove false keys from mapping and generate to a new object to avoid potential mutation from outside the middleware\n */\nfunction validateMapping<TState>(\n mapping: Mapping<TState>,\n mappingType: \"storageMapping\" | \"presenceMapping\"\n): Mapping<TState> {\n if (process.env.NODE_ENV !== \"production\") {\n if (!isObject(mapping)) {\n throw mappingShouldBeAnObject(mappingType);\n }\n }\n\n const result: Mapping<TState> = {};\n for (const key in mapping) {\n if (\n process.env.NODE_ENV !== \"production\" &&\n typeof mapping[key] !== \"boolean\"\n ) {\n throw mappingValueShouldBeABoolean(mappingType, key);\n }\n\n if (mapping[key] === true) {\n result[key] = true;\n }\n }\n return result;\n}\n","export const ERROR_PREFIX = \"Invalid @liveblocks/redux middleware config.\";\n\nexport function missingClient(): Error {\n return new Error(`${ERROR_PREFIX} client is missing`);\n}\n\nexport function mappingShouldBeAnObject(\n mappingType: \"storageMapping\" | \"presenceMapping\"\n): Error {\n return new Error(\n `${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`\n );\n}\n\nexport function mappingValueShouldBeABoolean(\n mappingType: \"storageMapping\" | \"presenceMapping\",\n key: string\n): Error {\n return new Error(\n `${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`\n );\n}\n\nexport function mappingShouldNotHaveTheSameKeys(key: string): Error {\n return new Error(\n `${ERROR_PREFIX} \"${key}\" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`\n );\n}\n\nexport function mappingToFunctionIsNotAllowed(key: string): Error {\n return new Error(\n `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`\n );\n}\n"]}
package/dist/index.mjs CHANGED
@@ -1,349 +1,2 @@
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));
20
-
21
- // src/index.ts
22
- import {
23
- legacy_patchImmutableObject,
24
- lsonToJson,
25
- patchLiveObjectKey
26
- } from "@liveblocks/core";
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) => {
81
- return (reducer, initialState, enhancer2) => {
82
- let room = null;
83
- let isPatching = false;
84
- let storageRoot = null;
85
- let unsubscribeCallbacks = [];
86
- const newReducer = (state, action) => {
87
- switch (action.type) {
88
- case ACTION_TYPES.PATCH_REDUX_STATE:
89
- return __spreadValues(__spreadValues({}, state), action.state);
90
- case ACTION_TYPES.INIT_STORAGE:
91
- return __spreadProps(__spreadValues(__spreadValues({}, state), action.state), {
92
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
93
- isStorageLoading: false
94
- })
95
- });
96
- case ACTION_TYPES.START_LOADING_STORAGE:
97
- return __spreadProps(__spreadValues({}, state), {
98
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
99
- isStorageLoading: true
100
- })
101
- });
102
- case ACTION_TYPES.UPDATE_CONNECTION: {
103
- return __spreadProps(__spreadValues({}, state), {
104
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
105
- connection: action.connection,
106
- status: action.status
107
- })
108
- });
109
- }
110
- case ACTION_TYPES.UPDATE_OTHERS: {
111
- return __spreadProps(__spreadValues({}, state), {
112
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
113
- others: action.others
114
- })
115
- });
116
- }
117
- default: {
118
- const newState = reducer(state, action);
119
- if (room) {
120
- isPatching = true;
121
- updatePresence(room, state, newState, presenceMapping);
122
- room.batch(() => {
123
- if (storageRoot) {
124
- patchLiveblocksStorage(
125
- storageRoot,
126
- state,
127
- newState,
128
- mapping
129
- );
130
- }
131
- });
132
- isPatching = false;
133
- }
134
- if (newState.liveblocks == null) {
135
- return __spreadProps(__spreadValues({}, newState), {
136
- liveblocks: {
137
- others: [],
138
- isStorageLoading: false,
139
- connection: "closed",
140
- status: "initial"
141
- }
142
- });
143
- }
144
- return newState;
145
- }
146
- }
147
- };
148
- const store = createStore(newReducer, initialState, enhancer2);
149
- function enterRoom2(roomId) {
150
- if (storageRoot) {
151
- return;
152
- }
153
- const initialPresence = selectFields(
154
- store.getState(),
155
- presenceMapping
156
- );
157
- room = client.enter(roomId, { initialPresence });
158
- unsubscribeCallbacks.push(
159
- room.events.connection.subscribe(() => {
160
- store.dispatch({
161
- type: ACTION_TYPES.UPDATE_CONNECTION,
162
- connection: room.getConnectionState(),
163
- status: room.getStatus()
164
- });
165
- })
166
- );
167
- unsubscribeCallbacks.push(
168
- room.events.others.subscribe(({ others }) => {
169
- store.dispatch({
170
- type: ACTION_TYPES.UPDATE_OTHERS,
171
- others
172
- });
173
- })
174
- );
175
- unsubscribeCallbacks.push(
176
- room.events.me.subscribe(() => {
177
- if (isPatching === false) {
178
- store.dispatch({
179
- type: ACTION_TYPES.PATCH_REDUX_STATE,
180
- state: selectFields(room.getPresence(), presenceMapping)
181
- });
182
- }
183
- })
184
- );
185
- store.dispatch({
186
- type: ACTION_TYPES.START_LOADING_STORAGE
187
- });
188
- void room.getStorage().then(({ root }) => {
189
- const updates = {};
190
- room.batch(() => {
191
- for (const key in mapping) {
192
- const liveblocksStatePart = root.get(key);
193
- if (liveblocksStatePart == null) {
194
- updates[key] = store.getState()[key];
195
- patchLiveObjectKey(root, key, void 0, store.getState()[key]);
196
- } else {
197
- updates[key] = lsonToJson(liveblocksStatePart);
198
- }
199
- }
200
- });
201
- store.dispatch({
202
- type: ACTION_TYPES.INIT_STORAGE,
203
- state: updates
204
- });
205
- storageRoot = root;
206
- unsubscribeCallbacks.push(
207
- room.subscribe(
208
- root,
209
- (updates2) => {
210
- if (isPatching === false) {
211
- store.dispatch({
212
- type: ACTION_TYPES.PATCH_REDUX_STATE,
213
- state: patchState(
214
- store.getState(),
215
- updates2,
216
- mapping
217
- )
218
- });
219
- }
220
- },
221
- { isDeep: true }
222
- )
223
- );
224
- });
225
- }
226
- function leaveRoom2(roomId) {
227
- for (const unsubscribe of unsubscribeCallbacks) {
228
- unsubscribe();
229
- }
230
- storageRoot = null;
231
- room = null;
232
- isPatching = false;
233
- unsubscribeCallbacks = [];
234
- client.leave(roomId);
235
- }
236
- function newDispatch(action) {
237
- if (action.type === ACTION_TYPES.ENTER) {
238
- enterRoom2(action.roomId);
239
- } else if (action.type === ACTION_TYPES.LEAVE) {
240
- leaveRoom2(action.roomId);
241
- } else {
242
- store.dispatch(action);
243
- }
244
- }
245
- return __spreadProps(__spreadValues({}, store), {
246
- dispatch: newDispatch
247
- });
248
- };
249
- };
250
- };
251
- var actions = {
252
- /**
253
- * Enters a room and starts sync it with Redux state
254
- * @param roomId The id of the room
255
- */
256
- enterRoom,
257
- /**
258
- * Leaves a room and stops sync it with Redux state.
259
- * @param roomId The id of the room
260
- */
261
- leaveRoom
262
- };
263
- function enterRoom(roomId) {
264
- return {
265
- type: ACTION_TYPES.ENTER,
266
- roomId
267
- };
268
- }
269
- function leaveRoom(roomId) {
270
- return {
271
- type: ACTION_TYPES.LEAVE,
272
- roomId
273
- };
274
- }
275
- var liveblocksEnhancer = internalEnhancer;
276
- var enhancer = liveblocksEnhancer;
277
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
278
- for (const key in mapping) {
279
- if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
280
- throw mappingToFunctionIsNotAllowed("value");
281
- }
282
- if (oldState[key] !== newState[key]) {
283
- const oldVal = oldState[key];
284
- const newVal = newState[key];
285
- patchLiveObjectKey(root, key, oldVal, newVal);
286
- }
287
- }
288
- }
289
- function updatePresence(room, oldState, newState, presenceMapping) {
290
- for (const key in presenceMapping) {
291
- if (typeof newState[key] === "function") {
292
- throw mappingToFunctionIsNotAllowed("value");
293
- }
294
- if (oldState[key] !== newState[key]) {
295
- room.updatePresence({ [key]: newState[key] });
296
- }
297
- }
298
- }
299
- function isObject(value) {
300
- return Object.prototype.toString.call(value) === "[object Object]";
301
- }
302
- function validateNoDuplicateKeys(storageMapping, presenceMapping) {
303
- for (const key in storageMapping) {
304
- if (presenceMapping[key] !== void 0) {
305
- throw mappingShouldNotHaveTheSameKeys(key);
306
- }
307
- }
308
- }
309
- function selectFields(presence, mapping) {
310
- const partialState = {};
311
- for (const key in mapping) {
312
- partialState[key] = presence[key];
313
- }
314
- return partialState;
315
- }
316
- function patchState(state, updates, mapping) {
317
- const partialState = {};
318
- for (const key in mapping) {
319
- partialState[key] = state[key];
320
- }
321
- const patched = legacy_patchImmutableObject(partialState, updates);
322
- const result = {};
323
- for (const key in mapping) {
324
- result[key] = patched[key];
325
- }
326
- return result;
327
- }
328
- function validateMapping(mapping, mappingType) {
329
- if (process.env.NODE_ENV !== "production") {
330
- if (!isObject(mapping)) {
331
- throw mappingShouldBeAnObject(mappingType);
332
- }
333
- }
334
- const result = {};
335
- for (const key in mapping) {
336
- if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
337
- throw mappingValueShouldBeABoolean(mappingType, key);
338
- }
339
- if (mapping[key] === true) {
340
- result[key] = true;
341
- }
342
- }
343
- return result;
344
- }
345
- export {
346
- actions,
347
- enhancer,
348
- liveblocksEnhancer
349
- };
1
+ var D=Object.defineProperty,j=Object.defineProperties;var V=Object.getOwnPropertyDescriptors;var A=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,B=Object.prototype.propertyIsEnumerable;var M=(e,n,t)=>n in e?D(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,c=(e,n)=>{for(var t in n||(n={}))w.call(n,t)&&M(e,t,n[t]);if(A)for(var t of A(n))B.call(n,t)&&M(e,t,n[t]);return e},T=(e,n)=>j(e,V(n));import{legacy_patchImmutableObject as G,lsonToJson as K,patchLiveObjectKey as k}from"@liveblocks/core";var y="Invalid @liveblocks/redux middleware config.";function L(){return new Error(`${y} client is missing`)}function R(e){return new Error(`${y} ${e} should be an object where the values are boolean.`)}function m(e,n){return new Error(`${y} ${e}.${n} value should be a boolean`)}function N(e){return new Error(`${y} "${e}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`)}function h(e){return new Error(`${y} mapping.${e} is invalid. Mapping to a function is not allowed.`)}var a={ENTER:"@@LIVEBLOCKS/ENTER",LEAVE:"@@LIVEBLOCKS/LEAVE",START_LOADING_STORAGE:"@@LIVEBLOCKS/START_LOADING_STORAGE",INIT_STORAGE:"@@LIVEBLOCKS/INIT_STORAGE",PATCH_REDUX_STATE:"@@LIVEBLOCKS/PATCH_REDUX_STATE",UPDATE_CONNECTION:"@@LIVEBLOCKS/UPDATE_CONNECTION",UPDATE_OTHERS:"@@LIVEBLOCKS/UPDATE_OTHERS"},H=e=>{if(process.env.NODE_ENV!=="production"&&e.client==null)throw L();let n=e.client,t=P(e.storageMapping||{},"storageMapping"),s=P(e.presenceMapping||{},"presenceMapping");return process.env.NODE_ENV!=="production"&&Y(t,s),r=>(E,S,I)=>{let i=null,b=!1,f=null,d=[],p=r((o,u)=>{switch(u.type){case a.PATCH_REDUX_STATE:return c(c({},o),u.state);case a.INIT_STORAGE:return T(c(c({},o),u.state),{liveblocks:T(c({},o.liveblocks),{isStorageLoading:!1})});case a.START_LOADING_STORAGE:return T(c({},o),{liveblocks:T(c({},o.liveblocks),{isStorageLoading:!0})});case a.UPDATE_CONNECTION:return T(c({},o),{liveblocks:T(c({},o.liveblocks),{connection:u.connection,status:u.status})});case a.UPDATE_OTHERS:return T(c({},o),{liveblocks:T(c({},o.liveblocks),{others:u.others})});default:{let l=E(o,u);return i&&(b=!0,q(i,o,l,s),i.batch(()=>{f&&F(f,o,l,t)}),b=!1),l.liveblocks==null?T(c({},l),{liveblocks:{others:[],isStorageLoading:!1,connection:"closed",status:"initial"}}):l}}},S,I);function C(o){if(f)return;let u=_(p.getState(),s);i=n.enter(o,{initialPresence:u}),d.push(i.events.connection.subscribe(()=>{p.dispatch({type:a.UPDATE_CONNECTION,connection:i.getConnectionState(),status:i.getStatus()})})),d.push(i.events.others.subscribe(({others:l})=>{p.dispatch({type:a.UPDATE_OTHERS,others:l})})),d.push(i.events.me.subscribe(()=>{b===!1&&p.dispatch({type:a.PATCH_REDUX_STATE,state:_(i.getPresence(),s)})})),p.dispatch({type:a.START_LOADING_STORAGE}),i.getStorage().then(({root:l})=>{let O={};i.batch(()=>{for(let g in t){let v=l.get(g);v==null?(O[g]=p.getState()[g],k(l,g,void 0,p.getState()[g])):O[g]=K(v)}}),p.dispatch({type:a.INIT_STORAGE,state:O}),f=l,d.push(i.subscribe(l,g=>{b===!1&&p.dispatch({type:a.PATCH_REDUX_STATE,state:z(p.getState(),g,t)})},{isDeep:!0}))})}function x(o){for(let u of d)u();f=null,i=null,b=!1,d=[],n.leave(o)}function U(o){o.type===a.ENTER?C(o.roomId):o.type===a.LEAVE?x(o.roomId):p.dispatch(o)}return T(c({},p),{dispatch:U})}},oe={enterRoom:J,leaveRoom:$};function J(e){return{type:a.ENTER,roomId:e}}function $(e){return{type:a.LEAVE,roomId:e}}var X=H,se=X;function F(e,n,t,s){for(let r in s){if(process.env.NODE_ENV!=="production"&&typeof t[r]=="function")throw h("value");if(n[r]!==t[r]){let E=n[r],S=t[r];k(e,r,E,S)}}}function q(e,n,t,s){for(let r in s){if(typeof t[r]=="function")throw h("value");n[r]!==t[r]&&e.updatePresence({[r]:t[r]})}}function W(e){return Object.prototype.toString.call(e)==="[object Object]"}function Y(e,n){for(let t in e)if(n[t]!==void 0)throw N(t)}function _(e,n){let t={};for(let s in n)t[s]=e[s];return t}function z(e,n,t){let s={};for(let S in t)s[S]=e[S];let r=G(s,n),E={};for(let S in t)E[S]=r[S];return E}function P(e,n){if(process.env.NODE_ENV!=="production"&&!W(e))throw R(n);let t={};for(let s in e){if(process.env.NODE_ENV!=="production"&&typeof e[s]!="boolean")throw m(n,s);e[s]===!0&&(t[s]=!0)}return t}export{oe as actions,se as enhancer,X as liveblocksEnhancer};
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/errors.ts"],"sourcesContent":["import type {\n BaseUserMeta,\n Client,\n Json,\n JsonObject,\n LiveObject,\n LsonObject,\n Room,\n Status,\n User,\n} from \"@liveblocks/client\";\nimport type { LegacyConnectionStatus } from \"@liveblocks/core\";\nimport {\n legacy_patchImmutableObject,\n lsonToJson,\n patchLiveObjectKey,\n} from \"@liveblocks/core\";\nimport type { StoreEnhancer } from \"redux\";\n\nimport {\n mappingShouldBeAnObject,\n mappingShouldNotHaveTheSameKeys,\n mappingToFunctionIsNotAllowed,\n mappingValueShouldBeABoolean,\n missingClient,\n} from \"./errors\";\n\nexport type Mapping<T> = {\n [K in keyof T]?: boolean;\n};\n\nconst ACTION_TYPES = {\n ENTER: \"@@LIVEBLOCKS/ENTER\",\n LEAVE: \"@@LIVEBLOCKS/LEAVE\",\n START_LOADING_STORAGE: \"@@LIVEBLOCKS/START_LOADING_STORAGE\",\n INIT_STORAGE: \"@@LIVEBLOCKS/INIT_STORAGE\",\n PATCH_REDUX_STATE: \"@@LIVEBLOCKS/PATCH_REDUX_STATE\",\n UPDATE_CONNECTION: \"@@LIVEBLOCKS/UPDATE_CONNECTION\",\n UPDATE_OTHERS: \"@@LIVEBLOCKS/UPDATE_OTHERS\",\n};\n\ntype LiveblocksContext<\n TPresence extends JsonObject,\n TUserMeta extends BaseUserMeta\n> = {\n /**\n * Other users in the room. Empty no room is currently synced\n */\n readonly others: readonly User<TPresence, TUserMeta>[];\n /**\n * Whether or not the room storage is currently loading\n */\n readonly isStorageLoading: boolean;\n /**\n * Legacy connection status of the room.\n *\n * @deprecated This API will be removed in a future version of Liveblocks.\n * Prefer using the newer `.status` property.\n *\n * We recommend making the following changes if you use these APIs:\n *\n * OLD STATUSES NEW STATUSES\n * closed --> initial\n * authenticating --> connecting\n * connecting --> connecting\n * open --> connected\n * unavailable --> reconnecting\n * failed --> disconnected\n */\n readonly connection: LegacyConnectionStatus;\n /**\n * Connection status of the room.\n */\n readonly status: Status;\n};\n\n/**\n * @deprecated Please rename to WithLiveblocks<...>\n */\nexport type LiveblocksState<\n TState,\n TPresence extends JsonObject,\n TUserMeta extends BaseUserMeta\n> = WithLiveblocks<TState, TPresence, TUserMeta>;\n\n/**\n * Adds the `liveblocks` property to your custom Redux state.\n */\nexport type WithLiveblocks<\n TState,\n TPresence extends JsonObject,\n TUserMeta extends BaseUserMeta\n> = TState & { readonly liveblocks: LiveblocksContext<TPresence, TUserMeta> };\n\nconst internalEnhancer = <TState>(options: {\n client: Client;\n storageMapping?: Mapping<TState>;\n presenceMapping?: Mapping<TState>;\n}) => {\n type OpaqueRoom = Room<JsonObject, LsonObject, BaseUserMeta, Json>;\n\n if (process.env.NODE_ENV !== \"production\" && options.client == null) {\n throw missingClient();\n }\n const client = options.client;\n const mapping = validateMapping(\n options.storageMapping || {},\n \"storageMapping\"\n );\n const presenceMapping = validateMapping(\n options.presenceMapping || {},\n \"presenceMapping\"\n );\n if (process.env.NODE_ENV !== \"production\") {\n validateNoDuplicateKeys(mapping, presenceMapping);\n }\n\n return (createStore: any) => {\n return (reducer: any, initialState: any, enhancer: any) => {\n let room: OpaqueRoom | null = null;\n let isPatching: boolean = false;\n let storageRoot: LiveObject<LsonObject> | null = null;\n let unsubscribeCallbacks: Array<() => void> = [];\n\n const newReducer = (state: any, action: any) => {\n switch (action.type) {\n case ACTION_TYPES.PATCH_REDUX_STATE:\n return {\n ...state,\n ...action.state,\n };\n case ACTION_TYPES.INIT_STORAGE:\n return {\n ...state,\n ...action.state,\n liveblocks: {\n ...state.liveblocks,\n isStorageLoading: false,\n },\n };\n case ACTION_TYPES.START_LOADING_STORAGE:\n return {\n ...state,\n liveblocks: {\n ...state.liveblocks,\n isStorageLoading: true,\n },\n };\n case ACTION_TYPES.UPDATE_CONNECTION: {\n return {\n ...state,\n liveblocks: {\n ...state.liveblocks,\n connection: action.connection,\n status: action.status,\n },\n };\n }\n case ACTION_TYPES.UPDATE_OTHERS: {\n return {\n ...state,\n liveblocks: {\n ...state.liveblocks,\n others: action.others,\n },\n };\n }\n default: {\n const newState = reducer(state, action);\n\n if (room) {\n isPatching = true;\n updatePresence(room, state, newState, presenceMapping as any);\n\n room.batch(() => {\n if (storageRoot) {\n patchLiveblocksStorage(\n storageRoot,\n state,\n newState,\n mapping as any\n );\n }\n });\n isPatching = false;\n }\n\n if (newState.liveblocks == null) {\n return {\n ...newState,\n liveblocks: {\n others: [],\n isStorageLoading: false,\n connection: \"closed\",\n status: \"initial\",\n },\n };\n }\n return newState;\n }\n }\n };\n\n const store = createStore(newReducer, initialState, enhancer);\n\n function enterRoom(roomId: string) {\n if (storageRoot) {\n return;\n }\n\n const initialPresence = selectFields(\n store.getState(),\n presenceMapping\n ) as any;\n\n room = client.enter(roomId, { initialPresence });\n\n unsubscribeCallbacks.push(\n room.events.connection.subscribe(() => {\n store.dispatch({\n type: ACTION_TYPES.UPDATE_CONNECTION,\n connection: room!.getConnectionState(),\n status: room!.getStatus(),\n });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.others.subscribe(({ others }) => {\n store.dispatch({\n type: ACTION_TYPES.UPDATE_OTHERS,\n others,\n });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.me.subscribe(() => {\n if (isPatching === false) {\n store.dispatch({\n type: ACTION_TYPES.PATCH_REDUX_STATE,\n state: selectFields(room!.getPresence(), presenceMapping),\n });\n }\n })\n );\n\n store.dispatch({\n type: ACTION_TYPES.START_LOADING_STORAGE,\n });\n\n void room.getStorage().then(({ root }) => {\n const updates: any = {};\n\n room!.batch(() => {\n for (const key in mapping) {\n const liveblocksStatePart = root.get(key);\n\n if (liveblocksStatePart == null) {\n updates[key] = store.getState()[key];\n patchLiveObjectKey(root, key, undefined, store.getState()[key]);\n } else {\n updates[key] = lsonToJson(liveblocksStatePart);\n }\n }\n });\n\n store.dispatch({\n type: ACTION_TYPES.INIT_STORAGE,\n state: updates,\n });\n\n storageRoot = root;\n unsubscribeCallbacks.push(\n room!.subscribe(\n root,\n (updates) => {\n if (isPatching === false) {\n store.dispatch({\n type: ACTION_TYPES.PATCH_REDUX_STATE,\n state: patchState(\n store.getState(),\n updates,\n mapping as any\n ),\n });\n }\n },\n { isDeep: true }\n )\n );\n });\n }\n\n function leaveRoom(roomId: string) {\n for (const unsubscribe of unsubscribeCallbacks) {\n unsubscribe();\n }\n\n storageRoot = null;\n room = null;\n isPatching = false;\n unsubscribeCallbacks = [];\n\n client.leave(roomId);\n }\n\n function newDispatch(action: any) {\n if (action.type === ACTION_TYPES.ENTER) {\n enterRoom(action.roomId);\n } else if (action.type === ACTION_TYPES.LEAVE) {\n leaveRoom(action.roomId);\n } else {\n store.dispatch(action);\n }\n }\n\n return {\n ...store,\n dispatch: newDispatch,\n };\n };\n };\n};\n\n/**\n * Actions used to interact with Liveblocks\n */\nexport const actions = {\n /**\n * Enters a room and starts sync it with Redux state\n * @param roomId The id of the room\n */\n enterRoom,\n /**\n * Leaves a room and stops sync it with Redux state.\n * @param roomId The id of the room\n */\n leaveRoom,\n};\n\nfunction enterRoom(roomId: string): {\n type: string;\n roomId: string;\n} {\n return {\n type: ACTION_TYPES.ENTER,\n roomId,\n };\n}\n\nfunction leaveRoom(roomId: string): {\n type: string;\n roomId: string;\n} {\n return {\n type: ACTION_TYPES.LEAVE,\n roomId,\n };\n}\n\n/**\n * Redux store enhancer that will make the `liveblocks` key available on your\n * Redux store.\n */\nexport const liveblocksEnhancer = internalEnhancer as <TState>(options: {\n client: Client;\n storageMapping?: Mapping<TState>;\n presenceMapping?: Mapping<TState>;\n}) => StoreEnhancer;\n\n/**\n * @deprecated Renamed to `liveblocksEnhancer`.\n */\nexport const enhancer = liveblocksEnhancer;\n\nfunction patchLiveblocksStorage<O extends LsonObject, TState>(\n root: LiveObject<O>,\n oldState: TState,\n newState: TState,\n mapping: Mapping<TState>\n) {\n for (const key in mapping) {\n if (\n process.env.NODE_ENV !== \"production\" &&\n typeof newState[key] === \"function\"\n ) {\n throw mappingToFunctionIsNotAllowed(\"value\");\n }\n\n if (oldState[key] !== newState[key]) {\n const oldVal = oldState[key];\n const newVal = newState[key];\n patchLiveObjectKey(root, key, oldVal as any, newVal);\n }\n }\n}\n\nfunction updatePresence<TPresence extends JsonObject>(\n room: Room<TPresence, any, any, any>,\n oldState: TPresence,\n newState: TPresence,\n presenceMapping: Mapping<TPresence>\n) {\n for (const key in presenceMapping) {\n if (typeof newState[key] === \"function\") {\n throw mappingToFunctionIsNotAllowed(\"value\");\n }\n\n if (oldState[key] !== newState[key]) {\n room.updatePresence({ [key]: newState[key] } as TPresence);\n }\n }\n}\n\nfunction isObject(value: any): value is object {\n return Object.prototype.toString.call(value) === \"[object Object]\";\n}\n\nfunction validateNoDuplicateKeys<TState>(\n storageMapping: Mapping<TState>,\n presenceMapping: Mapping<TState>\n) {\n for (const key in storageMapping) {\n if (presenceMapping[key] !== undefined) {\n throw mappingShouldNotHaveTheSameKeys(key);\n }\n }\n}\n\nfunction selectFields<TState>(\n presence: TState,\n mapping: Mapping<TState>\n): /* TODO: Actually, Pick<TState, keyof Mapping<TState>> ? */\nPartial<TState> {\n const partialState = {} as Partial<TState>;\n for (const key in mapping) {\n partialState[key] = presence[key];\n }\n return partialState;\n}\n\nfunction patchState<TState extends JsonObject>(\n state: TState,\n updates: any[], // StorageUpdate\n mapping: Mapping<TState>\n) {\n const partialState: Partial<TState> = {};\n\n for (const key in mapping) {\n partialState[key] = state[key];\n }\n\n const patched = legacy_patchImmutableObject(partialState, updates);\n\n const result: Partial<TState> = {};\n\n for (const key in mapping) {\n result[key] = patched[key];\n }\n\n return result;\n}\n\n/**\n * Remove false keys from mapping and generate to a new object to avoid potential mutation from outside the middleware\n */\nfunction validateMapping<TState>(\n mapping: Mapping<TState>,\n mappingType: \"storageMapping\" | \"presenceMapping\"\n): Mapping<TState> {\n if (process.env.NODE_ENV !== \"production\") {\n if (!isObject(mapping)) {\n throw mappingShouldBeAnObject(mappingType);\n }\n }\n\n const result: Mapping<TState> = {};\n for (const key in mapping) {\n if (\n process.env.NODE_ENV !== \"production\" &&\n typeof mapping[key] !== \"boolean\"\n ) {\n throw mappingValueShouldBeABoolean(mappingType, key);\n }\n\n if (mapping[key] === true) {\n result[key] = true;\n }\n }\n return result;\n}\n","export const ERROR_PREFIX = \"Invalid @liveblocks/redux middleware config.\";\n\nexport function missingClient(): Error {\n return new Error(`${ERROR_PREFIX} client is missing`);\n}\n\nexport function mappingShouldBeAnObject(\n mappingType: \"storageMapping\" | \"presenceMapping\"\n): Error {\n return new Error(\n `${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`\n );\n}\n\nexport function mappingValueShouldBeABoolean(\n mappingType: \"storageMapping\" | \"presenceMapping\",\n key: string\n): Error {\n return new Error(\n `${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`\n );\n}\n\nexport function mappingShouldNotHaveTheSameKeys(key: string): Error {\n return new Error(\n `${ERROR_PREFIX} \"${key}\" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`\n );\n}\n\nexport function mappingToFunctionIsNotAllowed(key: string): Error {\n return new Error(\n `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`\n );\n}\n"],"mappings":"6aAYA,OACE,+BAAAA,EACA,cAAAC,EACA,sBAAAC,MACK,mBChBA,IAAMC,EAAe,+CAErB,SAASC,GAAuB,CACrC,OAAO,IAAI,MAAM,GAAGD,CAAY,oBAAoB,CACtD,CAEO,SAASE,EACdC,EACO,CACP,OAAO,IAAI,MACT,GAAGH,CAAY,IAAIG,CAAW,oDAChC,CACF,CAEO,SAASC,EACdD,EACAE,EACO,CACP,OAAO,IAAI,MACT,GAAGL,CAAY,IAAIG,CAAW,IAAIE,CAAG,4BACvC,CACF,CAEO,SAASC,EAAgCD,EAAoB,CAClE,OAAO,IAAI,MACT,GAAGL,CAAY,KAAKK,CAAG,2FACzB,CACF,CAEO,SAASE,EAA8BF,EAAoB,CAChE,OAAO,IAAI,MACT,GAAGL,CAAY,YAAYK,CAAG,oDAChC,CACF,CDFA,IAAMG,EAAe,CACnB,MAAO,qBACP,MAAO,qBACP,sBAAuB,qCACvB,aAAc,4BACd,kBAAmB,iCACnB,kBAAmB,iCACnB,cAAe,4BACjB,EAuDMC,EAA4BC,GAI5B,CAGJ,GAAI,QAAQ,IAAI,WAAa,cAAgBA,EAAQ,QAAU,KAC7D,MAAMC,EAAc,EAEtB,IAAMC,EAASF,EAAQ,OACjBG,EAAUC,EACdJ,EAAQ,gBAAkB,CAAC,EAC3B,gBACF,EACMK,EAAkBD,EACtBJ,EAAQ,iBAAmB,CAAC,EAC5B,iBACF,EACA,OAAI,QAAQ,IAAI,WAAa,cAC3BM,EAAwBH,EAASE,CAAe,EAG1CE,GACC,CAACC,EAAcC,EAAmBC,IAAkB,CACzD,IAAIC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GAChCnB,EAAK,eAAe,CAAE,CAACmB,CAAG,EAAGV,EAASU,CAAG,CAAE,CAAc,CAE7D,CACF,CAEA,SAASc,EAASC,EAA6B,CAC7C,OAAO,OAAO,UAAU,SAAS,KAAKA,CAAK,IAAM,iBACnD,CAEA,SAASvC,EACPwC,EACAzC,EACA,CACA,QAAWyB,KAAOgB,EAChB,GAAIzC,EAAgByB,CAAG,IAAM,OAC3B,MAAMiB,EAAgCjB,CAAG,CAG/C,CAEA,SAASJ,EACPsB,EACA7C,EAEc,CACd,IAAM8C,EAAe,CAAC,EACtB,QAAWnB,KAAO3B,EAChB8C,EAAanB,CAAG,EAAIkB,EAASlB,CAAG,EAElC,OAAOmB,CACT,CAEA,SAASf,EACPlB,EACAa,EACA1B,EACA,CACA,IAAM8C,EAAgC,CAAC,EAEvC,QAAWnB,KAAO3B,EAChB8C,EAAanB,CAAG,EAAId,EAAMc,CAAG,EAG/B,IAAMoB,EAAUC,EAA4BF,EAAcpB,CAAO,EAE3DuB,EAA0B,CAAC,EAEjC,QAAWtB,KAAO3B,EAChBiD,EAAOtB,CAAG,EAAIoB,EAAQpB,CAAG,EAG3B,OAAOsB,CACT,CAKA,SAAShD,EACPD,EACAkD,EACiB,CACjB,GAAI,QAAQ,IAAI,WAAa,cACvB,CAACT,EAASzC,CAAO,EACnB,MAAMmD,EAAwBD,CAAW,EAI7C,IAAMD,EAA0B,CAAC,EACjC,QAAWtB,KAAO3B,EAAS,CACzB,GACE,QAAQ,IAAI,WAAa,cACzB,OAAOA,EAAQ2B,CAAG,GAAM,UAExB,MAAMyB,EAA6BF,EAAavB,CAAG,EAGjD3B,EAAQ2B,CAAG,IAAM,KACnBsB,EAAOtB,CAAG,EAAI,GAElB,CACA,OAAOsB,CACT","names":["legacy_patchImmutableObject","lsonToJson","patchLiveObjectKey","ERROR_PREFIX","missingClient","mappingShouldBeAnObject","mappingType","mappingValueShouldBeABoolean","key","mappingShouldNotHaveTheSameKeys","mappingToFunctionIsNotAllowed","ACTION_TYPES","internalEnhancer","options","missingClient","client","mapping","validateMapping","presenceMapping","validateNoDuplicateKeys","createStore","reducer","initialState","enhancer","room","isPatching","storageRoot","unsubscribeCallbacks","store","state","action","__spreadValues","__spreadProps","newState","updatePresence","patchLiveblocksStorage","enterRoom","roomId","initialPresence","selectFields","others","root","updates","key","liveblocksStatePart","patchLiveObjectKey","lsonToJson","patchState","leaveRoom","unsubscribe","newDispatch","actions","liveblocksEnhancer","oldState","mappingToFunctionIsNotAllowed","oldVal","newVal","isObject","value","storageMapping","mappingShouldNotHaveTheSameKeys","presence","partialState","patched","legacy_patchImmutableObject","result","mappingType","mappingShouldBeAnObject","mappingValueShouldBeABoolean"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/redux",
3
- "version": "1.1.1-dual3",
3
+ "version": "1.1.1-dual5",
4
4
  "description": "A store enhancer to integrate Liveblocks into Redux stores. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -32,8 +32,8 @@
32
32
  "test:watch": "jest --silent --verbose --color=always --watch"
33
33
  },
34
34
  "dependencies": {
35
- "@liveblocks/client": "1.1.1-dual3",
36
- "@liveblocks/core": "1.1.1-dual3"
35
+ "@liveblocks/client": "1.1.1-dual5",
36
+ "@liveblocks/core": "1.1.1-dual5"
37
37
  },
38
38
  "peerDependencies": {
39
39
  "redux": "^4"