@liveblocks/zustand 0.16.4-beta2 → 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.js +31 -34
- package/index.mjs +1 -2
- package/package.json +2 -2
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) {
|
|
@@ -80,9 +80,6 @@ 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
|
-
lsonToJson = client.internals.lsonToJson;
|
|
86
83
|
function middleware(config, options) {
|
|
87
84
|
if (process.env.NODE_ENV !== "production" && options.client == null) {
|
|
88
85
|
throw missingClient();
|
|
@@ -150,14 +147,14 @@ function middleware(config, options) {
|
|
|
150
147
|
var root = _ref.root;
|
|
151
148
|
var updates = {};
|
|
152
149
|
room.batch(function () {
|
|
153
|
-
for (var
|
|
154
|
-
var liveblocksStatePart = root.get(
|
|
150
|
+
for (var key in storageMapping) {
|
|
151
|
+
var liveblocksStatePart = root.get(key);
|
|
155
152
|
|
|
156
153
|
if (liveblocksStatePart == null) {
|
|
157
|
-
updates[
|
|
158
|
-
patchLiveObjectKey(root,
|
|
154
|
+
updates[key] = initialState[key];
|
|
155
|
+
internal.patchLiveObjectKey(root, key, undefined, initialState[key]);
|
|
159
156
|
} else {
|
|
160
|
-
updates[
|
|
157
|
+
updates[key] = internal.lsonToJson(liveblocksStatePart);
|
|
161
158
|
}
|
|
162
159
|
}
|
|
163
160
|
});
|
|
@@ -211,15 +208,15 @@ function middleware(config, options) {
|
|
|
211
208
|
function patchState(state, updates, mapping) {
|
|
212
209
|
var partialState = {};
|
|
213
210
|
|
|
214
|
-
for (var
|
|
215
|
-
partialState[
|
|
211
|
+
for (var key in mapping) {
|
|
212
|
+
partialState[key] = state[key];
|
|
216
213
|
}
|
|
217
214
|
|
|
218
|
-
var patched = patchImmutableObject(partialState, updates);
|
|
215
|
+
var patched = internal.patchImmutableObject(partialState, updates);
|
|
219
216
|
var result = {};
|
|
220
217
|
|
|
221
|
-
for (var
|
|
222
|
-
result[
|
|
218
|
+
for (var _key in mapping) {
|
|
219
|
+
result[_key] = patched[_key];
|
|
223
220
|
}
|
|
224
221
|
|
|
225
222
|
return result;
|
|
@@ -228,8 +225,8 @@ function patchState(state, updates, mapping) {
|
|
|
228
225
|
function patchPresenceState(presence, mapping) {
|
|
229
226
|
var partialState = {};
|
|
230
227
|
|
|
231
|
-
for (var
|
|
232
|
-
partialState[
|
|
228
|
+
for (var key in mapping) {
|
|
229
|
+
partialState[key] = presence[key];
|
|
233
230
|
}
|
|
234
231
|
|
|
235
232
|
return partialState;
|
|
@@ -244,35 +241,35 @@ function updateZustandLiveblocksState(set, partial) {
|
|
|
244
241
|
}
|
|
245
242
|
|
|
246
243
|
function broadcastInitialPresence(room, state, mapping) {
|
|
247
|
-
for (var
|
|
244
|
+
for (var key in mapping) {
|
|
248
245
|
var _room$updatePresence;
|
|
249
246
|
|
|
250
|
-
room == null ? void 0 : room.updatePresence((_room$updatePresence = {}, _room$updatePresence[
|
|
247
|
+
room == null ? void 0 : room.updatePresence((_room$updatePresence = {}, _room$updatePresence[key] = state[key], _room$updatePresence));
|
|
251
248
|
}
|
|
252
249
|
}
|
|
253
250
|
|
|
254
251
|
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
255
|
-
for (var
|
|
256
|
-
if (typeof newState[
|
|
252
|
+
for (var key in presenceMapping) {
|
|
253
|
+
if (typeof newState[key] === "function") {
|
|
257
254
|
throw mappingToFunctionIsNotAllowed("value");
|
|
258
255
|
}
|
|
259
256
|
|
|
260
|
-
if (oldState[
|
|
257
|
+
if (oldState[key] !== newState[key]) {
|
|
261
258
|
var _room$updatePresence2;
|
|
262
259
|
|
|
263
|
-
room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[
|
|
260
|
+
room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[key] = newState[key], _room$updatePresence2));
|
|
264
261
|
}
|
|
265
262
|
}
|
|
266
263
|
}
|
|
267
264
|
|
|
268
265
|
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
269
|
-
for (var
|
|
270
|
-
if (process.env.NODE_ENV !== "production" && typeof newState[
|
|
266
|
+
for (var key in mapping) {
|
|
267
|
+
if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
|
|
271
268
|
throw mappingToFunctionIsNotAllowed("value");
|
|
272
269
|
}
|
|
273
270
|
|
|
274
|
-
if (oldState[
|
|
275
|
-
patchLiveObjectKey(root,
|
|
271
|
+
if (oldState[key] !== newState[key]) {
|
|
272
|
+
internal.patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
276
273
|
}
|
|
277
274
|
}
|
|
278
275
|
}
|
|
@@ -282,9 +279,9 @@ function isObject(value) {
|
|
|
282
279
|
}
|
|
283
280
|
|
|
284
281
|
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
285
|
-
for (var
|
|
286
|
-
if (presenceMapping[
|
|
287
|
-
throw mappingShouldNotHaveTheSameKeys(
|
|
282
|
+
for (var key in storageMapping) {
|
|
283
|
+
if (presenceMapping[key] !== undefined) {
|
|
284
|
+
throw mappingShouldNotHaveTheSameKeys(key);
|
|
288
285
|
}
|
|
289
286
|
}
|
|
290
287
|
}
|
|
@@ -302,13 +299,13 @@ function validateMapping(mapping, mappingType) {
|
|
|
302
299
|
|
|
303
300
|
var result = {};
|
|
304
301
|
|
|
305
|
-
for (var
|
|
306
|
-
if (process.env.NODE_ENV !== "production" && typeof mapping[
|
|
307
|
-
throw mappingValueShouldBeABoolean(mappingType,
|
|
302
|
+
for (var key in mapping) {
|
|
303
|
+
if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
|
|
304
|
+
throw mappingValueShouldBeABoolean(mappingType, key);
|
|
308
305
|
}
|
|
309
306
|
|
|
310
|
-
if (mapping[
|
|
311
|
-
result[
|
|
307
|
+
if (mapping[key] === true) {
|
|
308
|
+
result[key] = true;
|
|
312
309
|
}
|
|
313
310
|
}
|
|
314
311
|
|
package/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { patchLiveObjectKey, lsonToJson, patchImmutableObject } from '@liveblocks/client/internal';
|
|
2
2
|
|
|
3
3
|
const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
4
4
|
function missingClient() {
|
|
@@ -20,7 +20,6 @@ function mappingToFunctionIsNotAllowed(key) {
|
|
|
20
20
|
return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
const { patchLiveObjectKey, patchImmutableObject, lsonToJson } = internals;
|
|
24
23
|
function middleware(config, options) {
|
|
25
24
|
if (process.env.NODE_ENV !== "production" && options.client == null) {
|
|
26
25
|
throw missingClient();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/zustand",
|
|
3
|
-
"version": "0.16.4
|
|
3
|
+
"version": "0.16.4",
|
|
4
4
|
"description": "A middleware to integrate Liveblocks into Zustand stores.",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.mjs",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"directory": "packages/liveblocks-zustand"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@liveblocks/client": "0.16.4
|
|
33
|
+
"@liveblocks/client": "0.16.4",
|
|
34
34
|
"zustand": "^3"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|