@liveblocks/zustand 0.15.0-alpha.3 → 0.15.0-alpha.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/lib/esm/index.js +7 -6
- package/lib/esm/index.mjs +7 -6
- package/lib/index.d.ts +1 -1
- package/lib/index.js +38 -35
- package/package.json +4 -3
package/lib/esm/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { internals } from '@liveblocks/client';
|
|
2
2
|
|
|
3
3
|
const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
4
4
|
function missingClient() {
|
|
@@ -39,15 +39,16 @@ var __spreadValues = (a, b) => {
|
|
|
39
39
|
return a;
|
|
40
40
|
};
|
|
41
41
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
42
|
+
const { patchLiveObjectKey, patchImmutableObject, liveNodeToJson } = internals;
|
|
42
43
|
function middleware(config, options) {
|
|
43
44
|
if (process.env.NODE_ENV !== "production" && options.client == null) {
|
|
44
45
|
throw missingClient();
|
|
45
46
|
}
|
|
46
47
|
const client = options.client;
|
|
47
|
-
const
|
|
48
|
+
const storageMapping = validateMapping(options.storageMapping || {}, "storageMapping");
|
|
48
49
|
const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
|
|
49
50
|
if (process.env.NODE_ENV !== "production") {
|
|
50
|
-
validateNoDuplicateKeys(
|
|
51
|
+
validateNoDuplicateKeys(storageMapping, presenceMapping);
|
|
51
52
|
}
|
|
52
53
|
return (set, get, api) => {
|
|
53
54
|
const typedSet = set;
|
|
@@ -64,7 +65,7 @@ function middleware(config, options) {
|
|
|
64
65
|
updatePresence(room, oldState, newState, presenceMapping);
|
|
65
66
|
room.batch(() => {
|
|
66
67
|
if (storageRoot) {
|
|
67
|
-
patchLiveblocksStorage(storageRoot, oldState, newState,
|
|
68
|
+
patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
|
|
68
69
|
}
|
|
69
70
|
});
|
|
70
71
|
isPatching = false;
|
|
@@ -89,7 +90,7 @@ function middleware(config, options) {
|
|
|
89
90
|
room.getStorage().then(({ root }) => {
|
|
90
91
|
const updates = {};
|
|
91
92
|
room.batch(() => {
|
|
92
|
-
for (const key in
|
|
93
|
+
for (const key in storageMapping) {
|
|
93
94
|
const liveblocksStatePart = root.get(key);
|
|
94
95
|
if (liveblocksStatePart == null) {
|
|
95
96
|
updates[key] = initialState[key];
|
|
@@ -103,7 +104,7 @@ function middleware(config, options) {
|
|
|
103
104
|
storageRoot = root;
|
|
104
105
|
unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
|
|
105
106
|
if (isPatching === false) {
|
|
106
|
-
set(patchState(get(), updates2,
|
|
107
|
+
set(patchState(get(), updates2, storageMapping));
|
|
107
108
|
}
|
|
108
109
|
}, { isDeep: true }));
|
|
109
110
|
updateZustandLiveblocksState(set, { isStorageLoading: false });
|
package/lib/esm/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { internals } from '@liveblocks/client';
|
|
2
2
|
|
|
3
3
|
const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
4
4
|
function missingClient() {
|
|
@@ -39,15 +39,16 @@ var __spreadValues = (a, b) => {
|
|
|
39
39
|
return a;
|
|
40
40
|
};
|
|
41
41
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
42
|
+
const { patchLiveObjectKey, patchImmutableObject, liveNodeToJson } = internals;
|
|
42
43
|
function middleware(config, options) {
|
|
43
44
|
if (process.env.NODE_ENV !== "production" && options.client == null) {
|
|
44
45
|
throw missingClient();
|
|
45
46
|
}
|
|
46
47
|
const client = options.client;
|
|
47
|
-
const
|
|
48
|
+
const storageMapping = validateMapping(options.storageMapping || {}, "storageMapping");
|
|
48
49
|
const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
|
|
49
50
|
if (process.env.NODE_ENV !== "production") {
|
|
50
|
-
validateNoDuplicateKeys(
|
|
51
|
+
validateNoDuplicateKeys(storageMapping, presenceMapping);
|
|
51
52
|
}
|
|
52
53
|
return (set, get, api) => {
|
|
53
54
|
const typedSet = set;
|
|
@@ -64,7 +65,7 @@ function middleware(config, options) {
|
|
|
64
65
|
updatePresence(room, oldState, newState, presenceMapping);
|
|
65
66
|
room.batch(() => {
|
|
66
67
|
if (storageRoot) {
|
|
67
|
-
patchLiveblocksStorage(storageRoot, oldState, newState,
|
|
68
|
+
patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
|
|
68
69
|
}
|
|
69
70
|
});
|
|
70
71
|
isPatching = false;
|
|
@@ -89,7 +90,7 @@ function middleware(config, options) {
|
|
|
89
90
|
room.getStorage().then(({ root }) => {
|
|
90
91
|
const updates = {};
|
|
91
92
|
room.batch(() => {
|
|
92
|
-
for (const key in
|
|
93
|
+
for (const key in storageMapping) {
|
|
93
94
|
const liveblocksStatePart = root.get(key);
|
|
94
95
|
if (liveblocksStatePart == null) {
|
|
95
96
|
updates[key] = initialState[key];
|
|
@@ -103,7 +104,7 @@ function middleware(config, options) {
|
|
|
103
104
|
storageRoot = root;
|
|
104
105
|
unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
|
|
105
106
|
if (isPatching === false) {
|
|
106
|
-
set(patchState(get(), updates2,
|
|
107
|
+
set(patchState(get(), updates2, storageMapping));
|
|
107
108
|
}
|
|
108
109
|
}, { isDeep: true }));
|
|
109
110
|
updateZustandLiveblocksState(set, { isStorageLoading: false });
|
package/lib/index.d.ts
CHANGED
|
@@ -45,7 +45,7 @@ declare type Options<T> = {
|
|
|
45
45
|
/**
|
|
46
46
|
* Mapping used to synchronize a part of your zustand state with one Liveblocks Room storage.
|
|
47
47
|
*/
|
|
48
|
-
storageMapping
|
|
48
|
+
storageMapping?: Mapping<T>;
|
|
49
49
|
/**
|
|
50
50
|
* Mapping used to synchronize a part of your zustand state with one Liveblocks Room presence.
|
|
51
51
|
*/
|
package/lib/index.js
CHANGED
|
@@ -80,17 +80,20 @@ function mappingToFunctionIsNotAllowed(key) {
|
|
|
80
80
|
return new Error(ERROR_PREFIX + " mapping." + key + " is invalid. Mapping to a function is not allowed.");
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
var patchLiveObjectKey = client.internals.patchLiveObjectKey,
|
|
84
|
+
patchImmutableObject = client.internals.patchImmutableObject,
|
|
85
|
+
liveNodeToJson = client.internals.liveNodeToJson;
|
|
83
86
|
function middleware(config, options) {
|
|
84
87
|
if (process.env.NODE_ENV !== "production" && options.client == null) {
|
|
85
88
|
throw missingClient();
|
|
86
89
|
}
|
|
87
90
|
|
|
88
|
-
var client
|
|
89
|
-
var
|
|
91
|
+
var client = options.client;
|
|
92
|
+
var storageMapping = validateMapping(options.storageMapping || {}, "storageMapping");
|
|
90
93
|
var presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
|
|
91
94
|
|
|
92
95
|
if (process.env.NODE_ENV !== "production") {
|
|
93
|
-
validateNoDuplicateKeys(
|
|
96
|
+
validateNoDuplicateKeys(storageMapping, presenceMapping);
|
|
94
97
|
}
|
|
95
98
|
|
|
96
99
|
return function (set, get, api) {
|
|
@@ -109,7 +112,7 @@ function middleware(config, options) {
|
|
|
109
112
|
updatePresence(room, oldState, newState, presenceMapping);
|
|
110
113
|
room.batch(function () {
|
|
111
114
|
if (storageRoot) {
|
|
112
|
-
patchLiveblocksStorage(storageRoot, oldState, newState,
|
|
115
|
+
patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
|
|
113
116
|
}
|
|
114
117
|
});
|
|
115
118
|
isPatching = false;
|
|
@@ -121,7 +124,7 @@ function middleware(config, options) {
|
|
|
121
124
|
return;
|
|
122
125
|
}
|
|
123
126
|
|
|
124
|
-
room = client
|
|
127
|
+
room = client.enter(roomId);
|
|
125
128
|
updateZustandLiveblocksState(set, {
|
|
126
129
|
isStorageLoading: true,
|
|
127
130
|
room: room
|
|
@@ -142,14 +145,14 @@ function middleware(config, options) {
|
|
|
142
145
|
var root = _ref.root;
|
|
143
146
|
var updates = {};
|
|
144
147
|
room.batch(function () {
|
|
145
|
-
for (var
|
|
146
|
-
var liveblocksStatePart = root.get(
|
|
148
|
+
for (var _key in storageMapping) {
|
|
149
|
+
var liveblocksStatePart = root.get(_key);
|
|
147
150
|
|
|
148
151
|
if (liveblocksStatePart == null) {
|
|
149
|
-
updates[
|
|
150
|
-
|
|
152
|
+
updates[_key] = initialState[_key];
|
|
153
|
+
patchLiveObjectKey(root, _key, undefined, initialState[_key]);
|
|
151
154
|
} else {
|
|
152
|
-
updates[
|
|
155
|
+
updates[_key] = liveNodeToJson(liveblocksStatePart);
|
|
153
156
|
}
|
|
154
157
|
}
|
|
155
158
|
});
|
|
@@ -157,7 +160,7 @@ function middleware(config, options) {
|
|
|
157
160
|
storageRoot = root;
|
|
158
161
|
unsubscribeCallbacks.push(room.subscribe(root, function (updates) {
|
|
159
162
|
if (isPatching === false) {
|
|
160
|
-
set(patchState(get(), updates,
|
|
163
|
+
set(patchState(get(), updates, storageMapping));
|
|
161
164
|
}
|
|
162
165
|
}, {
|
|
163
166
|
isDeep: true
|
|
@@ -178,7 +181,7 @@ function middleware(config, options) {
|
|
|
178
181
|
room = null;
|
|
179
182
|
isPatching = false;
|
|
180
183
|
unsubscribeCallbacks = [];
|
|
181
|
-
client
|
|
184
|
+
client.leave(roomId);
|
|
182
185
|
updateZustandLiveblocksState(set, {
|
|
183
186
|
others: [],
|
|
184
187
|
connection: "closed",
|
|
@@ -203,15 +206,15 @@ function middleware(config, options) {
|
|
|
203
206
|
function patchState(state, updates, mapping) {
|
|
204
207
|
var partialState = {};
|
|
205
208
|
|
|
206
|
-
for (var
|
|
207
|
-
partialState[
|
|
209
|
+
for (var _key2 in mapping) {
|
|
210
|
+
partialState[_key2] = state[_key2];
|
|
208
211
|
}
|
|
209
212
|
|
|
210
|
-
var patched =
|
|
213
|
+
var patched = patchImmutableObject(partialState, updates);
|
|
211
214
|
var result = {};
|
|
212
215
|
|
|
213
|
-
for (var
|
|
214
|
-
result[
|
|
216
|
+
for (var _key3 in mapping) {
|
|
217
|
+
result[_key3] = patched[_key3];
|
|
215
218
|
}
|
|
216
219
|
|
|
217
220
|
return result;
|
|
@@ -226,35 +229,35 @@ function updateZustandLiveblocksState(set, partial) {
|
|
|
226
229
|
}
|
|
227
230
|
|
|
228
231
|
function broadcastInitialPresence(room, state, mapping) {
|
|
229
|
-
for (var
|
|
232
|
+
for (var _key4 in mapping) {
|
|
230
233
|
var _room$updatePresence;
|
|
231
234
|
|
|
232
|
-
room == null ? void 0 : room.updatePresence((_room$updatePresence = {}, _room$updatePresence[
|
|
235
|
+
room == null ? void 0 : room.updatePresence((_room$updatePresence = {}, _room$updatePresence[_key4] = state[_key4], _room$updatePresence));
|
|
233
236
|
}
|
|
234
237
|
}
|
|
235
238
|
|
|
236
239
|
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
237
|
-
for (var
|
|
238
|
-
if (typeof newState[
|
|
240
|
+
for (var _key5 in presenceMapping) {
|
|
241
|
+
if (typeof newState[_key5] === "function") {
|
|
239
242
|
throw mappingToFunctionIsNotAllowed("value");
|
|
240
243
|
}
|
|
241
244
|
|
|
242
|
-
if (oldState[
|
|
245
|
+
if (oldState[_key5] !== newState[_key5]) {
|
|
243
246
|
var _room$updatePresence2;
|
|
244
247
|
|
|
245
|
-
room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[
|
|
248
|
+
room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[_key5] = newState[_key5], _room$updatePresence2));
|
|
246
249
|
}
|
|
247
250
|
}
|
|
248
251
|
}
|
|
249
252
|
|
|
250
253
|
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
251
|
-
for (var
|
|
252
|
-
if (process.env.NODE_ENV !== "production" && typeof newState[
|
|
254
|
+
for (var _key6 in mapping) {
|
|
255
|
+
if (process.env.NODE_ENV !== "production" && typeof newState[_key6] === "function") {
|
|
253
256
|
throw mappingToFunctionIsNotAllowed("value");
|
|
254
257
|
}
|
|
255
258
|
|
|
256
|
-
if (oldState[
|
|
257
|
-
|
|
259
|
+
if (oldState[_key6] !== newState[_key6]) {
|
|
260
|
+
patchLiveObjectKey(root, _key6, oldState[_key6], newState[_key6]);
|
|
258
261
|
}
|
|
259
262
|
}
|
|
260
263
|
}
|
|
@@ -264,9 +267,9 @@ function isObject(value) {
|
|
|
264
267
|
}
|
|
265
268
|
|
|
266
269
|
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
267
|
-
for (var
|
|
268
|
-
if (presenceMapping[
|
|
269
|
-
throw mappingShouldNotHaveTheSameKeys(
|
|
270
|
+
for (var _key7 in storageMapping) {
|
|
271
|
+
if (presenceMapping[_key7] !== undefined) {
|
|
272
|
+
throw mappingShouldNotHaveTheSameKeys(_key7);
|
|
270
273
|
}
|
|
271
274
|
}
|
|
272
275
|
}
|
|
@@ -284,13 +287,13 @@ function validateMapping(mapping, mappingType) {
|
|
|
284
287
|
|
|
285
288
|
var result = {};
|
|
286
289
|
|
|
287
|
-
for (var
|
|
288
|
-
if (process.env.NODE_ENV !== "production" && typeof mapping[
|
|
289
|
-
throw mappingValueShouldBeABoolean(mappingType,
|
|
290
|
+
for (var _key8 in mapping) {
|
|
291
|
+
if (process.env.NODE_ENV !== "production" && typeof mapping[_key8] !== "boolean") {
|
|
292
|
+
throw mappingValueShouldBeABoolean(mappingType, _key8);
|
|
290
293
|
}
|
|
291
294
|
|
|
292
|
-
if (mapping[
|
|
293
|
-
result[
|
|
295
|
+
if (mapping[_key8] === true) {
|
|
296
|
+
result[_key8] = true;
|
|
294
297
|
}
|
|
295
298
|
}
|
|
296
299
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/zustand",
|
|
3
|
-
"version": "0.15.0-alpha.
|
|
3
|
+
"version": "0.15.0-alpha.4",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"directory": "packages/liveblocks-zustand"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@liveblocks/client": "0.15.0-alpha.
|
|
39
|
+
"@liveblocks/client": "0.15.0-alpha.4",
|
|
40
40
|
"zustand": "^3"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
"@testing-library/react": "^12.1.2",
|
|
52
52
|
"@testing-library/react-hooks": "^7.0.2",
|
|
53
53
|
"@types/jest": "^27.4.0",
|
|
54
|
+
"@types/regenerator-runtime": "^0.13.1",
|
|
54
55
|
"esbuild": "0.14.11",
|
|
55
56
|
"jest": "^27.4.7",
|
|
56
57
|
"msw": "^0.36.4",
|
|
@@ -59,4 +60,4 @@
|
|
|
59
60
|
"whatwg-fetch": "^3.6.2",
|
|
60
61
|
"zustand": "^3.6.9"
|
|
61
62
|
}
|
|
62
|
-
}
|
|
63
|
+
}
|