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