@liveblocks/zustand 1.1.5-test3 → 1.1.5-test4

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.
@@ -0,0 +1,84 @@
1
+ import { JsonObject, LsonObject, BaseUserMeta, Json, Room, User, Status, Client } from '@liveblocks/client';
2
+ import { LegacyConnectionStatus } from '@liveblocks/core';
3
+ import { StoreMutatorIdentifier, StateCreator } from 'zustand';
4
+
5
+ declare type LiveblocksContext<TPresence extends JsonObject, TStorage extends LsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = {
6
+ /**
7
+ * Enters a room and starts sync it with zustand state
8
+ * @param roomId The id of the room
9
+ */
10
+ readonly enterRoom: (roomId: string) => void;
11
+ /**
12
+ * Leaves a room and stops sync it with zustand state.
13
+ * @param roomId The id of the room
14
+ */
15
+ readonly leaveRoom: (roomId: string) => void;
16
+ /**
17
+ * The room currently synced to your zustand state.
18
+ */
19
+ readonly room: Room<TPresence, TStorage, TUserMeta, TRoomEvent> | null;
20
+ /**
21
+ * Other users in the room. Empty no room is currently synced
22
+ */
23
+ readonly others: readonly User<TPresence, TUserMeta>[];
24
+ /**
25
+ * Whether or not the room storage is currently loading
26
+ */
27
+ readonly isStorageLoading: boolean;
28
+ /**
29
+ * Legacy connection status of the room.
30
+ *
31
+ * @deprecated This API will be removed in a future version of Liveblocks.
32
+ * Prefer using the newer `.status` property.
33
+ *
34
+ * We recommend making the following changes if you use these APIs:
35
+ *
36
+ * OLD STATUSES NEW STATUSES
37
+ * closed --> initial
38
+ * authenticating --> connecting
39
+ * connecting --> connecting
40
+ * open --> connected
41
+ * unavailable --> reconnecting
42
+ * failed --> disconnected
43
+ */
44
+ readonly connection: LegacyConnectionStatus;
45
+ /**
46
+ * Connection status of the room.
47
+ */
48
+ readonly status: Status;
49
+ };
50
+ /**
51
+ * @deprecated Renamed to WithLiveblocks<...>
52
+ */
53
+ declare type LiveblocksState<TState, TPresence extends JsonObject = JsonObject, TStorage extends LsonObject = LsonObject, TUserMeta extends BaseUserMeta = BaseUserMeta, TRoomEvent extends Json = Json> = WithLiveblocks<TState, TPresence, TStorage, TUserMeta, TRoomEvent>;
54
+ /**
55
+ * Adds the `liveblocks` property to your custom Zustand state.
56
+ */
57
+ declare type WithLiveblocks<TState, TPresence extends JsonObject = JsonObject, TStorage extends LsonObject = LsonObject, TUserMeta extends BaseUserMeta = BaseUserMeta, TRoomEvent extends Json = Json> = TState & {
58
+ readonly liveblocks: LiveblocksContext<TPresence, TStorage, TUserMeta, TRoomEvent>;
59
+ };
60
+ declare type Mapping<T> = {
61
+ [K in keyof T]?: boolean;
62
+ };
63
+ declare type Options<T> = {
64
+ /**
65
+ * Liveblocks client created by @liveblocks/client createClient
66
+ */
67
+ client: Client;
68
+ /**
69
+ * Mapping used to synchronize a part of your zustand state with one Liveblocks Room storage.
70
+ */
71
+ storageMapping?: Mapping<T>;
72
+ /**
73
+ * Mapping used to synchronize a part of your zustand state with one Liveblocks Room presence.
74
+ */
75
+ presenceMapping?: Mapping<T>;
76
+ };
77
+ declare type OuterLiveblocksMiddleware = <TState, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = []>(config: StateCreator<TState, Mps, Mcs, Omit<TState, "liveblocks">>, options: Options<Omit<TState, "liveblocks">>) => StateCreator<TState, Mps, Mcs, TState>;
78
+ declare const liveblocks: OuterLiveblocksMiddleware;
79
+ /**
80
+ * @deprecated Renamed to `liveblocks`.
81
+ */
82
+ declare const middleware: OuterLiveblocksMiddleware;
83
+
84
+ export { LiveblocksContext, LiveblocksState, Mapping, WithLiveblocks, liveblocks, middleware };
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ var _core = require('@liveblocks/core');
9
9
 
10
10
  // src/version.ts
11
11
  var PKG_NAME = "@liveblocks/zustand";
12
- var PKG_VERSION = "1.1.5-test3";
12
+ var PKG_VERSION = "1.1.5-test4";
13
13
  var PKG_FORMAT = "cjs";
14
14
 
15
15
  // src/index.ts
package/dist/index.mjs ADDED
@@ -0,0 +1,253 @@
1
+ // src/index.ts
2
+ import {
3
+ detectDupes,
4
+ errorIf,
5
+ legacy_patchImmutableObject,
6
+ lsonToJson,
7
+ patchLiveObjectKey
8
+ } from "@liveblocks/core";
9
+
10
+ // src/version.ts
11
+ var PKG_NAME = "@liveblocks/zustand";
12
+ var PKG_VERSION = "1.1.5-test4";
13
+ var PKG_FORMAT = "esm";
14
+
15
+ // src/index.ts
16
+ detectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);
17
+ var ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
18
+ function mappingToFunctionIsNotAllowed(key) {
19
+ return new Error(
20
+ `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`
21
+ );
22
+ }
23
+ var middlewareImpl = (config, options) => {
24
+ const { client, presenceMapping, storageMapping } = validateOptions(options);
25
+ return (set, get, api) => {
26
+ let maybeRoom = null;
27
+ let isPatching = false;
28
+ let storageRoot = null;
29
+ let unsubscribeCallbacks = [];
30
+ function enterRoom(roomId) {
31
+ if (storageRoot) {
32
+ return;
33
+ }
34
+ const initialPresence = selectFields(
35
+ get(),
36
+ presenceMapping
37
+ );
38
+ const room = client.enter(roomId, {
39
+ initialPresence
40
+ });
41
+ maybeRoom = room;
42
+ updateLiveblocksContext(set, { isStorageLoading: true, room });
43
+ unsubscribeCallbacks.push(
44
+ room.events.others.subscribe(({ others }) => {
45
+ updateLiveblocksContext(set, { others });
46
+ })
47
+ );
48
+ unsubscribeCallbacks.push(
49
+ room.events.connection.subscribe(() => {
50
+ updateLiveblocksContext(set, {
51
+ connection: room.getConnectionState(),
52
+ status: room.getStatus()
53
+ });
54
+ })
55
+ );
56
+ unsubscribeCallbacks.push(
57
+ room.events.me.subscribe(() => {
58
+ if (isPatching === false) {
59
+ set(
60
+ selectFields(
61
+ room.getPresence(),
62
+ presenceMapping
63
+ )
64
+ );
65
+ }
66
+ })
67
+ );
68
+ void room.getStorage().then(({ root }) => {
69
+ const updates = {};
70
+ room.batch(() => {
71
+ for (const key in storageMapping) {
72
+ const liveblocksStatePart = root.get(key);
73
+ if (liveblocksStatePart === void 0) {
74
+ updates[key] = get()[key];
75
+ patchLiveObjectKey(root, key, void 0, get()[key]);
76
+ } else {
77
+ updates[key] = lsonToJson(
78
+ liveblocksStatePart
79
+ );
80
+ }
81
+ }
82
+ });
83
+ set(updates);
84
+ storageRoot = root;
85
+ unsubscribeCallbacks.push(
86
+ room.subscribe(
87
+ root,
88
+ (updates2) => {
89
+ if (isPatching === false) {
90
+ set(patchState(get(), updates2, storageMapping));
91
+ }
92
+ },
93
+ { isDeep: true }
94
+ )
95
+ );
96
+ updateLiveblocksContext(set, {
97
+ isStorageLoading: false
98
+ });
99
+ });
100
+ }
101
+ function leaveRoom(roomId) {
102
+ for (const unsubscribe of unsubscribeCallbacks) {
103
+ unsubscribe();
104
+ }
105
+ storageRoot = null;
106
+ maybeRoom = null;
107
+ isPatching = false;
108
+ unsubscribeCallbacks = [];
109
+ client.leave(roomId);
110
+ updateLiveblocksContext(set, {
111
+ others: [],
112
+ connection: "closed",
113
+ isStorageLoading: false,
114
+ room: null
115
+ });
116
+ }
117
+ const store = config(
118
+ (args) => {
119
+ const { liveblocks: _, ...oldState } = get();
120
+ set(args);
121
+ const { liveblocks: __, ...newState } = get();
122
+ if (maybeRoom) {
123
+ const room = maybeRoom;
124
+ isPatching = true;
125
+ updatePresence(room, oldState, newState, presenceMapping);
126
+ room.batch(() => {
127
+ if (storageRoot) {
128
+ patchLiveblocksStorage(
129
+ storageRoot,
130
+ oldState,
131
+ newState,
132
+ storageMapping
133
+ );
134
+ }
135
+ });
136
+ isPatching = false;
137
+ }
138
+ },
139
+ get,
140
+ api
141
+ );
142
+ return {
143
+ ...store,
144
+ liveblocks: {
145
+ enterRoom,
146
+ leaveRoom,
147
+ room: null,
148
+ others: [],
149
+ connection: "closed",
150
+ isStorageLoading: false
151
+ }
152
+ };
153
+ };
154
+ };
155
+ var liveblocks = middlewareImpl;
156
+ var middleware = liveblocks;
157
+ function patchState(state, updates, mapping) {
158
+ const partialState = {};
159
+ for (const key in mapping) {
160
+ partialState[key] = state[key];
161
+ }
162
+ const patched = legacy_patchImmutableObject(partialState, updates);
163
+ const result = {};
164
+ for (const key in mapping) {
165
+ result[key] = patched[key];
166
+ }
167
+ return result;
168
+ }
169
+ function selectFields(presence, mapping) {
170
+ const partialState = {};
171
+ for (const key in mapping) {
172
+ partialState[key] = presence[key];
173
+ }
174
+ return partialState;
175
+ }
176
+ function updateLiveblocksContext(set, partial) {
177
+ set((state) => ({ liveblocks: { ...state.liveblocks, ...partial } }));
178
+ }
179
+ function updatePresence(room, oldState, newState, presenceMapping) {
180
+ for (const key in presenceMapping) {
181
+ if (typeof newState[key] === "function") {
182
+ throw mappingToFunctionIsNotAllowed(key);
183
+ }
184
+ if (oldState[key] !== newState[key]) {
185
+ const val = newState?.[key];
186
+ const patch = {};
187
+ patch[key] = val;
188
+ room.updatePresence(patch);
189
+ }
190
+ }
191
+ }
192
+ function patchLiveblocksStorage(root, oldState, newState, mapping) {
193
+ for (const key in mapping) {
194
+ if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
195
+ throw mappingToFunctionIsNotAllowed(key);
196
+ }
197
+ if (oldState[key] !== newState[key]) {
198
+ const oldVal = oldState[key];
199
+ const newVal = newState[key];
200
+ patchLiveObjectKey(root, key, oldVal, newVal);
201
+ }
202
+ }
203
+ }
204
+ function isObject(value) {
205
+ return Object.prototype.toString.call(value) === "[object Object]";
206
+ }
207
+ function validateNoDuplicateKeys(storageMapping, presenceMapping) {
208
+ for (const key in storageMapping) {
209
+ if (presenceMapping[key] !== void 0) {
210
+ throw new Error(
211
+ `${ERROR_PREFIX} "${key}" is mapped on both presenceMapping and storageMapping. A key shouldn't exist on both mapping.`
212
+ );
213
+ }
214
+ }
215
+ }
216
+ function validateMapping(mapping, mappingType) {
217
+ errorIf(
218
+ !isObject(mapping),
219
+ `${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`
220
+ );
221
+ const result = {};
222
+ for (const key in mapping) {
223
+ errorIf(
224
+ typeof mapping[key] !== "boolean",
225
+ `${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`
226
+ );
227
+ if (mapping[key] === true) {
228
+ result[key] = true;
229
+ }
230
+ }
231
+ return result;
232
+ }
233
+ function validateOptions(options) {
234
+ const client = options.client;
235
+ errorIf(!client, `${ERROR_PREFIX} client is missing`);
236
+ const storageMapping = validateMapping(
237
+ options.storageMapping ?? {},
238
+ "storageMapping"
239
+ );
240
+ const presenceMapping = validateMapping(
241
+ options.presenceMapping ?? {},
242
+ "presenceMapping"
243
+ );
244
+ if (process.env.NODE_ENV !== "production") {
245
+ validateNoDuplicateKeys(storageMapping, presenceMapping);
246
+ }
247
+ return { client, storageMapping, presenceMapping };
248
+ }
249
+ export {
250
+ liveblocks,
251
+ middleware
252
+ };
253
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/version.ts"],"sourcesContent":["import type {\n BaseUserMeta,\n Client,\n Json,\n JsonObject,\n LiveObject,\n LsonObject,\n Room,\n Status,\n User,\n} from \"@liveblocks/client\";\nimport type { LegacyConnectionStatus, StorageUpdate } from \"@liveblocks/core\";\nimport {\n detectDupes,\n errorIf,\n legacy_patchImmutableObject,\n lsonToJson,\n patchLiveObjectKey,\n} from \"@liveblocks/core\";\nimport type { StateCreator, StoreMutatorIdentifier } from \"zustand\";\n\nimport { PKG_FORMAT, PKG_NAME, PKG_VERSION } from \"./version\";\n\ndetectDupes(PKG_NAME, PKG_VERSION, PKG_FORMAT);\n\nconst ERROR_PREFIX = \"Invalid @liveblocks/zustand middleware config.\";\n\nfunction mappingToFunctionIsNotAllowed(key: string): Error {\n return new Error(\n `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`\n );\n}\n\nexport type LiveblocksContext<\n TPresence extends JsonObject,\n TStorage extends LsonObject,\n TUserMeta extends BaseUserMeta,\n TRoomEvent extends Json,\n> = {\n /**\n * Enters a room and starts sync it with zustand state\n * @param roomId The id of the room\n */\n readonly enterRoom: (roomId: string) => void;\n /**\n * Leaves a room and stops sync it with zustand state.\n * @param roomId The id of the room\n */\n readonly leaveRoom: (roomId: string) => void;\n /**\n * The room currently synced to your zustand state.\n */\n readonly room: Room<TPresence, TStorage, TUserMeta, TRoomEvent> | null;\n /**\n * Other users in the room. Empty no room is currently synced\n */\n readonly others: readonly User<TPresence, TUserMeta>[];\n /**\n * Whether or not the room storage is currently loading\n */\n readonly isStorageLoading: boolean;\n /**\n * Legacy connection status of the room.\n *\n * @deprecated This API will be removed in a future version of Liveblocks.\n * Prefer using the newer `.status` property.\n *\n * We recommend making the following changes if you use these APIs:\n *\n * OLD STATUSES NEW STATUSES\n * closed --> initial\n * authenticating --> connecting\n * connecting --> connecting\n * open --> connected\n * unavailable --> reconnecting\n * failed --> disconnected\n */\n readonly connection: LegacyConnectionStatus;\n /**\n * Connection status of the room.\n */\n readonly status: Status;\n};\n\n/**\n * @deprecated Renamed to WithLiveblocks<...>\n */\nexport type LiveblocksState<\n TState,\n TPresence extends JsonObject = JsonObject,\n TStorage extends LsonObject = LsonObject,\n TUserMeta extends BaseUserMeta = BaseUserMeta,\n TRoomEvent extends Json = Json,\n> = WithLiveblocks<TState, TPresence, TStorage, TUserMeta, TRoomEvent>;\n\n/**\n * Adds the `liveblocks` property to your custom Zustand state.\n */\nexport type WithLiveblocks<\n TState,\n TPresence extends JsonObject = JsonObject,\n TStorage extends LsonObject = LsonObject,\n TUserMeta extends BaseUserMeta = BaseUserMeta,\n TRoomEvent extends Json = Json,\n> = TState & {\n readonly liveblocks: LiveblocksContext<\n TPresence,\n TStorage,\n TUserMeta,\n TRoomEvent\n >;\n};\n\nexport type Mapping<T> = {\n [K in keyof T]?: boolean;\n};\n\ntype Options<T> = {\n /**\n * Liveblocks client created by @liveblocks/client createClient\n */\n client: Client;\n /**\n * Mapping used to synchronize a part of your zustand state with one Liveblocks Room storage.\n */\n storageMapping?: Mapping<T>;\n /**\n * Mapping used to synchronize a part of your zustand state with one Liveblocks Room presence.\n */\n presenceMapping?: Mapping<T>;\n};\n\ntype OuterLiveblocksMiddleware = <\n TState,\n Mps extends [StoreMutatorIdentifier, unknown][] = [],\n Mcs extends [StoreMutatorIdentifier, unknown][] = [],\n>(\n config: StateCreator<TState, Mps, Mcs, Omit<TState, \"liveblocks\">>,\n options: Options<Omit<TState, \"liveblocks\">>\n) => StateCreator<TState, Mps, Mcs, TState>;\n\ntype InnerLiveblocksMiddleware = <\n TState extends {\n readonly liveblocks: LiveblocksContext<\n JsonObject,\n LsonObject,\n BaseUserMeta,\n Json\n >;\n },\n>(\n config: StateCreator<TState, [], []>,\n options: Options<TState>\n) => StateCreator<TState, [], []>;\n\ntype ExtractPresence<\n TRoom extends Room<JsonObject, LsonObject, BaseUserMeta, Json>,\n> = TRoom extends Room<infer P, any, any, any> ? P : never;\n\ntype ExtractStorage<\n TRoom extends Room<JsonObject, LsonObject, BaseUserMeta, Json>,\n> = TRoom extends Room<any, infer S, any, any> ? S : never;\n\nconst middlewareImpl: InnerLiveblocksMiddleware = (config, options) => {\n type TState = ReturnType<typeof config>;\n type TLiveblocksContext = TState[\"liveblocks\"];\n type TRoom = NonNullable<TLiveblocksContext[\"room\"]>;\n type TPresence = ExtractPresence<TRoom>;\n type TStorage = ExtractStorage<TRoom>;\n\n const { client, presenceMapping, storageMapping } = validateOptions(options);\n return (set, get, api) => {\n let maybeRoom: TRoom | null = null;\n let isPatching: boolean = false;\n let storageRoot: LiveObject<TStorage> | null = null;\n let unsubscribeCallbacks: Array<() => void> = [];\n\n function enterRoom(roomId: string) {\n if (storageRoot) {\n return;\n }\n\n const initialPresence = selectFields(\n get(),\n presenceMapping\n ) as unknown as TPresence;\n\n const room = client.enter(roomId, {\n initialPresence,\n }) as unknown as TRoom;\n maybeRoom = room;\n\n updateLiveblocksContext(set, { isStorageLoading: true, room });\n\n unsubscribeCallbacks.push(\n room.events.others.subscribe(({ others }) => {\n updateLiveblocksContext(set, { others });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.connection.subscribe(() => {\n updateLiveblocksContext(set, {\n connection: room.getConnectionState(),\n status: room.getStatus(),\n });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.me.subscribe(() => {\n if (isPatching === false) {\n set(\n selectFields(\n room.getPresence(),\n presenceMapping\n ) as Partial<TState>\n );\n }\n })\n );\n\n void room.getStorage().then(({ root }) => {\n const updates = {} as Partial<TState>;\n\n room.batch(() => {\n for (const key in storageMapping) {\n const liveblocksStatePart = root.get(key);\n if (liveblocksStatePart === undefined) {\n updates[key] = get()[key];\n patchLiveObjectKey(root, key, undefined, get()[key]);\n } else {\n updates[key] = lsonToJson(\n liveblocksStatePart\n ) as unknown as TState[Extract<keyof TState, string>];\n }\n }\n });\n\n set(updates);\n\n storageRoot = root as LiveObject<TStorage>;\n unsubscribeCallbacks.push(\n room.subscribe(\n root,\n (updates) => {\n if (isPatching === false) {\n set(patchState(get(), updates, storageMapping));\n }\n },\n { isDeep: true }\n )\n );\n\n // set isLoading storage to false once storage is loaded\n updateLiveblocksContext(set, {\n isStorageLoading: false,\n });\n });\n }\n\n function leaveRoom(roomId: string) {\n for (const unsubscribe of unsubscribeCallbacks) {\n unsubscribe();\n }\n storageRoot = null;\n maybeRoom = null;\n isPatching = false;\n unsubscribeCallbacks = [];\n client.leave(roomId);\n updateLiveblocksContext(set, {\n others: [],\n connection: \"closed\",\n isStorageLoading: false,\n room: null,\n });\n }\n\n const store = config(\n (args) => {\n const { liveblocks: _, ...oldState } = get();\n set(args);\n const { liveblocks: __, ...newState } = get();\n\n if (maybeRoom) {\n const room = maybeRoom;\n isPatching = true;\n updatePresence(room, oldState, newState, presenceMapping);\n\n room.batch(() => {\n if (storageRoot) {\n patchLiveblocksStorage(\n storageRoot,\n oldState,\n newState,\n storageMapping\n );\n }\n });\n\n isPatching = false;\n }\n },\n get,\n api\n );\n\n return {\n ...store,\n liveblocks: {\n enterRoom,\n leaveRoom,\n room: null,\n others: [],\n connection: \"closed\",\n isStorageLoading: false,\n },\n };\n };\n};\n\nexport const liveblocks =\n middlewareImpl as unknown as OuterLiveblocksMiddleware;\n\n/**\n * @deprecated Renamed to `liveblocks`.\n */\nexport const middleware = liveblocks;\n\nfunction patchState<T>(\n state: T,\n updates: StorageUpdate[],\n mapping: Mapping<T>\n) {\n const partialState: Partial<T> = {};\n\n for (const key in mapping) {\n partialState[key] = state[key];\n }\n\n const patched = legacy_patchImmutableObject(partialState, updates);\n\n const result: Partial<T> = {};\n\n for (const key in mapping) {\n result[key] = patched[key];\n }\n\n return result;\n}\n\nfunction selectFields<TState>(\n presence: TState,\n mapping: Mapping<TState>\n): /* TODO: Actually, Pick<TState, keyof Mapping<TState>> ? */\nPartial<TState> {\n const partialState = {} as Partial<TState>;\n for (const key in mapping) {\n partialState[key] = presence[key];\n }\n return partialState;\n}\n\nfunction updateLiveblocksContext<\n TState,\n TPresence extends JsonObject,\n TStorage extends LsonObject,\n TUserMeta extends BaseUserMeta,\n TRoomEvent extends Json,\n>(\n set: (\n callbackOrPartial: (\n current: WithLiveblocks<\n TState,\n TPresence,\n TStorage,\n TUserMeta,\n TRoomEvent\n >\n ) =>\n | WithLiveblocks<TState, TPresence, TStorage, TUserMeta, TRoomEvent>\n | Partial<any>\n ) => void,\n partial: Partial<\n LiveblocksContext<TPresence, TStorage, TUserMeta, TRoomEvent>\n >\n) {\n set((state) => ({ liveblocks: { ...state.liveblocks, ...partial } }));\n}\n\nfunction updatePresence<\n TPresence extends JsonObject,\n TStorage extends LsonObject,\n TUserMeta extends BaseUserMeta,\n TRoomEvent extends Json,\n>(\n room: Room<TPresence, TStorage, TUserMeta, TRoomEvent>,\n oldState: TPresence,\n newState: TPresence,\n presenceMapping: Mapping<TPresence>\n) {\n for (const key in presenceMapping) {\n if (typeof newState[key] === \"function\") {\n throw mappingToFunctionIsNotAllowed(key);\n }\n\n if (oldState[key] !== newState[key]) {\n const val = newState?.[key];\n const patch = {} as Partial<TPresence>;\n patch[key] = val;\n room.updatePresence(patch);\n }\n }\n}\n\nfunction patchLiveblocksStorage<O extends LsonObject, TState>(\n root: LiveObject<O>,\n oldState: TState,\n newState: TState,\n mapping: Mapping<TState>\n) {\n for (const key in mapping) {\n if (\n process.env.NODE_ENV !== \"production\" &&\n typeof newState[key] === \"function\"\n ) {\n throw mappingToFunctionIsNotAllowed(key);\n }\n\n if (oldState[key] !== newState[key]) {\n const oldVal = oldState[key];\n const newVal = newState[key];\n patchLiveObjectKey(root, key, oldVal, newVal);\n }\n }\n}\n\nfunction isObject(value: unknown): value is object {\n return Object.prototype.toString.call(value) === \"[object Object]\";\n}\n\nfunction validateNoDuplicateKeys<T>(\n storageMapping: Mapping<T>,\n presenceMapping: Mapping<T>\n) {\n for (const key in storageMapping) {\n if (presenceMapping[key] !== undefined) {\n throw new Error(\n `${ERROR_PREFIX} \"${key}\" is mapped on both presenceMapping and storageMapping. A key shouldn't exist on both mapping.`\n );\n }\n }\n}\n\n/**\n * Remove false keys from mapping and generate to a new object to avoid potential mutation from outside the middleware\n */\nfunction validateMapping<T>(\n mapping: Mapping<T>,\n mappingType: \"storageMapping\" | \"presenceMapping\"\n): Mapping<T> {\n errorIf(\n !isObject(mapping),\n `${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`\n );\n\n const result: Mapping<T> = {};\n for (const key in mapping) {\n errorIf(\n typeof mapping[key] !== \"boolean\",\n `${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`\n );\n\n if (mapping[key] === true) {\n result[key] = true;\n }\n }\n return result;\n}\n\nfunction validateOptions<TState>(options: Options<TState>): {\n client: Client;\n presenceMapping: Mapping<TState>;\n storageMapping: Mapping<TState>;\n} {\n const client = options.client;\n errorIf(!client, `${ERROR_PREFIX} client is missing`);\n\n const storageMapping = validateMapping(\n options.storageMapping ?? {},\n \"storageMapping\"\n );\n\n const presenceMapping = validateMapping(\n options.presenceMapping ?? {},\n \"presenceMapping\"\n );\n\n if (process.env.NODE_ENV !== \"production\") {\n validateNoDuplicateKeys(storageMapping, presenceMapping);\n }\n\n return { client, storageMapping, presenceMapping };\n}\n","declare const __VERSION__: string;\ndeclare const TSUP_FORMAT: string;\n\nexport const PKG_NAME = \"@liveblocks/zustand\";\nexport const PKG_VERSION = typeof __VERSION__ === \"string\" && __VERSION__;\nexport const PKG_FORMAT = typeof TSUP_FORMAT === \"string\" && TSUP_FORMAT;\n"],"mappings":";AAYA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;ACfA,IAAM,WAAW;AACjB,IAAM,cAAiD;AACvD,IAAM,aAAgD;;;ADkB7D,YAAY,UAAU,aAAa,UAAU;AAE7C,IAAM,eAAe;AAErB,SAAS,8BAA8B,KAAoB;AACzD,SAAO,IAAI;AAAA,IACT,GAAG,YAAY,YAAY,GAAG;AAAA,EAChC;AACF;AAoIA,IAAM,iBAA4C,CAAC,QAAQ,YAAY;AAOrE,QAAM,EAAE,QAAQ,iBAAiB,eAAe,IAAI,gBAAgB,OAAO;AAC3E,SAAO,CAAC,KAAK,KAAK,QAAQ;AACxB,QAAI,YAA0B;AAC9B,QAAI,aAAsB;AAC1B,QAAI,cAA2C;AAC/C,QAAI,uBAA0C,CAAC;AAE/C,aAAS,UAAU,QAAgB;AACjC,UAAI,aAAa;AACf;AAAA,MACF;AAEA,YAAM,kBAAkB;AAAA,QACtB,IAAI;AAAA,QACJ;AAAA,MACF;AAEA,YAAM,OAAO,OAAO,MAAM,QAAQ;AAAA,QAChC;AAAA,MACF,CAAC;AACD,kBAAY;AAEZ,8BAAwB,KAAK,EAAE,kBAAkB,MAAM,KAAK,CAAC;AAE7D,2BAAqB;AAAA,QACnB,KAAK,OAAO,OAAO,UAAU,CAAC,EAAE,OAAO,MAAM;AAC3C,kCAAwB,KAAK,EAAE,OAAO,CAAC;AAAA,QACzC,CAAC;AAAA,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,SAAqB,CAAC;AAC5B,aAAW,OAAO,SAAS;AACzB;AAAA,MACE,OAAO,QAAQ,GAAG,MAAM;AAAA,MACxB,GAAG,YAAY,IAAI,WAAW,IAAI,GAAG;AAAA,IACvC;AAEA,QAAI,QAAQ,GAAG,MAAM,MAAM;AACzB,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,gBAAwB,SAI/B;AACA,QAAM,SAAS,QAAQ;AACvB,UAAQ,CAAC,QAAQ,GAAG,YAAY,oBAAoB;AAEpD,QAAM,iBAAiB;AAAA,IACrB,QAAQ,kBAAkB,CAAC;AAAA,IAC3B;AAAA,EACF;AAEA,QAAM,kBAAkB;AAAA,IACtB,QAAQ,mBAAmB,CAAC;AAAA,IAC5B;AAAA,EACF;AAEA,MAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,4BAAwB,gBAAgB,eAAe;AAAA,EACzD;AAEA,SAAO,EAAE,QAAQ,gBAAgB,gBAAgB;AACnD;","names":["updates"]}
package/package.json CHANGED
@@ -1,10 +1,23 @@
1
1
  {
2
2
  "name": "@liveblocks/zustand",
3
- "version": "1.1.5-test3",
3
+ "version": "1.1.5-test4",
4
4
  "description": "A middleware to integrate Liveblocks into Zustand stores. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.mts",
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.ts",
16
+ "module": "./dist/index.mjs",
17
+ "default": "./dist/index.js"
18
+ }
19
+ }
20
+ },
8
21
  "files": [
9
22
  "dist/**",
10
23
  "README.md"
@@ -20,8 +33,8 @@
20
33
  "test:watch": "jest --silent --verbose --color=always --watch"
21
34
  },
22
35
  "dependencies": {
23
- "@liveblocks/client": "1.1.5-test3",
24
- "@liveblocks/core": "1.1.5-test3"
36
+ "@liveblocks/client": "1.1.5-test4",
37
+ "@liveblocks/core": "1.1.5-test4"
25
38
  },
26
39
  "peerDependencies": {
27
40
  "zustand": "^4.1.3"