@liveblocks/zustand 0.15.0-alpha.2 → 0.15.0

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.
@@ -1,4 +1,24 @@
1
- import { patchLiveObjectKey, liveNodeToJson, patchImmutableObject } from '@liveblocks/client';
1
+ import { internals } from '@liveblocks/client';
2
+
3
+ const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
4
+ function missingClient() {
5
+ return new Error(`${ERROR_PREFIX} client is missing`);
6
+ }
7
+ function missingMapping(mappingType) {
8
+ return new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
9
+ }
10
+ function mappingShouldBeAnObject(mappingType) {
11
+ return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
12
+ }
13
+ function mappingValueShouldBeABoolean(mappingType, key) {
14
+ return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
15
+ }
16
+ function mappingShouldNotHaveTheSameKeys(key) {
17
+ return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
18
+ }
19
+ function mappingToFunctionIsNotAllowed(key) {
20
+ return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
21
+ }
2
22
 
3
23
  var __defProp = Object.defineProperty;
4
24
  var __defProps = Object.defineProperties;
@@ -19,17 +39,17 @@ var __spreadValues = (a, b) => {
19
39
  return a;
20
40
  };
21
41
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
- const middleware = (config, {
23
- client,
24
- storageMapping: unvalidatedMapping,
25
- presenceMapping: unvalidatedPresenceMapping = {}
26
- }) => {
27
- if (client == null) {
28
- throw new Error(`${ERROR_PREFIX} client is missing`);
42
+ const { patchLiveObjectKey, patchImmutableObject, liveNodeToJson } = internals;
43
+ function middleware(config, options) {
44
+ if (process.env.NODE_ENV !== "production" && options.client == null) {
45
+ throw missingClient();
46
+ }
47
+ const client = options.client;
48
+ const storageMapping = validateMapping(options.storageMapping || {}, "storageMapping");
49
+ const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
50
+ if (process.env.NODE_ENV !== "production") {
51
+ validateNoDuplicateKeys(storageMapping, presenceMapping);
29
52
  }
30
- const mapping = validateMapping(unvalidatedMapping, "storageMapping");
31
- const presenceMapping = validateMapping(unvalidatedPresenceMapping, "presenceMapping");
32
- validateNoDuplicateKeys(mapping, presenceMapping);
33
53
  return (set, get, api) => {
34
54
  const typedSet = set;
35
55
  let room = null;
@@ -45,7 +65,7 @@ const middleware = (config, {
45
65
  updatePresence(room, oldState, newState, presenceMapping);
46
66
  room.batch(() => {
47
67
  if (storageRoot) {
48
- patchLiveblocksStorage(storageRoot, oldState, newState, mapping);
68
+ patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
49
69
  }
50
70
  });
51
71
  isPatching = false;
@@ -70,7 +90,7 @@ const middleware = (config, {
70
90
  room.getStorage().then(({ root }) => {
71
91
  const updates = {};
72
92
  room.batch(() => {
73
- for (const key in mapping) {
93
+ for (const key in storageMapping) {
74
94
  const liveblocksStatePart = root.get(key);
75
95
  if (liveblocksStatePart == null) {
76
96
  updates[key] = initialState[key];
@@ -84,7 +104,7 @@ const middleware = (config, {
84
104
  storageRoot = root;
85
105
  unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
86
106
  if (isPatching === false) {
87
- set(patchState(get(), updates2, mapping));
107
+ set(patchState(get(), updates2, storageMapping));
88
108
  }
89
109
  }, { isDeep: true }));
90
110
  updateZustandLiveblocksState(set, { isStorageLoading: false });
@@ -117,7 +137,7 @@ const middleware = (config, {
117
137
  }
118
138
  });
119
139
  };
120
- };
140
+ }
121
141
  function patchState(state, updates, mapping) {
122
142
  const partialState = {};
123
143
  for (const key in mapping) {
@@ -140,6 +160,9 @@ function broadcastInitialPresence(room, state, mapping) {
140
160
  }
141
161
  function updatePresence(room, oldState, newState, presenceMapping) {
142
162
  for (const key in presenceMapping) {
163
+ if (typeof newState[key] === "function") {
164
+ throw mappingToFunctionIsNotAllowed("value");
165
+ }
143
166
  if (oldState[key] !== newState[key]) {
144
167
  room.updatePresence({ [key]: newState[key] });
145
168
  }
@@ -147,6 +170,9 @@ function updatePresence(room, oldState, newState, presenceMapping) {
147
170
  }
148
171
  function patchLiveblocksStorage(root, oldState, newState, mapping) {
149
172
  for (const key in mapping) {
173
+ if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
174
+ throw mappingToFunctionIsNotAllowed("value");
175
+ }
150
176
  if (oldState[key] !== newState[key]) {
151
177
  patchLiveObjectKey(root, key, oldState[key], newState[key]);
152
178
  }
@@ -158,21 +184,23 @@ function isObject(value) {
158
184
  function validateNoDuplicateKeys(storageMapping, presenceMapping) {
159
185
  for (const key in storageMapping) {
160
186
  if (presenceMapping[key] !== void 0) {
161
- throw new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
187
+ throw mappingShouldNotHaveTheSameKeys(key);
162
188
  }
163
189
  }
164
190
  }
165
191
  function validateMapping(mapping, mappingType) {
166
- if (mapping == null) {
167
- throw new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
168
- }
169
- if (!isObject(mapping)) {
170
- throw new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
192
+ if (process.env.NODE_ENV !== "production") {
193
+ if (mapping == null) {
194
+ throw missingMapping(mappingType);
195
+ }
196
+ if (!isObject(mapping)) {
197
+ throw mappingShouldBeAnObject(mappingType);
198
+ }
171
199
  }
172
200
  const result = {};
173
201
  for (const key in mapping) {
174
- if (typeof mapping[key] !== "boolean") {
175
- throw new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
202
+ if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
203
+ throw mappingValueShouldBeABoolean(mappingType, key);
176
204
  }
177
205
  if (mapping[key] === true) {
178
206
  result[key] = true;
@@ -180,6 +208,5 @@ function validateMapping(mapping, mappingType) {
180
208
  }
181
209
  return result;
182
210
  }
183
- const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
184
211
 
185
212
  export { middleware };
@@ -1,4 +1,24 @@
1
- import { patchLiveObjectKey, liveNodeToJson, patchImmutableObject } from '@liveblocks/client';
1
+ import { internals } from '@liveblocks/client';
2
+
3
+ const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
4
+ function missingClient() {
5
+ return new Error(`${ERROR_PREFIX} client is missing`);
6
+ }
7
+ function missingMapping(mappingType) {
8
+ return new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
9
+ }
10
+ function mappingShouldBeAnObject(mappingType) {
11
+ return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
12
+ }
13
+ function mappingValueShouldBeABoolean(mappingType, key) {
14
+ return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
15
+ }
16
+ function mappingShouldNotHaveTheSameKeys(key) {
17
+ return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
18
+ }
19
+ function mappingToFunctionIsNotAllowed(key) {
20
+ return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
21
+ }
2
22
 
3
23
  var __defProp = Object.defineProperty;
4
24
  var __defProps = Object.defineProperties;
@@ -19,17 +39,17 @@ var __spreadValues = (a, b) => {
19
39
  return a;
20
40
  };
21
41
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
- const middleware = (config, {
23
- client,
24
- storageMapping: unvalidatedMapping,
25
- presenceMapping: unvalidatedPresenceMapping = {}
26
- }) => {
27
- if (client == null) {
28
- throw new Error(`${ERROR_PREFIX} client is missing`);
42
+ const { patchLiveObjectKey, patchImmutableObject, liveNodeToJson } = internals;
43
+ function middleware(config, options) {
44
+ if (process.env.NODE_ENV !== "production" && options.client == null) {
45
+ throw missingClient();
46
+ }
47
+ const client = options.client;
48
+ const storageMapping = validateMapping(options.storageMapping || {}, "storageMapping");
49
+ const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
50
+ if (process.env.NODE_ENV !== "production") {
51
+ validateNoDuplicateKeys(storageMapping, presenceMapping);
29
52
  }
30
- const mapping = validateMapping(unvalidatedMapping, "storageMapping");
31
- const presenceMapping = validateMapping(unvalidatedPresenceMapping, "presenceMapping");
32
- validateNoDuplicateKeys(mapping, presenceMapping);
33
53
  return (set, get, api) => {
34
54
  const typedSet = set;
35
55
  let room = null;
@@ -45,7 +65,7 @@ const middleware = (config, {
45
65
  updatePresence(room, oldState, newState, presenceMapping);
46
66
  room.batch(() => {
47
67
  if (storageRoot) {
48
- patchLiveblocksStorage(storageRoot, oldState, newState, mapping);
68
+ patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
49
69
  }
50
70
  });
51
71
  isPatching = false;
@@ -70,7 +90,7 @@ const middleware = (config, {
70
90
  room.getStorage().then(({ root }) => {
71
91
  const updates = {};
72
92
  room.batch(() => {
73
- for (const key in mapping) {
93
+ for (const key in storageMapping) {
74
94
  const liveblocksStatePart = root.get(key);
75
95
  if (liveblocksStatePart == null) {
76
96
  updates[key] = initialState[key];
@@ -84,7 +104,7 @@ const middleware = (config, {
84
104
  storageRoot = root;
85
105
  unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
86
106
  if (isPatching === false) {
87
- set(patchState(get(), updates2, mapping));
107
+ set(patchState(get(), updates2, storageMapping));
88
108
  }
89
109
  }, { isDeep: true }));
90
110
  updateZustandLiveblocksState(set, { isStorageLoading: false });
@@ -117,7 +137,7 @@ const middleware = (config, {
117
137
  }
118
138
  });
119
139
  };
120
- };
140
+ }
121
141
  function patchState(state, updates, mapping) {
122
142
  const partialState = {};
123
143
  for (const key in mapping) {
@@ -140,6 +160,9 @@ function broadcastInitialPresence(room, state, mapping) {
140
160
  }
141
161
  function updatePresence(room, oldState, newState, presenceMapping) {
142
162
  for (const key in presenceMapping) {
163
+ if (typeof newState[key] === "function") {
164
+ throw mappingToFunctionIsNotAllowed("value");
165
+ }
143
166
  if (oldState[key] !== newState[key]) {
144
167
  room.updatePresence({ [key]: newState[key] });
145
168
  }
@@ -147,6 +170,9 @@ function updatePresence(room, oldState, newState, presenceMapping) {
147
170
  }
148
171
  function patchLiveblocksStorage(root, oldState, newState, mapping) {
149
172
  for (const key in mapping) {
173
+ if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
174
+ throw mappingToFunctionIsNotAllowed("value");
175
+ }
150
176
  if (oldState[key] !== newState[key]) {
151
177
  patchLiveObjectKey(root, key, oldState[key], newState[key]);
152
178
  }
@@ -158,21 +184,23 @@ function isObject(value) {
158
184
  function validateNoDuplicateKeys(storageMapping, presenceMapping) {
159
185
  for (const key in storageMapping) {
160
186
  if (presenceMapping[key] !== void 0) {
161
- throw new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
187
+ throw mappingShouldNotHaveTheSameKeys(key);
162
188
  }
163
189
  }
164
190
  }
165
191
  function validateMapping(mapping, mappingType) {
166
- if (mapping == null) {
167
- throw new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
168
- }
169
- if (!isObject(mapping)) {
170
- throw new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
192
+ if (process.env.NODE_ENV !== "production") {
193
+ if (mapping == null) {
194
+ throw missingMapping(mappingType);
195
+ }
196
+ if (!isObject(mapping)) {
197
+ throw mappingShouldBeAnObject(mappingType);
198
+ }
171
199
  }
172
200
  const result = {};
173
201
  for (const key in mapping) {
174
- if (typeof mapping[key] !== "boolean") {
175
- throw new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
202
+ if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
203
+ throw mappingValueShouldBeABoolean(mappingType, key);
176
204
  }
177
205
  if (mapping[key] === true) {
178
206
  result[key] = true;
@@ -180,6 +208,5 @@ function validateMapping(mapping, mappingType) {
180
208
  }
181
209
  return result;
182
210
  }
183
- const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
184
211
 
185
212
  export { middleware };
package/lib/index.d.ts CHANGED
@@ -1,20 +1,55 @@
1
1
  import { StateCreator, SetState, GetState, StoreApi } from "zustand";
2
2
  import { Client, User, Room } from "@liveblocks/client";
3
3
  export declare type LiveblocksState<TState, TPresence = any> = TState & {
4
+ /**
5
+ * Liveblocks extra state attached by the middleware
6
+ */
4
7
  readonly liveblocks: {
5
- readonly enterRoom: (room: string, initialState: Partial<TState>) => void;
6
- readonly leaveRoom: (room: string) => void;
8
+ /**
9
+ * Enters a room and starts sync it with zustand state
10
+ * @param roomId The id of the room
11
+ * @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.
12
+ */
13
+ readonly enterRoom: (roomId: string, initialState: Partial<TState>) => void;
14
+ /**
15
+ * Leaves a room and stops sync it with zustand state.
16
+ * @param roomId The id of the room
17
+ */
18
+ readonly leaveRoom: (roomId: string) => void;
19
+ /**
20
+ * The room currently synced to your zustand state.
21
+ */
7
22
  readonly room: Room | null;
23
+ /**
24
+ * Other users in the room. Empty no room is currently synced
25
+ */
8
26
  readonly others: Array<User<TPresence>>;
27
+ /**
28
+ * Whether or not the room storage is currently loading
29
+ */
9
30
  readonly isStorageLoading: boolean;
31
+ /**
32
+ * Connection state of the room
33
+ */
10
34
  readonly connection: "closed" | "authenticating" | "unavailable" | "failed" | "open" | "connecting";
11
35
  };
12
36
  };
13
37
  export declare type Mapping<T> = Partial<{
14
38
  [Property in keyof T]: boolean;
15
39
  }>;
16
- export declare const middleware: <T extends Object, TPresence extends Object = any>(config: StateCreator<T, SetState<T>, GetState<LiveblocksState<T>>, StoreApi<T>>, options: {
40
+ declare type Options<T> = {
41
+ /**
42
+ * Liveblocks client created by @liveblocks/client createClient
43
+ */
17
44
  client: Client;
18
- storageMapping: Mapping<T>;
45
+ /**
46
+ * Mapping used to synchronize a part of your zustand state with one Liveblocks Room storage.
47
+ */
48
+ storageMapping?: Mapping<T>;
49
+ /**
50
+ * Mapping used to synchronize a part of your zustand state with one Liveblocks Room presence.
51
+ */
19
52
  presenceMapping?: Mapping<T>;
20
- }) => StateCreator<LiveblocksState<T, TPresence>, SetState<LiveblocksState<T, TPresence>>, GetState<LiveblocksState<T, TPresence>>, StoreApi<LiveblocksState<T, TPresence>>>;
53
+ };
54
+ export declare function middleware<T extends Object, TPresence extends Object = any>(config: StateCreator<T, SetState<T>, GetState<LiveblocksState<T>>, StoreApi<T>>, options: Options<T>): StateCreator<LiveblocksState<T, TPresence>, SetState<LiveblocksState<T, TPresence>>, GetState<LiveblocksState<T, TPresence>>, StoreApi<LiveblocksState<T, TPresence>>>;
55
+ export {};
package/lib/index.js CHANGED
@@ -60,19 +60,42 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
60
60
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
61
61
  }
62
62
 
63
- var middleware = function middleware(config, _ref) {
64
- var client$1 = _ref.client,
65
- unvalidatedMapping = _ref.storageMapping,
66
- _ref$presenceMapping = _ref.presenceMapping,
67
- unvalidatedPresenceMapping = _ref$presenceMapping === void 0 ? {} : _ref$presenceMapping;
68
-
69
- if (client$1 == null) {
70
- throw new Error(ERROR_PREFIX + " client is missing");
63
+ var ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
64
+ function missingClient() {
65
+ return new Error(ERROR_PREFIX + " client is missing");
66
+ }
67
+ function missingMapping(mappingType) {
68
+ return new Error(ERROR_PREFIX + " " + mappingType + " is missing.");
69
+ }
70
+ function mappingShouldBeAnObject(mappingType) {
71
+ return new Error(ERROR_PREFIX + " " + mappingType + " should be an object where the values are boolean.");
72
+ }
73
+ function mappingValueShouldBeABoolean(mappingType, key) {
74
+ return new Error(ERROR_PREFIX + " " + mappingType + "." + key + " value should be a boolean");
75
+ }
76
+ function mappingShouldNotHaveTheSameKeys(key) {
77
+ return new Error(ERROR_PREFIX + " \"" + key + "\" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.");
78
+ }
79
+ function mappingToFunctionIsNotAllowed(key) {
80
+ return new Error(ERROR_PREFIX + " mapping." + key + " is invalid. Mapping to a function is not allowed.");
81
+ }
82
+
83
+ var patchLiveObjectKey = client.internals.patchLiveObjectKey,
84
+ patchImmutableObject = client.internals.patchImmutableObject,
85
+ liveNodeToJson = client.internals.liveNodeToJson;
86
+ function middleware(config, options) {
87
+ if (process.env.NODE_ENV !== "production" && options.client == null) {
88
+ throw missingClient();
89
+ }
90
+
91
+ var client = options.client;
92
+ var storageMapping = validateMapping(options.storageMapping || {}, "storageMapping");
93
+ var presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
94
+
95
+ if (process.env.NODE_ENV !== "production") {
96
+ validateNoDuplicateKeys(storageMapping, presenceMapping);
71
97
  }
72
98
 
73
- var mapping = validateMapping(unvalidatedMapping, "storageMapping");
74
- var presenceMapping = validateMapping(unvalidatedPresenceMapping, "presenceMapping");
75
- validateNoDuplicateKeys(mapping, presenceMapping);
76
99
  return function (set, get, api) {
77
100
  var typedSet = set;
78
101
  var room = null;
@@ -89,7 +112,7 @@ var middleware = function middleware(config, _ref) {
89
112
  updatePresence(room, oldState, newState, presenceMapping);
90
113
  room.batch(function () {
91
114
  if (storageRoot) {
92
- patchLiveblocksStorage(storageRoot, oldState, newState, mapping);
115
+ patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
93
116
  }
94
117
  });
95
118
  isPatching = false;
@@ -101,7 +124,7 @@ var middleware = function middleware(config, _ref) {
101
124
  return;
102
125
  }
103
126
 
104
- room = client$1.enter(roomId);
127
+ room = client.enter(roomId);
105
128
  updateZustandLiveblocksState(set, {
106
129
  isStorageLoading: true,
107
130
  room: room
@@ -118,18 +141,18 @@ var middleware = function middleware(config, _ref) {
118
141
  connection: room.getConnectionState()
119
142
  });
120
143
  }));
121
- room.getStorage().then(function (_ref2) {
122
- var root = _ref2.root;
144
+ room.getStorage().then(function (_ref) {
145
+ var root = _ref.root;
123
146
  var updates = {};
124
147
  room.batch(function () {
125
- for (var key in mapping) {
126
- var liveblocksStatePart = root.get(key);
148
+ for (var _key in storageMapping) {
149
+ var liveblocksStatePart = root.get(_key);
127
150
 
128
151
  if (liveblocksStatePart == null) {
129
- updates[key] = initialState[key];
130
- client.patchLiveObjectKey(root, key, undefined, initialState[key]);
152
+ updates[_key] = initialState[_key];
153
+ patchLiveObjectKey(root, _key, undefined, initialState[_key]);
131
154
  } else {
132
- updates[key] = client.liveNodeToJson(liveblocksStatePart);
155
+ updates[_key] = liveNodeToJson(liveblocksStatePart);
133
156
  }
134
157
  }
135
158
  });
@@ -137,7 +160,7 @@ var middleware = function middleware(config, _ref) {
137
160
  storageRoot = root;
138
161
  unsubscribeCallbacks.push(room.subscribe(root, function (updates) {
139
162
  if (isPatching === false) {
140
- set(patchState(get(), updates, mapping));
163
+ set(patchState(get(), updates, storageMapping));
141
164
  }
142
165
  }, {
143
166
  isDeep: true
@@ -158,7 +181,7 @@ var middleware = function middleware(config, _ref) {
158
181
  room = null;
159
182
  isPatching = false;
160
183
  unsubscribeCallbacks = [];
161
- client$1.leave(roomId);
184
+ client.leave(roomId);
162
185
  updateZustandLiveblocksState(set, {
163
186
  others: [],
164
187
  connection: "closed",
@@ -178,20 +201,20 @@ var middleware = function middleware(config, _ref) {
178
201
  }
179
202
  });
180
203
  };
181
- };
204
+ }
182
205
 
183
206
  function patchState(state, updates, mapping) {
184
207
  var partialState = {};
185
208
 
186
- for (var key in mapping) {
187
- partialState[key] = state[key];
209
+ for (var _key2 in mapping) {
210
+ partialState[_key2] = state[_key2];
188
211
  }
189
212
 
190
- var patched = client.patchImmutableObject(partialState, updates);
213
+ var patched = patchImmutableObject(partialState, updates);
191
214
  var result = {};
192
215
 
193
- for (var _key in mapping) {
194
- result[_key] = patched[_key];
216
+ for (var _key3 in mapping) {
217
+ result[_key3] = patched[_key3];
195
218
  }
196
219
 
197
220
  return result;
@@ -206,27 +229,35 @@ function updateZustandLiveblocksState(set, partial) {
206
229
  }
207
230
 
208
231
  function broadcastInitialPresence(room, state, mapping) {
209
- for (var key in mapping) {
232
+ for (var _key4 in mapping) {
210
233
  var _room$updatePresence;
211
234
 
212
- 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));
213
236
  }
214
237
  }
215
238
 
216
239
  function updatePresence(room, oldState, newState, presenceMapping) {
217
- for (var key in presenceMapping) {
218
- if (oldState[key] !== newState[key]) {
240
+ for (var _key5 in presenceMapping) {
241
+ if (typeof newState[_key5] === "function") {
242
+ throw mappingToFunctionIsNotAllowed("value");
243
+ }
244
+
245
+ if (oldState[_key5] !== newState[_key5]) {
219
246
  var _room$updatePresence2;
220
247
 
221
- room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[key] = newState[key], _room$updatePresence2));
248
+ room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[_key5] = newState[_key5], _room$updatePresence2));
222
249
  }
223
250
  }
224
251
  }
225
252
 
226
253
  function patchLiveblocksStorage(root, oldState, newState, mapping) {
227
- for (var key in mapping) {
228
- if (oldState[key] !== newState[key]) {
229
- client.patchLiveObjectKey(root, key, oldState[key], newState[key]);
254
+ for (var _key6 in mapping) {
255
+ if (process.env.NODE_ENV !== "production" && typeof newState[_key6] === "function") {
256
+ throw mappingToFunctionIsNotAllowed("value");
257
+ }
258
+
259
+ if (oldState[_key6] !== newState[_key6]) {
260
+ patchLiveObjectKey(root, _key6, oldState[_key6], newState[_key6]);
230
261
  }
231
262
  }
232
263
  }
@@ -236,37 +267,37 @@ function isObject(value) {
236
267
  }
237
268
 
238
269
  function validateNoDuplicateKeys(storageMapping, presenceMapping) {
239
- for (var key in storageMapping) {
240
- if (presenceMapping[key] !== undefined) {
241
- throw new Error(ERROR_PREFIX + " \"" + key + "\" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.");
270
+ for (var _key7 in storageMapping) {
271
+ if (presenceMapping[_key7] !== undefined) {
272
+ throw mappingShouldNotHaveTheSameKeys(_key7);
242
273
  }
243
274
  }
244
275
  }
245
276
 
246
277
  function validateMapping(mapping, mappingType) {
247
- if (mapping == null) {
248
- throw new Error(ERROR_PREFIX + " " + mappingType + " is missing.");
249
- }
278
+ if (process.env.NODE_ENV !== "production") {
279
+ if (mapping == null) {
280
+ throw missingMapping(mappingType);
281
+ }
250
282
 
251
- if (!isObject(mapping)) {
252
- throw new Error(ERROR_PREFIX + " " + mappingType + " should be an object where the values are boolean.");
283
+ if (!isObject(mapping)) {
284
+ throw mappingShouldBeAnObject(mappingType);
285
+ }
253
286
  }
254
287
 
255
288
  var result = {};
256
289
 
257
- for (var key in mapping) {
258
- if (typeof mapping[key] !== "boolean") {
259
- throw new Error(ERROR_PREFIX + " " + mappingType + "." + key + " value should be a boolean");
290
+ for (var _key8 in mapping) {
291
+ if (process.env.NODE_ENV !== "production" && typeof mapping[_key8] !== "boolean") {
292
+ throw mappingValueShouldBeABoolean(mappingType, _key8);
260
293
  }
261
294
 
262
- if (mapping[key] === true) {
263
- result[key] = true;
295
+ if (mapping[_key8] === true) {
296
+ result[_key8] = true;
264
297
  }
265
298
  }
266
299
 
267
300
  return result;
268
301
  }
269
302
 
270
- var ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
271
-
272
303
  exports.middleware = middleware;
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@liveblocks/zustand",
3
- "version": "0.15.0-alpha.2",
3
+ "version": "0.15.0",
4
+ "sideEffects": false,
4
5
  "description": "",
5
6
  "main": "./lib/index.js",
6
7
  "types": "./lib/index.d.ts",
@@ -35,7 +36,7 @@
35
36
  "directory": "packages/liveblocks-zustand"
36
37
  },
37
38
  "peerDependencies": {
38
- "@liveblocks/client": "0.15.0-alpha.2",
39
+ "@liveblocks/client": "0.15.0",
39
40
  "zustand": "^3"
40
41
  },
41
42
  "devDependencies": {