@dudousxd/nestjs-durable-core 0.85.11 → 0.85.12

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
@@ -5031,6 +5031,8 @@ var ANNOUNCED_ROUTES_TTL_MS = 5e3;
5031
5031
  var VERSION_UNDECLARED_TAG = "version:undeclared";
5032
5032
  var versionKey = /* @__PURE__ */ __name((name, version) => `${name}@${version}`, "versionKey");
5033
5033
  var STEP_FAN_CHUNK = 200;
5034
+ var STEP_EVENTS_PER_BROADCAST = 500;
5035
+ var isStepEvent = /* @__PURE__ */ __name((event) => event.type.startsWith("step."), "isStepEvent");
5034
5036
  var FAN_REREAD_CHUNK = 1e3;
5035
5037
  var STEP_FAN_SEND_ROUND = 2e3;
5036
5038
  var PARENT_NOTIFY_ATTEMPTS = 4;
@@ -5501,6 +5503,13 @@ var WorkflowEngine = class {
5501
5503
  ...msg.event,
5502
5504
  at: new Date(msg.event.at)
5503
5505
  }));
5506
+ } else if (msg.kind === "events") {
5507
+ for (const event of msg.events) {
5508
+ this.deliver(markRemoteEvent({
5509
+ ...event,
5510
+ at: new Date(event.at)
5511
+ }));
5512
+ }
5504
5513
  } else if (msg.kind === "cancel") {
5505
5514
  this.notifyCancelled(msg.runId);
5506
5515
  } else if (msg.kind === "enqueued") {
@@ -6082,13 +6091,53 @@ var WorkflowEngine = class {
6082
6091
  at: /* @__PURE__ */ new Date()
6083
6092
  };
6084
6093
  this.deliver(full);
6085
- if (this.controlPlane) {
6086
- void this.controlPlane.publishControl({
6087
- kind: "event",
6088
- event: full,
6089
- from: this.instanceId
6090
- }).catch(() => {
6091
- });
6094
+ if (!this.controlPlane) return;
6095
+ this.eventsToBroadcast.push(full);
6096
+ if (!this.broadcastingEvents) void this.broadcastEvents();
6097
+ }
6098
+ /** Events waiting for the control-plane publish in flight, in emit order (see {@link broadcastEvents}). */
6099
+ eventsToBroadcast = [];
6100
+ broadcastingEvents = false;
6101
+ /**
6102
+ * Publish the queued events in the order they were emitted. A run's own events go out one per
6103
+ * message, as every instance expects them. Step events (a wide fan emits two per step) that queued
6104
+ * up while a publish was in flight go out together in one `events` message, so a fan of 40,000
6105
+ * steps costs the control plane a few hundred messages to every instance instead of 80,000.
6106
+ */
6107
+ async broadcastEvents() {
6108
+ this.broadcastingEvents = true;
6109
+ try {
6110
+ while (this.eventsToBroadcast.length > 0 && this.controlPlane) {
6111
+ const first = this.eventsToBroadcast[0];
6112
+ let msg;
6113
+ if (isStepEvent(first)) {
6114
+ let n = 1;
6115
+ while (n < STEP_EVENTS_PER_BROADCAST && n < this.eventsToBroadcast.length && isStepEvent(this.eventsToBroadcast[n])) {
6116
+ n += 1;
6117
+ }
6118
+ const events = this.eventsToBroadcast.splice(0, n);
6119
+ msg = events.length === 1 ? {
6120
+ kind: "event",
6121
+ event: first,
6122
+ from: this.instanceId
6123
+ } : {
6124
+ kind: "events",
6125
+ events,
6126
+ from: this.instanceId
6127
+ };
6128
+ } else {
6129
+ this.eventsToBroadcast.shift();
6130
+ msg = {
6131
+ kind: "event",
6132
+ event: first,
6133
+ from: this.instanceId
6134
+ };
6135
+ }
6136
+ await this.controlPlane.publishControl(msg).catch(() => {
6137
+ });
6138
+ }
6139
+ } finally {
6140
+ this.broadcastingEvents = false;
6092
6141
  }
6093
6142
  }
6094
6143
  /** Deliver an event to local subscribers only (no re-broadcast) — used for both locally-produced