cross-tab-worker-databus 0.20.88 → 0.20.89

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.
@@ -5085,6 +5085,10 @@ var CentrifugeSession = class {
5085
5085
  transferable = false;
5086
5086
  tokenBridge = false;
5087
5087
  nextRequestId = 1;
5088
+ // Bumped on every initialize/stop. Async client callbacks capture the value
5089
+ // from the connection that created them so a stopped client cannot emit into
5090
+ // a later session instance (including the main-thread local fallback).
5091
+ lifecycle = 0;
5088
5092
  pendingTokenRequests = /* @__PURE__ */ new Map();
5089
5093
  /** Dispatch an incoming Worker message to the matching operation.
5090
5094
  * Unknown message types are ignored rather than thrown, so a future protocol
@@ -5120,6 +5124,7 @@ var CentrifugeSession = class {
5120
5124
  /** Create the Centrifuge client, wire up lifecycle listeners, and connect. */
5121
5125
  initialize(url, config, transferable, tokenBridge) {
5122
5126
  if (this.client) return;
5127
+ const lifecycle = ++this.lifecycle;
5123
5128
  this.transferable = transferable;
5124
5129
  this.tokenBridge = tokenBridge;
5125
5130
  const clientOptions = tokenBridge ? {
@@ -5133,12 +5138,23 @@ var CentrifugeSession = class {
5133
5138
  const client = new Centrifuge(url, clientOptions);
5134
5139
  this.client = client;
5135
5140
  client.on("state", (context) => {
5141
+ if (this.lifecycle !== lifecycle) return;
5136
5142
  this.post({ type: CENTRIFUGE_OUTPUT_TYPE.STATUS, status: normalizeStatus(context.newState) });
5137
5143
  });
5138
- client.on("connected", () => this.post({ type: CENTRIFUGE_OUTPUT_TYPE.STATUS, status: WORKER_STATUS.CONNECTED }));
5139
- client.on("disconnected", () => this.post({ type: CENTRIFUGE_OUTPUT_TYPE.STATUS, status: WORKER_STATUS.DISCONNECTED }));
5140
- client.on("error", (context) => this.postError(context));
5144
+ client.on("connected", () => {
5145
+ if (this.lifecycle !== lifecycle) return;
5146
+ this.post({ type: CENTRIFUGE_OUTPUT_TYPE.STATUS, status: WORKER_STATUS.CONNECTED });
5147
+ });
5148
+ client.on("disconnected", () => {
5149
+ if (this.lifecycle !== lifecycle) return;
5150
+ this.post({ type: CENTRIFUGE_OUTPUT_TYPE.STATUS, status: WORKER_STATUS.DISCONNECTED });
5151
+ });
5152
+ client.on("error", (context) => {
5153
+ if (this.lifecycle !== lifecycle) return;
5154
+ this.postError(context);
5155
+ });
5141
5156
  client.on("publication", (context) => {
5157
+ if (this.lifecycle !== lifecycle) return;
5142
5158
  const topic = context.channel || getPayloadTopic(context.data);
5143
5159
  if (!topic || this.subscriptions.has(topic)) return;
5144
5160
  this.postPublication(topic, context.data);
@@ -5151,6 +5167,7 @@ var CentrifugeSession = class {
5151
5167
  * avoiding the removeAllListeners + re-on churn on every duplicate message. */
5152
5168
  subscribe(topic) {
5153
5169
  if (!this.client) return this.postError(new Error("Centrifuge client is not initialized."));
5170
+ const lifecycle = this.lifecycle;
5154
5171
  const existing = this.subscriptions.get(topic);
5155
5172
  if (existing) {
5156
5173
  existing.subscribe();
@@ -5163,10 +5180,16 @@ var CentrifugeSession = class {
5163
5180
  subscription.removeAllListeners("unsubscribed");
5164
5181
  this.subscriptions.set(topic, subscription);
5165
5182
  subscription.on("publication", (context) => {
5183
+ if (this.lifecycle !== lifecycle) return;
5166
5184
  this.postPublication(topic, context.data);
5167
5185
  });
5168
- subscription.on("error", (context) => this.postError(context));
5169
- subscription.on("unsubscribed", () => this.subscriptions.delete(topic));
5186
+ subscription.on("error", (context) => {
5187
+ if (this.lifecycle !== lifecycle) return;
5188
+ this.postError(context);
5189
+ });
5190
+ subscription.on("unsubscribed", () => {
5191
+ if (this.lifecycle === lifecycle) this.subscriptions.delete(topic);
5192
+ });
5170
5193
  subscription.subscribe();
5171
5194
  }
5172
5195
  /** Unsubscribe from a Centrifuge channel and clean up the local reference.
@@ -5184,13 +5207,16 @@ var CentrifugeSession = class {
5184
5207
  /** Publish a message to the Centrifuge channel. */
5185
5208
  publish(topic, data, messageId, timestamp) {
5186
5209
  if (!this.client) return this.postError(new Error("Centrifuge client is not initialized."));
5210
+ const lifecycle = this.lifecycle;
5187
5211
  const hasMetadata = messageId !== void 0 || timestamp !== void 0;
5188
5212
  const payload = hasMetadata ? {
5189
5213
  data,
5190
5214
  ...messageId === void 0 ? {} : { messageId },
5191
5215
  ...timestamp === void 0 ? {} : { timestamp }
5192
5216
  } : data;
5193
- void this.client.publish(topic, payload).catch((error) => this.postError(error));
5217
+ void this.client.publish(topic, payload).catch((error) => {
5218
+ if (this.lifecycle === lifecycle) this.postError(error);
5219
+ });
5194
5220
  }
5195
5221
  /** Forward a publication to the transport. Binary payloads take the
5196
5222
  * zero-copy `MESSAGE_BIN` path when `transferable` is enabled; everything
@@ -5213,6 +5239,7 @@ var CentrifugeSession = class {
5213
5239
  }
5214
5240
  /** Disconnect the client and clear all subscriptions. */
5215
5241
  stop() {
5242
+ this.lifecycle += 1;
5216
5243
  for (const [, pending] of this.pendingTokenRequests) {
5217
5244
  pending.reject(new Error("Centrifuge session stopped before the credential was resolved."));
5218
5245
  }
@@ -5226,9 +5253,14 @@ var CentrifugeSession = class {
5226
5253
  * Used as Centrifuge's `getToken` / `getChannelToken` when token bridging is
5227
5254
  * enabled; resolved or rejected by a matching TOKEN_RESPONSE / TOKEN_ERROR. */
5228
5255
  requestToken(kind, channel) {
5256
+ const lifecycle = this.lifecycle;
5229
5257
  const requestId = this.nextRequestId;
5230
5258
  this.nextRequestId += 1;
5231
5259
  return new Promise((resolve, reject) => {
5260
+ if (this.lifecycle !== lifecycle) {
5261
+ reject(new Error("Centrifuge session stopped before the credential was requested."));
5262
+ return;
5263
+ }
5232
5264
  this.pendingTokenRequests.set(requestId, { resolve, reject });
5233
5265
  this.post({
5234
5266
  type: CENTRIFUGE_OUTPUT_TYPE.TOKEN_REQUEST,