@liveblocks/zustand 0.15.0-alpha.3 → 0.15.1

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/README.md CHANGED
@@ -0,0 +1,40 @@
1
+ <p align="center">
2
+ <a href="https://liveblocks.io">
3
+ <img src="https://liveblocks.io/images/blog/introducing-liveblocks.png">
4
+ </a>
5
+ </p>
6
+
7
+ # Liveblocks · [![Twitter Follow](https://shields.io/twitter/follow/liveblocks?label=Follow)](https://twitter.com/liveblocks)
8
+
9
+ **At [Liveblocks](https://liveblocks.io), we’re building tools to help companies create world-class collaborative products that attract, engage and retain users.** This repository is a set of open-source packages for building performant and reliable multiplayer experiences.
10
+
11
+ ## Installation
12
+
13
+ ```
14
+ npm install @liveblocks/client @liveblocks/zustand
15
+ ```
16
+
17
+ ## Documentation
18
+
19
+ - Start with the [React-Zustand guide](https://liveblocks.io/docs/get-started/react-zustand).
20
+ - Explore the [API Reference](https://liveblocks.io/docs/api-reference/liveblocks-zustand).
21
+
22
+ ## Examples
23
+
24
+ - Browse our gallery of collaborative UI patterns. [View examples gallery](https://liveblocks.io/examples)
25
+ - Explore and clone any of our open-source examples. [View code examples](https://github.com/liveblocks/liveblocks/tree/main/examples)
26
+
27
+ ## Releases
28
+
29
+ For changelog, visit [https://github.com/liveblocks/liveblocks/releases](https://github.com/liveblocks/liveblocks/releases).
30
+
31
+ ## Community
32
+
33
+ - [Discord](https://discord.gg/X4YWJuH9VY) - To get involved with the Liveblocks community, ask questions and share tips.
34
+ - [Twitter](https://twitter.com/liveblocks) - To receive updates, announcements, blog posts, and general Liveblocks tips.
35
+
36
+ ## License
37
+
38
+ Licensed under the Apache License 2.0, Copyright © 2021-present [Liveblocks](https://liveblocks.io).
39
+
40
+ See [LICENSE](../../LICENSE) for more information.
package/lib/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { patchLiveObjectKey, liveNodeToJson, patchImmutableObject } from '@liveblocks/client';
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 mapping = validateMapping(options.storageMapping, "storageMapping");
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(mapping, presenceMapping);
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, mapping);
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 mapping) {
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, mapping));
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 { patchLiveObjectKey, liveNodeToJson, patchImmutableObject } from '@liveblocks/client';
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 mapping = validateMapping(options.storageMapping, "storageMapping");
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(mapping, presenceMapping);
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, mapping);
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 mapping) {
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, mapping));
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: Mapping<T>;
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$1 = options.client;
89
- var mapping = validateMapping(options.storageMapping, "storageMapping");
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(mapping, presenceMapping);
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, mapping);
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$1.enter(roomId);
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 key in mapping) {
146
- var liveblocksStatePart = root.get(key);
148
+ for (var _key in storageMapping) {
149
+ var liveblocksStatePart = root.get(_key);
147
150
 
148
151
  if (liveblocksStatePart == null) {
149
- updates[key] = initialState[key];
150
- client.patchLiveObjectKey(root, key, undefined, initialState[key]);
152
+ updates[_key] = initialState[_key];
153
+ patchLiveObjectKey(root, _key, undefined, initialState[_key]);
151
154
  } else {
152
- updates[key] = client.liveNodeToJson(liveblocksStatePart);
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, mapping));
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$1.leave(roomId);
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 key in mapping) {
207
- partialState[key] = state[key];
209
+ for (var _key2 in mapping) {
210
+ partialState[_key2] = state[_key2];
208
211
  }
209
212
 
210
- var patched = client.patchImmutableObject(partialState, updates);
213
+ var patched = patchImmutableObject(partialState, updates);
211
214
  var result = {};
212
215
 
213
- for (var _key in mapping) {
214
- result[_key] = patched[_key];
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 key in mapping) {
232
+ for (var _key4 in mapping) {
230
233
  var _room$updatePresence;
231
234
 
232
- room == null ? void 0 : room.updatePresence((_room$updatePresence = {}, _room$updatePresence[key] = state[key], _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 key in presenceMapping) {
238
- if (typeof newState[key] === "function") {
240
+ for (var _key5 in presenceMapping) {
241
+ if (typeof newState[_key5] === "function") {
239
242
  throw mappingToFunctionIsNotAllowed("value");
240
243
  }
241
244
 
242
- if (oldState[key] !== newState[key]) {
245
+ if (oldState[_key5] !== newState[_key5]) {
243
246
  var _room$updatePresence2;
244
247
 
245
- room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[key] = newState[key], _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 key in mapping) {
252
- if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
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[key] !== newState[key]) {
257
- client.patchLiveObjectKey(root, key, oldState[key], newState[key]);
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 key in storageMapping) {
268
- if (presenceMapping[key] !== undefined) {
269
- throw mappingShouldNotHaveTheSameKeys(key);
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 key in mapping) {
288
- if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
289
- throw mappingValueShouldBeABoolean(mappingType, key);
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[key] === true) {
293
- result[key] = true;
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",
3
+ "version": "0.15.1",
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.3",
39
+ "@liveblocks/client": "0.15.1",
40
40
  "zustand": "^3"
41
41
  },
42
42
  "devDependencies": {
@@ -59,4 +59,4 @@
59
59
  "whatwg-fetch": "^3.6.2",
60
60
  "zustand": "^3.6.9"
61
61
  }
62
- }
62
+ }