stream-chat 9.3.0 → 9.4.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.
@@ -192,6 +192,7 @@ __export(index_exports, {
192
192
  MODERATION_ENTITY_TYPES: () => MODERATION_ENTITY_TYPES,
193
193
  MaxPriority: () => MaxPriority,
194
194
  MentionsSearchSource: () => MentionsSearchSource,
195
+ MergedStateStore: () => MergedStateStore,
195
196
  MessageComposer: () => MessageComposer,
196
197
  MessageComposerMiddlewareExecutor: () => MessageComposerMiddlewareExecutor,
197
198
  MessageDraftComposerMiddlewareExecutor: () => MessageDraftComposerMiddlewareExecutor,
@@ -4048,26 +4049,14 @@ var ensureIsLocalAttachment = (attachment) => {
4048
4049
 
4049
4050
  // src/store.ts
4050
4051
  var isPatch = (value) => typeof value === "function";
4052
+ var noop2 = () => {
4053
+ };
4051
4054
  var StateStore = class {
4052
4055
  constructor(value) {
4053
4056
  this.value = value;
4054
- this.handlerSet = /* @__PURE__ */ new Set();
4055
- this.next = (newValueOrPatch) => {
4056
- const newValue = isPatch(newValueOrPatch) ? newValueOrPatch(this.value) : newValueOrPatch;
4057
- if (newValue === this.value) return;
4058
- const oldValue = this.value;
4059
- this.value = newValue;
4060
- this.handlerSet.forEach((handler) => handler(this.value, oldValue));
4061
- };
4057
+ this.handlers = /* @__PURE__ */ new Set();
4058
+ this.preprocessors = /* @__PURE__ */ new Set();
4062
4059
  this.partialNext = (partial) => this.next((current) => ({ ...current, ...partial }));
4063
- this.getLatestValue = () => this.value;
4064
- this.subscribe = (handler) => {
4065
- handler(this.value, void 0);
4066
- this.handlerSet.add(handler);
4067
- return () => {
4068
- this.handlerSet.delete(handler);
4069
- };
4070
- };
4071
4060
  this.subscribeWithSelector = (selector, handler) => {
4072
4061
  let previouslySelectedValues;
4073
4062
  const wrappedHandler = (nextValue) => {
@@ -4086,6 +4075,183 @@ var StateStore = class {
4086
4075
  return this.subscribe(wrappedHandler);
4087
4076
  };
4088
4077
  }
4078
+ /**
4079
+ * Allows merging two stores only if their keys differ otherwise there's no way to ensure the data type stability.
4080
+ * @experimental
4081
+ * This method is experimental and may change in future versions.
4082
+ */
4083
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4084
+ merge(stateStore) {
4085
+ return new MergedStateStore({
4086
+ original: this,
4087
+ merged: stateStore
4088
+ });
4089
+ }
4090
+ next(newValueOrPatch) {
4091
+ const newValue = isPatch(newValueOrPatch) ? newValueOrPatch(this.value) : newValueOrPatch;
4092
+ if (newValue === this.value) return;
4093
+ this.preprocessors.forEach((preprocessor) => preprocessor(newValue, this.value));
4094
+ const oldValue = this.value;
4095
+ this.value = newValue;
4096
+ this.handlers.forEach((handler) => handler(this.value, oldValue));
4097
+ }
4098
+ getLatestValue() {
4099
+ return this.value;
4100
+ }
4101
+ subscribe(handler) {
4102
+ handler(this.value, void 0);
4103
+ this.handlers.add(handler);
4104
+ return () => {
4105
+ this.handlers.delete(handler);
4106
+ };
4107
+ }
4108
+ /**
4109
+ * Registers a preprocessor function that will be called before the state is updated.
4110
+ *
4111
+ * Preprocessors are invoked with the new and previous values whenever `next` or `partialNext` methods
4112
+ * are called, allowing you to mutate or react to the new value before it is set. Preprocessors run in the
4113
+ * order they were registered.
4114
+ *
4115
+ * @example
4116
+ * ```ts
4117
+ * const store = new StateStore<{ count: number; isMaxValue: bool; }>({ count: 0, isMaxValue: false });
4118
+ *
4119
+ * store.addPreprocessor((nextValue, prevValue) => {
4120
+ * if (nextValue.count > 10) {
4121
+ * nextValue.count = 10; // Clamp the value to a maximum of 10
4122
+ * }
4123
+ *
4124
+ * if (nextValue.count === 10) {
4125
+ * nextValue.isMaxValue = true; // Set isMaxValue to true if count is 10
4126
+ * } else {
4127
+ * nextValue.isMaxValue = false; // Reset isMaxValue otherwise
4128
+ * }
4129
+ * });
4130
+ *
4131
+ * store.partialNext({ count: 15 });
4132
+ *
4133
+ * store.getLatestValue(); // { count: 10, isMaxValue: true }
4134
+ *
4135
+ * store.partialNext({ count: 5 });
4136
+ *
4137
+ * store.getLatestValue(); // { count: 5, isMaxValue: false }
4138
+ * ```
4139
+ *
4140
+ * @param preprocessor - The function to be called with the next and previous values before the state is updated.
4141
+ * @returns A `RemovePreprocessor` function that removes the preprocessor when called.
4142
+ */
4143
+ addPreprocessor(preprocessor) {
4144
+ this.preprocessors.add(preprocessor);
4145
+ return () => {
4146
+ this.preprocessors.delete(preprocessor);
4147
+ };
4148
+ }
4149
+ };
4150
+ var MergedStateStore = class _MergedStateStore extends StateStore {
4151
+ constructor({ original, merged }) {
4152
+ const originalValue = original.getLatestValue();
4153
+ const mergedValue = merged.getLatestValue();
4154
+ super({
4155
+ ...originalValue,
4156
+ ...mergedValue
4157
+ });
4158
+ // override original methods and "disable" them
4159
+ this.next = () => {
4160
+ console.warn(
4161
+ `${_MergedStateStore.name}.next is disabled, call original.next or merged.next instead`
4162
+ );
4163
+ };
4164
+ this.partialNext = () => {
4165
+ console.warn(
4166
+ `${_MergedStateStore.name}.partialNext is disabled, call original.partialNext or merged.partialNext instead`
4167
+ );
4168
+ };
4169
+ this.cachedOriginalValue = originalValue;
4170
+ this.cachedMergedValue = mergedValue;
4171
+ this.original = original;
4172
+ this.merged = merged;
4173
+ }
4174
+ /**
4175
+ * Subscribes to changes in the merged state store.
4176
+ *
4177
+ * This method extends the base subscribe functionality to handle the merged nature of this store:
4178
+ * 1. The first subscriber triggers registration of helper subscribers that listen to both source stores
4179
+ * 2. Changes from either source store are propagated to this merged store
4180
+ * 3. Source store values are cached to prevent unnecessary updates
4181
+ *
4182
+ * When the first subscriber is added, the method sets up listeners on both original and merged stores.
4183
+ * These listeners update the combined store value whenever either source store changes.
4184
+ * All subscriptions (helpers and the actual handler) are tracked so they can be properly cleaned up.
4185
+ *
4186
+ * @param handler - The callback function that will be executed when the state changes
4187
+ * @returns An unsubscribe function that, when called, removes the subscription and any helper subscriptions
4188
+ */
4189
+ subscribe(handler) {
4190
+ const unsubscribeFunctions = [];
4191
+ if (!this.handlers.size) {
4192
+ const base = (nextValue) => {
4193
+ super.next((currentValue) => ({
4194
+ ...currentValue,
4195
+ ...nextValue
4196
+ }));
4197
+ };
4198
+ unsubscribeFunctions.push(
4199
+ this.original.subscribe((nextValue) => {
4200
+ if (nextValue === this.cachedOriginalValue) return;
4201
+ this.cachedOriginalValue = nextValue;
4202
+ base(nextValue);
4203
+ }),
4204
+ this.merged.subscribe((nextValue) => {
4205
+ if (nextValue === this.cachedMergedValue) return;
4206
+ this.cachedMergedValue = nextValue;
4207
+ base(nextValue);
4208
+ })
4209
+ );
4210
+ }
4211
+ unsubscribeFunctions.push(super.subscribe(handler));
4212
+ return () => {
4213
+ unsubscribeFunctions.forEach((unsubscribe) => unsubscribe());
4214
+ };
4215
+ }
4216
+ /**
4217
+ * Retrieves the latest combined state from both original and merged stores.
4218
+ *
4219
+ * This method extends the base getLatestValue functionality to ensure the merged store
4220
+ * remains in sync with its source stores even when there are no active subscribers.
4221
+ *
4222
+ * When there are no handlers registered, the method:
4223
+ * 1. Fetches the latest values from both source stores
4224
+ * 2. Compares them with the cached values to detect changes
4225
+ * 3. If changes are detected, updates the internal value and caches
4226
+ * the new source values to maintain consistency
4227
+ *
4228
+ * This approach ensures that calling getLatestValue() always returns the most
4229
+ * up-to-date combined state, even if the merged store hasn't been actively
4230
+ * receiving updates through subscriptions.
4231
+ *
4232
+ * @returns The latest combined state from both original and merged stores
4233
+ */
4234
+ getLatestValue() {
4235
+ if (!this.handlers.size) {
4236
+ const originalValue = this.original.getLatestValue();
4237
+ const mergedValue = this.merged.getLatestValue();
4238
+ if (originalValue !== this.cachedOriginalValue || mergedValue !== this.cachedMergedValue) {
4239
+ this.value = {
4240
+ ...originalValue,
4241
+ ...mergedValue
4242
+ };
4243
+ this.cachedMergedValue = mergedValue;
4244
+ this.cachedOriginalValue = originalValue;
4245
+ }
4246
+ }
4247
+ return super.getLatestValue();
4248
+ }
4249
+ addPreprocessor() {
4250
+ console.warn(
4251
+ `${_MergedStateStore.name}.addPreprocessor is disabled, call original.addPreprocessor or merged.addPreprocessor instead`
4252
+ );
4253
+ return noop2;
4254
+ }
4089
4255
  };
4090
4256
 
4091
4257
  // src/utils/mergeWith/mergeWithCore.ts
@@ -5505,7 +5671,9 @@ var ErrorFromResponse = class extends Error {
5505
5671
  return {
5506
5672
  message: `(${joinable.join(", ")}) - ${this.message}`,
5507
5673
  stack: this.stack,
5508
- name: this.name
5674
+ name: this.name,
5675
+ code: this.code,
5676
+ status: this.status
5509
5677
  };
5510
5678
  }
5511
5679
  };
@@ -7638,7 +7806,7 @@ var initState5 = (composition) => {
7638
7806
  pollId: message.poll_id ?? null
7639
7807
  };
7640
7808
  };
7641
- var noop2 = () => void 0;
7809
+ var noop3 = () => void 0;
7642
7810
  var _MessageComposer = class _MessageComposer extends WithSubscriptions {
7643
7811
  // todo: mediaRecorder: MediaRecorderController;
7644
7812
  constructor({
@@ -7666,7 +7834,7 @@ var _MessageComposer = class _MessageComposer extends WithSubscriptions {
7666
7834
  this.initEditingAuditState = (composition) => initEditingAuditState(composition);
7667
7835
  this.registerSubscriptions = () => {
7668
7836
  if (this.hasSubscriptions) {
7669
- return noop2;
7837
+ return noop3;
7670
7838
  }
7671
7839
  this.addUnsubscribeFunction(this.subscribeMessageComposerSetupStateChange());
7672
7840
  this.addUnsubscribeFunction(this.subscribeMessageUpdated());
@@ -14442,7 +14610,7 @@ var StreamChat = class _StreamChat {
14442
14610
  if (this.userAgent) {
14443
14611
  return this.userAgent;
14444
14612
  }
14445
- const version = "9.3.0";
14613
+ const version = "9.4.0";
14446
14614
  const clientBundle = "browser-cjs";
14447
14615
  let userAgentString = "";
14448
14616
  if (this.sdkIdentifier) {