@liveblocks/zustand 3.17.0 → 3.18.0-rc1

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/dist/index.cjs CHANGED
@@ -1,15 +1,9 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/index.ts
2
-
3
-
4
-
5
-
6
-
7
-
8
2
  var _core = require('@liveblocks/core');
9
3
 
10
4
  // src/version.ts
11
5
  var PKG_NAME = "@liveblocks/zustand";
12
- var PKG_VERSION = "3.17.0";
6
+ var PKG_VERSION = "3.18.0-rc1";
13
7
  var PKG_FORMAT = "cjs";
14
8
 
15
9
  // src/index.ts
@@ -20,9 +14,17 @@ function mappingToFunctionIsNotAllowed(key) {
20
14
  `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`
21
15
  );
22
16
  }
17
+ function ensureNoFunctions(state) {
18
+ for (const key in state) {
19
+ if (typeof state[key] === "function") {
20
+ throw mappingToFunctionIsNotAllowed(key);
21
+ }
22
+ }
23
+ }
23
24
  var middlewareImpl = (config, options) => {
24
25
  const { client, presenceMapping, storageMapping } = validateOptions(options);
25
26
  const presenceKeys = Object.keys(presenceMapping);
27
+ const storageKeys = Object.keys(storageMapping);
26
28
  return (set, get, api) => {
27
29
  let maybeRoom = null;
28
30
  let isPatching = false;
@@ -65,31 +67,24 @@ var middlewareImpl = (config, options) => {
65
67
  })
66
68
  );
67
69
  void room.getStorage().then(({ root }) => {
68
- const updates = {};
69
- room.batch(() => {
70
- for (const key in storageMapping) {
71
- const liveblocksStatePart = root.get(key);
72
- if (liveblocksStatePart === void 0) {
73
- updates[key] = get()[key];
74
- _core.legacy_patchLiveObjectKey.call(void 0,
75
- root,
76
- key,
77
- void 0,
78
- get()[key]
79
- );
80
- } else {
81
- updates[key] = _core.lsonToJson.call(void 0,
82
- liveblocksStatePart
83
- );
84
- }
70
+ const state = get();
71
+ const missing = {};
72
+ for (const key of storageKeys) {
73
+ if (root.get(key) === void 0) {
74
+ missing[key] = state[key];
85
75
  }
76
+ }
77
+ room.history[_core.kInternal].withoutHistory(() => {
78
+ room.batch(() => {
79
+ root.reconcilePartially(missing);
80
+ });
86
81
  });
87
- set(updates);
82
+ set(pick(root.toJSON(), storageKeys));
88
83
  storageRoot = root;
89
84
  unsubscribeCallbacks.push(
90
- room.events.storageBatch.subscribe((updates2) => {
85
+ room.events.storageBatch.subscribe(() => {
91
86
  if (!isPatching) {
92
- set(patchState(get(), updates2, storageMapping));
87
+ set(pick(root.toJSON(), storageKeys));
93
88
  }
94
89
  })
95
90
  );
@@ -128,20 +123,19 @@ var middlewareImpl = (config, options) => {
128
123
  const room = maybeRoom;
129
124
  isPatching = true;
130
125
  try {
131
- updatePresence(
132
- room,
133
- oldState,
134
- newState,
135
- presenceMapping
136
- );
137
126
  room.batch(() => {
127
+ updatePresence(
128
+ room,
129
+ oldState,
130
+ newState,
131
+ presenceMapping
132
+ );
138
133
  if (storageRoot) {
139
- patchLiveblocksStorage(
140
- storageRoot,
141
- oldState,
142
- newState,
143
- storageMapping
144
- );
134
+ const partialState = pick(newState, storageKeys);
135
+ if (process.env.NODE_ENV !== "production") {
136
+ ensureNoFunctions(partialState);
137
+ }
138
+ storageRoot.reconcilePartially(partialState);
145
139
  }
146
140
  });
147
141
  } finally {
@@ -166,21 +160,6 @@ var middlewareImpl = (config, options) => {
166
160
  };
167
161
  };
168
162
  var liveblocks = middlewareImpl;
169
- function patchState(state, updates, mapping) {
170
- const partialState = {};
171
- for (const key in mapping) {
172
- partialState[key] = state[key];
173
- }
174
- const patched = _core.legacy_patchImmutableObject.call(void 0,
175
- partialState,
176
- updates
177
- );
178
- const result = {};
179
- for (const key in mapping) {
180
- result[key] = patched[key];
181
- }
182
- return result;
183
- }
184
163
  function pick(source, keys) {
185
164
  const result = {};
186
165
  for (const key of keys) {
@@ -204,18 +183,6 @@ function updatePresence(room, oldState, newState, presenceMapping) {
204
183
  }
205
184
  }
206
185
  }
207
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
208
- for (const key in mapping) {
209
- if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
210
- throw mappingToFunctionIsNotAllowed(key);
211
- }
212
- if (oldState[key] !== newState[key]) {
213
- const oldVal = oldState[key];
214
- const newVal = newState[key];
215
- _core.legacy_patchLiveObjectKey.call(void 0, root, key, oldVal, newVal);
216
- }
217
- }
218
- }
219
186
  function isObject(value) {
220
187
  return Object.prototype.toString.call(value) === "[object Object]";
221
188
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["/home/runner/work/liveblocks/liveblocks/packages/liveblocks-zustand/dist/index.cjs","../src/index.ts","../src/version.ts"],"names":[],"mappings":"AAAA;ACuBA;AACE;AACA;AACA;AACA;AACA;AAAA,wCACK;ADrBP;AACA;AENO,IAAM,SAAA,EAAW,qBAAA;AACjB,IAAM,YAAA,EAAiD,QAAA;AACvD,IAAM,WAAA,EAAgD,KAAA;AFQ7D;AACA;ACoBA,+BAAA,QAAY,EAAU,WAAA,EAAa,UAAU,CAAA;AAE7C,IAAM,aAAA,EAAe,gDAAA;AAErB,SAAS,6BAAA,CAA8B,GAAA,EAAoB;AACzD,EAAA,OAAO,IAAI,KAAA;AAAA,IACT,CAAA,EAAA;AACF,EAAA;AACF;AA2GM;AAOJ,EAAA;AACA,EAAA;AACA,EAAA;AACE,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AACA,IAAA;AAEA,IAAA;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AACA,IAAA;AACF,EAAA;AAEA,EAAA;AACE,IAAA;AACF,EAAA;AAEA,EAAA;AACF;ADhPK;AACA;AACA","file":"/home/runner/work/liveblocks/liveblocks/packages/liveblocks-zustand/dist/index.cjs","sourcesContent":[null,"import type {\n BaseUserMeta,\n Json,\n JsonObject,\n LiveObject,\n LsonObject,\n Room,\n Status,\n User,\n} from \"@liveblocks/client\";\nimport type {\n BaseMetadata,\n DCM,\n DE,\n DP,\n DS,\n DTM,\n DU,\n EnterOptions,\n OpaqueClient,\n OpaqueRoom,\n StorageUpdate,\n} from \"@liveblocks/core\";\nimport {\n detectDupes,\n errorIf,\n legacy_patchImmutableObject,\n legacy_patchLiveObjectKey,\n lsonToJson,\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 P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n> = {\n /**\n * Enters a room and starts sync it with zustand state\n * @param roomId The id of the room\n * @param options Optional. Options to pass to the underlying client.enterRoom call (e.g. `engine`).\n */\n readonly enterRoom: (\n roomId: string,\n options?: Pick<EnterOptions, \"engine\">\n ) => () => void;\n /**\n * Leaves the currently entered room and stops sync it with zustand state, if\n * any. If enterRoom was not called before, this is a no-op.\n */\n readonly leaveRoom: () => void;\n /**\n * The room currently synced to your zustand state.\n */\n readonly room: Room<P, S, U, E, TM, CM> | null;\n /**\n * Other users in the room. Empty no room is currently synced\n */\n readonly others: readonly User<P, U>[];\n /**\n * Whether or not the room storage is currently loading\n */\n readonly isStorageLoading: boolean;\n /**\n * Connection status of the room.\n */\n readonly status: Status;\n};\n\n/**\n * Adds the `liveblocks` property to your custom Zustand state.\n */\nexport type WithLiveblocks<\n TState,\n P extends JsonObject = DP,\n S extends LsonObject = DS,\n U extends BaseUserMeta = DU,\n E extends Json = DE,\n TM extends BaseMetadata = DTM,\n CM extends BaseMetadata = DCM,\n> = TState & {\n readonly liveblocks: LiveblocksContext<P, S, U, E, TM, CM>;\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: OpaqueClient;\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 BaseMetadata,\n BaseMetadata\n >;\n },\n>(\n config: StateCreator<TState, [], []>,\n options: Options<TState>\n) => StateCreator<TState, [], []>;\n\ntype ExtractPresence<TRoom extends OpaqueRoom> =\n TRoom extends Room<infer P, any, any, any, any> ? P : never;\n\ntype ExtractStorage<TRoom extends OpaqueRoom> =\n TRoom extends Room<any, infer S, any, 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 P = ExtractPresence<TRoom>;\n type S = ExtractStorage<TRoom>;\n\n const { client, presenceMapping, storageMapping } = validateOptions(options);\n const presenceKeys = Object.keys(presenceMapping);\n return (set, get, api) => {\n let maybeRoom: TRoom | null = null;\n let isPatching = false;\n let storageRoot: LiveObject<S> | null = null;\n let unsubscribeCallbacks: Array<() => void> = [];\n let lastRoomId: string | null = null;\n let lastLeaveFn: (() => void) | null = null;\n\n function enterRoom(\n newRoomId: string,\n options?: Pick<EnterOptions, \"engine\">\n ): void {\n if (lastRoomId === newRoomId) {\n return;\n }\n\n lastRoomId = newRoomId;\n if (lastLeaveFn !== null) {\n // First leave the old room before entering a potential new one\n lastLeaveFn();\n }\n\n const initialPresence = pick(get(), presenceKeys) as unknown as P;\n\n const { room, leave } = client.enterRoom(newRoomId, {\n engine: options?.engine,\n initialPresence,\n }) as unknown as { room: TRoom; leave: () => void };\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.status.subscribe((status) => {\n updateLiveblocksContext(set, {\n status,\n });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.myPresence.subscribe(() => {\n if (!isPatching) {\n set(pick(room.getPresence(), presenceKeys) as Partial<TState>);\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 legacy_patchLiveObjectKey(\n root,\n key,\n undefined,\n get()[key] as Json | undefined\n );\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<S>;\n unsubscribeCallbacks.push(\n room.events.storageBatch.subscribe((updates) => {\n if (!isPatching) {\n set(patchState(get(), updates, storageMapping));\n }\n })\n );\n\n // set isLoading storage to false once storage is loaded\n updateLiveblocksContext(set, {\n isStorageLoading: false,\n });\n });\n\n lastLeaveFn = () => {\n for (const unsubscribe of unsubscribeCallbacks) {\n unsubscribe();\n }\n unsubscribeCallbacks = [];\n\n storageRoot = null;\n maybeRoom = null;\n isPatching = false;\n\n lastRoomId = null;\n lastLeaveFn = null;\n leave();\n\n updateLiveblocksContext(set, {\n others: [],\n status: \"initial\",\n isStorageLoading: false,\n room: null,\n });\n };\n }\n\n function leaveRoom() {\n lastLeaveFn?.();\n }\n\n const store = config(\n (...args) => {\n const { liveblocks: _, ...oldState } = get();\n // @ts-expect-error a bit too dynamic to type\n set(...args);\n const { liveblocks: __, ...newState } = get();\n\n if (maybeRoom) {\n const room = maybeRoom;\n isPatching = true;\n try {\n updatePresence(\n room,\n oldState as JsonObject,\n newState as JsonObject,\n presenceMapping\n );\n\n room.batch(() => {\n if (storageRoot) {\n patchLiveblocksStorage(\n storageRoot,\n oldState,\n newState,\n storageMapping\n );\n }\n });\n } finally {\n isPatching = false;\n }\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\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(\n partialState as JsonObject,\n updates\n );\n\n const result: Partial<T> = {};\n\n for (const key in mapping) {\n // @ts-expect-error key is a key of T\n result[key] = patched[key];\n }\n\n return result;\n}\n\nfunction pick(\n source: Record<string, unknown>,\n keys: Iterable<string>\n): Record<string, unknown> {\n const result: Record<string, unknown> = {};\n for (const key of keys) {\n result[key] = source[key];\n }\n return result;\n}\n\nfunction updateLiveblocksContext<\n TState,\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n>(\n set: (\n callbackOrPartial: (\n current: WithLiveblocks<TState, P, S, U, E, TM, CM>\n ) => WithLiveblocks<TState, P, S, U, E> | Partial<any>\n ) => void,\n partial: Partial<LiveblocksContext<P, S, U, E, TM, CM>>\n) {\n set((state) => ({ liveblocks: { ...state.liveblocks, ...partial } }));\n}\n\nfunction updatePresence<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n>(\n room: Room<P, S, U, E, TM, CM>,\n oldState: P,\n newState: P,\n presenceMapping: Mapping<P>\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<P>;\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] as Json | undefined;\n const newVal = newState[key] as Json | undefined;\n legacy_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: OpaqueClient;\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"]}
1
+ {"version":3,"sources":["/home/runner/work/liveblocks/liveblocks/packages/liveblocks-zustand/dist/index.cjs","../src/index.ts","../src/version.ts"],"names":[],"mappings":"AAAA;ACsBA,wCAAgD;ADpBhD;AACA;AEAO,IAAM,SAAA,EAAW,qBAAA;AACjB,IAAM,YAAA,EAAiD,YAAA;AACvD,IAAM,WAAA,EAAgD,KAAA;AFE7D;AACA;ACmBA,+BAAA,QAAY,EAAU,WAAA,EAAa,UAAU,CAAA;AAE7C,IAAM,aAAA,EAAe,gDAAA;AAErB,SAAS,6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;AACF;AD/NK;AACA;AACA","file":"/home/runner/work/liveblocks/liveblocks/packages/liveblocks-zustand/dist/index.cjs","sourcesContent":[null,"import type {\n BaseUserMeta,\n Json,\n JsonObject,\n LiveObject,\n LsonObject,\n Room,\n Status,\n User,\n} from \"@liveblocks/client\";\nimport type {\n BaseMetadata,\n DCM,\n DE,\n DP,\n DS,\n DTM,\n DU,\n EnterOptions,\n OpaqueClient,\n OpaqueRoom,\n} from \"@liveblocks/core\";\nimport { detectDupes, errorIf, kInternal } 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 P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n> = {\n /**\n * Enters a room and starts sync it with zustand state\n * @param roomId The id of the room\n * @param options Optional. Options to pass to the underlying client.enterRoom call (e.g. `engine`).\n */\n readonly enterRoom: (\n roomId: string,\n options?: Pick<EnterOptions, \"engine\">\n ) => () => void;\n /**\n * Leaves the currently entered room and stops sync it with zustand state, if\n * any. If enterRoom was not called before, this is a no-op.\n */\n readonly leaveRoom: () => void;\n /**\n * The room currently synced to your zustand state.\n */\n readonly room: Room<P, S, U, E, TM, CM> | null;\n /**\n * Other users in the room. Empty no room is currently synced\n */\n readonly others: readonly User<P, U>[];\n /**\n * Whether or not the room storage is currently loading\n */\n readonly isStorageLoading: boolean;\n /**\n * Connection status of the room.\n */\n readonly status: Status;\n};\n\n/**\n * Adds the `liveblocks` property to your custom Zustand state.\n */\nexport type WithLiveblocks<\n TState,\n P extends JsonObject = DP,\n S extends LsonObject = DS,\n U extends BaseUserMeta = DU,\n E extends Json = DE,\n TM extends BaseMetadata = DTM,\n CM extends BaseMetadata = DCM,\n> = TState & {\n readonly liveblocks: LiveblocksContext<P, S, U, E, TM, CM>;\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: OpaqueClient;\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 BaseMetadata,\n BaseMetadata\n >;\n },\n>(\n config: StateCreator<TState, [], []>,\n options: Options<TState>\n) => StateCreator<TState, [], []>;\n\ntype ExtractPresence<TRoom extends OpaqueRoom> =\n TRoom extends Room<infer P, any, any, any, any> ? P : never;\n\ntype ExtractStorage<TRoom extends OpaqueRoom> =\n TRoom extends Room<any, infer S, any, any, any> ? S : never;\n\n/** Ensures values of the provided object are not functions */\nfunction ensureNoFunctions(state: Record<string, unknown>): void {\n for (const key in state) {\n if (typeof state[key] === \"function\") {\n throw mappingToFunctionIsNotAllowed(key);\n }\n }\n}\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 P = ExtractPresence<TRoom>;\n type S = ExtractStorage<TRoom>;\n\n const { client, presenceMapping, storageMapping } = validateOptions(options);\n const presenceKeys = Object.keys(presenceMapping);\n const storageKeys = Object.keys(storageMapping);\n return (set, get, api) => {\n let maybeRoom: TRoom | null = null;\n let isPatching = false;\n let storageRoot: LiveObject<S> | null = null;\n let unsubscribeCallbacks: Array<() => void> = [];\n let lastRoomId: string | null = null;\n let lastLeaveFn: (() => void) | null = null;\n\n function enterRoom(\n newRoomId: string,\n options?: Pick<EnterOptions, \"engine\">\n ): void {\n if (lastRoomId === newRoomId) {\n return;\n }\n\n lastRoomId = newRoomId;\n if (lastLeaveFn !== null) {\n // First leave the old room before entering a potential new one\n lastLeaveFn();\n }\n\n const initialPresence = pick(get(), presenceKeys) as unknown as P;\n\n const { room, leave } = client.enterRoom(newRoomId, {\n engine: options?.engine,\n initialPresence,\n }) as unknown as { room: TRoom; leave: () => void };\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.status.subscribe((status) => {\n updateLiveblocksContext(set, {\n status,\n });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.myPresence.subscribe(() => {\n if (!isPatching) {\n set(pick(room.getPresence(), presenceKeys) as Partial<TState>);\n }\n })\n );\n\n void room.getStorage().then(({ root }) => {\n // Seed any missing storage keys from the current Zustand state.\n // Only writes keys that don't exist yet in storage — existing\n // storage values are left untouched and will be read back below.\n const state = get() as Record<string, unknown>;\n const missing: JsonObject = {};\n for (const key of storageKeys) {\n if (root.get(key) === undefined) {\n missing[key] = state[key] as Json;\n }\n }\n\n room.history[kInternal].withoutHistory(() => {\n room.batch(() => {\n root.reconcilePartially(missing);\n });\n });\n\n set(pick(root.toJSON(), storageKeys) as Partial<TState>);\n\n storageRoot = root as LiveObject<S>;\n unsubscribeCallbacks.push(\n room.events.storageBatch.subscribe(() => {\n if (!isPatching) {\n set(pick(root.toJSON(), storageKeys) as Partial<TState>);\n }\n })\n );\n\n // set isLoading storage to false once storage is loaded\n updateLiveblocksContext(set, {\n isStorageLoading: false,\n });\n });\n\n lastLeaveFn = () => {\n for (const unsubscribe of unsubscribeCallbacks) {\n unsubscribe();\n }\n unsubscribeCallbacks = [];\n\n storageRoot = null;\n maybeRoom = null;\n isPatching = false;\n\n lastRoomId = null;\n lastLeaveFn = null;\n leave();\n\n updateLiveblocksContext(set, {\n others: [],\n status: \"initial\",\n isStorageLoading: false,\n room: null,\n });\n };\n }\n\n function leaveRoom() {\n lastLeaveFn?.();\n }\n\n const store = config(\n (...args) => {\n const { liveblocks: _, ...oldState } = get();\n // @ts-expect-error a bit too dynamic to type\n set(...args);\n const { liveblocks: __, ...newState } = get();\n\n if (maybeRoom) {\n const room = maybeRoom;\n isPatching = true;\n try {\n room.batch(() => {\n updatePresence(\n room,\n oldState as JsonObject,\n newState as JsonObject,\n presenceMapping\n );\n\n if (storageRoot) {\n const partialState = pick(newState, storageKeys) as JsonObject;\n if (process.env.NODE_ENV !== \"production\") {\n ensureNoFunctions(partialState);\n }\n storageRoot.reconcilePartially(partialState);\n }\n });\n } finally {\n isPatching = false;\n }\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\nfunction pick(\n source: Record<string, unknown>,\n keys: Iterable<string>\n): Record<string, unknown> {\n const result: Record<string, unknown> = {};\n for (const key of keys) {\n result[key] = source[key];\n }\n return result;\n}\n\nfunction updateLiveblocksContext<\n TState,\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n>(\n set: (\n callbackOrPartial: (\n current: WithLiveblocks<TState, P, S, U, E, TM, CM>\n ) => WithLiveblocks<TState, P, S, U, E> | Partial<any>\n ) => void,\n partial: Partial<LiveblocksContext<P, S, U, E, TM, CM>>\n) {\n set((state) => ({ liveblocks: { ...state.liveblocks, ...partial } }));\n}\n\nfunction updatePresence<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n>(\n room: Room<P, S, U, E, TM, CM>,\n oldState: P,\n newState: P,\n presenceMapping: Mapping<P>\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<P>;\n patch[key] = val;\n room.updatePresence(patch);\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: OpaqueClient;\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"]}
package/dist/index.js CHANGED
@@ -1,15 +1,9 @@
1
1
  // src/index.ts
2
- import {
3
- detectDupes,
4
- errorIf,
5
- legacy_patchImmutableObject,
6
- legacy_patchLiveObjectKey,
7
- lsonToJson
8
- } from "@liveblocks/core";
2
+ import { detectDupes, errorIf, kInternal } from "@liveblocks/core";
9
3
 
10
4
  // src/version.ts
11
5
  var PKG_NAME = "@liveblocks/zustand";
12
- var PKG_VERSION = "3.17.0";
6
+ var PKG_VERSION = "3.18.0-rc1";
13
7
  var PKG_FORMAT = "esm";
14
8
 
15
9
  // src/index.ts
@@ -20,9 +14,17 @@ function mappingToFunctionIsNotAllowed(key) {
20
14
  `${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`
21
15
  );
22
16
  }
17
+ function ensureNoFunctions(state) {
18
+ for (const key in state) {
19
+ if (typeof state[key] === "function") {
20
+ throw mappingToFunctionIsNotAllowed(key);
21
+ }
22
+ }
23
+ }
23
24
  var middlewareImpl = (config, options) => {
24
25
  const { client, presenceMapping, storageMapping } = validateOptions(options);
25
26
  const presenceKeys = Object.keys(presenceMapping);
27
+ const storageKeys = Object.keys(storageMapping);
26
28
  return (set, get, api) => {
27
29
  let maybeRoom = null;
28
30
  let isPatching = false;
@@ -65,31 +67,24 @@ var middlewareImpl = (config, options) => {
65
67
  })
66
68
  );
67
69
  void room.getStorage().then(({ root }) => {
68
- const updates = {};
69
- room.batch(() => {
70
- for (const key in storageMapping) {
71
- const liveblocksStatePart = root.get(key);
72
- if (liveblocksStatePart === void 0) {
73
- updates[key] = get()[key];
74
- legacy_patchLiveObjectKey(
75
- root,
76
- key,
77
- void 0,
78
- get()[key]
79
- );
80
- } else {
81
- updates[key] = lsonToJson(
82
- liveblocksStatePart
83
- );
84
- }
70
+ const state = get();
71
+ const missing = {};
72
+ for (const key of storageKeys) {
73
+ if (root.get(key) === void 0) {
74
+ missing[key] = state[key];
85
75
  }
76
+ }
77
+ room.history[kInternal].withoutHistory(() => {
78
+ room.batch(() => {
79
+ root.reconcilePartially(missing);
80
+ });
86
81
  });
87
- set(updates);
82
+ set(pick(root.toJSON(), storageKeys));
88
83
  storageRoot = root;
89
84
  unsubscribeCallbacks.push(
90
- room.events.storageBatch.subscribe((updates2) => {
85
+ room.events.storageBatch.subscribe(() => {
91
86
  if (!isPatching) {
92
- set(patchState(get(), updates2, storageMapping));
87
+ set(pick(root.toJSON(), storageKeys));
93
88
  }
94
89
  })
95
90
  );
@@ -128,20 +123,19 @@ var middlewareImpl = (config, options) => {
128
123
  const room = maybeRoom;
129
124
  isPatching = true;
130
125
  try {
131
- updatePresence(
132
- room,
133
- oldState,
134
- newState,
135
- presenceMapping
136
- );
137
126
  room.batch(() => {
127
+ updatePresence(
128
+ room,
129
+ oldState,
130
+ newState,
131
+ presenceMapping
132
+ );
138
133
  if (storageRoot) {
139
- patchLiveblocksStorage(
140
- storageRoot,
141
- oldState,
142
- newState,
143
- storageMapping
144
- );
134
+ const partialState = pick(newState, storageKeys);
135
+ if (process.env.NODE_ENV !== "production") {
136
+ ensureNoFunctions(partialState);
137
+ }
138
+ storageRoot.reconcilePartially(partialState);
145
139
  }
146
140
  });
147
141
  } finally {
@@ -166,21 +160,6 @@ var middlewareImpl = (config, options) => {
166
160
  };
167
161
  };
168
162
  var liveblocks = middlewareImpl;
169
- function patchState(state, updates, mapping) {
170
- const partialState = {};
171
- for (const key in mapping) {
172
- partialState[key] = state[key];
173
- }
174
- const patched = legacy_patchImmutableObject(
175
- partialState,
176
- updates
177
- );
178
- const result = {};
179
- for (const key in mapping) {
180
- result[key] = patched[key];
181
- }
182
- return result;
183
- }
184
163
  function pick(source, keys) {
185
164
  const result = {};
186
165
  for (const key of keys) {
@@ -204,18 +183,6 @@ function updatePresence(room, oldState, newState, presenceMapping) {
204
183
  }
205
184
  }
206
185
  }
207
- function patchLiveblocksStorage(root, oldState, newState, mapping) {
208
- for (const key in mapping) {
209
- if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
210
- throw mappingToFunctionIsNotAllowed(key);
211
- }
212
- if (oldState[key] !== newState[key]) {
213
- const oldVal = oldState[key];
214
- const newVal = newState[key];
215
- legacy_patchLiveObjectKey(root, key, oldVal, newVal);
216
- }
217
- }
218
- }
219
186
  function isObject(value) {
220
187
  return Object.prototype.toString.call(value) === "[object Object]";
221
188
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/version.ts"],"sourcesContent":["import type {\n BaseUserMeta,\n Json,\n JsonObject,\n LiveObject,\n LsonObject,\n Room,\n Status,\n User,\n} from \"@liveblocks/client\";\nimport type {\n BaseMetadata,\n DCM,\n DE,\n DP,\n DS,\n DTM,\n DU,\n EnterOptions,\n OpaqueClient,\n OpaqueRoom,\n StorageUpdate,\n} from \"@liveblocks/core\";\nimport {\n detectDupes,\n errorIf,\n legacy_patchImmutableObject,\n legacy_patchLiveObjectKey,\n lsonToJson,\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 P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n> = {\n /**\n * Enters a room and starts sync it with zustand state\n * @param roomId The id of the room\n * @param options Optional. Options to pass to the underlying client.enterRoom call (e.g. `engine`).\n */\n readonly enterRoom: (\n roomId: string,\n options?: Pick<EnterOptions, \"engine\">\n ) => () => void;\n /**\n * Leaves the currently entered room and stops sync it with zustand state, if\n * any. If enterRoom was not called before, this is a no-op.\n */\n readonly leaveRoom: () => void;\n /**\n * The room currently synced to your zustand state.\n */\n readonly room: Room<P, S, U, E, TM, CM> | null;\n /**\n * Other users in the room. Empty no room is currently synced\n */\n readonly others: readonly User<P, U>[];\n /**\n * Whether or not the room storage is currently loading\n */\n readonly isStorageLoading: boolean;\n /**\n * Connection status of the room.\n */\n readonly status: Status;\n};\n\n/**\n * Adds the `liveblocks` property to your custom Zustand state.\n */\nexport type WithLiveblocks<\n TState,\n P extends JsonObject = DP,\n S extends LsonObject = DS,\n U extends BaseUserMeta = DU,\n E extends Json = DE,\n TM extends BaseMetadata = DTM,\n CM extends BaseMetadata = DCM,\n> = TState & {\n readonly liveblocks: LiveblocksContext<P, S, U, E, TM, CM>;\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: OpaqueClient;\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 BaseMetadata,\n BaseMetadata\n >;\n },\n>(\n config: StateCreator<TState, [], []>,\n options: Options<TState>\n) => StateCreator<TState, [], []>;\n\ntype ExtractPresence<TRoom extends OpaqueRoom> =\n TRoom extends Room<infer P, any, any, any, any> ? P : never;\n\ntype ExtractStorage<TRoom extends OpaqueRoom> =\n TRoom extends Room<any, infer S, any, 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 P = ExtractPresence<TRoom>;\n type S = ExtractStorage<TRoom>;\n\n const { client, presenceMapping, storageMapping } = validateOptions(options);\n const presenceKeys = Object.keys(presenceMapping);\n return (set, get, api) => {\n let maybeRoom: TRoom | null = null;\n let isPatching = false;\n let storageRoot: LiveObject<S> | null = null;\n let unsubscribeCallbacks: Array<() => void> = [];\n let lastRoomId: string | null = null;\n let lastLeaveFn: (() => void) | null = null;\n\n function enterRoom(\n newRoomId: string,\n options?: Pick<EnterOptions, \"engine\">\n ): void {\n if (lastRoomId === newRoomId) {\n return;\n }\n\n lastRoomId = newRoomId;\n if (lastLeaveFn !== null) {\n // First leave the old room before entering a potential new one\n lastLeaveFn();\n }\n\n const initialPresence = pick(get(), presenceKeys) as unknown as P;\n\n const { room, leave } = client.enterRoom(newRoomId, {\n engine: options?.engine,\n initialPresence,\n }) as unknown as { room: TRoom; leave: () => void };\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.status.subscribe((status) => {\n updateLiveblocksContext(set, {\n status,\n });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.myPresence.subscribe(() => {\n if (!isPatching) {\n set(pick(room.getPresence(), presenceKeys) as Partial<TState>);\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 legacy_patchLiveObjectKey(\n root,\n key,\n undefined,\n get()[key] as Json | undefined\n );\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<S>;\n unsubscribeCallbacks.push(\n room.events.storageBatch.subscribe((updates) => {\n if (!isPatching) {\n set(patchState(get(), updates, storageMapping));\n }\n })\n );\n\n // set isLoading storage to false once storage is loaded\n updateLiveblocksContext(set, {\n isStorageLoading: false,\n });\n });\n\n lastLeaveFn = () => {\n for (const unsubscribe of unsubscribeCallbacks) {\n unsubscribe();\n }\n unsubscribeCallbacks = [];\n\n storageRoot = null;\n maybeRoom = null;\n isPatching = false;\n\n lastRoomId = null;\n lastLeaveFn = null;\n leave();\n\n updateLiveblocksContext(set, {\n others: [],\n status: \"initial\",\n isStorageLoading: false,\n room: null,\n });\n };\n }\n\n function leaveRoom() {\n lastLeaveFn?.();\n }\n\n const store = config(\n (...args) => {\n const { liveblocks: _, ...oldState } = get();\n // @ts-expect-error a bit too dynamic to type\n set(...args);\n const { liveblocks: __, ...newState } = get();\n\n if (maybeRoom) {\n const room = maybeRoom;\n isPatching = true;\n try {\n updatePresence(\n room,\n oldState as JsonObject,\n newState as JsonObject,\n presenceMapping\n );\n\n room.batch(() => {\n if (storageRoot) {\n patchLiveblocksStorage(\n storageRoot,\n oldState,\n newState,\n storageMapping\n );\n }\n });\n } finally {\n isPatching = false;\n }\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\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(\n partialState as JsonObject,\n updates\n );\n\n const result: Partial<T> = {};\n\n for (const key in mapping) {\n // @ts-expect-error key is a key of T\n result[key] = patched[key];\n }\n\n return result;\n}\n\nfunction pick(\n source: Record<string, unknown>,\n keys: Iterable<string>\n): Record<string, unknown> {\n const result: Record<string, unknown> = {};\n for (const key of keys) {\n result[key] = source[key];\n }\n return result;\n}\n\nfunction updateLiveblocksContext<\n TState,\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n>(\n set: (\n callbackOrPartial: (\n current: WithLiveblocks<TState, P, S, U, E, TM, CM>\n ) => WithLiveblocks<TState, P, S, U, E> | Partial<any>\n ) => void,\n partial: Partial<LiveblocksContext<P, S, U, E, TM, CM>>\n) {\n set((state) => ({ liveblocks: { ...state.liveblocks, ...partial } }));\n}\n\nfunction updatePresence<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n>(\n room: Room<P, S, U, E, TM, CM>,\n oldState: P,\n newState: P,\n presenceMapping: Mapping<P>\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<P>;\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] as Json | undefined;\n const newVal = newState[key] as Json | undefined;\n legacy_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: OpaqueClient;\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":";AAuBA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;AC1BA,IAAM,WAAW;AACjB,IAAM,cAAiD;AACvD,IAAM,aAAgD;;;AD6B7D,YAAY,UAAU,aAAa,UAAU;AAE7C,IAAM,eAAe;AAErB,SAAS,8BAA8B,KAAoB;AACzD,SAAO,IAAI;AAAA,IACT,GAAG,YAAY,YAAY,GAAG;AAAA,EAChC;AACF;AA2GA,IAAM,iBAA4C,CAAC,QAAQ,YAAY;AAOrE,QAAM,EAAE,QAAQ,iBAAiB,eAAe,IAAI,gBAAgB,OAAO;AAC3E,QAAM,eAAe,OAAO,KAAK,eAAe;AAChD,SAAO,CAAC,KAAK,KAAK,QAAQ;AACxB,QAAI,YAA0B;AAC9B,QAAI,aAAa;AACjB,QAAI,cAAoC;AACxC,QAAI,uBAA0C,CAAC;AAC/C,QAAI,aAA4B;AAChC,QAAI,cAAmC;AAEvC,aAAS,UACP,WACAA,UACM;AACN,UAAI,eAAe,WAAW;AAC5B;AAAA,MACF;AAEA,mBAAa;AACb,UAAI,gBAAgB,MAAM;AAExB,oBAAY;AAAA,MACd;AAEA,YAAM,kBAAkB,KAAK,IAAI,GAAG,YAAY;AAEhD,YAAM,EAAE,MAAM,MAAM,IAAI,OAAO,UAAU,WAAW;AAAA,QAClD,QAAQA,UAAS;AAAA,QACjB;AAAA,MACF,CAAC;AACD,kBAAY;AAEZ,8BAAwB,KAAK,EAAE,kBAAkB,MAAM,KAAK,CAAC;AAE7D,2BAAqB;AAAA,QACnB,KAAK,OAAO,OAAO,UAAU,CAAC,EAAE,OAAO,MA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ACZ;AAAA,IACE,CAAC,SAAS,OAAO;AAAA,IACjB,GAAG,YAAY,IAAI,WAAW;AAAA,EAChC;AAEA,QAAM,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":["options","updates"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/version.ts"],"sourcesContent":["import type {\n BaseUserMeta,\n Json,\n JsonObject,\n LiveObject,\n LsonObject,\n Room,\n Status,\n User,\n} from \"@liveblocks/client\";\nimport type {\n BaseMetadata,\n DCM,\n DE,\n DP,\n DS,\n DTM,\n DU,\n EnterOptions,\n OpaqueClient,\n OpaqueRoom,\n} from \"@liveblocks/core\";\nimport { detectDupes, errorIf, kInternal } 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 P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n> = {\n /**\n * Enters a room and starts sync it with zustand state\n * @param roomId The id of the room\n * @param options Optional. Options to pass to the underlying client.enterRoom call (e.g. `engine`).\n */\n readonly enterRoom: (\n roomId: string,\n options?: Pick<EnterOptions, \"engine\">\n ) => () => void;\n /**\n * Leaves the currently entered room and stops sync it with zustand state, if\n * any. If enterRoom was not called before, this is a no-op.\n */\n readonly leaveRoom: () => void;\n /**\n * The room currently synced to your zustand state.\n */\n readonly room: Room<P, S, U, E, TM, CM> | null;\n /**\n * Other users in the room. Empty no room is currently synced\n */\n readonly others: readonly User<P, U>[];\n /**\n * Whether or not the room storage is currently loading\n */\n readonly isStorageLoading: boolean;\n /**\n * Connection status of the room.\n */\n readonly status: Status;\n};\n\n/**\n * Adds the `liveblocks` property to your custom Zustand state.\n */\nexport type WithLiveblocks<\n TState,\n P extends JsonObject = DP,\n S extends LsonObject = DS,\n U extends BaseUserMeta = DU,\n E extends Json = DE,\n TM extends BaseMetadata = DTM,\n CM extends BaseMetadata = DCM,\n> = TState & {\n readonly liveblocks: LiveblocksContext<P, S, U, E, TM, CM>;\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: OpaqueClient;\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 BaseMetadata,\n BaseMetadata\n >;\n },\n>(\n config: StateCreator<TState, [], []>,\n options: Options<TState>\n) => StateCreator<TState, [], []>;\n\ntype ExtractPresence<TRoom extends OpaqueRoom> =\n TRoom extends Room<infer P, any, any, any, any> ? P : never;\n\ntype ExtractStorage<TRoom extends OpaqueRoom> =\n TRoom extends Room<any, infer S, any, any, any> ? S : never;\n\n/** Ensures values of the provided object are not functions */\nfunction ensureNoFunctions(state: Record<string, unknown>): void {\n for (const key in state) {\n if (typeof state[key] === \"function\") {\n throw mappingToFunctionIsNotAllowed(key);\n }\n }\n}\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 P = ExtractPresence<TRoom>;\n type S = ExtractStorage<TRoom>;\n\n const { client, presenceMapping, storageMapping } = validateOptions(options);\n const presenceKeys = Object.keys(presenceMapping);\n const storageKeys = Object.keys(storageMapping);\n return (set, get, api) => {\n let maybeRoom: TRoom | null = null;\n let isPatching = false;\n let storageRoot: LiveObject<S> | null = null;\n let unsubscribeCallbacks: Array<() => void> = [];\n let lastRoomId: string | null = null;\n let lastLeaveFn: (() => void) | null = null;\n\n function enterRoom(\n newRoomId: string,\n options?: Pick<EnterOptions, \"engine\">\n ): void {\n if (lastRoomId === newRoomId) {\n return;\n }\n\n lastRoomId = newRoomId;\n if (lastLeaveFn !== null) {\n // First leave the old room before entering a potential new one\n lastLeaveFn();\n }\n\n const initialPresence = pick(get(), presenceKeys) as unknown as P;\n\n const { room, leave } = client.enterRoom(newRoomId, {\n engine: options?.engine,\n initialPresence,\n }) as unknown as { room: TRoom; leave: () => void };\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.status.subscribe((status) => {\n updateLiveblocksContext(set, {\n status,\n });\n })\n );\n\n unsubscribeCallbacks.push(\n room.events.myPresence.subscribe(() => {\n if (!isPatching) {\n set(pick(room.getPresence(), presenceKeys) as Partial<TState>);\n }\n })\n );\n\n void room.getStorage().then(({ root }) => {\n // Seed any missing storage keys from the current Zustand state.\n // Only writes keys that don't exist yet in storage — existing\n // storage values are left untouched and will be read back below.\n const state = get() as Record<string, unknown>;\n const missing: JsonObject = {};\n for (const key of storageKeys) {\n if (root.get(key) === undefined) {\n missing[key] = state[key] as Json;\n }\n }\n\n room.history[kInternal].withoutHistory(() => {\n room.batch(() => {\n root.reconcilePartially(missing);\n });\n });\n\n set(pick(root.toJSON(), storageKeys) as Partial<TState>);\n\n storageRoot = root as LiveObject<S>;\n unsubscribeCallbacks.push(\n room.events.storageBatch.subscribe(() => {\n if (!isPatching) {\n set(pick(root.toJSON(), storageKeys) as Partial<TState>);\n }\n })\n );\n\n // set isLoading storage to false once storage is loaded\n updateLiveblocksContext(set, {\n isStorageLoading: false,\n });\n });\n\n lastLeaveFn = () => {\n for (const unsubscribe of unsubscribeCallbacks) {\n unsubscribe();\n }\n unsubscribeCallbacks = [];\n\n storageRoot = null;\n maybeRoom = null;\n isPatching = false;\n\n lastRoomId = null;\n lastLeaveFn = null;\n leave();\n\n updateLiveblocksContext(set, {\n others: [],\n status: \"initial\",\n isStorageLoading: false,\n room: null,\n });\n };\n }\n\n function leaveRoom() {\n lastLeaveFn?.();\n }\n\n const store = config(\n (...args) => {\n const { liveblocks: _, ...oldState } = get();\n // @ts-expect-error a bit too dynamic to type\n set(...args);\n const { liveblocks: __, ...newState } = get();\n\n if (maybeRoom) {\n const room = maybeRoom;\n isPatching = true;\n try {\n room.batch(() => {\n updatePresence(\n room,\n oldState as JsonObject,\n newState as JsonObject,\n presenceMapping\n );\n\n if (storageRoot) {\n const partialState = pick(newState, storageKeys) as JsonObject;\n if (process.env.NODE_ENV !== \"production\") {\n ensureNoFunctions(partialState);\n }\n storageRoot.reconcilePartially(partialState);\n }\n });\n } finally {\n isPatching = false;\n }\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\nfunction pick(\n source: Record<string, unknown>,\n keys: Iterable<string>\n): Record<string, unknown> {\n const result: Record<string, unknown> = {};\n for (const key of keys) {\n result[key] = source[key];\n }\n return result;\n}\n\nfunction updateLiveblocksContext<\n TState,\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n>(\n set: (\n callbackOrPartial: (\n current: WithLiveblocks<TState, P, S, U, E, TM, CM>\n ) => WithLiveblocks<TState, P, S, U, E> | Partial<any>\n ) => void,\n partial: Partial<LiveblocksContext<P, S, U, E, TM, CM>>\n) {\n set((state) => ({ liveblocks: { ...state.liveblocks, ...partial } }));\n}\n\nfunction updatePresence<\n P extends JsonObject,\n S extends LsonObject,\n U extends BaseUserMeta,\n E extends Json,\n TM extends BaseMetadata,\n CM extends BaseMetadata,\n>(\n room: Room<P, S, U, E, TM, CM>,\n oldState: P,\n newState: P,\n presenceMapping: Mapping<P>\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<P>;\n patch[key] = val;\n room.updatePresence(patch);\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: OpaqueClient;\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":";AAsBA,SAAS,aAAa,SAAS,iBAAiB;;;ACnBzC,IAAM,WAAW;AACjB,IAAM,cAAiD;AACvD,IAAM,aAAgD;;;ADsB7D,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": "@liveblocks/zustand",
3
- "version": "3.17.0",
3
+ "version": "3.18.0-rc1",
4
4
  "description": "A middleware for Zustand to automatically synchronize your stores with Liveblocks. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Liveblocks Inc.",
@@ -36,8 +36,8 @@
36
36
  "test:watch": "vitest"
37
37
  },
38
38
  "dependencies": {
39
- "@liveblocks/client": "3.17.0",
40
- "@liveblocks/core": "3.17.0"
39
+ "@liveblocks/client": "3.18.0-rc1",
40
+ "@liveblocks/core": "3.18.0-rc1"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "zustand": "^5.0.1"