@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.
- package/EventStore.ts +2 -2
- package/WellKnownTelemetryNames.ts +2 -0
- package/auditing/CausationType.ts +5 -0
- package/dist/EventStore.js +2 -2
- package/dist/EventStore.js.map +1 -1
- package/dist/WellKnownTelemetryNames.d.ts +2 -0
- package/dist/WellKnownTelemetryNames.d.ts.map +1 -1
- package/dist/WellKnownTelemetryNames.js +2 -0
- package/dist/WellKnownTelemetryNames.js.map +1 -1
- package/dist/auditing/CausationType.d.ts +4 -0
- package/dist/auditing/CausationType.d.ts.map +1 -1
- package/dist/auditing/CausationType.js +4 -0
- package/dist/auditing/CausationType.js.map +1 -1
- package/dist/eventSequences/CompensateOptions.d.ts +7 -0
- package/dist/eventSequences/CompensateOptions.d.ts.map +1 -0
- package/dist/eventSequences/CompensateOptions.js +4 -0
- package/dist/eventSequences/CompensateOptions.js.map +1 -0
- package/dist/eventSequences/EventSequence.d.ts +3 -0
- package/dist/eventSequences/EventSequence.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.js +45 -0
- package/dist/eventSequences/EventSequence.js.map +1 -1
- package/dist/eventSequences/IEventSequence.d.ts +13 -0
- package/dist/eventSequences/IEventSequence.d.ts.map +1 -1
- package/dist/eventSequences/index.d.ts +1 -0
- package/dist/eventSequences/index.d.ts.map +1 -1
- package/dist/eventSequences/index.js.map +1 -1
- package/dist/events/EventContext.d.ts +5 -0
- package/dist/events/EventContext.d.ts.map +1 -1
- package/dist/events/compensationForDecorator.d.ts +32 -0
- package/dist/events/compensationForDecorator.d.ts.map +1 -0
- package/dist/events/compensationForDecorator.js +44 -0
- package/dist/events/compensationForDecorator.js.map +1 -0
- package/dist/events/eventTypeDecorator.d.ts +7 -0
- package/dist/events/eventTypeDecorator.d.ts.map +1 -1
- package/dist/events/eventTypeDecorator.js +13 -4
- package/dist/events/eventTypeDecorator.js.map +1 -1
- package/dist/events/index.d.ts +2 -1
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +2 -1
- package/dist/events/index.js.map +1 -1
- package/dist/events/toClientEventContext.d.ts.map +1 -1
- package/dist/events/toClientEventContext.js +20 -0
- package/dist/events/toClientEventContext.js.map +1 -1
- package/dist/observation/batches/BatchContext.d.ts +43 -0
- package/dist/observation/batches/BatchContext.d.ts.map +1 -0
- package/dist/observation/batches/BatchContext.js +60 -0
- package/dist/observation/batches/BatchContext.js.map +1 -0
- package/dist/observation/batches/BatchContextObserver.d.ts +14 -0
- package/dist/observation/batches/BatchContextObserver.d.ts.map +1 -0
- package/dist/observation/batches/BatchContextObserver.js +4 -0
- package/dist/observation/batches/BatchContextObserver.js.map +1 -0
- package/dist/observation/batches/BatchContextValues.d.ts +13 -0
- package/dist/observation/batches/BatchContextValues.d.ts.map +1 -0
- package/dist/observation/batches/BatchContextValues.js +4 -0
- package/dist/observation/batches/BatchContextValues.js.map +1 -0
- package/dist/observation/batches/BatchMethod.d.ts +9 -0
- package/dist/observation/batches/BatchMethod.d.ts.map +1 -0
- package/dist/observation/batches/BatchMethod.js +4 -0
- package/dist/observation/batches/BatchMethod.js.map +1 -0
- package/dist/observation/batches/BatchMethodReturnedAValue.d.ts +12 -0
- package/dist/observation/batches/BatchMethodReturnedAValue.d.ts.map +1 -0
- package/dist/observation/batches/BatchMethodReturnedAValue.js +20 -0
- package/dist/observation/batches/BatchMethodReturnedAValue.js.map +1 -0
- package/dist/observation/batches/BatchMethods.d.ts +31 -0
- package/dist/observation/batches/BatchMethods.d.ts.map +1 -0
- package/dist/observation/batches/BatchMethods.js +81 -0
- package/dist/observation/batches/BatchMethods.js.map +1 -0
- package/dist/observation/batches/BatchOutcome.d.ts +15 -0
- package/dist/observation/batches/BatchOutcome.d.ts.map +1 -0
- package/dist/observation/batches/BatchOutcome.js +4 -0
- package/dist/observation/batches/BatchOutcome.js.map +1 -0
- package/dist/observation/batches/BatchOutcomeKind.d.ts +8 -0
- package/dist/observation/batches/BatchOutcomeKind.d.ts.map +1 -0
- package/dist/observation/batches/BatchOutcomeKind.js +11 -0
- package/dist/observation/batches/BatchOutcomeKind.js.map +1 -0
- package/dist/observation/batches/DeferralNotSupportedByKernel.d.ts +13 -0
- package/dist/observation/batches/DeferralNotSupportedByKernel.d.ts.map +1 -0
- package/dist/observation/batches/DeferralNotSupportedByKernel.js +19 -0
- package/dist/observation/batches/DeferralNotSupportedByKernel.js.map +1 -0
- package/dist/observation/batches/DeliveredPartition.d.ts +11 -0
- package/dist/observation/batches/DeliveredPartition.d.ts.map +1 -0
- package/dist/observation/batches/DeliveredPartition.js +4 -0
- package/dist/observation/batches/DeliveredPartition.js.map +1 -0
- package/dist/observation/batches/EventAndContext.d.ts +28 -0
- package/dist/observation/batches/EventAndContext.d.ts.map +1 -0
- package/dist/observation/batches/EventAndContext.js +35 -0
- package/dist/observation/batches/EventAndContext.js.map +1 -0
- package/dist/observation/batches/EventBatch.d.ts +52 -0
- package/dist/observation/batches/EventBatch.d.ts.map +1 -0
- package/dist/observation/batches/EventBatch.js +92 -0
- package/dist/observation/batches/EventBatch.js.map +1 -0
- package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.d.ts +14 -0
- package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.d.ts.map +1 -0
- package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.js +23 -0
- package/dist/observation/batches/EventTypeObservedByMoreThanOneMethod.js.map +1 -0
- package/dist/observation/batches/InvalidBatchMethod.d.ts +13 -0
- package/dist/observation/batches/InvalidBatchMethod.d.ts.map +1 -0
- package/dist/observation/batches/InvalidBatchMethod.js +20 -0
- package/dist/observation/batches/InvalidBatchMethod.js.map +1 -0
- package/dist/observation/batches/ObservationOrigin.d.ts +21 -0
- package/dist/observation/batches/ObservationOrigin.d.ts.map +1 -0
- package/dist/observation/batches/ObservationOrigin.js +32 -0
- package/dist/observation/batches/ObservationOrigin.js.map +1 -0
- package/dist/observation/batches/ObservedEventType.d.ts +11 -0
- package/dist/observation/batches/ObservedEventType.d.ts.map +1 -0
- package/dist/observation/batches/ObservedEventType.js +4 -0
- package/dist/observation/batches/ObservedEventType.js.map +1 -0
- package/dist/observation/batches/PageMessages.d.ts +22 -0
- package/dist/observation/batches/PageMessages.d.ts.map +1 -0
- package/dist/observation/batches/PageMessages.js +32 -0
- package/dist/observation/batches/PageMessages.js.map +1 -0
- package/dist/observation/batches/PartitionFailedInBatch.d.ts +10 -0
- package/dist/observation/batches/PartitionFailedInBatch.d.ts.map +1 -0
- package/dist/observation/batches/PartitionFailedInBatch.js +16 -0
- package/dist/observation/batches/PartitionFailedInBatch.js.map +1 -0
- package/dist/observation/batches/PartitionNotInBatch.d.ts +10 -0
- package/dist/observation/batches/PartitionNotInBatch.d.ts.map +1 -0
- package/dist/observation/batches/PartitionNotInBatch.js +16 -0
- package/dist/observation/batches/PartitionNotInBatch.js.map +1 -0
- package/dist/observation/batches/PartitionOutcome.d.ts +19 -0
- package/dist/observation/batches/PartitionOutcome.d.ts.map +1 -0
- package/dist/observation/batches/PartitionOutcome.js +4 -0
- package/dist/observation/batches/PartitionOutcome.js.map +1 -0
- package/dist/observation/batches/PartitionProblem.d.ts +17 -0
- package/dist/observation/batches/PartitionProblem.d.ts.map +1 -0
- package/dist/observation/batches/PartitionProblem.js +4 -0
- package/dist/observation/batches/PartitionProblem.js.map +1 -0
- package/dist/observation/batches/PartitionSettlement.d.ts +29 -0
- package/dist/observation/batches/PartitionSettlement.d.ts.map +1 -0
- package/dist/observation/batches/PartitionSettlement.js +64 -0
- package/dist/observation/batches/PartitionSettlement.js.map +1 -0
- package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.d.ts +12 -0
- package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.d.ts.map +1 -0
- package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.js +20 -0
- package/dist/observation/batches/ReducerMixesBatchAndPerEventMethods.js.map +1 -0
- package/dist/observation/batches/batchSize.d.ts +42 -0
- package/dist/observation/batches/batchSize.d.ts.map +1 -0
- package/dist/observation/batches/batchSize.js +55 -0
- package/dist/observation/batches/batchSize.js.map +1 -0
- package/dist/observation/batches/createBatchContext.d.ts +13 -0
- package/dist/observation/batches/createBatchContext.d.ts.map +1 -0
- package/dist/observation/batches/createBatchContext.js +27 -0
- package/dist/observation/batches/createBatchContext.js.map +1 -0
- package/dist/observation/batches/observes.d.ts +39 -0
- package/dist/observation/batches/observes.d.ts.map +1 -0
- package/dist/observation/batches/observes.js +63 -0
- package/dist/observation/batches/observes.js.map +1 -0
- package/dist/observation/batches/runConcurrently.d.ts +9 -0
- package/dist/observation/batches/runConcurrently.d.ts.map +1 -0
- package/dist/observation/batches/runConcurrently.js +22 -0
- package/dist/observation/batches/runConcurrently.js.map +1 -0
- package/dist/observation/createHandlerInstanceValidator.d.ts +1 -1
- package/dist/observation/createHandlerInstanceValidator.d.ts.map +1 -1
- package/dist/observation/createHandlerInstanceValidator.js +5 -7
- package/dist/observation/createHandlerInstanceValidator.js.map +1 -1
- package/dist/observation/getInstanceMethods.d.ts +3 -1
- package/dist/observation/getInstanceMethods.d.ts.map +1 -1
- package/dist/observation/getInstanceMethods.js +13 -4
- package/dist/observation/getInstanceMethods.js.map +1 -1
- package/dist/observation/index.d.ts +14 -0
- package/dist/observation/index.d.ts.map +1 -1
- package/dist/observation/index.js +13 -0
- package/dist/observation/index.js.map +1 -1
- package/dist/projections/eventContextPropertyExpression.js +1 -1
- package/dist/projections/eventContextPropertyExpression.js.map +1 -1
- package/dist/reactors/ReactorDispatcher.d.ts +15 -0
- package/dist/reactors/ReactorDispatcher.d.ts.map +1 -1
- package/dist/reactors/ReactorDispatcher.js +25 -2
- package/dist/reactors/ReactorDispatcher.js.map +1 -1
- package/dist/reactors/ReactorObservationTarget.d.ts +45 -0
- package/dist/reactors/ReactorObservationTarget.d.ts.map +1 -0
- package/dist/reactors/ReactorObservationTarget.js +4 -0
- package/dist/reactors/ReactorObservationTarget.js.map +1 -0
- package/dist/reactors/ReactorPageDispatcher.d.ts +33 -0
- package/dist/reactors/ReactorPageDispatcher.d.ts.map +1 -0
- package/dist/reactors/ReactorPageDispatcher.js +293 -0
- package/dist/reactors/ReactorPageDispatcher.js.map +1 -0
- package/dist/reactors/Reactors.d.ts.map +1 -1
- package/dist/reactors/Reactors.js +21 -149
- package/dist/reactors/Reactors.js.map +1 -1
- package/dist/reactors/for_Reactors/given/observe_reactor_messages.d.ts +14 -0
- package/dist/reactors/for_Reactors/given/observe_reactor_messages.d.ts.map +1 -0
- package/dist/reactors/for_Reactors/given/observe_reactor_messages.js +61 -0
- package/dist/reactors/for_Reactors/given/observe_reactor_messages.js.map +1 -0
- package/dist/reactors/index.d.ts +2 -0
- package/dist/reactors/index.d.ts.map +1 -1
- package/dist/reactors/index.js +1 -0
- package/dist/reactors/index.js.map +1 -1
- package/dist/reducers/ReducerEventDispatcher.d.ts +26 -1
- package/dist/reducers/ReducerEventDispatcher.d.ts.map +1 -1
- package/dist/reducers/ReducerEventDispatcher.js +35 -3
- package/dist/reducers/ReducerEventDispatcher.js.map +1 -1
- package/dist/reducers/Reducers.d.ts +2 -0
- package/dist/reducers/Reducers.d.ts.map +1 -1
- package/dist/reducers/Reducers.js +94 -22
- package/dist/reducers/Reducers.js.map +1 -1
- package/dist/reducers/ReductionOutcome.d.ts +27 -3
- package/dist/reducers/ReductionOutcome.d.ts.map +1 -1
- package/dist/reducers/ReductionOutcome.js +51 -7
- package/dist/reducers/ReductionOutcome.js.map +1 -1
- package/dist/reducers/for_Reducers/given/observe_reducer_messages.d.ts +19 -0
- package/dist/reducers/for_Reducers/given/observe_reducer_messages.d.ts.map +1 -0
- package/dist/reducers/for_Reducers/given/observe_reducer_messages.js +61 -0
- package/dist/reducers/for_Reducers/given/observe_reducer_messages.js.map +1 -0
- package/dist/reducers/index.d.ts +2 -0
- package/dist/reducers/index.d.ts.map +1 -1
- package/dist/reducers/index.js +1 -0
- package/dist/reducers/index.js.map +1 -1
- package/dist/schemas/JsonSchema.d.ts +2 -0
- package/dist/schemas/JsonSchema.d.ts.map +1 -1
- package/dist/testing/InProcessEventSequence.d.ts +1 -0
- package/dist/testing/InProcessEventSequence.d.ts.map +1 -1
- package/dist/testing/InProcessEventSequence.js +1 -0
- package/dist/testing/InProcessEventSequence.js.map +1 -1
- package/dist/testing/ReactorScenario.d.ts.map +1 -1
- package/dist/testing/ReactorScenario.js +4 -1
- package/dist/testing/ReactorScenario.js.map +1 -1
- package/dist/testing/ReducerReadModelProcessor.d.ts +1 -0
- package/dist/testing/ReducerReadModelProcessor.d.ts.map +1 -1
- package/dist/testing/ReducerReadModelProcessor.js +45 -0
- package/dist/testing/ReducerReadModelProcessor.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eventSequences/CompensateOptions.ts +10 -0
- package/eventSequences/EventSequence.compensate.spec.ts +72 -0
- package/eventSequences/EventSequence.ts +51 -0
- package/eventSequences/IEventSequence.ts +14 -0
- package/eventSequences/index.ts +1 -0
- package/events/EventContext.ts +6 -0
- package/events/compensationFor.spec.ts +73 -0
- package/events/compensationForDecorator.ts +48 -0
- package/events/eventTypeDecorator.ts +13 -4
- package/events/index.ts +2 -1
- package/events/toClientEventContext.ts +21 -0
- package/observation/batches/BatchContext.ts +75 -0
- package/observation/batches/BatchContextObserver.ts +16 -0
- package/observation/batches/BatchContextValues.ts +16 -0
- package/observation/batches/BatchMethod.ts +13 -0
- package/observation/batches/BatchMethodReturnedAValue.ts +16 -0
- package/observation/batches/BatchMethods.ts +90 -0
- package/observation/batches/BatchOutcome.ts +22 -0
- package/observation/batches/BatchOutcomeKind.ts +11 -0
- package/observation/batches/DeferralNotSupportedByKernel.ts +17 -0
- package/observation/batches/DeliveredPartition.ts +16 -0
- package/observation/batches/EventAndContext.ts +37 -0
- package/observation/batches/EventBatch.ts +105 -0
- package/observation/batches/EventTypeObservedByMoreThanOneMethod.ts +17 -0
- package/observation/batches/InvalidBatchMethod.ts +16 -0
- package/observation/batches/ObservationOrigin.ts +37 -0
- package/observation/batches/ObservedEventType.ts +16 -0
- package/observation/batches/PageMessages.ts +37 -0
- package/observation/batches/PartitionFailedInBatch.ts +14 -0
- package/observation/batches/PartitionNotInBatch.ts +14 -0
- package/observation/batches/PartitionOutcome.ts +22 -0
- package/observation/batches/PartitionProblem.ts +25 -0
- package/observation/batches/PartitionSettlement.ts +70 -0
- package/observation/batches/ReducerMixesBatchAndPerEventMethods.ts +16 -0
- package/observation/batches/batchSize.ts +79 -0
- package/observation/batches/createBatchContext.ts +32 -0
- package/observation/batches/for_BatchMethods/when_discovering_batch_methods.spec.ts +155 -0
- package/observation/batches/for_EventBatch/when_reporting_outcomes.spec.ts +66 -0
- package/observation/batches/for_PartitionSettlement/when_settling_a_partition.spec.ts +31 -0
- package/observation/batches/for_batchSize/when_declaring_bounds.spec.ts +23 -0
- package/observation/batches/for_observes/when_decorating_methods.legacy.spec.ts +28 -0
- package/observation/batches/for_observes/when_decorating_methods.spec.ts +33 -0
- package/observation/batches/for_runConcurrently/when_running_items.spec.ts +22 -0
- package/observation/batches/observes.ts +68 -0
- package/observation/batches/runConcurrently.ts +23 -0
- package/observation/createHandlerInstanceValidator.ts +5 -7
- package/observation/getInstanceMethods.ts +13 -4
- package/observation/index.ts +14 -0
- package/package.json +2 -2
- package/projections/eventContextPropertyExpression.ts +2 -2
- package/reactors/ReactorDispatcher.ts +27 -2
- package/reactors/ReactorObservationTarget.ts +48 -0
- package/reactors/ReactorPageDispatcher.ts +321 -0
- package/reactors/Reactors.ts +22 -164
- package/reactors/for_Reactors/given/observe_reactor_messages.ts +72 -0
- package/reactors/for_Reactors/when_delivering_pages/as_a_legacy_single_partition.spec.ts +51 -0
- package/reactors/for_Reactors/when_delivering_pages/returning_a_value_from_a_batch_method.spec.ts +34 -0
- package/reactors/for_Reactors/when_delivering_pages/with_batch_and_per_event_methods.spec.ts +81 -0
- package/reactors/for_Reactors/when_delivering_pages/with_batch_outcomes.spec.ts +86 -0
- package/reactors/for_Reactors/when_delivering_pages/with_per_event_handlers.spec.ts +52 -0
- package/reactors/index.ts +2 -0
- package/reducers/ReducerEventDispatcher.ts +44 -3
- package/reducers/Reducers.ts +109 -25
- package/reducers/ReductionOutcome.ts +55 -8
- package/reducers/for_Reducers/given/observe_reducer_messages.ts +77 -0
- package/reducers/for_Reducers/when_delivering_pages/to_a_batch_reducer.spec.ts +111 -0
- package/reducers/for_Reducers/when_delivering_pages/to_a_per_event_reducer.spec.ts +53 -0
- package/reducers/index.ts +2 -0
- package/schemas/JsonSchema.ts +2 -0
- package/telemetry/for_ChronicleTelemetry/given/a_client.fixture.ts +1 -0
- package/telemetry/for_ChronicleTelemetry/given/a_span_name_registry.fixture.ts +2 -0
- package/telemetry/for_ChronicleTelemetry/when_naming_spans/with_convention_names.spec.ts +2 -1
- package/testing/InProcessEventSequence.ts +1 -0
- package/testing/ReactorScenario.ts +5 -1
- package/testing/ReducerReadModelProcessor.ts +44 -0
- package/testing/for_ReactorScenario/when_the_reactor_has_a_batch_method.spec.ts +26 -0
- package/testing/for_ReadModelScenario/when_reducing_with_a_batch_method/with_events_for_two_event_sources.spec.ts +43 -0
|
@@ -0,0 +1,33 @@
|
|
|
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, isBatchMethod, 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
|
+
single() {}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('when decorating methods with observes', () => {
|
|
21
|
+
it('should collect every observed event type of a method', () =>
|
|
22
|
+
[...getObservedEventTypes(Observer.prototype.both)].sort((left, right) => left.name.localeCompare(right.name)).should.deep.equal([First, Second]));
|
|
23
|
+
it('should make the method a batch method', () => isBatchMethod(Observer.prototype.both).should.be.true);
|
|
24
|
+
it('should leave other methods alone', () => isBatchMethod(Observer.prototype.single).should.be.false);
|
|
25
|
+
it('should refuse the same event type twice', () => (() => {
|
|
26
|
+
class Duplicate {
|
|
27
|
+
@observes(First)
|
|
28
|
+
@observes(First)
|
|
29
|
+
twice() {}
|
|
30
|
+
}
|
|
31
|
+
return Duplicate;
|
|
32
|
+
}).should.throw(TypeError, "observes 'First' more than once"));
|
|
33
|
+
});
|
|
@@ -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 { chai, describe, it } from 'vitest';
|
|
5
|
+
import { runConcurrently } from '../runConcurrently.js';
|
|
6
|
+
|
|
7
|
+
chai.should();
|
|
8
|
+
|
|
9
|
+
describe('when running items concurrently', () => {
|
|
10
|
+
it('should keep the results in item order and never exceed the limit', async () => {
|
|
11
|
+
let running = 0;
|
|
12
|
+
let highest = 0;
|
|
13
|
+
const results = await runConcurrently([30, 10, 20, 5], 2, async delay => {
|
|
14
|
+
highest = Math.max(highest, ++running);
|
|
15
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
16
|
+
running--;
|
|
17
|
+
return delay * 2;
|
|
18
|
+
});
|
|
19
|
+
results.should.deep.equal([60, 20, 40, 10]);
|
|
20
|
+
highest.should.equal(2);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
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 { Constructor } from '@cratis/fundamentals';
|
|
6
|
+
|
|
7
|
+
const METADATA_KEY = 'chronicle:observation:observes';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Makes a reactor or reducer method a batch method and declares one event type it observes. Apply it once per event
|
|
11
|
+
* type; it is the TypeScript equivalent of the C# `[Observes<TEvent>]` attribute.
|
|
12
|
+
*
|
|
13
|
+
* A batch method receives an {@link EventBatch} of every event of the types it observes, in sequence order, and a
|
|
14
|
+
* {@link BatchContext} describing the page. Each event type reaches exactly one method of an observer: a batch method
|
|
15
|
+
* or a per-event method, never both.
|
|
16
|
+
*
|
|
17
|
+
* @param eventType - The event constructor observed by this method. The event type must be registered with the owning event store.
|
|
18
|
+
* @returns A method decorator for legacy and standard TypeScript decorators.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* @reactor()
|
|
23
|
+
* class Users {
|
|
24
|
+
* @observes(UserRegistered)
|
|
25
|
+
* @observes(UserRenamed)
|
|
26
|
+
* async index(batch: EventBatch, context: BatchContext): Promise<void> {
|
|
27
|
+
* // ...
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function observes(eventType: Constructor): MethodDecorator & ((value: Function, context: ClassMethodDecoratorContext) => void) {
|
|
33
|
+
return (target: object, propertyOrContext: string | symbol | ClassMethodDecoratorContext, descriptor?: PropertyDescriptor) => {
|
|
34
|
+
const isStandard = typeof propertyOrContext === 'object';
|
|
35
|
+
const method = isStandard ? target : descriptor?.value;
|
|
36
|
+
const name = isStandard ? propertyOrContext.name : propertyOrContext;
|
|
37
|
+
if ((isStandard && (propertyOrContext.kind !== 'method' || propertyOrContext.static || propertyOrContext.private)) ||
|
|
38
|
+
(!isStandard && typeof target === 'function') || typeof name !== 'string' || typeof method !== 'function') {
|
|
39
|
+
throw new TypeError('Observes requires a public, string-named instance method.');
|
|
40
|
+
}
|
|
41
|
+
if (typeof eventType !== 'function') {
|
|
42
|
+
throw new TypeError('Observes requires an event constructor.');
|
|
43
|
+
}
|
|
44
|
+
const observed = (Reflect.getOwnMetadata(METADATA_KEY, method) as Constructor[] | undefined) ?? [];
|
|
45
|
+
if (observed.includes(eventType)) {
|
|
46
|
+
throw new TypeError(`Batch method '${name}' observes '${(eventType as Function).name}' more than once.`);
|
|
47
|
+
}
|
|
48
|
+
Reflect.defineMetadata(METADATA_KEY, [...observed, eventType], method);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Gets the event constructors a batch method observes, without inheriting metadata.
|
|
54
|
+
* @param method - The method.
|
|
55
|
+
* @returns The observed event constructors, or an empty array when the method is not a batch method.
|
|
56
|
+
*/
|
|
57
|
+
export function getObservedEventTypes(method: Function): readonly Constructor[] {
|
|
58
|
+
return (Reflect.getOwnMetadata(METADATA_KEY, method) as Constructor[] | undefined) ?? [];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Checks whether a method is a batch method, that is decorated with {@link observes}.
|
|
63
|
+
* @param method - The method.
|
|
64
|
+
* @returns True when it is a batch method.
|
|
65
|
+
*/
|
|
66
|
+
export function isBatchMethod(method: Function): boolean {
|
|
67
|
+
return getObservedEventTypes(method).length > 0;
|
|
68
|
+
}
|
|
@@ -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
|
+
/**
|
|
5
|
+
* Runs a callback for every item, with at most a given number running at the same time, and keeps the results in item order.
|
|
6
|
+
* @param items - The items.
|
|
7
|
+
* @param maxConcurrency - The most callbacks running at the same time; `1` runs them one after another.
|
|
8
|
+
* @param callback - The callback for one item.
|
|
9
|
+
* @returns The results, in the order of the items.
|
|
10
|
+
*/
|
|
11
|
+
export async function runConcurrently<TItem, TResult>(items: readonly TItem[], maxConcurrency: number,
|
|
12
|
+
callback: (item: TItem, index: number) => Promise<TResult>): Promise<TResult[]> {
|
|
13
|
+
const results = new Array<TResult>(items.length);
|
|
14
|
+
let next = 0;
|
|
15
|
+
const worker = async () => {
|
|
16
|
+
while (next < items.length) {
|
|
17
|
+
const index = next++;
|
|
18
|
+
results[index] = await callback(items[index], index);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
await Promise.all(Array.from({ length: Math.max(1, Math.min(maxConcurrency, items.length)) }, worker));
|
|
22
|
+
return results;
|
|
23
|
+
}
|
|
@@ -2,20 +2,18 @@
|
|
|
2
2
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
3
|
|
|
4
4
|
import type { Constructor } from '@cratis/fundamentals';
|
|
5
|
-
import {
|
|
6
|
-
import { getInstanceMethods } from './getInstanceMethods.js';
|
|
5
|
+
import { describeExplicitHandler, getInstanceMethods } from './getInstanceMethods.js';
|
|
7
6
|
|
|
8
|
-
/** Captures @handles prototype methods during preflight and rejects instance properties hiding them, except bound methods. */
|
|
7
|
+
/** Captures @handles and @observes prototype methods during preflight and rejects instance properties hiding them, except bound methods. */
|
|
9
8
|
export function createHandlerInstanceValidator(type: Constructor, names: Iterable<string>, methods = getInstanceMethods(type)):
|
|
10
9
|
(instance: object) => void {
|
|
11
|
-
const handlers = [...new Set(names)].filter(name =>
|
|
10
|
+
const handlers = [...new Set(names)].filter(name => describeExplicitHandler(methods.get(name)!) !== undefined).map(name => {
|
|
12
11
|
const method = methods.get(name)!;
|
|
13
12
|
let declaring = type.prototype;
|
|
14
13
|
while (declaring && Object.getOwnPropertyDescriptor(declaring, name)?.value !== method) {
|
|
15
14
|
declaring = Object.getPrototypeOf(declaring);
|
|
16
15
|
}
|
|
17
|
-
const
|
|
18
|
-
const declaration = `@handles(${(eventType as Function).name})`;
|
|
16
|
+
const declaration = describeExplicitHandler(method)!;
|
|
19
17
|
return { name, method, declaration, declaringType: declaring?.constructor.name ?? type.name };
|
|
20
18
|
});
|
|
21
19
|
return instance => {
|
|
@@ -25,7 +23,7 @@ export function createHandlerInstanceValidator(type: Constructor, names: Iterabl
|
|
|
25
23
|
const value: unknown = current ? Object.getOwnPropertyDescriptor(current, name)?.value : undefined;
|
|
26
24
|
if (current === instance && typeof value === 'function' && isBoundCopyOf(value, method)) continue;
|
|
27
25
|
if (current === instance || !current || value !== method) {
|
|
28
|
-
throw new Error(`Override '${name}' on '${type.name}' hides ${declaration} declared on '${declaringType}'; use a method with @handles instead of an instance field.`);
|
|
26
|
+
throw new Error(`Override '${name}' on '${type.name}' hides ${declaration} declared on '${declaringType}'; use a method with ${declaration.startsWith('@observes') ? '@observes' : '@handles'} instead of an instance field.`);
|
|
29
27
|
}
|
|
30
28
|
}
|
|
31
29
|
};
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
|
|
4
4
|
import type { Constructor } from '@cratis/fundamentals';
|
|
5
5
|
import { getHandledEventType } from '../events/handles.js';
|
|
6
|
+
import { getObservedEventTypes } from './batches/observes.js';
|
|
6
7
|
|
|
7
|
-
/** Finds prototype methods without invoking getters; explicit handlers require explicitly decorated overrides. */
|
|
8
|
+
/** Finds prototype methods without invoking getters; explicit handlers and batch methods require explicitly decorated overrides. */
|
|
8
9
|
export function getInstanceMethods(type: Constructor): Map<string, Function> {
|
|
9
10
|
const methods = new Map<string, Function>();
|
|
10
11
|
const declaringTypes = new Map<string, string>();
|
|
@@ -14,10 +15,10 @@ export function getInstanceMethods(type: Constructor): Map<string, Function> {
|
|
|
14
15
|
const method = Object.getOwnPropertyDescriptor(current, name)?.value;
|
|
15
16
|
const declaringType = declaringTypes.get(name);
|
|
16
17
|
if (declaringType !== undefined) {
|
|
17
|
-
const
|
|
18
|
+
const declaration = typeof method === 'function' ? describeExplicitHandler(method) : undefined;
|
|
18
19
|
const override = methods.get(name);
|
|
19
|
-
if (
|
|
20
|
-
throw new Error(`Override '${name}' on '${declaringType}' hides
|
|
20
|
+
if (declaration && (!override || describeExplicitHandler(override) === undefined)) {
|
|
21
|
+
throw new Error(`Override '${name}' on '${declaringType}' hides ${declaration} declared on '${current.constructor.name}'; redecorate the override.`);
|
|
21
22
|
}
|
|
22
23
|
continue;
|
|
23
24
|
}
|
|
@@ -27,3 +28,11 @@ export function getInstanceMethods(type: Constructor): Map<string, Function> {
|
|
|
27
28
|
}
|
|
28
29
|
return methods;
|
|
29
30
|
}
|
|
31
|
+
|
|
32
|
+
/** Describes the explicit handler decorator of a method, or undefined when it has none. */
|
|
33
|
+
export function describeExplicitHandler(method: Function): string | undefined {
|
|
34
|
+
const eventType = getHandledEventType(method);
|
|
35
|
+
if (eventType) return `@handles(${(eventType as Function).name})`;
|
|
36
|
+
const observed = getObservedEventTypes(method);
|
|
37
|
+
return observed.length > 0 ? observed.map(type => `@observes(${(type as Function).name})`).join(' ') : undefined;
|
|
38
|
+
}
|
package/observation/index.ts
CHANGED
|
@@ -21,3 +21,17 @@ export type { IObservers } from './IObservers.js';
|
|
|
21
21
|
export { Observers } from './Observers.js';
|
|
22
22
|
export type { ICanBeNotifiedWhenReplay } from './ICanBeNotifiedWhenReplay.js';
|
|
23
23
|
export type { ICanBeNotifiedWhenPartitionReplayed } from './ICanBeNotifiedWhenPartitionReplayed.js';
|
|
24
|
+
export { observes } from './batches/observes.js';
|
|
25
|
+
export { batchSize } from './batches/batchSize.js';
|
|
26
|
+
export type { BatchSizeOptions } from './batches/batchSize.js';
|
|
27
|
+
export { EventBatch } from './batches/EventBatch.js';
|
|
28
|
+
export { EventAndContext } from './batches/EventAndContext.js';
|
|
29
|
+
export { BatchContext } from './batches/BatchContext.js';
|
|
30
|
+
export { ObservationOrigin } from './batches/ObservationOrigin.js';
|
|
31
|
+
export { PartitionNotInBatch } from './batches/PartitionNotInBatch.js';
|
|
32
|
+
export { PartitionFailedInBatch } from './batches/PartitionFailedInBatch.js';
|
|
33
|
+
export { DeferralNotSupportedByKernel } from './batches/DeferralNotSupportedByKernel.js';
|
|
34
|
+
export { EventTypeObservedByMoreThanOneMethod } from './batches/EventTypeObservedByMoreThanOneMethod.js';
|
|
35
|
+
export { InvalidBatchMethod } from './batches/InvalidBatchMethod.js';
|
|
36
|
+
export { ReducerMixesBatchAndPerEventMethods } from './batches/ReducerMixesBatchAndPerEventMethods.js';
|
|
37
|
+
export { BatchMethodReturnedAValue } from './batches/BatchMethodReturnedAValue.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cratis/chronicle",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.58.0",
|
|
4
4
|
"description": "Event sourcing for TypeScript and Node.js — the idiomatic client for Cratis Chronicle, the open-source event-sourcing database",
|
|
5
5
|
"author": "Cratis",
|
|
6
6
|
"license": "MIT",
|
|
@@ -178,7 +178,7 @@
|
|
|
178
178
|
},
|
|
179
179
|
"dependencies": {
|
|
180
180
|
"@bufbuild/protobuf": "^2.16.0",
|
|
181
|
-
"@cratis/chronicle.contracts": "19.
|
|
181
|
+
"@cratis/chronicle.contracts": "19.125.2",
|
|
182
182
|
"@grpc/grpc-js": "^1.14.5",
|
|
183
183
|
"@opentelemetry/api": "^1.9.1",
|
|
184
184
|
"glob": "^13.0.6",
|
|
@@ -23,9 +23,9 @@ const contextProperties = {
|
|
|
23
23
|
correlationId: true,
|
|
24
24
|
causation: true,
|
|
25
25
|
tags: true
|
|
26
|
-
// eventSource and
|
|
26
|
+
// eventSource, namedTags and compensationFor are deliberately not projectable: their kernel resolution is not part of the
|
|
27
27
|
// supported expression set.
|
|
28
|
-
} satisfies Record<Exclude<keyof EventContext, 'eventSource' | 'namedTags'>, true>;
|
|
28
|
+
} satisfies Record<Exclude<keyof EventContext, 'eventSource' | 'namedTags' | 'compensationFor'>, true>;
|
|
29
29
|
|
|
30
30
|
// Mirror the kernel's DerivedPropertyFunctions registry: Week() applies to dates only.
|
|
31
31
|
const derivedFunctions: Record<string, string> = { week: 'Week' };
|
|
@@ -11,6 +11,8 @@ import { getEventTypeMetadata } from '../events/eventTypeDecorator.js';
|
|
|
11
11
|
import { getHandledEventType } from '../events/handles.js';
|
|
12
12
|
import { getEventHandlers } from '../observation/getEventHandlers.js';
|
|
13
13
|
import { getInstanceMethods } from '../observation/getInstanceMethods.js';
|
|
14
|
+
import { assertEachEventTypeReachesOneMethod, getBatchMethods, withoutBatchMethods } from '../observation/batches/BatchMethods.js';
|
|
15
|
+
import type { BatchMethod } from '../observation/batches/BatchMethod.js';
|
|
14
16
|
import { isOnceOnly } from './onceOnly.js';
|
|
15
17
|
import { getReplayEventType } from './replay.js';
|
|
16
18
|
import type { ReactorServices } from './ReactorServices.js';
|
|
@@ -24,8 +26,27 @@ export interface ReactorEventTypeEntry {
|
|
|
24
26
|
readonly replayMethodName?: string;
|
|
25
27
|
}
|
|
26
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Discovers the batch methods of a reactor, the methods decorated with \`@observes\`.
|
|
31
|
+
* @param reactorType - The reactor class.
|
|
32
|
+
* @param registeredTypes - The event types registered with the event store.
|
|
33
|
+
* @returns The batch methods.
|
|
34
|
+
*/
|
|
35
|
+
export function getReactorBatchMethods(reactorType: Constructor, registeredTypes: readonly Constructor[]): BatchMethod[] {
|
|
36
|
+
return getBatchMethods(reactorType, registeredTypes);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Discovers the per-event handlers of a reactor and checks that each event type reaches exactly one method,
|
|
41
|
+
* a batch method or a per-event method.
|
|
42
|
+
* @param reactorType - The reactor class.
|
|
43
|
+
* @param registeredTypes - The event types registered with the event store.
|
|
44
|
+
* @returns The per-event handler entries.
|
|
45
|
+
*/
|
|
27
46
|
export function getReactorEventTypes(reactorType: Constructor, registeredTypes: readonly Constructor[]): ReactorEventTypeEntry[] {
|
|
28
|
-
const
|
|
47
|
+
const allMethods = getInstanceMethods(reactorType);
|
|
48
|
+
const batchMethods = getBatchMethods(reactorType, registeredTypes, allMethods);
|
|
49
|
+
const methods = withoutBatchMethods(allMethods, batchMethods);
|
|
29
50
|
const replayHandlers = new Map<string, string>();
|
|
30
51
|
const eventTypes = new Map<string, { eventTypeClass: Function; id: string; generation: number }>();
|
|
31
52
|
|
|
@@ -67,7 +88,11 @@ export function getReactorEventTypes(reactorType: Constructor, registeredTypes:
|
|
|
67
88
|
entries.set(eventType.id, { id: eventType.id, generation: eventType.generation, ...entries.get(eventType.id), replayMethodName });
|
|
68
89
|
}
|
|
69
90
|
}
|
|
70
|
-
|
|
91
|
+
const handlers = [...entries.values()];
|
|
92
|
+
assertEachEventTypeReachesOneMethod(reactorType, batchMethods, handlers.map(handler => ({
|
|
93
|
+
id: handler.id, methodNames: [handler.methodName, handler.replayMethodName].filter((name): name is string => !!name)
|
|
94
|
+
})));
|
|
95
|
+
return handlers;
|
|
71
96
|
}
|
|
72
97
|
|
|
73
98
|
export function selectReactorHandler(entries: readonly ReactorEventTypeEntry[], reactorType: Constructor,
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
import type { ClientArtifactsActivator } from '../artifacts/ClientArtifactsActivator.js';
|
|
6
|
+
import type { IEventLog } from '../eventSequences/IEventLog.js';
|
|
7
|
+
import type { createLogger } from '../logging/createLogger.js';
|
|
8
|
+
import type { IChronicleLogger } from '../logging/IChronicleLogger.js';
|
|
9
|
+
import type { BatchMethod } from '../observation/batches/BatchMethod.js';
|
|
10
|
+
import type { ReactorEventTypeEntry } from './ReactorDispatcher.js';
|
|
11
|
+
import type { ReactorResultHandler } from './ReactorResultHandler.js';
|
|
12
|
+
import type { ReactorServices } from './ReactorServices.js';
|
|
13
|
+
|
|
14
|
+
/** Everything a {@link ReactorPageDispatcher} needs to know about one reactor observation. */
|
|
15
|
+
export interface ReactorObservationTarget {
|
|
16
|
+
/** The reactor identifier. */
|
|
17
|
+
readonly id: string;
|
|
18
|
+
/** The reactor class. */
|
|
19
|
+
readonly reactorType: Constructor;
|
|
20
|
+
/** The event sequence the reactor observes. */
|
|
21
|
+
readonly eventSequenceId: string;
|
|
22
|
+
/** The event store the reactor is registered with. */
|
|
23
|
+
readonly eventStoreName: string;
|
|
24
|
+
/** The namespace the reactor is registered in. */
|
|
25
|
+
readonly namespace: string;
|
|
26
|
+
/** The per-event handlers. */
|
|
27
|
+
readonly entries: readonly ReactorEventTypeEntry[];
|
|
28
|
+
/** The batch methods. */
|
|
29
|
+
readonly batchMethods: readonly BatchMethod[];
|
|
30
|
+
/** Rejects instances whose fields hide decorated methods. */
|
|
31
|
+
readonly validateInstance: (instance: object) => void;
|
|
32
|
+
/** The long-lived reactor instance, or undefined when instances are activated per delivery. */
|
|
33
|
+
readonly reactorInstance: Record<string, Function> | undefined;
|
|
34
|
+
/** The services handed to per-event handlers. */
|
|
35
|
+
readonly services: ReactorServices | undefined;
|
|
36
|
+
/** Aborted when the observation stops. */
|
|
37
|
+
readonly signal: AbortSignal;
|
|
38
|
+
/** The event log side effects returned by per-event handlers are appended to. */
|
|
39
|
+
readonly eventLog: IEventLog;
|
|
40
|
+
/** Optional handler of side-effect results. */
|
|
41
|
+
readonly resultHandler?: ReactorResultHandler;
|
|
42
|
+
/** Optional per-delivery activator. */
|
|
43
|
+
readonly artifactActivator?: ClientArtifactsActivator;
|
|
44
|
+
/** The reactor logger. */
|
|
45
|
+
readonly logger: ReturnType<typeof createLogger>;
|
|
46
|
+
/** The connection logger, for activation diagnostics. */
|
|
47
|
+
readonly connectionLogger?: IChronicleLogger;
|
|
48
|
+
}
|