@monque/core 1.5.1 → 1.5.2

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.mjs CHANGED
@@ -754,6 +754,7 @@ var ChangeStreamHandler = class {
754
754
  */
755
755
  setup() {
756
756
  if (!this.ctx.isRunning()) return;
757
+ this.clearReconnectTimer();
757
758
  try {
758
759
  this.changeStream = this.ctx.collection.watch([{ $match: { $or: [{ operationType: "insert" }, {
759
760
  operationType: "update",
@@ -813,30 +814,35 @@ var ChangeStreamHandler = class {
813
814
  this.reconnectAttempts++;
814
815
  if (this.reconnectAttempts > this.maxReconnectAttempts) {
815
816
  this.usingChangeStreams = false;
816
- if (this.reconnectTimer) {
817
- clearTimeout(this.reconnectTimer);
818
- this.reconnectTimer = null;
819
- }
820
- if (this.changeStream) {
821
- this.changeStream.close().catch(() => {});
822
- this.changeStream = null;
823
- }
817
+ this.clearReconnectTimer();
818
+ this.closeChangeStream();
824
819
  this.ctx.emit("changestream:fallback", { reason: `Exhausted ${this.maxReconnectAttempts} reconnection attempts: ${error.message}` });
825
820
  return;
826
821
  }
827
822
  const delay = 2 ** (this.reconnectAttempts - 1) * 1e3;
828
- if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
823
+ this.clearReconnectTimer();
824
+ if (!this.ctx.isRunning()) return;
829
825
  this.reconnectTimer = setTimeout(() => {
830
- this.reconnectTimer = null;
831
- if (this.ctx.isRunning()) {
832
- if (this.changeStream) {
833
- this.changeStream.close().catch(() => {});
834
- this.changeStream = null;
835
- }
836
- this.setup();
837
- }
826
+ this.clearReconnectTimer();
827
+ this.reconnect();
838
828
  }, delay);
839
829
  }
830
+ reconnect() {
831
+ if (!this.ctx.isRunning()) return;
832
+ this.closeChangeStream();
833
+ if (!this.ctx.isRunning()) return;
834
+ this.setup();
835
+ }
836
+ clearReconnectTimer() {
837
+ if (!this.reconnectTimer) return;
838
+ clearTimeout(this.reconnectTimer);
839
+ this.reconnectTimer = null;
840
+ }
841
+ closeChangeStream() {
842
+ if (!this.changeStream) return;
843
+ this.changeStream.close().catch(() => {});
844
+ this.changeStream = null;
845
+ }
840
846
  /**
841
847
  * Close the change stream cursor and emit closed event.
842
848
  */
@@ -845,10 +851,7 @@ var ChangeStreamHandler = class {
845
851
  clearTimeout(this.debounceTimer);
846
852
  this.debounceTimer = null;
847
853
  }
848
- if (this.reconnectTimer) {
849
- clearTimeout(this.reconnectTimer);
850
- this.reconnectTimer = null;
851
- }
854
+ this.clearReconnectTimer();
852
855
  if (this.changeStream) {
853
856
  try {
854
857
  await this.changeStream.close();