@cratis/chronicle 6.69.0 → 6.70.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/ChronicleClient.ts +1 -1
- package/ChronicleOptions.ts +9 -0
- package/EventStore.ts +6 -3
- package/dist/ChronicleClient.js +1 -1
- package/dist/ChronicleClient.js.map +1 -1
- package/dist/ChronicleOptions.d.ts +4 -0
- package/dist/ChronicleOptions.d.ts.map +1 -1
- package/dist/ChronicleOptions.js +5 -0
- package/dist/ChronicleOptions.js.map +1 -1
- package/dist/EventStore.d.ts +3 -1
- package/dist/EventStore.d.ts.map +1 -1
- package/dist/EventStore.js +6 -3
- package/dist/EventStore.js.map +1 -1
- package/dist/eventSequences/AppendMetricsContext.d.ts +7 -0
- package/dist/eventSequences/AppendMetricsContext.d.ts.map +1 -0
- package/dist/eventSequences/AppendMetricsContext.js +4 -0
- package/dist/eventSequences/AppendMetricsContext.js.map +1 -0
- package/dist/eventSequences/ConcurrencyScopeStrategies.d.ts +8 -0
- package/dist/eventSequences/ConcurrencyScopeStrategies.d.ts.map +1 -0
- package/dist/eventSequences/ConcurrencyScopeStrategies.js +11 -0
- package/dist/eventSequences/ConcurrencyScopeStrategies.js.map +1 -0
- package/dist/eventSequences/ConcurrencyScopeStrategy.d.ts +11 -0
- package/dist/eventSequences/ConcurrencyScopeStrategy.d.ts.map +1 -0
- package/dist/eventSequences/ConcurrencyScopeStrategy.js +4 -0
- package/dist/eventSequences/ConcurrencyScopeStrategy.js.map +1 -0
- package/dist/eventSequences/ConcurrencyScopeStrategyContext.d.ts +15 -0
- package/dist/eventSequences/ConcurrencyScopeStrategyContext.d.ts.map +1 -0
- package/dist/eventSequences/ConcurrencyScopeStrategyContext.js +4 -0
- package/dist/eventSequences/ConcurrencyScopeStrategyContext.js.map +1 -0
- package/dist/eventSequences/EventLog.d.ts +2 -1
- package/dist/eventSequences/EventLog.d.ts.map +1 -1
- package/dist/eventSequences/EventLog.js +3 -2
- package/dist/eventSequences/EventLog.js.map +1 -1
- package/dist/eventSequences/EventSequence.d.ts +3 -1
- package/dist/eventSequences/EventSequence.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.js +27 -21
- package/dist/eventSequences/EventSequence.js.map +1 -1
- package/dist/eventSequences/index.d.ts +3 -0
- package/dist/eventSequences/index.d.ts.map +1 -1
- package/dist/eventSequences/index.js +1 -0
- package/dist/eventSequences/index.js.map +1 -1
- package/dist/eventSequences/planConcurrencyScopeStrategies.d.ts +12 -0
- package/dist/eventSequences/planConcurrencyScopeStrategies.d.ts.map +1 -0
- package/dist/eventSequences/planConcurrencyScopeStrategies.js +21 -0
- package/dist/eventSequences/planConcurrencyScopeStrategies.js.map +1 -0
- package/dist/eventSequences/resolveConcurrencyScope.d.ts +7 -0
- package/dist/eventSequences/resolveConcurrencyScope.d.ts.map +1 -0
- package/dist/eventSequences/resolveConcurrencyScope.js +16 -0
- package/dist/eventSequences/resolveConcurrencyScope.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eventSequences/AppendMetricsContext.ts +9 -0
- package/eventSequences/ConcurrencyScopeStrategies.ts +10 -0
- package/eventSequences/ConcurrencyScopeStrategy.ts +15 -0
- package/eventSequences/ConcurrencyScopeStrategyContext.ts +18 -0
- package/eventSequences/EventLog.ts +5 -2
- package/eventSequences/EventSequence.ts +29 -24
- package/eventSequences/for_EventSequence/when_resolving_concurrency_scopes/from_client_options.spec.ts +60 -0
- package/eventSequences/for_EventSequence/when_resolving_concurrency_scopes/given.fixture.ts +31 -0
- package/eventSequences/for_EventSequence/when_resolving_concurrency_scopes/strategies.spec.ts +174 -0
- package/eventSequences/index.ts +3 -0
- package/eventSequences/planConcurrencyScopeStrategies.ts +30 -0
- package/eventSequences/resolveConcurrencyScope.ts +23 -0
- package/package.json +1 -1
|
@@ -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 { vi } from 'vitest';
|
|
5
|
+
import type { ChronicleConnection } from '../../../connection/index.js';
|
|
6
|
+
import type { IClientArtifactsProvider } from '../../../artifacts/index.js';
|
|
7
|
+
import { ConcurrencyDimensions, eventSource, eventStream, EventSources } from '../../../eventSources/index.js';
|
|
8
|
+
import { eventType } from '../../../events/index.js';
|
|
9
|
+
import type { IUnitOfWorkManager } from '../../../transactions/index.js';
|
|
10
|
+
import { EventSequence, EventSequenceId, type ConcurrencyScopeStrategy } from '../../index.js';
|
|
11
|
+
|
|
12
|
+
export class Deposited { constructor(readonly amount = 1) {} }
|
|
13
|
+
eventType('ConcurrencyStrategyDeposited')(Deposited);
|
|
14
|
+
|
|
15
|
+
@eventSource({ name: 'Account' })
|
|
16
|
+
@eventStream('Transactions', { concurrency: ConcurrencyDimensions.eventSourceId | ConcurrencyDimensions.eventStreamType | ConcurrencyDimensions.eventStreamId })
|
|
17
|
+
class AccountEventSource {}
|
|
18
|
+
|
|
19
|
+
export const routing = { eventSource: AccountEventSource, eventStream: 'Transactions', streamId: 'tx-1' };
|
|
20
|
+
|
|
21
|
+
export async function create(strategy?: ConcurrencyScopeStrategy, tail = 7n) {
|
|
22
|
+
const append = vi.fn(async (_request: Parameters<ChronicleConnection['eventSequences']['append']>[0]) => ({ Response: { SequenceNumber: 0n } }));
|
|
23
|
+
const appendManyForEventSources = vi.fn(async (_request: Parameters<ChronicleConnection['eventSequences']['appendManyForEventSources']>[0]) => ({ Response: { SequenceNumbers: [0n, 1n] } }));
|
|
24
|
+
const tailSequenceNumber = vi.fn(async (_request: Parameters<ChronicleConnection['eventSequences']['tailSequenceNumber']>[0]) => ({ Data: { SequenceNumber: tail } }));
|
|
25
|
+
const connection = { eventSequences: { append, appendManyForEventSources, tailSequenceNumber } } as unknown as ChronicleConnection;
|
|
26
|
+
const sources = new EventSources('store', connection, { eventSources: [AccountEventSource] } as unknown as IClientArtifactsProvider);
|
|
27
|
+
await sources.discover();
|
|
28
|
+
const sequence = new EventSequence(EventSequenceId.eventLog, 'store', 'ns', connection, {} as IUnitOfWorkManager,
|
|
29
|
+
undefined, undefined, sources, [], strategy);
|
|
30
|
+
return { sequence, append, appendManyForEventSources, tailSequenceNumber };
|
|
31
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
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, vi } from 'vitest';
|
|
5
|
+
import { ConcurrencyScopeStrategies, EventSequenceNumber, type ConcurrencyScopeStrategyContext } from '../../index.js';
|
|
6
|
+
import { create, Deposited, routing } from './given.fixture.js';
|
|
7
|
+
|
|
8
|
+
const should = chai.should();
|
|
9
|
+
|
|
10
|
+
for (const batch of [false, true]) {
|
|
11
|
+
const append = async (sequence: Awaited<ReturnType<typeof create>>['sequence']) => batch
|
|
12
|
+
? sequence.appendMany('a', [new Deposited()], routing)
|
|
13
|
+
: sequence.append('a', new Deposited(), routing);
|
|
14
|
+
|
|
15
|
+
describe(`when resolving ${batch ? 'batch' : 'single'} append concurrency scopes`, () => {
|
|
16
|
+
it('should preserve the default scoped tail and scope when optimistic is selected', async () => {
|
|
17
|
+
const original = await create();
|
|
18
|
+
const optimistic = await create(ConcurrencyScopeStrategies.optimistic);
|
|
19
|
+
await append(original.sequence);
|
|
20
|
+
await append(optimistic.sequence);
|
|
21
|
+
optimistic.tailSequenceNumber.mock.calls.should.deep.equal(original.tailSequenceNumber.mock.calls);
|
|
22
|
+
const tail = optimistic.tailSequenceNumber.mock.calls[0][0];
|
|
23
|
+
tail.EventSourceId.should.equal('a');
|
|
24
|
+
tail.EventStreamType.should.equal('Transactions');
|
|
25
|
+
tail.EventStreamId.should.equal('tx-1');
|
|
26
|
+
tail.EventSourceType.should.equal('');
|
|
27
|
+
if (batch) {
|
|
28
|
+
optimistic.appendManyForEventSources.mock.calls[0][0].ConcurrencyScopes.should.deep.equal(
|
|
29
|
+
original.appendManyForEventSources.mock.calls[0][0].ConcurrencyScopes);
|
|
30
|
+
} else {
|
|
31
|
+
optimistic.append.mock.calls[0][0].ConcurrencyScope.should.deep.equal(original.append.mock.calls[0][0].ConcurrencyScope);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should preserve the first append guard on an empty scoped stream', async () => {
|
|
36
|
+
const c = await create(ConcurrencyScopeStrategies.optimistic, EventSequenceNumber.unset.value);
|
|
37
|
+
await append(c.sequence);
|
|
38
|
+
const scope = batch ? c.appendManyForEventSources.mock.calls[0][0].ConcurrencyScopes[0].Scope
|
|
39
|
+
: c.append.mock.calls[0][0].ConcurrencyScope;
|
|
40
|
+
scope.ExpectsNoMatchingEvent.should.equal(true);
|
|
41
|
+
scope.SequenceNumber.should.equal(EventSequenceNumber.unset.value);
|
|
42
|
+
scope.EventStreamId.should.equal('tx-1');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
for (const strategy of [ConcurrencyScopeStrategies.none, () => undefined, async () => undefined]) {
|
|
46
|
+
it('should send no check and never read the tail when the strategy returns none', async () => {
|
|
47
|
+
const c = await create(strategy);
|
|
48
|
+
await append(c.sequence);
|
|
49
|
+
c.tailSequenceNumber.mock.calls.should.have.length(0);
|
|
50
|
+
const scope = batch ? c.appendManyForEventSources.mock.calls[0][0].ConcurrencyScopes[0].Scope
|
|
51
|
+
: c.append.mock.calls[0][0].ConcurrencyScope;
|
|
52
|
+
// Preserve the existing wire representation of an absent scope (the no-check sentinel).
|
|
53
|
+
scope.SequenceNumber.should.equal(EventSequenceNumber.unset.value);
|
|
54
|
+
scope.ExpectsNoMatchingEvent.should.equal(false);
|
|
55
|
+
scope.EventSourceId.should.equal(false);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (const asynchronous of [false, true]) {
|
|
60
|
+
it(`should use a custom ${asynchronous ? 'async' : 'sync'} scope without default tail reads`, async () => {
|
|
61
|
+
const scope = { sequenceNumber: 3n, eventSourceId: true };
|
|
62
|
+
const strategy = vi.fn((context: ConcurrencyScopeStrategyContext) => {
|
|
63
|
+
context.eventSourceId.should.equal('a');
|
|
64
|
+
context.events.should.have.length(1);
|
|
65
|
+
context.events[0].event.should.be.instanceOf(Deposited);
|
|
66
|
+
context.options.should.equal(routing);
|
|
67
|
+
return asynchronous ? Promise.resolve(scope) : scope;
|
|
68
|
+
});
|
|
69
|
+
const c = await create(strategy);
|
|
70
|
+
await append(c.sequence);
|
|
71
|
+
strategy.mock.calls.should.have.length(1);
|
|
72
|
+
c.tailSequenceNumber.mock.calls.should.have.length(0);
|
|
73
|
+
const sent = batch ? c.appendManyForEventSources.mock.calls[0][0].ConcurrencyScopes[0].Scope
|
|
74
|
+
: c.append.mock.calls[0][0].ConcurrencyScope;
|
|
75
|
+
sent.SequenceNumber.should.equal(3n);
|
|
76
|
+
sent.EventSourceId.should.equal(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it(`should fail closed when a custom strategy ${asynchronous ? 'rejects' : 'throws'}`, async () => {
|
|
80
|
+
const failure = new Error('scope unavailable');
|
|
81
|
+
const c = await create(() => {
|
|
82
|
+
if (asynchronous) return Promise.reject(failure);
|
|
83
|
+
throw failure;
|
|
84
|
+
});
|
|
85
|
+
let caught: unknown;
|
|
86
|
+
try { await append(c.sequence); } catch (error) { caught = error; }
|
|
87
|
+
should.equal(caught, failure);
|
|
88
|
+
c.append.mock.calls.should.have.length(0);
|
|
89
|
+
c.appendManyForEventSources.mock.calls.should.have.length(0);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
describe('when explicit scopes are supplied', () => {
|
|
96
|
+
it('should bypass a custom strategy for a single append', async () => {
|
|
97
|
+
const strategy = vi.fn(() => { throw new Error('must not run'); });
|
|
98
|
+
const c = await create(strategy);
|
|
99
|
+
await c.sequence.append('a', new Deposited(), { ...routing, concurrencyScope: { sequenceNumber: 4n } });
|
|
100
|
+
strategy.mock.calls.should.have.length(0);
|
|
101
|
+
c.tailSequenceNumber.mock.calls.should.have.length(0);
|
|
102
|
+
c.append.mock.calls[0][0].ConcurrencyScope.SequenceNumber.should.equal(4n);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
for (const strategy of [ConcurrencyScopeStrategies.optimistic, ConcurrencyScopeStrategies.none]) {
|
|
106
|
+
it('should preserve explicit scopes under built-in policies', async () => {
|
|
107
|
+
const c = await create(strategy);
|
|
108
|
+
await c.sequence.append('a', new Deposited(), { ...routing, concurrencyScope: { sequenceNumber: 4n } });
|
|
109
|
+
await c.sequence.appendMany('a', [new Deposited()], { ...routing, concurrencyScope: { sequenceNumber: 4n } });
|
|
110
|
+
c.tailSequenceNumber.mock.calls.should.have.length(0);
|
|
111
|
+
c.append.mock.calls[0][0].ConcurrencyScope.SequenceNumber.should.equal(4n);
|
|
112
|
+
c.appendManyForEventSources.mock.calls[0][0].ConcurrencyScopes[0].Scope.SequenceNumber.should.equal(4n);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
it('should resolve only unscoped ids in a mixed batch and group their events', async () => {
|
|
117
|
+
const strategy = vi.fn(async (context: ConcurrencyScopeStrategyContext) => {
|
|
118
|
+
context.events.should.have.length(2);
|
|
119
|
+
const tail = await context.readTail(context.eventSourceId);
|
|
120
|
+
return { sequenceNumber: tail.value, eventSourceId: true };
|
|
121
|
+
});
|
|
122
|
+
const c = await create(strategy);
|
|
123
|
+
await c.sequence.appendMany([
|
|
124
|
+
{ eventSourceId: 'explicit', event: new Deposited() },
|
|
125
|
+
{ eventSourceId: 'derived', event: new Deposited() },
|
|
126
|
+
{ eventSourceId: 'derived', event: new Deposited() }
|
|
127
|
+
], { ...routing, concurrencyScopes: { explicit: { sequenceNumber: 2n } } });
|
|
128
|
+
strategy.mock.calls.should.have.length(1);
|
|
129
|
+
strategy.mock.calls[0][0].eventSourceId.should.equal('derived');
|
|
130
|
+
const scopes = c.appendManyForEventSources.mock.calls[0][0].ConcurrencyScopes;
|
|
131
|
+
scopes.map(entry => [entry.EventSourceId, entry.Scope.SequenceNumber]).should.deep.equal([['explicit', 2n], ['derived', 7n]]);
|
|
132
|
+
c.tailSequenceNumber.mock.calls.should.have.length(1);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should bypass the custom strategy when every id has an explicit shared scope', async () => {
|
|
136
|
+
const strategy = vi.fn(() => { throw new Error('must not run'); });
|
|
137
|
+
const c = await create(strategy);
|
|
138
|
+
await c.sequence.appendMany([
|
|
139
|
+
{ eventSourceId: 'a', event: new Deposited() },
|
|
140
|
+
{ eventSourceId: 'b', event: new Deposited() }
|
|
141
|
+
], { ...routing, concurrencyScope: { sequenceNumber: 4n } });
|
|
142
|
+
strategy.mock.calls.should.have.length(0);
|
|
143
|
+
c.appendManyForEventSources.mock.calls[0][0].ConcurrencyScopes.map(entry => entry.Scope.SequenceNumber).should.deep.equal([4n, 4n]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should not append a partly resolved batch when a later strategy call fails', async () => {
|
|
147
|
+
const failure = new Error('second scope unavailable');
|
|
148
|
+
const c = await create(async context => {
|
|
149
|
+
if (context.eventSourceId === 'b') throw failure;
|
|
150
|
+
return { sequenceNumber: 2n };
|
|
151
|
+
});
|
|
152
|
+
let caught: unknown;
|
|
153
|
+
try {
|
|
154
|
+
await c.sequence.appendMany([
|
|
155
|
+
{ eventSourceId: 'a', event: new Deposited() },
|
|
156
|
+
{ eventSourceId: 'b', event: new Deposited() }
|
|
157
|
+
]);
|
|
158
|
+
} catch (error) { caught = error; }
|
|
159
|
+
should.equal(caught, failure);
|
|
160
|
+
c.appendManyForEventSources.mock.calls.should.have.length(0);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should retain explicit ids and derive the rest using optimistic routing', async () => {
|
|
164
|
+
const c = await create();
|
|
165
|
+
await c.sequence.appendMany([
|
|
166
|
+
{ eventSourceId: 'explicit', event: new Deposited() },
|
|
167
|
+
{ eventSourceId: 'derived', event: new Deposited() }
|
|
168
|
+
], { ...routing, concurrencyScopes: { explicit: { sequenceNumber: 2n } } });
|
|
169
|
+
const scopes = c.appendManyForEventSources.mock.calls[0][0].ConcurrencyScopes;
|
|
170
|
+
scopes.map(entry => [entry.EventSourceId, entry.Scope.SequenceNumber]).should.deep.equal([['explicit', 2n], ['derived', 7n]]);
|
|
171
|
+
c.tailSequenceNumber.mock.calls.should.have.length(1);
|
|
172
|
+
c.tailSequenceNumber.mock.calls[0][0].EventSourceId.should.equal('derived');
|
|
173
|
+
});
|
|
174
|
+
});
|
package/eventSequences/index.ts
CHANGED
|
@@ -16,6 +16,9 @@ export { CompleteStreamError } from './CompleteStreamError.js';
|
|
|
16
16
|
export { NamedTagsWithRegisteredEventSourceNotSupported } from './NamedTagsWithRegisteredEventSourceNotSupported.js';
|
|
17
17
|
export type { CompleteStreamResult } from './CompleteStreamResult.js';
|
|
18
18
|
export type { ConcurrencyScope } from './ConcurrencyScope.js';
|
|
19
|
+
export { ConcurrencyScopeStrategies } from './ConcurrencyScopeStrategies.js';
|
|
20
|
+
export type { ConcurrencyScopeStrategy } from './ConcurrencyScopeStrategy.js';
|
|
21
|
+
export type { ConcurrencyScopeStrategyContext } from './ConcurrencyScopeStrategyContext.js';
|
|
19
22
|
export type { EventForEventSourceId } from './EventForEventSourceId.js';
|
|
20
23
|
export type { WaitForCompletionResult } from './WaitForCompletionResult.js';
|
|
21
24
|
export type { WaitForCompletionOptions } from './WaitForCompletionOptions.js';
|
|
@@ -0,0 +1,30 @@
|
|
|
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 { ResolvedEventRouting } from '../eventSources/ResolvedEventRouting.js';
|
|
5
|
+
import { ConcurrencyScopeStrategies } from './ConcurrencyScopeStrategies.js';
|
|
6
|
+
import type { ConcurrencyScopeStrategy } from './ConcurrencyScopeStrategy.js';
|
|
7
|
+
import type { EventForEventSourceId } from './EventForEventSourceId.js';
|
|
8
|
+
import type { PreparedBatchAppend } from './PreparedBatchAppend.js';
|
|
9
|
+
import { planDerivedGuards, type RequiredGuard } from './resolveEventSourceRouting.js';
|
|
10
|
+
|
|
11
|
+
/** Plans one scope per event source id, retaining optimistic guard validation and tail-read order. */
|
|
12
|
+
export function planConcurrencyScopeStrategies(
|
|
13
|
+
strategy: ConcurrencyScopeStrategy,
|
|
14
|
+
prepared: PreparedBatchAppend,
|
|
15
|
+
routings: Array<ResolvedEventRouting | undefined>
|
|
16
|
+
) {
|
|
17
|
+
const { eventsForEventSourceIds, appendOptions, concurrencyScopes } = prepared;
|
|
18
|
+
const explicitIds = new Set([...concurrencyScopes].filter(([, scope]) => scope !== undefined).map(([id]) => id));
|
|
19
|
+
const guards = strategy === ConcurrencyScopeStrategies.optimistic
|
|
20
|
+
? planDerivedGuards(routings, eventsForEventSourceIds, appendOptions?.streamId, explicitIds)
|
|
21
|
+
: new Map<string, RequiredGuard>();
|
|
22
|
+
const groups = new Map<string, EventForEventSourceId[]>();
|
|
23
|
+
for (const entry of eventsForEventSourceIds) {
|
|
24
|
+
const group = groups.get(entry.eventSourceId) ?? [];
|
|
25
|
+
group.push(entry);
|
|
26
|
+
groups.set(entry.eventSourceId, group);
|
|
27
|
+
}
|
|
28
|
+
const ids = strategy === ConcurrencyScopeStrategies.optimistic ? [...guards.keys()] : [...groups.keys()];
|
|
29
|
+
return ids.map(eventSourceId => ({ eventSourceId, events: groups.get(eventSourceId)!, guard: guards.get(eventSourceId) }));
|
|
30
|
+
}
|
|
@@ -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 type { ConcurrencyScope } from './ConcurrencyScope.js';
|
|
5
|
+
import { ConcurrencyScopeStrategies } from './ConcurrencyScopeStrategies.js';
|
|
6
|
+
import type { ConcurrencyScopeStrategy } from './ConcurrencyScopeStrategy.js';
|
|
7
|
+
import type { ConcurrencyScopeStrategyContext } from './ConcurrencyScopeStrategyContext.js';
|
|
8
|
+
import { deriveConcurrencyScope, type RequiredGuard } from './resolveEventSourceRouting.js';
|
|
9
|
+
|
|
10
|
+
/** Resolves a single or batch append's scope, always preserving an explicit caller scope. */
|
|
11
|
+
export async function resolveConcurrencyScope(
|
|
12
|
+
strategy: ConcurrencyScopeStrategy,
|
|
13
|
+
explicitScope: ConcurrencyScope | undefined,
|
|
14
|
+
context: ConcurrencyScopeStrategyContext,
|
|
15
|
+
guard?: RequiredGuard
|
|
16
|
+
): Promise<ConcurrencyScope | undefined> {
|
|
17
|
+
if (explicitScope !== undefined) return explicitScope;
|
|
18
|
+
if (typeof strategy === 'function') return await strategy(context);
|
|
19
|
+
if (strategy === ConcurrencyScopeStrategies.optimistic && guard) {
|
|
20
|
+
return await deriveConcurrencyScope(guard.routing, context.eventSourceId, guard.streamId, context.readTail);
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
package/package.json
CHANGED