@cratis/chronicle 6.56.0 → 6.58.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.
Files changed (299) hide show
  1. package/EventStore.ts +2 -2
  2. package/WellKnownTelemetryNames.ts +2 -0
  3. package/auditing/CausationType.ts +5 -0
  4. package/dist/EventStore.js +2 -2
  5. package/dist/EventStore.js.map +1 -1
  6. package/dist/WellKnownTelemetryNames.d.ts +2 -0
  7. package/dist/WellKnownTelemetryNames.d.ts.map +1 -1
  8. package/dist/WellKnownTelemetryNames.js +2 -0
  9. package/dist/WellKnownTelemetryNames.js.map +1 -1
  10. package/dist/auditing/CausationType.d.ts +4 -0
  11. package/dist/auditing/CausationType.d.ts.map +1 -1
  12. package/dist/auditing/CausationType.js +4 -0
  13. package/dist/auditing/CausationType.js.map +1 -1
  14. package/dist/eventSequences/CompensateOptions.d.ts +7 -0
  15. package/dist/eventSequences/CompensateOptions.d.ts.map +1 -0
  16. package/dist/eventSequences/CompensateOptions.js +4 -0
  17. package/dist/eventSequences/CompensateOptions.js.map +1 -0
  18. package/dist/eventSequences/EventSequence.d.ts +3 -0
  19. package/dist/eventSequences/EventSequence.d.ts.map +1 -1
  20. package/dist/eventSequences/EventSequence.js +45 -0
  21. package/dist/eventSequences/EventSequence.js.map +1 -1
  22. package/dist/eventSequences/IEventSequence.d.ts +13 -0
  23. package/dist/eventSequences/IEventSequence.d.ts.map +1 -1
  24. package/dist/eventSequences/index.d.ts +1 -0
  25. package/dist/eventSequences/index.d.ts.map +1 -1
  26. package/dist/eventSequences/index.js.map +1 -1
  27. package/dist/events/EventContext.d.ts +5 -0
  28. package/dist/events/EventContext.d.ts.map +1 -1
  29. package/dist/events/compensationForDecorator.d.ts +32 -0
  30. package/dist/events/compensationForDecorator.d.ts.map +1 -0
  31. package/dist/events/compensationForDecorator.js +44 -0
  32. package/dist/events/compensationForDecorator.js.map +1 -0
  33. package/dist/events/eventTypeDecorator.d.ts +7 -0
  34. package/dist/events/eventTypeDecorator.d.ts.map +1 -1
  35. package/dist/events/eventTypeDecorator.js +13 -4
  36. package/dist/events/eventTypeDecorator.js.map +1 -1
  37. package/dist/events/index.d.ts +2 -1
  38. package/dist/events/index.d.ts.map +1 -1
  39. package/dist/events/index.js +2 -1
  40. package/dist/events/index.js.map +1 -1
  41. package/dist/events/toClientEventContext.d.ts.map +1 -1
  42. package/dist/events/toClientEventContext.js +20 -0
  43. package/dist/events/toClientEventContext.js.map +1 -1
  44. package/dist/observation/batches/BatchContext.d.ts +43 -0
  45. package/dist/observation/batches/BatchContext.d.ts.map +1 -0
  46. package/dist/observation/batches/BatchContext.js +60 -0
  47. package/dist/observation/batches/BatchContext.js.map +1 -0
  48. package/dist/observation/batches/BatchContextObserver.d.ts +14 -0
  49. package/dist/observation/batches/BatchContextObserver.d.ts.map +1 -0
  50. package/dist/observation/batches/BatchContextObserver.js +4 -0
  51. package/dist/observation/batches/BatchContextObserver.js.map +1 -0
  52. package/dist/observation/batches/BatchContextValues.d.ts +13 -0
  53. package/dist/observation/batches/BatchContextValues.d.ts.map +1 -0
  54. package/dist/observation/batches/BatchContextValues.js +4 -0
  55. package/dist/observation/batches/BatchContextValues.js.map +1 -0
  56. package/dist/observation/batches/BatchMethod.d.ts +9 -0
  57. package/dist/observation/batches/BatchMethod.d.ts.map +1 -0
  58. package/dist/observation/batches/BatchMethod.js +4 -0
  59. package/dist/observation/batches/BatchMethod.js.map +1 -0
  60. package/dist/observation/batches/BatchMethodReturnedAValue.d.ts +12 -0
  61. package/dist/observation/batches/BatchMethodReturnedAValue.d.ts.map +1 -0
  62. package/dist/observation/batches/BatchMethodReturnedAValue.js +20 -0
  63. package/dist/observation/batches/BatchMethodReturnedAValue.js.map +1 -0
  64. package/dist/observation/batches/BatchMethods.d.ts +31 -0
  65. package/dist/observation/batches/BatchMethods.d.ts.map +1 -0
  66. package/dist/observation/batches/BatchMethods.js +81 -0
  67. package/dist/observation/batches/BatchMethods.js.map +1 -0
  68. package/dist/observation/batches/BatchOutcome.d.ts +15 -0
  69. package/dist/observation/batches/BatchOutcome.d.ts.map +1 -0
  70. package/dist/observation/batches/BatchOutcome.js +4 -0
  71. package/dist/observation/batches/BatchOutcome.js.map +1 -0
  72. package/dist/observation/batches/BatchOutcomeKind.d.ts +8 -0
  73. package/dist/observation/batches/BatchOutcomeKind.d.ts.map +1 -0
  74. package/dist/observation/batches/BatchOutcomeKind.js +11 -0
  75. package/dist/observation/batches/BatchOutcomeKind.js.map +1 -0
  76. package/dist/observation/batches/DeferralNotSupportedByKernel.d.ts +13 -0
  77. package/dist/observation/batches/DeferralNotSupportedByKernel.d.ts.map +1 -0
  78. package/dist/observation/batches/DeferralNotSupportedByKernel.js +19 -0
  79. package/dist/observation/batches/DeferralNotSupportedByKernel.js.map +1 -0
  80. package/dist/observation/batches/DeliveredPartition.d.ts +11 -0
  81. package/dist/observation/batches/DeliveredPartition.d.ts.map +1 -0
  82. package/dist/observation/batches/DeliveredPartition.js +4 -0
  83. package/dist/observation/batches/DeliveredPartition.js.map +1 -0
  84. package/dist/observation/batches/EventAndContext.d.ts +28 -0
  85. package/dist/observation/batches/EventAndContext.d.ts.map +1 -0
  86. package/dist/observation/batches/EventAndContext.js +35 -0
  87. package/dist/observation/batches/EventAndContext.js.map +1 -0
  88. package/dist/observation/batches/EventBatch.d.ts +52 -0
  89. package/dist/observation/batches/EventBatch.d.ts.map +1 -0
  90. package/dist/observation/batches/EventBatch.js +92 -0
  91. package/dist/observation/batches/EventBatch.js.map +1 -0
  92. package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.d.ts +14 -0
  93. package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.d.ts.map +1 -0
  94. package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.js +23 -0
  95. package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.js.map +1 -0
  96. package/dist/observation/batches/InvalidBatchMethod.d.ts +13 -0
  97. package/dist/observation/batches/InvalidBatchMethod.d.ts.map +1 -0
  98. package/dist/observation/batches/InvalidBatchMethod.js +20 -0
  99. package/dist/observation/batches/InvalidBatchMethod.js.map +1 -0
  100. package/dist/observation/batches/ObservationOrigin.d.ts +21 -0
  101. package/dist/observation/batches/ObservationOrigin.d.ts.map +1 -0
  102. package/dist/observation/batches/ObservationOrigin.js +32 -0
  103. package/dist/observation/batches/ObservationOrigin.js.map +1 -0
  104. package/dist/observation/batches/ObservedEventType.d.ts +11 -0
  105. package/dist/observation/batches/ObservedEventType.d.ts.map +1 -0
  106. package/dist/observation/batches/ObservedEventType.js +4 -0
  107. package/dist/observation/batches/ObservedEventType.js.map +1 -0
  108. package/dist/observation/batches/PageMessages.d.ts +22 -0
  109. package/dist/observation/batches/PageMessages.d.ts.map +1 -0
  110. package/dist/observation/batches/PageMessages.js +32 -0
  111. package/dist/observation/batches/PageMessages.js.map +1 -0
  112. package/dist/observation/batches/PartitionFailedInBatch.d.ts +10 -0
  113. package/dist/observation/batches/PartitionFailedInBatch.d.ts.map +1 -0
  114. package/dist/observation/batches/PartitionFailedInBatch.js +16 -0
  115. package/dist/observation/batches/PartitionFailedInBatch.js.map +1 -0
  116. package/dist/observation/batches/PartitionNotInBatch.d.ts +10 -0
  117. package/dist/observation/batches/PartitionNotInBatch.d.ts.map +1 -0
  118. package/dist/observation/batches/PartitionNotInBatch.js +16 -0
  119. package/dist/observation/batches/PartitionNotInBatch.js.map +1 -0
  120. package/dist/observation/batches/PartitionOutcome.d.ts +19 -0
  121. package/dist/observation/batches/PartitionOutcome.d.ts.map +1 -0
  122. package/dist/observation/batches/PartitionOutcome.js +4 -0
  123. package/dist/observation/batches/PartitionOutcome.js.map +1 -0
  124. package/dist/observation/batches/PartitionProblem.d.ts +17 -0
  125. package/dist/observation/batches/PartitionProblem.d.ts.map +1 -0
  126. package/dist/observation/batches/PartitionProblem.js +4 -0
  127. package/dist/observation/batches/PartitionProblem.js.map +1 -0
  128. package/dist/observation/batches/PartitionSettlement.d.ts +29 -0
  129. package/dist/observation/batches/PartitionSettlement.d.ts.map +1 -0
  130. package/dist/observation/batches/PartitionSettlement.js +64 -0
  131. package/dist/observation/batches/PartitionSettlement.js.map +1 -0
  132. package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.d.ts +12 -0
  133. package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.d.ts.map +1 -0
  134. package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.js +20 -0
  135. package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.js.map +1 -0
  136. package/dist/observation/batches/batchSize.d.ts +42 -0
  137. package/dist/observation/batches/batchSize.d.ts.map +1 -0
  138. package/dist/observation/batches/batchSize.js +55 -0
  139. package/dist/observation/batches/batchSize.js.map +1 -0
  140. package/dist/observation/batches/createBatchContext.d.ts +13 -0
  141. package/dist/observation/batches/createBatchContext.d.ts.map +1 -0
  142. package/dist/observation/batches/createBatchContext.js +27 -0
  143. package/dist/observation/batches/createBatchContext.js.map +1 -0
  144. package/dist/observation/batches/observes.d.ts +39 -0
  145. package/dist/observation/batches/observes.d.ts.map +1 -0
  146. package/dist/observation/batches/observes.js +63 -0
  147. package/dist/observation/batches/observes.js.map +1 -0
  148. package/dist/observation/batches/runConcurrently.d.ts +9 -0
  149. package/dist/observation/batches/runConcurrently.d.ts.map +1 -0
  150. package/dist/observation/batches/runConcurrently.js +22 -0
  151. package/dist/observation/batches/runConcurrently.js.map +1 -0
  152. package/dist/observation/createHandlerInstanceValidator.d.ts +1 -1
  153. package/dist/observation/createHandlerInstanceValidator.d.ts.map +1 -1
  154. package/dist/observation/createHandlerInstanceValidator.js +5 -7
  155. package/dist/observation/createHandlerInstanceValidator.js.map +1 -1
  156. package/dist/observation/getInstanceMethods.d.ts +3 -1
  157. package/dist/observation/getInstanceMethods.d.ts.map +1 -1
  158. package/dist/observation/getInstanceMethods.js +13 -4
  159. package/dist/observation/getInstanceMethods.js.map +1 -1
  160. package/dist/observation/index.d.ts +14 -0
  161. package/dist/observation/index.d.ts.map +1 -1
  162. package/dist/observation/index.js +13 -0
  163. package/dist/observation/index.js.map +1 -1
  164. package/dist/projections/eventContextPropertyExpression.js +1 -1
  165. package/dist/projections/eventContextPropertyExpression.js.map +1 -1
  166. package/dist/reactors/ReactorDispatcher.d.ts +15 -0
  167. package/dist/reactors/ReactorDispatcher.d.ts.map +1 -1
  168. package/dist/reactors/ReactorDispatcher.js +25 -2
  169. package/dist/reactors/ReactorDispatcher.js.map +1 -1
  170. package/dist/reactors/ReactorObservationTarget.d.ts +45 -0
  171. package/dist/reactors/ReactorObservationTarget.d.ts.map +1 -0
  172. package/dist/reactors/ReactorObservationTarget.js +4 -0
  173. package/dist/reactors/ReactorObservationTarget.js.map +1 -0
  174. package/dist/reactors/ReactorPageDispatcher.d.ts +33 -0
  175. package/dist/reactors/ReactorPageDispatcher.d.ts.map +1 -0
  176. package/dist/reactors/ReactorPageDispatcher.js +293 -0
  177. package/dist/reactors/ReactorPageDispatcher.js.map +1 -0
  178. package/dist/reactors/Reactors.d.ts.map +1 -1
  179. package/dist/reactors/Reactors.js +21 -149
  180. package/dist/reactors/Reactors.js.map +1 -1
  181. package/dist/reactors/for_Reactors/given/observe_reactor_messages.d.ts +14 -0
  182. package/dist/reactors/for_Reactors/given/observe_reactor_messages.d.ts.map +1 -0
  183. package/dist/reactors/for_Reactors/given/observe_reactor_messages.js +61 -0
  184. package/dist/reactors/for_Reactors/given/observe_reactor_messages.js.map +1 -0
  185. package/dist/reactors/index.d.ts +2 -0
  186. package/dist/reactors/index.d.ts.map +1 -1
  187. package/dist/reactors/index.js +1 -0
  188. package/dist/reactors/index.js.map +1 -1
  189. package/dist/reducers/ReducerEventDispatcher.d.ts +26 -1
  190. package/dist/reducers/ReducerEventDispatcher.d.ts.map +1 -1
  191. package/dist/reducers/ReducerEventDispatcher.js +35 -3
  192. package/dist/reducers/ReducerEventDispatcher.js.map +1 -1
  193. package/dist/reducers/Reducers.d.ts +2 -0
  194. package/dist/reducers/Reducers.d.ts.map +1 -1
  195. package/dist/reducers/Reducers.js +94 -22
  196. package/dist/reducers/Reducers.js.map +1 -1
  197. package/dist/reducers/ReductionOutcome.d.ts +27 -3
  198. package/dist/reducers/ReductionOutcome.d.ts.map +1 -1
  199. package/dist/reducers/ReductionOutcome.js +51 -7
  200. package/dist/reducers/ReductionOutcome.js.map +1 -1
  201. package/dist/reducers/for_Reducers/given/observe_reducer_messages.d.ts +19 -0
  202. package/dist/reducers/for_Reducers/given/observe_reducer_messages.d.ts.map +1 -0
  203. package/dist/reducers/for_Reducers/given/observe_reducer_messages.js +61 -0
  204. package/dist/reducers/for_Reducers/given/observe_reducer_messages.js.map +1 -0
  205. package/dist/reducers/index.d.ts +2 -0
  206. package/dist/reducers/index.d.ts.map +1 -1
  207. package/dist/reducers/index.js +1 -0
  208. package/dist/reducers/index.js.map +1 -1
  209. package/dist/schemas/JsonSchema.d.ts +2 -0
  210. package/dist/schemas/JsonSchema.d.ts.map +1 -1
  211. package/dist/testing/InProcessEventSequence.d.ts +1 -0
  212. package/dist/testing/InProcessEventSequence.d.ts.map +1 -1
  213. package/dist/testing/InProcessEventSequence.js +1 -0
  214. package/dist/testing/InProcessEventSequence.js.map +1 -1
  215. package/dist/testing/ReactorScenario.d.ts.map +1 -1
  216. package/dist/testing/ReactorScenario.js +4 -1
  217. package/dist/testing/ReactorScenario.js.map +1 -1
  218. package/dist/testing/ReducerReadModelProcessor.d.ts +1 -0
  219. package/dist/testing/ReducerReadModelProcessor.d.ts.map +1 -1
  220. package/dist/testing/ReducerReadModelProcessor.js +45 -0
  221. package/dist/testing/ReducerReadModelProcessor.js.map +1 -1
  222. package/dist/tsconfig.tsbuildinfo +1 -1
  223. package/eventSequences/CompensateOptions.ts +10 -0
  224. package/eventSequences/EventSequence.compensate.spec.ts +72 -0
  225. package/eventSequences/EventSequence.ts +51 -0
  226. package/eventSequences/IEventSequence.ts +14 -0
  227. package/eventSequences/index.ts +1 -0
  228. package/events/EventContext.ts +6 -0
  229. package/events/compensationFor.spec.ts +73 -0
  230. package/events/compensationForDecorator.ts +48 -0
  231. package/events/eventTypeDecorator.ts +13 -4
  232. package/events/index.ts +2 -1
  233. package/events/toClientEventContext.ts +21 -0
  234. package/observation/batches/BatchContext.ts +75 -0
  235. package/observation/batches/BatchContextObserver.ts +16 -0
  236. package/observation/batches/BatchContextValues.ts +16 -0
  237. package/observation/batches/BatchMethod.ts +13 -0
  238. package/observation/batches/BatchMethodReturnedAValue.ts +16 -0
  239. package/observation/batches/BatchMethods.ts +90 -0
  240. package/observation/batches/BatchOutcome.ts +22 -0
  241. package/observation/batches/BatchOutcomeKind.ts +11 -0
  242. package/observation/batches/DeferralNotSupportedByKernel.ts +17 -0
  243. package/observation/batches/DeliveredPartition.ts +16 -0
  244. package/observation/batches/EventAndContext.ts +37 -0
  245. package/observation/batches/EventBatch.ts +105 -0
  246. package/observation/batches/EventTypeObservedByMoreThanOneMethod.ts +17 -0
  247. package/observation/batches/InvalidBatchMethod.ts +16 -0
  248. package/observation/batches/ObservationOrigin.ts +37 -0
  249. package/observation/batches/ObservedEventType.ts +16 -0
  250. package/observation/batches/PageMessages.ts +37 -0
  251. package/observation/batches/PartitionFailedInBatch.ts +14 -0
  252. package/observation/batches/PartitionNotInBatch.ts +14 -0
  253. package/observation/batches/PartitionOutcome.ts +22 -0
  254. package/observation/batches/PartitionProblem.ts +25 -0
  255. package/observation/batches/PartitionSettlement.ts +70 -0
  256. package/observation/batches/ReducerMixesBatchAndPerEventMethods.ts +16 -0
  257. package/observation/batches/batchSize.ts +79 -0
  258. package/observation/batches/createBatchContext.ts +32 -0
  259. package/observation/batches/for_BatchMethods/when_discovering_batch_methods.spec.ts +155 -0
  260. package/observation/batches/for_EventBatch/when_reporting_outcomes.spec.ts +66 -0
  261. package/observation/batches/for_PartitionSettlement/when_settling_a_partition.spec.ts +31 -0
  262. package/observation/batches/for_batchSize/when_declaring_bounds.spec.ts +23 -0
  263. package/observation/batches/for_observes/when_decorating_methods.legacy.spec.ts +28 -0
  264. package/observation/batches/for_observes/when_decorating_methods.spec.ts +33 -0
  265. package/observation/batches/for_runConcurrently/when_running_items.spec.ts +22 -0
  266. package/observation/batches/observes.ts +68 -0
  267. package/observation/batches/runConcurrently.ts +23 -0
  268. package/observation/createHandlerInstanceValidator.ts +5 -7
  269. package/observation/getInstanceMethods.ts +13 -4
  270. package/observation/index.ts +14 -0
  271. package/package.json +2 -2
  272. package/projections/eventContextPropertyExpression.ts +2 -2
  273. package/reactors/ReactorDispatcher.ts +27 -2
  274. package/reactors/ReactorObservationTarget.ts +48 -0
  275. package/reactors/ReactorPageDispatcher.ts +321 -0
  276. package/reactors/Reactors.ts +22 -164
  277. package/reactors/for_Reactors/given/observe_reactor_messages.ts +72 -0
  278. package/reactors/for_Reactors/when_delivering_pages/as_a_legacy_single_partition.spec.ts +51 -0
  279. package/reactors/for_Reactors/when_delivering_pages/returning_a_value_from_a_batch_method.spec.ts +34 -0
  280. package/reactors/for_Reactors/when_delivering_pages/with_batch_and_per_event_methods.spec.ts +81 -0
  281. package/reactors/for_Reactors/when_delivering_pages/with_batch_outcomes.spec.ts +86 -0
  282. package/reactors/for_Reactors/when_delivering_pages/with_per_event_handlers.spec.ts +52 -0
  283. package/reactors/index.ts +2 -0
  284. package/reducers/ReducerEventDispatcher.ts +44 -3
  285. package/reducers/Reducers.ts +109 -25
  286. package/reducers/ReductionOutcome.ts +55 -8
  287. package/reducers/for_Reducers/given/observe_reducer_messages.ts +77 -0
  288. package/reducers/for_Reducers/when_delivering_pages/to_a_batch_reducer.spec.ts +111 -0
  289. package/reducers/for_Reducers/when_delivering_pages/to_a_per_event_reducer.spec.ts +53 -0
  290. package/reducers/index.ts +2 -0
  291. package/schemas/JsonSchema.ts +2 -0
  292. package/telemetry/for_ChronicleTelemetry/given/a_client.fixture.ts +1 -0
  293. package/telemetry/for_ChronicleTelemetry/given/a_span_name_registry.fixture.ts +2 -0
  294. package/telemetry/for_ChronicleTelemetry/when_naming_spans/with_convention_names.spec.ts +2 -1
  295. package/testing/InProcessEventSequence.ts +1 -0
  296. package/testing/ReactorScenario.ts +5 -1
  297. package/testing/ReducerReadModelProcessor.ts +44 -0
  298. package/testing/for_ReactorScenario/when_the_reactor_has_a_batch_method.spec.ts +26 -0
  299. package/testing/for_ReadModelScenario/when_reducing_with_a_batch_method/with_events_for_two_event_sources.spec.ts +43 -0
@@ -0,0 +1,16 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import type { Constructor } from '@cratis/fundamentals';
5
+
6
+ /** An event type a batch method observes. */
7
+ export interface ObservedEventType {
8
+ /** The event constructor. */
9
+ readonly type: Constructor;
10
+
11
+ /** The event type identifier. */
12
+ readonly id: string;
13
+
14
+ /** The event type generation. */
15
+ readonly generation: number;
16
+ }
@@ -0,0 +1,37 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import type { EventsToObserve, ReduceOperationMessage } from '@cratis/chronicle.contracts';
5
+ import type { DeliveredPartition } from './DeliveredPartition.js';
6
+
7
+ /**
8
+ * Gets whether the kernel delivered a page of partitions. A kernel that does not know page delivery sends one partition
9
+ * in the legacy fields, and cannot take deferrals or per-partition results.
10
+ * @param message - The delivered message.
11
+ * @returns True when the message is a page.
12
+ */
13
+ export function isPage(message: EventsToObserve | ReduceOperationMessage): boolean {
14
+ return (message.Partitions?.length ?? 0) > 0;
15
+ }
16
+
17
+ /**
18
+ * Gets the partitions of a reactor delivery, treating a single-partition delivery as a page of one.
19
+ * @param message - The {@link EventsToObserve} delivered.
20
+ * @returns The {@link DeliveredPartition} instances.
21
+ */
22
+ export function partitionsOfEvents(message: EventsToObserve): DeliveredPartition[] {
23
+ return isPage(message)
24
+ ? message.Partitions.map(partition => ({ partition: partition.Partition, events: partition.Events, initialState: '' }))
25
+ : [{ partition: message.Partition, events: message.Events ?? [], initialState: '' }];
26
+ }
27
+
28
+ /**
29
+ * Gets the partitions of a reducer delivery, treating a single-partition delivery as a page of one.
30
+ * @param message - The {@link ReduceOperationMessage} delivered.
31
+ * @returns The {@link DeliveredPartition} instances.
32
+ */
33
+ export function partitionsOfOperation(message: ReduceOperationMessage): DeliveredPartition[] {
34
+ return isPage(message)
35
+ ? message.Partitions.map(partition => ({ partition: partition.Partition, events: partition.Events, initialState: partition.InitialState }))
36
+ : [{ partition: message.Partition, events: message.Events ?? [], initialState: message.InitialState }];
37
+ }
@@ -0,0 +1,14 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /** Reported when a batch method fails a partition without giving a reason. */
5
+ export class PartitionFailedInBatch extends Error {
6
+ /**
7
+ * Creates a new {@link PartitionFailedInBatch}.
8
+ * @param partition - The partition that was failed.
9
+ */
10
+ constructor(readonly partition: string) {
11
+ super(`Partition '${partition}' was failed by a batch method.`);
12
+ this.name = 'PartitionFailedInBatch';
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /** Thrown when a batch method reports an outcome for a partition the batch holds no events for. */
5
+ export class PartitionNotInBatch extends Error {
6
+ /**
7
+ * Creates a new {@link PartitionNotInBatch}.
8
+ * @param partition - The partition the outcome was reported for.
9
+ */
10
+ constructor(readonly partition: string) {
11
+ super(`The batch holds no events for partition '${partition}', so no outcome can be reported for it.`);
12
+ this.name = 'PartitionNotInBatch';
13
+ }
14
+ }
@@ -0,0 +1,22 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import type { ObservationState } from '@cratis/chronicle.contracts';
5
+
6
+ /** The outcome of observing the events of one partition of a delivery. */
7
+ export interface PartitionOutcome {
8
+ /** The partition. */
9
+ readonly partition: string;
10
+ /** The settled state of the partition. */
11
+ readonly state: ObservationState;
12
+ /** The last event of the partition that was observed successfully. */
13
+ readonly lastSuccessfulObservation: bigint;
14
+ /** The messages describing a failure. */
15
+ readonly exceptionMessages: readonly string[];
16
+ /** The stack trace of a failure. */
17
+ readonly exceptionStackTrace: string;
18
+ /** How long to wait before delivering again, for a deferral. */
19
+ readonly retryAfterMilliseconds: number;
20
+ /** The reduced read model state as JSON, for reducers. */
21
+ readonly readModelState?: string;
22
+ }
@@ -0,0 +1,25 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import type { ObservationState } from '@cratis/chronicle.contracts';
5
+
6
+ /** The first event of a partition that one method of an observer did not handle. */
7
+ export interface PartitionProblem {
8
+ /** The partition. */
9
+ readonly partition: string;
10
+
11
+ /** The sequence number of the event. */
12
+ readonly at: bigint;
13
+
14
+ /** Whether the partition failed or was deferred. */
15
+ readonly state: ObservationState.Failed | ObservationState.Deferred;
16
+
17
+ /** The messages describing a failure. */
18
+ readonly exceptionMessages: readonly string[];
19
+
20
+ /** The stack trace of a failure. */
21
+ readonly exceptionStackTrace: string;
22
+
23
+ /** How long to wait before delivering again, for a deferral. */
24
+ readonly retryAfterMilliseconds: number;
25
+ }
@@ -0,0 +1,70 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { ObservationState, type PartitionResult } from '@cratis/chronicle.contracts';
5
+ import type { PartitionOutcome } from './PartitionOutcome.js';
6
+ import type { PartitionProblem } from './PartitionProblem.js';
7
+
8
+ /** Sequence number reported when no event of a partition was observed successfully. */
9
+ export const SEQUENCE_NUMBER_UNAVAILABLE = 4294967295n;
10
+
11
+ /**
12
+ * Creates a {@link PartitionProblem} for a failure.
13
+ * @param partition - The partition.
14
+ * @param at - The sequence number of the event it failed at.
15
+ * @param error - What went wrong.
16
+ * @returns The {@link PartitionProblem}.
17
+ */
18
+ export function failedAt(partition: string, at: bigint, error: unknown): PartitionProblem {
19
+ return {
20
+ partition, at, state: ObservationState.Failed,
21
+ exceptionMessages: [String(error)],
22
+ exceptionStackTrace: error instanceof Error ? (error.stack ?? '') : '',
23
+ retryAfterMilliseconds: 0
24
+ };
25
+ }
26
+
27
+ /**
28
+ * Settles a partition: it succeeds when no method reported a problem for it, and otherwise settles at its earliest problem.
29
+ * Events before that problem count as observed.
30
+ * @param partition - The partition.
31
+ * @param sequenceNumbers - The sequence numbers of the partition's events, in order.
32
+ * @param problems - The problems reported by any method, for any partition.
33
+ * @returns The {@link PartitionOutcome}.
34
+ */
35
+ export function settlePartition(partition: string, sequenceNumbers: readonly bigint[], problems: readonly PartitionProblem[]): PartitionOutcome {
36
+ const earliest = problems
37
+ .filter(problem => problem.partition === partition)
38
+ .reduce<PartitionProblem | undefined>((first, problem) => !first || problem.at < first.at ? problem : first, undefined);
39
+ if (!earliest) {
40
+ return {
41
+ partition, state: ObservationState.Success,
42
+ lastSuccessfulObservation: sequenceNumbers.at(-1) ?? SEQUENCE_NUMBER_UNAVAILABLE,
43
+ exceptionMessages: [], exceptionStackTrace: '', retryAfterMilliseconds: 0
44
+ };
45
+ }
46
+ const handled = sequenceNumbers.filter(sequenceNumber => sequenceNumber < earliest.at);
47
+ return {
48
+ partition, state: earliest.state,
49
+ lastSuccessfulObservation: handled.at(-1) ?? SEQUENCE_NUMBER_UNAVAILABLE,
50
+ exceptionMessages: earliest.exceptionMessages, exceptionStackTrace: earliest.exceptionStackTrace,
51
+ retryAfterMilliseconds: earliest.retryAfterMilliseconds
52
+ };
53
+ }
54
+
55
+ /**
56
+ * Converts a {@link PartitionOutcome} to the {@link PartitionResult} sent to the kernel.
57
+ * @param outcome - The outcome.
58
+ * @returns The {@link PartitionResult}.
59
+ */
60
+ export function toPartitionResult(outcome: PartitionOutcome): PartitionResult {
61
+ return {
62
+ Partition: outcome.partition,
63
+ State: outcome.state,
64
+ LastSuccessfulObservation: outcome.lastSuccessfulObservation,
65
+ ExceptionMessages: [...outcome.exceptionMessages],
66
+ ExceptionStackTrace: outcome.exceptionStackTrace,
67
+ RetryAfterMilliseconds: BigInt(Math.max(0, Math.round(outcome.retryAfterMilliseconds))),
68
+ ReadModelState: outcome.readModelState ?? ''
69
+ };
70
+ }
@@ -0,0 +1,16 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /** Thrown at registration when a reducer declares a batch method and per-event methods, or more than one batch method. */
5
+ export class ReducerMixesBatchAndPerEventMethods extends Error {
6
+ /**
7
+ * Creates a new {@link ReducerMixesBatchAndPerEventMethods}.
8
+ * @param reducerType - The name of the reducer class.
9
+ * @param methods - The methods that conflict.
10
+ */
11
+ constructor(readonly reducerType: string, readonly methods: readonly string[]) {
12
+ super(`Reducer '${reducerType}' declares ${methods.map(method => `'${method}'`).join(', ')}. ` +
13
+ 'A reducer has either per-event methods or exactly one batch method, never both, because it is a single fold over one event source.');
14
+ this.name = 'ReducerMixesBatchAndPerEventMethods';
15
+ }
16
+ }
@@ -0,0 +1,79 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import 'reflect-metadata';
5
+ import type { PageDelivery } from '@cratis/chronicle.contracts';
6
+ import { availableParallelism } from 'node:os';
7
+
8
+ const METADATA_KEY = 'chronicle:observation:batchSize';
9
+
10
+ /** The bounds an observer declares for the pages the kernel delivers to it. */
11
+ export interface BatchSizeOptions {
12
+ /** The most events in one page. The kernel's own maximum caps it. Defaults to the kernel's default. */
13
+ readonly maxEvents?: number;
14
+
15
+ /** The most bytes in one page. The kernel's own maximum caps it. Defaults to the kernel's default. */
16
+ readonly maxBytes?: number;
17
+
18
+ /** How long, in milliseconds, the kernel may wait to fill a live page. Defaults to no delay. */
19
+ readonly maxDelayMilliseconds?: number;
20
+
21
+ /**
22
+ * How many event sources of one page a reducer reduces at the same time. Defaults to the number of processors;
23
+ * `1` reduces them one after another. Reactors call their methods for a page in order and ignore it.
24
+ */
25
+ readonly maxConcurrency?: number;
26
+ }
27
+
28
+ /**
29
+ * Declares the bounds of the pages a reactor or reducer receives, the TypeScript equivalent of the C# `[BatchSize]` attribute.
30
+ * @param options - The {@link BatchSizeOptions}.
31
+ * @returns A class decorator for legacy and standard TypeScript decorators.
32
+ */
33
+ export function batchSize(options: BatchSizeOptions): ClassDecorator & ((value: Function, context: ClassDecoratorContext) => void) {
34
+ for (const [name, value] of Object.entries(options)) {
35
+ if (value !== undefined && (!Number.isInteger(value) || (value as number) < (name === 'maxDelayMilliseconds' ? 0 : 1))) {
36
+ throw new RangeError(`Batch size '${name}' must be a ${name === 'maxDelayMilliseconds' ? 'non-negative' : 'positive'} integer.`);
37
+ }
38
+ }
39
+ return (target: object, context?: ClassDecoratorContext) => {
40
+ if (context !== undefined && context.kind !== 'class') {
41
+ throw new TypeError('Batch size can only decorate a reactor or reducer class.');
42
+ }
43
+ Reflect.defineMetadata(METADATA_KEY, Object.freeze({ ...options }), target);
44
+ };
45
+ }
46
+
47
+ /**
48
+ * Gets the {@link BatchSizeOptions} declared on an observer class.
49
+ * @param target - The observer class.
50
+ * @returns The declared options, or an empty object when none are declared.
51
+ */
52
+ export function getBatchSize(target: Function): BatchSizeOptions {
53
+ return (Reflect.getOwnMetadata(METADATA_KEY, target) as BatchSizeOptions | undefined) ?? {};
54
+ }
55
+
56
+ /**
57
+ * Gets how many event sources of one page a reducer reduces at the same time.
58
+ * @param target - The reducer class.
59
+ * @returns The declared concurrency, or the number of processors.
60
+ */
61
+ export function getMaxConcurrency(target: Function): number {
62
+ return getBatchSize(target).maxConcurrency ?? availableParallelism();
63
+ }
64
+
65
+ /**
66
+ * Creates the page delivery the client opts in to at registration. A kernel that does not know page delivery ignores it
67
+ * and keeps delivering one partition at a time.
68
+ * @param target - The observer class.
69
+ * @returns The {@link PageDelivery} for the registration.
70
+ */
71
+ export function toPageDelivery(target: Function): PageDelivery {
72
+ const options = getBatchSize(target);
73
+ return {
74
+ Enabled: true,
75
+ MaxEventsPerPage: options.maxEvents ?? 0,
76
+ MaxBytesPerPage: options.maxBytes ?? 0,
77
+ MaxDelayMilliseconds: options.maxDelayMilliseconds ?? 0
78
+ };
79
+ }
@@ -0,0 +1,32 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import type { EventsToObserve, ReduceOperationMessage } from '@cratis/chronicle.contracts';
5
+ import { BatchContext } from './BatchContext.js';
6
+ import type { BatchContextObserver } from './BatchContextObserver.js';
7
+ import type { DeliveredPartition } from './DeliveredPartition.js';
8
+ import { toObservationOrigin } from './ObservationOrigin.js';
9
+ import { isPage } from './PageMessages.js';
10
+
11
+ /**
12
+ * Creates the {@link BatchContext} describing a delivered page.
13
+ * @param observer - The observer the page is delivered to.
14
+ * @param message - The delivered message.
15
+ * @param partitions - The partitions of the delivery.
16
+ * @returns The {@link BatchContext}.
17
+ */
18
+ export function createBatchContext(observer: BatchContextObserver, message: EventsToObserve | ReduceOperationMessage,
19
+ partitions: readonly DeliveredPartition[]): BatchContext {
20
+ const sequenceNumbers = partitions.flatMap(partition => partition.events.map(event => event.Context!.SequenceNumber));
21
+ const deliverWithin = Number(message.DeliverWithinMilliseconds ?? 0n);
22
+ return new BatchContext({
23
+ ...observer,
24
+ origin: toObservationOrigin(message.Origin),
25
+ partitions: partitions.map(partition => partition.partition),
26
+ eventCount: sequenceNumbers.length,
27
+ firstSequenceNumber: sequenceNumbers.length > 0 ? sequenceNumbers.reduce((min, value) => value < min ? value : min) : undefined,
28
+ lastSequenceNumber: sequenceNumbers.length > 0 ? sequenceNumbers.reduce((max, value) => value > max ? value : max) : undefined,
29
+ deadline: deliverWithin > 0 ? new Date(Date.now() + deliverWithin) : undefined,
30
+ canDefer: isPage(message)
31
+ });
32
+ }
@@ -0,0 +1,155 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { chai, describe, it } from 'vitest';
5
+ import { eventType } from '../../../events/eventTypeDecorator.js';
6
+ import { handles } from '../../../events/handles.js';
7
+ import { onceOnly } from '../../../reactors/onceOnly.js';
8
+ import { replay } from '../../../reactors/replay.js';
9
+ import { getReactorEventTypes } from '../../../reactors/ReactorDispatcher.js';
10
+ import { ReducerEventDispatcher } from '../../../reducers/ReducerEventDispatcher.js';
11
+ import { getBatchMethods } from '../BatchMethods.js';
12
+ import { EventTypeObservedByMoreThanOneMethod } from '../EventTypeObservedByMoreThanOneMethod.js';
13
+ import { InvalidBatchMethod } from '../InvalidBatchMethod.js';
14
+ import { observes } from '../observes.js';
15
+ import { ReducerMixesBatchAndPerEventMethods } from '../ReducerMixesBatchAndPerEventMethods.js';
16
+
17
+ chai.should();
18
+
19
+ @eventType('discovery-account-opened')
20
+ class AccountOpened {}
21
+
22
+ @eventType('discovery-money-deposited')
23
+ class MoneyDeposited {}
24
+
25
+ class Unregistered {}
26
+
27
+ const registered = [AccountOpened, MoneyDeposited];
28
+
29
+ describe('when discovering batch methods', () => {
30
+ it('should find each batch method with the event types it observes', () => {
31
+ class Accounts {
32
+ @observes(AccountOpened)
33
+ @observes(MoneyDeposited)
34
+ all() {}
35
+ }
36
+ const [method] = getBatchMethods(Accounts, registered);
37
+ method.methodName.should.equal('all');
38
+ [...method.eventTypes.keys()].sort().should.deep.equal(['discovery-account-opened', 'discovery-money-deposited']);
39
+ });
40
+
41
+ it('should refuse an event type observed by two batch methods', () => {
42
+ class Accounts {
43
+ @observes(AccountOpened)
44
+ first() {}
45
+
46
+ @observes(AccountOpened)
47
+ second() {}
48
+ }
49
+ (() => getBatchMethods(Accounts, registered)).should.throw(EventTypeObservedByMoreThanOneMethod);
50
+ });
51
+
52
+ it('should refuse an event type that is not registered', () => {
53
+ class Accounts {
54
+ @observes(Unregistered)
55
+ all() {}
56
+ }
57
+ (() => getBatchMethods(Accounts, registered)).should.throw(InvalidBatchMethod, 'no registered event type');
58
+ });
59
+
60
+ it('should refuse a batch method that also declares per-event decorators', () => {
61
+ class WithHandles {
62
+ @observes(AccountOpened)
63
+ @handles(MoneyDeposited)
64
+ all() {}
65
+ }
66
+ class WithReplay {
67
+ @observes(AccountOpened)
68
+ @replay(AccountOpened)
69
+ all() {}
70
+ }
71
+ class WithOnceOnly {
72
+ @observes(AccountOpened)
73
+ @onceOnly()
74
+ all() {}
75
+ }
76
+ (() => getBatchMethods(WithHandles, registered)).should.throw(InvalidBatchMethod, '@handles');
77
+ (() => getBatchMethods(WithReplay, registered)).should.throw(InvalidBatchMethod, '@replay');
78
+ (() => getBatchMethods(WithOnceOnly, registered)).should.throw(InvalidBatchMethod, '@onceOnly');
79
+ });
80
+ });
81
+
82
+ describe('when registering a reactor with batch methods', () => {
83
+ it('should accept batch and per-event methods for different event types', () => {
84
+ class Accounts {
85
+ @observes(AccountOpened)
86
+ opened() {}
87
+
88
+ moneyDeposited() {}
89
+ }
90
+ getReactorEventTypes(Accounts, registered).map(entry => entry.methodName).should.deep.equal(['moneyDeposited']);
91
+ });
92
+
93
+ it('should refuse an event type reaching a batch method and a conventionally named per-event method', () => {
94
+ class Accounts {
95
+ @observes(AccountOpened)
96
+ opened() {}
97
+
98
+ accountOpened() {}
99
+ }
100
+ (() => getReactorEventTypes(Accounts, registered)).should.throw(EventTypeObservedByMoreThanOneMethod, "'opened', 'accountOpened'");
101
+ });
102
+
103
+ it('should refuse an event type reaching a batch method and an explicit per-event method', () => {
104
+ class Accounts {
105
+ @observes(AccountOpened)
106
+ opened() {}
107
+
108
+ @handles(AccountOpened)
109
+ alsoOpened() {}
110
+ }
111
+ (() => getReactorEventTypes(Accounts, registered)).should.throw(EventTypeObservedByMoreThanOneMethod);
112
+ });
113
+
114
+ it('should not treat a batch method named after an event type as a per-event method', () => {
115
+ class Accounts {
116
+ @observes(AccountOpened)
117
+ accountOpened() {}
118
+ }
119
+ getReactorEventTypes(Accounts, registered).should.be.empty;
120
+ });
121
+ });
122
+
123
+ describe('when registering a reducer with a batch method', () => {
124
+ it('should register the observed event types of its batch method', () => {
125
+ class Balance {
126
+ @observes(AccountOpened)
127
+ @observes(MoneyDeposited)
128
+ reduce() {}
129
+ }
130
+ const dispatcher = new ReducerEventDispatcher(Balance, registered);
131
+ dispatcher.batchMethod!.methodName.should.equal('reduce');
132
+ dispatcher.eventTypes.map(eventType => eventType.id).sort().should.deep.equal(['discovery-account-opened', 'discovery-money-deposited']);
133
+ });
134
+
135
+ it('should refuse a reducer mixing a batch method and per-event methods', () => {
136
+ class Balance {
137
+ @observes(AccountOpened)
138
+ reduce() {}
139
+
140
+ moneyDeposited() {}
141
+ }
142
+ (() => new ReducerEventDispatcher(Balance, registered)).should.throw(ReducerMixesBatchAndPerEventMethods);
143
+ });
144
+
145
+ it('should refuse a reducer with two batch methods', () => {
146
+ class Balance {
147
+ @observes(AccountOpened)
148
+ opened() {}
149
+
150
+ @observes(MoneyDeposited)
151
+ deposited() {}
152
+ }
153
+ (() => new ReducerEventDispatcher(Balance, registered)).should.throw(ReducerMixesBatchAndPerEventMethods);
154
+ });
155
+ });
@@ -0,0 +1,66 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { chai, describe, it } from 'vitest';
5
+ import type { EventContext } from '../../../events/EventContext.js';
6
+ import { BatchOutcomeKind } from '../BatchOutcomeKind.js';
7
+ import { DeferralNotSupportedByKernel } from '../DeferralNotSupportedByKernel.js';
8
+ import { EventAndContext } from '../EventAndContext.js';
9
+ import { EventBatch } from '../EventBatch.js';
10
+ import { PartitionNotInBatch } from '../PartitionNotInBatch.js';
11
+
12
+ chai.should();
13
+
14
+ class Opened { constructor(readonly owner: string = '') {} }
15
+ class Closed {}
16
+
17
+ const contextFor = (eventSourceId: string, sequenceNumber: bigint) => ({ eventSourceId, sequenceNumber } as EventContext);
18
+ const events = [
19
+ new EventAndContext({ owner: 'Jane' }, contextFor('a', 1n), Opened),
20
+ new EventAndContext({}, contextFor('b', 2n), Opened),
21
+ new EventAndContext({}, contextFor('a', 3n), Closed)
22
+ ];
23
+
24
+ describe('when reporting outcomes from an event batch', () => {
25
+ it('should list its partitions in the order their first event appears', () => new EventBatch(events).partitions.should.deep.equal(['a', 'b']));
26
+
27
+ it('should narrow events by type', () => {
28
+ const opened = new EventBatch(events).ofType(Opened);
29
+ opened.length.should.equal(2);
30
+ opened[0].event.owner.should.equal('Jane');
31
+ events[2].is(Opened).should.be.false;
32
+ });
33
+
34
+ it('should fail a partition from its first event in the batch', () => {
35
+ const batch = new EventBatch(events);
36
+ const error = new Error('down');
37
+ batch.fail('a', error);
38
+ batch.outcomes.should.deep.equal([{ partition: 'a', at: 1n, kind: BatchOutcomeKind.Failed, error, retryAfterMilliseconds: 0 }]);
39
+ });
40
+
41
+ it('should fail the partition of an event from that event', () => {
42
+ const batch = new EventBatch(events);
43
+ batch.fail(contextFor('a', 3n));
44
+ batch.outcomes[0].at.should.equal(3n);
45
+ batch.outcomes[0].partition.should.equal('a');
46
+ });
47
+
48
+ it('should defer a partition with its delay', () => {
49
+ const batch = new EventBatch(events);
50
+ batch.defer(contextFor('b', 2n), 250);
51
+ batch.outcomes.should.deep.equal([{ partition: 'b', at: 2n, kind: BatchOutcomeKind.Deferred, retryAfterMilliseconds: 250 }]);
52
+ });
53
+
54
+ it('should fail instead of deferring when the kernel cannot take deferrals', () => {
55
+ const batch = new EventBatch(events, false);
56
+ batch.defer('b', 250);
57
+ batch.outcomes[0].kind.should.equal(BatchOutcomeKind.Failed);
58
+ batch.outcomes[0].error!.should.be.instanceOf(DeferralNotSupportedByKernel);
59
+ });
60
+
61
+ it('should refuse a partition it holds no events for', () => {
62
+ const batch = new EventBatch(events);
63
+ (() => batch.fail('c')).should.throw(PartitionNotInBatch);
64
+ (() => batch.defer(contextFor('c', 9n), 10)).should.throw(PartitionNotInBatch);
65
+ });
66
+ });
@@ -0,0 +1,31 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { chai, describe, it } from 'vitest';
5
+ import { ObservationState } from '@cratis/chronicle.contracts';
6
+ import { failedAt, settlePartition, SEQUENCE_NUMBER_UNAVAILABLE } from '../PartitionSettlement.js';
7
+
8
+ chai.should();
9
+
10
+ describe('when settling a partition', () => {
11
+ const sequenceNumbers = [3n, 5n, 8n];
12
+
13
+ it('should succeed at its last event when nothing was reported for it', () => {
14
+ const outcome = settlePartition('a', sequenceNumbers, [failedAt('b', 1n, new Error('other'))]);
15
+ outcome.state.should.equal(ObservationState.Success);
16
+ outcome.lastSuccessfulObservation.should.equal(8n);
17
+ });
18
+
19
+ it('should settle at the earliest problem, counting the events before it as observed', () => {
20
+ const outcome = settlePartition('a', sequenceNumbers, [
21
+ failedAt('a', 8n, new Error('late')),
22
+ { ...failedAt('a', 5n, new Error('early')), state: ObservationState.Deferred, retryAfterMilliseconds: 20 }
23
+ ]);
24
+ outcome.state.should.equal(ObservationState.Deferred);
25
+ outcome.lastSuccessfulObservation.should.equal(3n);
26
+ outcome.retryAfterMilliseconds.should.equal(20);
27
+ });
28
+
29
+ it('should report no observed event when it fails at its first event', () =>
30
+ settlePartition('a', sequenceNumbers, [failedAt('a', 3n, new Error('first'))]).lastSuccessfulObservation.should.equal(SEQUENCE_NUMBER_UNAVAILABLE));
31
+ });
@@ -0,0 +1,23 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { availableParallelism } from 'node:os';
5
+ import { chai, describe, it } from 'vitest';
6
+ import { batchSize, getMaxConcurrency, toPageDelivery } from '../batchSize.js';
7
+
8
+ chai.should();
9
+
10
+ @batchSize({ maxEvents: 1000, maxBytes: 4096, maxDelayMilliseconds: 50, maxConcurrency: 2 })
11
+ class Bounded {}
12
+
13
+ class Unbounded {}
14
+
15
+ describe('when declaring the bounds of pages', () => {
16
+ it('should opt in to pages with the declared bounds', () =>
17
+ toPageDelivery(Bounded).should.deep.equal({ Enabled: true, MaxEventsPerPage: 1000, MaxBytesPerPage: 4096, MaxDelayMilliseconds: 50 }));
18
+ it('should opt in to pages with the kernel defaults when nothing is declared', () =>
19
+ toPageDelivery(Unbounded).should.deep.equal({ Enabled: true, MaxEventsPerPage: 0, MaxBytesPerPage: 0, MaxDelayMilliseconds: 0 }));
20
+ it('should use the declared concurrency', () => getMaxConcurrency(Bounded).should.equal(2));
21
+ it('should default the concurrency to the number of processors', () => getMaxConcurrency(Unbounded).should.equal(availableParallelism()));
22
+ it('should refuse a concurrency below one', () => (() => batchSize({ maxConcurrency: 0 })).should.throw(RangeError));
23
+ });
@@ -0,0 +1,28 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { chai, describe, it } from 'vitest';
5
+ import { getObservedEventTypes, observes } from '../observes.js';
6
+
7
+ chai.should();
8
+
9
+ class First {}
10
+ class Second {}
11
+
12
+ class Observer {
13
+ @observes(First)
14
+ @observes(Second)
15
+ both() {}
16
+ }
17
+
18
+ describe('when decorating methods with observes using legacy decorators', () => {
19
+ it('should collect every observed event type of a method', () =>
20
+ getObservedEventTypes(Observer.prototype.both).length.should.equal(2));
21
+ it('should refuse a static method', () => (() => {
22
+ class Static {
23
+ @observes(First)
24
+ static batch() {}
25
+ }
26
+ return Static;
27
+ }).should.throw(TypeError, 'public, string-named instance method'));
28
+ });