@liveblocks/redux 0.16.2 → 0.16.4-beta2

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/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- import { Client, User, Presence } from "@liveblocks/client";
2
- import { StoreEnhancer } from "redux";
3
- export declare type Mapping<T> = Partial<{
1
+ import { Presence, User, Client } from '@liveblocks/client';
2
+ import { StoreEnhancer } from 'redux';
3
+
4
+ declare type Mapping<T> = Partial<{
4
5
  [Property in keyof T]: boolean;
5
6
  }>;
6
- export declare type LiveblocksState<TState, TPresence extends Presence = Presence> = TState & {
7
+ declare type LiveblocksState<TState, TPresence extends Presence = Presence> = TState & {
7
8
  /**
8
9
  * Liveblocks extra state attached by the enhancer
9
10
  */
@@ -25,7 +26,7 @@ export declare type LiveblocksState<TState, TPresence extends Presence = Presenc
25
26
  /**
26
27
  * Actions used to interact with Liveblocks
27
28
  */
28
- export declare const actions: {
29
+ declare const actions: {
29
30
  /**
30
31
  * Enters a room and starts sync it with zustand state
31
32
  * @param roomId The id of the room
@@ -47,9 +48,10 @@ declare function leaveRoom(roomId: string): {
47
48
  type: string;
48
49
  roomId: string;
49
50
  };
50
- export declare const enhancer: <T>(options: {
51
+ declare const enhancer: <T>(options: {
51
52
  client: Client;
52
53
  storageMapping?: Partial<{ [Property in keyof T]: boolean; }> | undefined;
53
54
  presenceMapping?: Partial<{ [Property in keyof T]: boolean; }> | undefined;
54
55
  }) => StoreEnhancer;
55
- export {};
56
+
57
+ export { LiveblocksState, Mapping, actions, enhancer };
package/index.mjs ADDED
@@ -0,0 +1,273 @@
1
+ import { internals } from '@liveblocks/client';
2
+
3
+ const ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
4
+ function missingClient() {
5
+ return new Error(`${ERROR_PREFIX} client is missing`);
6
+ }
7
+ function mappingShouldBeAnObject(mappingType) {
8
+ return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
9
+ }
10
+ function mappingValueShouldBeABoolean(mappingType, key) {
11
+ return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
12
+ }
13
+ function mappingShouldNotHaveTheSameKeys(key) {
14
+ return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
15
+ }
16
+ function mappingToFunctionIsNotAllowed(key) {
17
+ return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
18
+ }
19
+
20
+ const { patchImmutableObject, patchLiveObjectKey, lsonToJson } = internals;
21
+ const ACTION_TYPES = {
22
+ ENTER: "@@LIVEBLOCKS/ENTER",
23
+ LEAVE: "@@LIVEBLOCKS/LEAVE",
24
+ START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
25
+ INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
26
+ PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
27
+ UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
28
+ UPDATE_OTHERS: "@@LIVEBLOCKS/UPDATE_OTHERS",
29
+ };
30
+ const internalEnhancer = (options) => {
31
+ if (process.env.NODE_ENV !== "production" && options.client == null) {
32
+ throw missingClient();
33
+ }
34
+ const client = options.client;
35
+ const mapping = validateMapping(options.storageMapping || {}, "storageMapping");
36
+ const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
37
+ if (process.env.NODE_ENV !== "production") {
38
+ validateNoDuplicateKeys(mapping, presenceMapping);
39
+ }
40
+ return (createStore) => (reducer, initialState, enhancer) => {
41
+ let room = null;
42
+ let isPatching = false;
43
+ let storageRoot = null;
44
+ let unsubscribeCallbacks = [];
45
+ const newReducer = (state, action) => {
46
+ switch (action.type) {
47
+ case ACTION_TYPES.PATCH_REDUX_STATE:
48
+ return Object.assign(Object.assign({}, state), action.state);
49
+ case ACTION_TYPES.INIT_STORAGE:
50
+ return Object.assign(Object.assign(Object.assign({}, state), action.state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { isStorageLoading: false }) });
51
+ case ACTION_TYPES.START_LOADING_STORAGE:
52
+ return Object.assign(Object.assign({}, state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { isStorageLoading: true }) });
53
+ case ACTION_TYPES.UPDATE_CONNECTION: {
54
+ return Object.assign(Object.assign({}, state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { connection: action.connection }) });
55
+ }
56
+ case ACTION_TYPES.UPDATE_OTHERS: {
57
+ return Object.assign(Object.assign({}, state), { liveblocks: Object.assign(Object.assign({}, state.liveblocks), { others: action.others }) });
58
+ }
59
+ default: {
60
+ const newState = reducer(state, action);
61
+ if (room) {
62
+ isPatching = true;
63
+ updatePresence(room, state, newState, presenceMapping);
64
+ room.batch(() => {
65
+ if (storageRoot) {
66
+ patchLiveblocksStorage(storageRoot, state, newState, mapping);
67
+ }
68
+ });
69
+ isPatching = false;
70
+ }
71
+ if (newState.liveblocks == null) {
72
+ return Object.assign(Object.assign({}, newState), { liveblocks: {
73
+ others: [],
74
+ isStorageLoading: false,
75
+ connection: "closed",
76
+ } });
77
+ }
78
+ return newState;
79
+ }
80
+ }
81
+ };
82
+ const store = createStore(newReducer, initialState, enhancer);
83
+ function enterRoom(roomId, storageInitialState = {}, reduxState) {
84
+ if (storageRoot) {
85
+ return;
86
+ }
87
+ room = client.enter(roomId);
88
+ broadcastInitialPresence(room, reduxState, presenceMapping);
89
+ unsubscribeCallbacks.push(room.subscribe("connection", () => {
90
+ store.dispatch({
91
+ type: ACTION_TYPES.UPDATE_CONNECTION,
92
+ connection: room.getConnectionState(),
93
+ });
94
+ }));
95
+ unsubscribeCallbacks.push(room.subscribe("others", (others) => {
96
+ store.dispatch({
97
+ type: ACTION_TYPES.UPDATE_OTHERS,
98
+ others: others.toArray(),
99
+ });
100
+ }));
101
+ unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
102
+ if (isPatching === false) {
103
+ store.dispatch({
104
+ type: ACTION_TYPES.PATCH_REDUX_STATE,
105
+ state: patchPresenceState(room.getPresence(), presenceMapping),
106
+ });
107
+ }
108
+ }));
109
+ store.dispatch({
110
+ type: ACTION_TYPES.START_LOADING_STORAGE,
111
+ });
112
+ room.getStorage().then(({ root }) => {
113
+ const updates = {};
114
+ room.batch(() => {
115
+ for (const key in mapping) {
116
+ const liveblocksStatePart = root.get(key);
117
+ if (liveblocksStatePart == null) {
118
+ updates[key] = storageInitialState[key];
119
+ patchLiveObjectKey(root, key, undefined, storageInitialState[key]);
120
+ }
121
+ else {
122
+ updates[key] = lsonToJson(liveblocksStatePart);
123
+ }
124
+ }
125
+ });
126
+ store.dispatch({
127
+ type: ACTION_TYPES.INIT_STORAGE,
128
+ state: updates,
129
+ });
130
+ storageRoot = root;
131
+ unsubscribeCallbacks.push(room.subscribe(root, (updates) => {
132
+ if (isPatching === false) {
133
+ store.dispatch({
134
+ type: ACTION_TYPES.PATCH_REDUX_STATE,
135
+ state: patchState(store.getState(), updates, mapping),
136
+ });
137
+ }
138
+ }, { isDeep: true }));
139
+ });
140
+ }
141
+ function leaveRoom(roomId) {
142
+ for (const unsubscribe of unsubscribeCallbacks) {
143
+ unsubscribe();
144
+ }
145
+ storageRoot = null;
146
+ room = null;
147
+ isPatching = false;
148
+ unsubscribeCallbacks = [];
149
+ client.leave(roomId);
150
+ }
151
+ function newDispatch(action, state) {
152
+ if (action.type === ACTION_TYPES.ENTER) {
153
+ enterRoom(action.roomId, action.initialState, store.getState());
154
+ }
155
+ else if (action.type === ACTION_TYPES.LEAVE) {
156
+ leaveRoom(action.roomId);
157
+ }
158
+ else {
159
+ store.dispatch(action, state);
160
+ }
161
+ }
162
+ return Object.assign(Object.assign({}, store), { dispatch: newDispatch });
163
+ };
164
+ };
165
+ /**
166
+ * Actions used to interact with Liveblocks
167
+ */
168
+ const actions = {
169
+ /**
170
+ * Enters a room and starts sync it with zustand state
171
+ * @param roomId The id of the room
172
+ * @param initialState The initial state of the room storage. If a key does not exist if your room storage root, initialState[key] will be used.
173
+ */
174
+ enterRoom,
175
+ /**
176
+ * Leaves a room and stops sync it with zustand state.
177
+ * @param roomId The id of the room
178
+ */
179
+ leaveRoom,
180
+ };
181
+ function enterRoom(roomId, initialState) {
182
+ return {
183
+ type: ACTION_TYPES.ENTER,
184
+ roomId,
185
+ initialState,
186
+ };
187
+ }
188
+ function leaveRoom(roomId) {
189
+ return {
190
+ type: ACTION_TYPES.LEAVE,
191
+ roomId,
192
+ };
193
+ }
194
+ const enhancer = internalEnhancer;
195
+ function patchLiveblocksStorage(root, oldState, newState, mapping) {
196
+ for (const key in mapping) {
197
+ if (process.env.NODE_ENV !== "production" &&
198
+ typeof newState[key] === "function") {
199
+ throw mappingToFunctionIsNotAllowed("value");
200
+ }
201
+ if (oldState[key] !== newState[key]) {
202
+ patchLiveObjectKey(root, key, oldState[key], newState[key]);
203
+ }
204
+ }
205
+ }
206
+ function broadcastInitialPresence(room, state, mapping) {
207
+ for (const key in mapping) {
208
+ room === null || room === void 0 ? void 0 : room.updatePresence({ [key]: state[key] });
209
+ }
210
+ }
211
+ function updatePresence(room, oldState, newState, presenceMapping) {
212
+ for (const key in presenceMapping) {
213
+ if (typeof newState[key] === "function") {
214
+ throw mappingToFunctionIsNotAllowed("value");
215
+ }
216
+ if (oldState[key] !== newState[key]) {
217
+ room.updatePresence({ [key]: newState[key] });
218
+ }
219
+ }
220
+ }
221
+ function isObject(value) {
222
+ return Object.prototype.toString.call(value) === "[object Object]";
223
+ }
224
+ function validateNoDuplicateKeys(storageMapping, presenceMapping) {
225
+ for (const key in storageMapping) {
226
+ if (presenceMapping[key] !== undefined) {
227
+ throw mappingShouldNotHaveTheSameKeys(key);
228
+ }
229
+ }
230
+ }
231
+ function patchPresenceState(presence, mapping) {
232
+ const partialState = {};
233
+ for (const key in mapping) {
234
+ partialState[key] = presence[key];
235
+ }
236
+ return partialState;
237
+ }
238
+ function patchState(state, updates, // StorageUpdate
239
+ mapping) {
240
+ const partialState = {};
241
+ for (const key in mapping) {
242
+ partialState[key] = state[key];
243
+ }
244
+ const patched = patchImmutableObject(partialState, updates);
245
+ const result = {};
246
+ for (const key in mapping) {
247
+ result[key] = patched[key];
248
+ }
249
+ return result;
250
+ }
251
+ /**
252
+ * Remove false keys from mapping and generate to a new object to avoid potential mutation from outside the middleware
253
+ */
254
+ function validateMapping(mapping, mappingType) {
255
+ if (process.env.NODE_ENV !== "production") {
256
+ if (!isObject(mapping)) {
257
+ throw mappingShouldBeAnObject(mappingType);
258
+ }
259
+ }
260
+ const result = {};
261
+ for (const key in mapping) {
262
+ if (process.env.NODE_ENV !== "production" &&
263
+ typeof mapping[key] !== "boolean") {
264
+ throw mappingValueShouldBeABoolean(mappingType, key);
265
+ }
266
+ if (mapping[key] === true) {
267
+ result[key] = true;
268
+ }
269
+ }
270
+ return result;
271
+ }
272
+
273
+ export { actions, enhancer };
package/package.json CHANGED
@@ -1,21 +1,13 @@
1
1
  {
2
2
  "name": "@liveblocks/redux",
3
- "version": "0.16.2",
4
- "sideEffects": false,
3
+ "version": "0.16.4-beta2",
5
4
  "description": "A store enhancer to integrate Liveblocks into Redux stores.",
6
5
  "main": "./index.js",
6
+ "module": "./index.mjs",
7
7
  "types": "./index.d.ts",
8
8
  "files": [
9
9
  "**"
10
10
  ],
11
- "exports": {
12
- ".": {
13
- "types": "./index.d.ts",
14
- "module": "./esm/index.js",
15
- "import": "./esm/index.mjs",
16
- "default": "./index.js"
17
- }
18
- },
19
11
  "keywords": [
20
12
  "redux",
21
13
  "react",
@@ -26,7 +18,9 @@
26
18
  ],
27
19
  "scripts": {
28
20
  "build": "rollup -c && cp ./package.json ./README.md ./lib",
21
+ "lint": "eslint src/ test/",
29
22
  "test": "jest --watch",
23
+ "test-ci": "jest",
30
24
  "dtslint": "dtslint --localTs node_modules/typescript/lib --expectOnly types"
31
25
  },
32
26
  "license": "Apache-2.0",
@@ -36,7 +30,7 @@
36
30
  "directory": "packages/liveblocks-redux"
37
31
  },
38
32
  "peerDependencies": {
39
- "@liveblocks/client": "0.16.2",
33
+ "@liveblocks/client": "0.16.4-beta2",
40
34
  "redux": "^4"
41
35
  },
42
36
  "devDependencies": {
@@ -47,6 +41,7 @@
47
41
  "@reduxjs/toolkit": "^1.7.2",
48
42
  "@rollup/plugin-babel": "^5.3.0",
49
43
  "@rollup/plugin-node-resolve": "^13.1.3",
44
+ "@rollup/plugin-replace": "^4.0.0",
50
45
  "@rollup/plugin-typescript": "^8.3.0",
51
46
  "@testing-library/jest-dom": "^5.16.1",
52
47
  "@testing-library/react": "^12.1.2",
@@ -54,13 +49,14 @@
54
49
  "@types/jest": "^27.4.1",
55
50
  "@typescript-eslint/eslint-plugin": "^5.18.0",
56
51
  "@typescript-eslint/parser": "^5.18.0",
57
- "esbuild": "0.14.11",
58
52
  "eslint": "^8.12.0",
59
53
  "jest": "^27.4.7",
60
54
  "msw": "^0.36.4",
61
55
  "redux": "^4.1.2",
62
56
  "rollup": "^2.64.0",
63
- "rollup-plugin-esbuild": "^4.8.2",
57
+ "rollup-plugin-command": "^1.1.3",
58
+ "rollup-plugin-dts": "^4.2.1",
64
59
  "whatwg-fetch": "^3.6.2"
65
- }
60
+ },
61
+ "sideEffects": false
66
62
  }
package/esm/index.js DELETED
@@ -1,291 +0,0 @@
1
- import { internals } from '@liveblocks/client';
2
-
3
- const ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
4
- function missingClient() {
5
- return new Error(`${ERROR_PREFIX} client is missing`);
6
- }
7
- function mappingShouldBeAnObject(mappingType) {
8
- return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
9
- }
10
- function mappingValueShouldBeABoolean(mappingType, key) {
11
- return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
12
- }
13
- function mappingShouldNotHaveTheSameKeys(key) {
14
- return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
15
- }
16
- function mappingToFunctionIsNotAllowed(key) {
17
- return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
18
- }
19
-
20
- var __defProp = Object.defineProperty;
21
- var __defProps = Object.defineProperties;
22
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
23
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
24
- var __hasOwnProp = Object.prototype.hasOwnProperty;
25
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
26
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
27
- var __spreadValues = (a, b) => {
28
- for (var prop in b || (b = {}))
29
- if (__hasOwnProp.call(b, prop))
30
- __defNormalProp(a, prop, b[prop]);
31
- if (__getOwnPropSymbols)
32
- for (var prop of __getOwnPropSymbols(b)) {
33
- if (__propIsEnum.call(b, prop))
34
- __defNormalProp(a, prop, b[prop]);
35
- }
36
- return a;
37
- };
38
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
39
- const { patchImmutableObject, patchLiveObjectKey, lsonToJson } = internals;
40
- const ACTION_TYPES = {
41
- ENTER: "@@LIVEBLOCKS/ENTER",
42
- LEAVE: "@@LIVEBLOCKS/LEAVE",
43
- START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
44
- INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
45
- PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
46
- UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
47
- UPDATE_OTHERS: "@@LIVEBLOCKS/UPDATE_OTHERS"
48
- };
49
- const internalEnhancer = (options) => {
50
- if (process.env.NODE_ENV !== "production" && options.client == null) {
51
- throw missingClient();
52
- }
53
- const client = options.client;
54
- const mapping = validateMapping(options.storageMapping || {}, "storageMapping");
55
- const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
56
- if (process.env.NODE_ENV !== "production") {
57
- validateNoDuplicateKeys(mapping, presenceMapping);
58
- }
59
- return (createStore) => (reducer, initialState, enhancer2) => {
60
- let room = null;
61
- let isPatching = false;
62
- let storageRoot = null;
63
- let unsubscribeCallbacks = [];
64
- const newReducer = (state, action) => {
65
- switch (action.type) {
66
- case ACTION_TYPES.PATCH_REDUX_STATE:
67
- return __spreadValues(__spreadValues({}, state), action.state);
68
- case ACTION_TYPES.INIT_STORAGE:
69
- return __spreadProps(__spreadValues(__spreadValues({}, state), action.state), {
70
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
71
- isStorageLoading: false
72
- })
73
- });
74
- case ACTION_TYPES.START_LOADING_STORAGE:
75
- return __spreadProps(__spreadValues({}, state), {
76
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
77
- isStorageLoading: true
78
- })
79
- });
80
- case ACTION_TYPES.UPDATE_CONNECTION: {
81
- return __spreadProps(__spreadValues({}, state), {
82
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
83
- connection: action.connection
84
- })
85
- });
86
- }
87
- case ACTION_TYPES.UPDATE_OTHERS: {
88
- return __spreadProps(__spreadValues({}, state), {
89
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
90
- others: action.others
91
- })
92
- });
93
- }
94
- default: {
95
- const newState = reducer(state, action);
96
- if (room) {
97
- isPatching = true;
98
- updatePresence(room, state, newState, presenceMapping);
99
- room.batch(() => {
100
- if (storageRoot) {
101
- patchLiveblocksStorage(storageRoot, state, newState, mapping);
102
- }
103
- });
104
- isPatching = false;
105
- }
106
- if (newState.liveblocks == null) {
107
- return __spreadProps(__spreadValues({}, newState), {
108
- liveblocks: {
109
- others: [],
110
- isStorageLoading: false,
111
- connection: "closed"
112
- }
113
- });
114
- }
115
- return newState;
116
- }
117
- }
118
- };
119
- const store = createStore(newReducer, initialState, enhancer2);
120
- function enterRoom2(roomId, storageInitialState = {}, reduxState) {
121
- if (storageRoot) {
122
- return;
123
- }
124
- room = client.enter(roomId);
125
- broadcastInitialPresence(room, reduxState, presenceMapping);
126
- unsubscribeCallbacks.push(room.subscribe("connection", () => {
127
- store.dispatch({
128
- type: ACTION_TYPES.UPDATE_CONNECTION,
129
- connection: room.getConnectionState()
130
- });
131
- }));
132
- unsubscribeCallbacks.push(room.subscribe("others", (others) => {
133
- store.dispatch({
134
- type: ACTION_TYPES.UPDATE_OTHERS,
135
- others: others.toArray()
136
- });
137
- }));
138
- unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
139
- if (isPatching === false) {
140
- store.dispatch({
141
- type: ACTION_TYPES.PATCH_REDUX_STATE,
142
- state: patchPresenceState(room.getPresence(), presenceMapping)
143
- });
144
- }
145
- }));
146
- store.dispatch({
147
- type: ACTION_TYPES.START_LOADING_STORAGE
148
- });
149
- room.getStorage().then(({ root }) => {
150
- const updates = {};
151
- room.batch(() => {
152
- for (const key in mapping) {
153
- const liveblocksStatePart = root.get(key);
154
- if (liveblocksStatePart == null) {
155
- updates[key] = storageInitialState[key];
156
- patchLiveObjectKey(root, key, void 0, storageInitialState[key]);
157
- } else {
158
- updates[key] = lsonToJson(liveblocksStatePart);
159
- }
160
- }
161
- });
162
- store.dispatch({
163
- type: ACTION_TYPES.INIT_STORAGE,
164
- state: updates
165
- });
166
- storageRoot = root;
167
- unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
168
- if (isPatching === false) {
169
- store.dispatch({
170
- type: ACTION_TYPES.PATCH_REDUX_STATE,
171
- state: patchState(store.getState(), updates2, mapping)
172
- });
173
- }
174
- }, { isDeep: true }));
175
- });
176
- }
177
- function leaveRoom2(roomId) {
178
- for (const unsubscribe of unsubscribeCallbacks) {
179
- unsubscribe();
180
- }
181
- storageRoot = null;
182
- room = null;
183
- isPatching = false;
184
- unsubscribeCallbacks = [];
185
- client.leave(roomId);
186
- }
187
- function newDispatch(action, state) {
188
- if (action.type === ACTION_TYPES.ENTER) {
189
- enterRoom2(action.roomId, action.initialState, store.getState());
190
- } else if (action.type === ACTION_TYPES.LEAVE) {
191
- leaveRoom2(action.roomId);
192
- } else {
193
- store.dispatch(action, state);
194
- }
195
- }
196
- return __spreadProps(__spreadValues({}, store), {
197
- dispatch: newDispatch
198
- });
199
- };
200
- };
201
- const actions = {
202
- enterRoom,
203
- leaveRoom
204
- };
205
- function enterRoom(roomId, initialState) {
206
- return {
207
- type: ACTION_TYPES.ENTER,
208
- roomId,
209
- initialState
210
- };
211
- }
212
- function leaveRoom(roomId) {
213
- return {
214
- type: ACTION_TYPES.LEAVE,
215
- roomId
216
- };
217
- }
218
- const enhancer = internalEnhancer;
219
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
220
- for (const key in mapping) {
221
- if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
222
- throw mappingToFunctionIsNotAllowed("value");
223
- }
224
- if (oldState[key] !== newState[key]) {
225
- patchLiveObjectKey(root, key, oldState[key], newState[key]);
226
- }
227
- }
228
- }
229
- function broadcastInitialPresence(room, state, mapping) {
230
- for (const key in mapping) {
231
- room == null ? void 0 : room.updatePresence({ [key]: state[key] });
232
- }
233
- }
234
- function updatePresence(room, oldState, newState, presenceMapping) {
235
- for (const key in presenceMapping) {
236
- if (typeof newState[key] === "function") {
237
- throw mappingToFunctionIsNotAllowed("value");
238
- }
239
- if (oldState[key] !== newState[key]) {
240
- room.updatePresence({ [key]: newState[key] });
241
- }
242
- }
243
- }
244
- function isObject(value) {
245
- return Object.prototype.toString.call(value) === "[object Object]";
246
- }
247
- function validateNoDuplicateKeys(storageMapping, presenceMapping) {
248
- for (const key in storageMapping) {
249
- if (presenceMapping[key] !== void 0) {
250
- throw mappingShouldNotHaveTheSameKeys(key);
251
- }
252
- }
253
- }
254
- function patchPresenceState(presence, mapping) {
255
- const partialState = {};
256
- for (const key in mapping) {
257
- partialState[key] = presence[key];
258
- }
259
- return partialState;
260
- }
261
- function patchState(state, updates, mapping) {
262
- const partialState = {};
263
- for (const key in mapping) {
264
- partialState[key] = state[key];
265
- }
266
- const patched = patchImmutableObject(partialState, updates);
267
- const result = {};
268
- for (const key in mapping) {
269
- result[key] = patched[key];
270
- }
271
- return result;
272
- }
273
- function validateMapping(mapping, mappingType) {
274
- if (process.env.NODE_ENV !== "production") {
275
- if (!isObject(mapping)) {
276
- throw mappingShouldBeAnObject(mappingType);
277
- }
278
- }
279
- const result = {};
280
- for (const key in mapping) {
281
- if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
282
- throw mappingValueShouldBeABoolean(mappingType, key);
283
- }
284
- if (mapping[key] === true) {
285
- result[key] = true;
286
- }
287
- }
288
- return result;
289
- }
290
-
291
- export { actions, enhancer };
package/esm/index.mjs DELETED
@@ -1,291 +0,0 @@
1
- import { internals } from '@liveblocks/client';
2
-
3
- const ERROR_PREFIX = "Invalid @liveblocks/redux middleware config.";
4
- function missingClient() {
5
- return new Error(`${ERROR_PREFIX} client is missing`);
6
- }
7
- function mappingShouldBeAnObject(mappingType) {
8
- return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
9
- }
10
- function mappingValueShouldBeABoolean(mappingType, key) {
11
- return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
12
- }
13
- function mappingShouldNotHaveTheSameKeys(key) {
14
- return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
15
- }
16
- function mappingToFunctionIsNotAllowed(key) {
17
- return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
18
- }
19
-
20
- var __defProp = Object.defineProperty;
21
- var __defProps = Object.defineProperties;
22
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
23
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
24
- var __hasOwnProp = Object.prototype.hasOwnProperty;
25
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
26
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
27
- var __spreadValues = (a, b) => {
28
- for (var prop in b || (b = {}))
29
- if (__hasOwnProp.call(b, prop))
30
- __defNormalProp(a, prop, b[prop]);
31
- if (__getOwnPropSymbols)
32
- for (var prop of __getOwnPropSymbols(b)) {
33
- if (__propIsEnum.call(b, prop))
34
- __defNormalProp(a, prop, b[prop]);
35
- }
36
- return a;
37
- };
38
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
39
- const { patchImmutableObject, patchLiveObjectKey, lsonToJson } = internals;
40
- const ACTION_TYPES = {
41
- ENTER: "@@LIVEBLOCKS/ENTER",
42
- LEAVE: "@@LIVEBLOCKS/LEAVE",
43
- START_LOADING_STORAGE: "@@LIVEBLOCKS/START_LOADING_STORAGE",
44
- INIT_STORAGE: "@@LIVEBLOCKS/INIT_STORAGE",
45
- PATCH_REDUX_STATE: "@@LIVEBLOCKS/PATCH_REDUX_STATE",
46
- UPDATE_CONNECTION: "@@LIVEBLOCKS/UPDATE_CONNECTION",
47
- UPDATE_OTHERS: "@@LIVEBLOCKS/UPDATE_OTHERS"
48
- };
49
- const internalEnhancer = (options) => {
50
- if (process.env.NODE_ENV !== "production" && options.client == null) {
51
- throw missingClient();
52
- }
53
- const client = options.client;
54
- const mapping = validateMapping(options.storageMapping || {}, "storageMapping");
55
- const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
56
- if (process.env.NODE_ENV !== "production") {
57
- validateNoDuplicateKeys(mapping, presenceMapping);
58
- }
59
- return (createStore) => (reducer, initialState, enhancer2) => {
60
- let room = null;
61
- let isPatching = false;
62
- let storageRoot = null;
63
- let unsubscribeCallbacks = [];
64
- const newReducer = (state, action) => {
65
- switch (action.type) {
66
- case ACTION_TYPES.PATCH_REDUX_STATE:
67
- return __spreadValues(__spreadValues({}, state), action.state);
68
- case ACTION_TYPES.INIT_STORAGE:
69
- return __spreadProps(__spreadValues(__spreadValues({}, state), action.state), {
70
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
71
- isStorageLoading: false
72
- })
73
- });
74
- case ACTION_TYPES.START_LOADING_STORAGE:
75
- return __spreadProps(__spreadValues({}, state), {
76
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
77
- isStorageLoading: true
78
- })
79
- });
80
- case ACTION_TYPES.UPDATE_CONNECTION: {
81
- return __spreadProps(__spreadValues({}, state), {
82
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
83
- connection: action.connection
84
- })
85
- });
86
- }
87
- case ACTION_TYPES.UPDATE_OTHERS: {
88
- return __spreadProps(__spreadValues({}, state), {
89
- liveblocks: __spreadProps(__spreadValues({}, state.liveblocks), {
90
- others: action.others
91
- })
92
- });
93
- }
94
- default: {
95
- const newState = reducer(state, action);
96
- if (room) {
97
- isPatching = true;
98
- updatePresence(room, state, newState, presenceMapping);
99
- room.batch(() => {
100
- if (storageRoot) {
101
- patchLiveblocksStorage(storageRoot, state, newState, mapping);
102
- }
103
- });
104
- isPatching = false;
105
- }
106
- if (newState.liveblocks == null) {
107
- return __spreadProps(__spreadValues({}, newState), {
108
- liveblocks: {
109
- others: [],
110
- isStorageLoading: false,
111
- connection: "closed"
112
- }
113
- });
114
- }
115
- return newState;
116
- }
117
- }
118
- };
119
- const store = createStore(newReducer, initialState, enhancer2);
120
- function enterRoom2(roomId, storageInitialState = {}, reduxState) {
121
- if (storageRoot) {
122
- return;
123
- }
124
- room = client.enter(roomId);
125
- broadcastInitialPresence(room, reduxState, presenceMapping);
126
- unsubscribeCallbacks.push(room.subscribe("connection", () => {
127
- store.dispatch({
128
- type: ACTION_TYPES.UPDATE_CONNECTION,
129
- connection: room.getConnectionState()
130
- });
131
- }));
132
- unsubscribeCallbacks.push(room.subscribe("others", (others) => {
133
- store.dispatch({
134
- type: ACTION_TYPES.UPDATE_OTHERS,
135
- others: others.toArray()
136
- });
137
- }));
138
- unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
139
- if (isPatching === false) {
140
- store.dispatch({
141
- type: ACTION_TYPES.PATCH_REDUX_STATE,
142
- state: patchPresenceState(room.getPresence(), presenceMapping)
143
- });
144
- }
145
- }));
146
- store.dispatch({
147
- type: ACTION_TYPES.START_LOADING_STORAGE
148
- });
149
- room.getStorage().then(({ root }) => {
150
- const updates = {};
151
- room.batch(() => {
152
- for (const key in mapping) {
153
- const liveblocksStatePart = root.get(key);
154
- if (liveblocksStatePart == null) {
155
- updates[key] = storageInitialState[key];
156
- patchLiveObjectKey(root, key, void 0, storageInitialState[key]);
157
- } else {
158
- updates[key] = lsonToJson(liveblocksStatePart);
159
- }
160
- }
161
- });
162
- store.dispatch({
163
- type: ACTION_TYPES.INIT_STORAGE,
164
- state: updates
165
- });
166
- storageRoot = root;
167
- unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
168
- if (isPatching === false) {
169
- store.dispatch({
170
- type: ACTION_TYPES.PATCH_REDUX_STATE,
171
- state: patchState(store.getState(), updates2, mapping)
172
- });
173
- }
174
- }, { isDeep: true }));
175
- });
176
- }
177
- function leaveRoom2(roomId) {
178
- for (const unsubscribe of unsubscribeCallbacks) {
179
- unsubscribe();
180
- }
181
- storageRoot = null;
182
- room = null;
183
- isPatching = false;
184
- unsubscribeCallbacks = [];
185
- client.leave(roomId);
186
- }
187
- function newDispatch(action, state) {
188
- if (action.type === ACTION_TYPES.ENTER) {
189
- enterRoom2(action.roomId, action.initialState, store.getState());
190
- } else if (action.type === ACTION_TYPES.LEAVE) {
191
- leaveRoom2(action.roomId);
192
- } else {
193
- store.dispatch(action, state);
194
- }
195
- }
196
- return __spreadProps(__spreadValues({}, store), {
197
- dispatch: newDispatch
198
- });
199
- };
200
- };
201
- const actions = {
202
- enterRoom,
203
- leaveRoom
204
- };
205
- function enterRoom(roomId, initialState) {
206
- return {
207
- type: ACTION_TYPES.ENTER,
208
- roomId,
209
- initialState
210
- };
211
- }
212
- function leaveRoom(roomId) {
213
- return {
214
- type: ACTION_TYPES.LEAVE,
215
- roomId
216
- };
217
- }
218
- const enhancer = internalEnhancer;
219
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
220
- for (const key in mapping) {
221
- if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
222
- throw mappingToFunctionIsNotAllowed("value");
223
- }
224
- if (oldState[key] !== newState[key]) {
225
- patchLiveObjectKey(root, key, oldState[key], newState[key]);
226
- }
227
- }
228
- }
229
- function broadcastInitialPresence(room, state, mapping) {
230
- for (const key in mapping) {
231
- room == null ? void 0 : room.updatePresence({ [key]: state[key] });
232
- }
233
- }
234
- function updatePresence(room, oldState, newState, presenceMapping) {
235
- for (const key in presenceMapping) {
236
- if (typeof newState[key] === "function") {
237
- throw mappingToFunctionIsNotAllowed("value");
238
- }
239
- if (oldState[key] !== newState[key]) {
240
- room.updatePresence({ [key]: newState[key] });
241
- }
242
- }
243
- }
244
- function isObject(value) {
245
- return Object.prototype.toString.call(value) === "[object Object]";
246
- }
247
- function validateNoDuplicateKeys(storageMapping, presenceMapping) {
248
- for (const key in storageMapping) {
249
- if (presenceMapping[key] !== void 0) {
250
- throw mappingShouldNotHaveTheSameKeys(key);
251
- }
252
- }
253
- }
254
- function patchPresenceState(presence, mapping) {
255
- const partialState = {};
256
- for (const key in mapping) {
257
- partialState[key] = presence[key];
258
- }
259
- return partialState;
260
- }
261
- function patchState(state, updates, mapping) {
262
- const partialState = {};
263
- for (const key in mapping) {
264
- partialState[key] = state[key];
265
- }
266
- const patched = patchImmutableObject(partialState, updates);
267
- const result = {};
268
- for (const key in mapping) {
269
- result[key] = patched[key];
270
- }
271
- return result;
272
- }
273
- function validateMapping(mapping, mappingType) {
274
- if (process.env.NODE_ENV !== "production") {
275
- if (!isObject(mapping)) {
276
- throw mappingShouldBeAnObject(mappingType);
277
- }
278
- }
279
- const result = {};
280
- for (const key in mapping) {
281
- if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
282
- throw mappingValueShouldBeABoolean(mappingType, key);
283
- }
284
- if (mapping[key] === true) {
285
- result[key] = true;
286
- }
287
- }
288
- return result;
289
- }
290
-
291
- export { actions, enhancer };