@event-driven-io/emmett-esdb 0.43.0-beta.11 → 0.43.0-beta.13

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.js CHANGED
@@ -719,7 +719,40 @@ var reactor = (options) => {
719
719
  canHandle,
720
720
  stopAfter
721
721
  } = options;
722
- const eachMessage = "eachMessage" in options && options.eachMessage ? options.eachMessage : () => Promise.resolve();
722
+ const isCustomBatch = "eachBatch" in options && !!options.eachBatch;
723
+ const eachBatch = isCustomBatch ? options.eachBatch : async (messages, context) => {
724
+ let result = void 0;
725
+ for (let i = 0; i < messages.length; i++) {
726
+ const message2 = messages[i];
727
+ const messageProcessingResult = await options.eachMessage(
728
+ message2,
729
+ context
730
+ );
731
+ if (messageProcessingResult && messageProcessingResult.type === "STOP") {
732
+ result = {
733
+ ...messageProcessingResult,
734
+ lastSuccessfulMessage: messageProcessingResult.error ? messages[i - 1] : message2
735
+ };
736
+ break;
737
+ }
738
+ if (stopAfter && stopAfter(message2)) {
739
+ result = {
740
+ type: "STOP",
741
+ reason: "Stop condition reached",
742
+ lastSuccessfulMessage: message2
743
+ };
744
+ break;
745
+ }
746
+ if (messageProcessingResult && messageProcessingResult.type === "SKIP") {
747
+ result = {
748
+ ...messageProcessingResult,
749
+ lastSuccessfulMessage: message2
750
+ };
751
+ continue;
752
+ }
753
+ }
754
+ return result;
755
+ };
723
756
  let isInitiated = false;
724
757
  let isActive = false;
725
758
  let lastCheckpoint = null;
@@ -757,10 +790,11 @@ var reactor = (options) => {
757
790
  await init(startOptions);
758
791
  isActive = true;
759
792
  closeSignal = onShutdown(() => close(startOptions));
760
- if (lastCheckpoint !== null)
793
+ if (lastCheckpoint !== null) {
761
794
  return {
762
795
  lastCheckpoint
763
796
  };
797
+ }
764
798
  return await processingScope(async (context) => {
765
799
  if (hooks.onStart) {
766
800
  await hooks.onStart(context);
@@ -789,46 +823,48 @@ var reactor = (options) => {
789
823
  handle: async (messages, partialContext) => {
790
824
  if (!isActive) return Promise.resolve();
791
825
  return await processingScope(async (context) => {
792
- let result = void 0;
793
- for (const message2 of messages) {
794
- if (wasMessageHandled(message2, lastCheckpoint)) continue;
795
- const upcasted = upcastRecordedMessage(
826
+ const messagesAboveCheckpoint = messages.filter(
827
+ (message2) => !wasMessageHandled(message2, lastCheckpoint)
828
+ );
829
+ const upcastedMessages = messagesAboveCheckpoint.map(
830
+ (message2) => upcastRecordedMessage(
796
831
  // TODO: Make it smarter
797
832
  message2,
798
833
  options.messageOptions?.schema?.versioning
834
+ )
835
+ ).filter(
836
+ (upcasted) => !canHandle || canHandle.includes(upcasted.type)
837
+ );
838
+ const stopMessageIndex = isCustomBatch && stopAfter ? upcastedMessages.findIndex(stopAfter) : -1;
839
+ const unhandledMessages = stopMessageIndex !== -1 ? upcastedMessages.slice(0, stopMessageIndex + 1) : upcastedMessages;
840
+ const batchResult = await eachBatch(unhandledMessages, context);
841
+ const messageProcessingResult = batchResult?.type === "STOP" ? batchResult : stopMessageIndex !== -1 ? {
842
+ type: "STOP",
843
+ reason: "Stop condition reached",
844
+ lastSuccessfulMessage: unhandledMessages[stopMessageIndex]
845
+ } : batchResult;
846
+ const isStop = messageProcessingResult && messageProcessingResult.type === "STOP";
847
+ const checkpointMessage = messageProcessingResult?.type === "STOP" ? messageProcessingResult.lastSuccessfulMessage : messagesAboveCheckpoint[messagesAboveCheckpoint.length - 1];
848
+ if (checkpointMessage && checkpoints) {
849
+ const storeCheckpointResult = await checkpoints.store(
850
+ {
851
+ processorId,
852
+ version,
853
+ message: checkpointMessage,
854
+ lastCheckpoint,
855
+ partition
856
+ },
857
+ context
799
858
  );
800
- if (canHandle !== void 0 && !canHandle.includes(upcasted.type))
801
- continue;
802
- const messageProcessingResult = await eachMessage(upcasted, context);
803
- if (checkpoints) {
804
- const storeCheckpointResult = await checkpoints.store(
805
- {
806
- processorId,
807
- version,
808
- message: upcasted,
809
- lastCheckpoint,
810
- partition
811
- },
812
- context
813
- );
814
- if (storeCheckpointResult.success) {
815
- lastCheckpoint = storeCheckpointResult.newCheckpoint;
816
- }
817
- }
818
- if (messageProcessingResult && messageProcessingResult.type === "STOP") {
819
- isActive = false;
820
- result = messageProcessingResult;
821
- break;
859
+ if (storeCheckpointResult.success) {
860
+ lastCheckpoint = storeCheckpointResult.newCheckpoint;
822
861
  }
823
- if (stopAfter && stopAfter(upcasted)) {
824
- isActive = false;
825
- result = { type: "STOP", reason: "Stop condition reached" };
826
- break;
827
- }
828
- if (messageProcessingResult && messageProcessingResult.type === "SKIP")
829
- continue;
830
862
  }
831
- return result;
863
+ if (isStop) {
864
+ isActive = false;
865
+ return messageProcessingResult;
866
+ }
867
+ return void 0;
832
868
  }, partialContext);
833
869
  }
834
870
  };
@@ -856,7 +892,7 @@ var projector = (options) => {
856
892
  } : void 0,
857
893
  onClose: options.hooks?.onClose
858
894
  },
859
- eachMessage: async (event2, context) => projection2.handle([event2], context)
895
+ eachBatch: async (events, context) => projection2.handle(events, context)
860
896
  });
861
897
  };
862
898
  var inMemoryCheckpointer = () => {