@sincpro/client-framework 1.2.0 → 1.4.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 (55) hide show
  1. package/AGENTS.md +57 -27
  2. package/MANIFESTO.md +5 -4
  3. package/README.md +225 -157
  4. package/dist/client/services/propagation.js +4 -3
  5. package/dist/codegen/services/scaffold.d.ts +12 -10
  6. package/dist/codegen/services/scaffold.js +23 -40
  7. package/dist/core/domain/bus.d.ts +5 -3
  8. package/dist/core/domain/bus.js +8 -1
  9. package/dist/core/domain/consumers.d.ts +19 -0
  10. package/dist/core/domain/consumers.js +1 -0
  11. package/dist/core/domain/dto.d.ts +68 -6
  12. package/dist/core/domain/dto.js +22 -2
  13. package/dist/core/domain/pipeline.d.ts +7 -3
  14. package/dist/core/domain/services.d.ts +97 -14
  15. package/dist/core/domain/services.js +65 -6
  16. package/dist/core/entrypoint/bounded-context.d.ts +86 -35
  17. package/dist/core/entrypoint/bounded-context.js +224 -71
  18. package/dist/core/entrypoint/screens.js +0 -5
  19. package/dist/core/index.d.ts +6 -3
  20. package/dist/core/index.js +4 -3
  21. package/dist/core/infrastructure/listeners.d.ts +5 -4
  22. package/dist/core/infrastructure/listeners.js +1 -1
  23. package/dist/core/services/dto-keys.d.ts +44 -0
  24. package/dist/core/services/dto-keys.js +97 -0
  25. package/dist/core/services/event-handlers.d.ts +57 -0
  26. package/dist/core/services/event-handlers.js +114 -0
  27. package/dist/core/services/execution.d.ts +15 -21
  28. package/dist/core/services/execution.js +38 -32
  29. package/dist/core/services/interceptors.d.ts +2 -2
  30. package/dist/core/services/interceptors.js +5 -5
  31. package/dist/core/services/listening.d.ts +6 -8
  32. package/dist/core/services/listening.js +3 -2
  33. package/dist/event_driven/adapters/stored-queue.d.ts +5 -5
  34. package/dist/event_driven/adapters/stored-queue.js +5 -10
  35. package/dist/event_driven/domain/envelope.d.ts +2 -2
  36. package/dist/event_driven/domain/envelope.js +7 -1
  37. package/dist/event_driven/domain/messages.d.ts +34 -0
  38. package/dist/event_driven/domain/messages.js +1 -0
  39. package/dist/event_driven/domain/queue.d.ts +9 -5
  40. package/dist/event_driven/entrypoint/publisher.d.ts +2 -2
  41. package/dist/event_driven/entrypoint/publisher.js +4 -0
  42. package/dist/event_driven/entrypoint/subscriber.d.ts +122 -29
  43. package/dist/event_driven/entrypoint/subscriber.js +277 -33
  44. package/dist/event_driven/index.d.ts +11 -10
  45. package/dist/event_driven/index.js +9 -9
  46. package/dist/event_driven/services/messages.d.ts +41 -0
  47. package/dist/event_driven/services/messages.js +147 -0
  48. package/dist/observability/domain/logging.d.ts +4 -8
  49. package/dist/observability/services/logging.d.ts +3 -3
  50. package/dist/observability/services/logging.js +1 -1
  51. package/dist/observability/services/trace-context.d.ts +10 -0
  52. package/dist/observability/services/trace-context.js +29 -0
  53. package/package.json +2 -2
  54. package/dist/event_driven/entrypoint/subscriptions.d.ts +0 -110
  55. package/dist/event_driven/entrypoint/subscriptions.js +0 -184
@@ -8,8 +8,8 @@ import type { InterceptedRun, Interceptor } from "../domain/pipeline.js";
8
8
  * runAround([<passes another class on>], …) -> TypeError, naming both classes
9
9
  *
10
10
  * 1. Each interceptor gets a `next` bound to the position after it.
11
- * 2. What reaches `next` must be of the class the call started with: the bus routes by class,
12
- * so another class would be another use case under this one's name.
11
+ * 2. What reaches `next` must be the DTO the call started with — its class, or its generated
12
+ * identity: the bus routes by it, so another would be another use case under this one's name.
13
13
  * Final: the outermost interceptor's answer — the service's, when none answers by itself.
14
14
  */
15
15
  export declare function runAround(interceptors: readonly Interceptor[], handler: (executable: Executable) => Promise<unknown>, executable: Executable, run: InterceptedRun): Promise<unknown>;
@@ -1,3 +1,4 @@
1
+ import { describeDto, sameDto } from "./dto-keys.js";
1
2
  /**
2
3
  * `handler` run on `executable`, wrapped by `interceptors` — the first the outermost.
3
4
  *
@@ -6,15 +7,14 @@
6
7
  * runAround([<passes another class on>], …) -> TypeError, naming both classes
7
8
  *
8
9
  * 1. Each interceptor gets a `next` bound to the position after it.
9
- * 2. What reaches `next` must be of the class the call started with: the bus routes by class,
10
- * so another class would be another use case under this one's name.
10
+ * 2. What reaches `next` must be the DTO the call started with — its class, or its generated
11
+ * identity: the bus routes by it, so another would be another use case under this one's name.
11
12
  * Final: the outermost interceptor's answer — the service's, when none answers by itself.
12
13
  */
13
14
  export function runAround(interceptors, handler, executable, run) {
14
- const started = Object.getPrototypeOf(executable)?.constructor;
15
15
  const at = async (position, current) => {
16
- if (Object.getPrototypeOf(current)?.constructor !== started) {
17
- throw new TypeError(`An interceptor of ${run.useCase} passed on a ${current?.constructor?.name} where it received a ${started?.name}`);
16
+ if (current !== executable && !sameDto(current, executable)) {
17
+ throw new TypeError(`An interceptor of ${run.useCase} passed on a ${describeDto(current)} where it received a ${describeDto(executable)}`);
18
18
  }
19
19
  const interceptor = interceptors[position];
20
20
  if (interceptor === undefined)
@@ -1,17 +1,15 @@
1
- import type { Executable } from "../domain/dto.js";
1
+ import type { DtoKey, Executable } from "../domain/dto.js";
2
2
  import type { Listener } from "../domain/pipeline.js";
3
- /** A class a service, an interceptor or a listener is for. */
4
- export type ExecutableClass = abstract new (...args: never[]) => Executable;
5
- /** An interceptor or a listener, and the classes it is for — every class when it names none. */
3
+ /** An interceptor or a listener, and the DTOs it is for — every one when it names none. */
6
4
  export type Hearing<F> = {
7
5
  readonly what: F;
8
- readonly dtos: readonly ExecutableClass[];
6
+ readonly dtos: readonly DtoKey[];
9
7
  };
10
- /** Whether something registered for `dtos` concerns `executable`: by its exact class. */
11
- export declare function concerns(dtos: readonly ExecutableClass[], executable: Executable): boolean;
8
+ /** Whether something registered for `dtos` concerns `executable`: its exact class, or its generated identity. */
9
+ export declare function concerns(dtos: readonly DtoKey[], executable: Executable): boolean;
12
10
  /** Each listener that hears it, in turn; one that fails is ignored. */
13
11
  export declare function announceTo<E extends {
14
12
  executable: Executable;
15
13
  }>(hearing: ReadonlySet<Hearing<Listener<E>>>, heard: E): void;
16
14
  /** `listener` added to `hearing` for `dtos`; answers how to stop. */
17
- export declare function listenIn<E>(hearing: Set<Hearing<Listener<E>>>, listener: Listener<E>, dtos: readonly ExecutableClass[]): () => void;
15
+ export declare function listenIn<E>(hearing: Set<Hearing<Listener<E>>>, listener: Listener<E>, dtos: readonly DtoKey[]): () => void;
@@ -1,6 +1,7 @@
1
- /** Whether something registered for `dtos` concerns `executable`: by its exact class. */
1
+ import { matches } from "./dto-keys.js";
2
+ /** Whether something registered for `dtos` concerns `executable`: its exact class, or its generated identity. */
2
3
  export function concerns(dtos, executable) {
3
- return dtos.length === 0 || dtos.includes(Object.getPrototypeOf(executable)?.constructor);
4
+ return dtos.length === 0 || dtos.some((dto) => matches(dto, executable));
4
5
  }
5
6
  /** Each listener that hears it, in turn; one that fails is ignored. */
6
7
  export function announceTo(hearing, heard) {
@@ -1,6 +1,6 @@
1
1
  import { type JsonValue } from "../../common/serialization.js";
2
- import type { DomainEvent, EventClass } from "../../core/domain/dto.js";
3
- import type { EventQueue, Subscribes } from "../domain/queue.js";
2
+ import type { DomainEvent, RemoteEvent } from "../../core/domain/dto.js";
3
+ import type { EventQueue, EventType, Subscribes } from "../domain/queue.js";
4
4
  import type { EventStore, StoredEvent } from "../domain/store.js";
5
5
  export type StoredQueueOptions = {
6
6
  /** Where the events wait: memory, a phone's SQLite, a browser's IndexedDB. */
@@ -12,7 +12,7 @@ export type StoredQueueOptions = {
12
12
  * has one, else its values set on an instance of it — as JSON gave them: an event that carries
13
13
  * a `Money`, a `Decimal` or a date declares `fromJSON` to read them back as such.
14
14
  */
15
- readonly rebuild?: (type: EventClass<DomainEvent>, payload: JsonValue) => DomainEvent;
15
+ readonly rebuild?: (type: EventType, payload: JsonValue) => DomainEvent | RemoteEvent;
16
16
  /**
17
17
  * Whether an event may be delivered now — a phone asks for a connection, or a session. One
18
18
  * that is not waits while the rest go on; `drain()` again when what it waits for comes back.
@@ -61,8 +61,8 @@ export type StoredQueue = EventQueue & {
61
61
  *
62
62
  * Delivery is at least once: a process that stops after a context executed an event and before
63
63
  * the store saved it executes it there again on the next drain. What must not act twice keys what
64
- * it does by the envelope's id — `this.execution.causationId` in a use case of the event, or in an
65
- * `EventSubscriber`, which hands it to the Command it executes — or by the entity the event names.
64
+ * it does by the envelope's id — `this.execution.causationId` in the use case of the event — or
65
+ * by the entity the event names.
66
66
  * And one process drains a store: two tabs over one IndexedDB claim it first (the Web Locks API).
67
67
  */
68
68
  export declare function storedQueue(options: StoredQueueOptions): StoredQueue;
@@ -1,12 +1,7 @@
1
1
  import { failureOf } from "../../common/failures.js";
2
- import { readValues, rebuilt, valuesOf, } from "../../common/serialization.js";
2
+ import { valuesOf } from "../../common/serialization.js";
3
+ import { eventFrom } from "../services/messages.js";
3
4
  import { loggerAdapter } from "../../observability/services/logging.js";
4
- function rebuildEvent(type, payload) {
5
- const values = readValues(payload);
6
- if (typeof type.fromJSON === "function")
7
- return rebuilt(values, type);
8
- return Object.assign(Object.create(type.prototype), values);
9
- }
10
5
  function described(boundedContext, error) {
11
6
  const failure = failureOf(error);
12
7
  return {
@@ -42,8 +37,8 @@ function described(boundedContext, error) {
42
37
  *
43
38
  * Delivery is at least once: a process that stops after a context executed an event and before
44
39
  * the store saved it executes it there again on the next drain. What must not act twice keys what
45
- * it does by the envelope's id — `this.execution.causationId` in a use case of the event, or in an
46
- * `EventSubscriber`, which hands it to the Command it executes — or by the entity the event names.
40
+ * it does by the envelope's id — `this.execution.causationId` in the use case of the event — or
41
+ * by the entity the event names.
47
42
  * And one process drains a store: two tabs over one IndexedDB claim it first (the Web Locks API).
48
43
  */
49
44
  export function storedQueue(options) {
@@ -53,7 +48,7 @@ export function storedQueue(options) {
53
48
  const maxAttempts = options.maxAttempts ?? 10;
54
49
  if (!(maxAttempts >= 1))
55
50
  throw new TypeError("A stored queue needs one attempt at least");
56
- const rebuild = options.rebuild ?? rebuildEvent;
51
+ const rebuild = options.rebuild ?? eventFrom;
57
52
  let draining;
58
53
  let again = false;
59
54
  let timer;
@@ -1,4 +1,4 @@
1
- import type { DomainEvent } from "../../core/domain/dto.js";
1
+ import { type DomainEvent, type RemoteEvent } from "../../core/domain/dto.js";
2
2
  import type { Cause } from "../../core/domain/pipeline.js";
3
3
  /**
4
4
  * What travels beside a published event: who it is, which flow it belongs to and what caused it
@@ -25,4 +25,4 @@ export type Envelope = Cause & {
25
25
  * The envelope of `event`, published by the run `cause` names — or the head of its own flow when
26
26
  * nothing caused it (its `correlationId` is its own `id`).
27
27
  */
28
- export declare function envelopeOf(event: DomainEvent, cause?: Cause): Envelope;
28
+ export declare function envelopeOf(event: DomainEvent | RemoteEvent, cause?: Cause): Envelope;
@@ -1,13 +1,17 @@
1
1
  import { newEntityId } from "../../common/ids.js";
2
+ import { EVENT_NAME, } from "../../core/domain/dto.js";
2
3
  /**
3
4
  * The envelope of `event`, published by the run `cause` names — or the head of its own flow when
4
5
  * nothing caused it (its `correlationId` is its own `id`).
5
6
  */
6
7
  export function envelopeOf(event, cause) {
7
8
  const name = event.name;
8
- if (name.trim() === "") {
9
+ if (typeof name !== "string" || name.trim() === "") {
9
10
  throw new Error(`${event.constructor.name} has no eventName: an event is published by its wire name`);
10
11
  }
12
+ if (!EVENT_NAME.test(name)) {
13
+ throw new Error(`${name} is not an event's name: <context>.<aggregate>.vN.<fact>`);
14
+ }
11
15
  const id = newEntityId();
12
16
  return {
13
17
  name,
@@ -15,5 +19,7 @@ export function envelopeOf(event, cause) {
15
19
  correlationId: cause?.correlationId ?? id,
16
20
  causationId: cause?.id ?? null,
17
21
  publishedAt: new Date().toISOString(),
22
+ ...(cause?.traceparent ? { traceparent: cause.traceparent } : {}),
23
+ ...(cause?.tracestate ? { tracestate: cause.tracestate } : {}),
18
24
  };
19
25
  }
@@ -0,0 +1,34 @@
1
+ import type { JsonValue } from "../../common/serialization.js";
2
+ /**
3
+ * A Command or an event as it travels — CloudEvents 1.0, as sincpro_framework writes it on every
4
+ * queue (`Envelope`): `type` says what it is — an event's `name`, a Command's identity
5
+ * (`<context>.<Command>`) —, `data` is the DTO as JSON, the chain and the trace ride beside it.
6
+ *
7
+ * { specversion: "1.0", id: "0193…", source: "urn:svc:billing", type: "billing.invoice.v1.issued",
8
+ * time: "2026-11-11T03:00:00Z", correlationid: "0192…", causationid: "0191…",
9
+ * traceparent: "00-…-01", datacontenttype: "application/json", data: { name: …, invoice_id: … } }
10
+ *
11
+ * Context: one shape for both; what differs is the route — an event to every bus that registered
12
+ * its name, a Command to the one bus that registered its identity, and only when declared. A
13
+ * broker's binary mode carries the same attributes as `ce_` headers (`readMessage`).
14
+ */
15
+ export type Message = {
16
+ readonly specversion: "1.0";
17
+ readonly id: string;
18
+ readonly source: string;
19
+ readonly type: string;
20
+ readonly subject?: string;
21
+ /** When it was produced, ISO. */
22
+ readonly time: string;
23
+ readonly correlationid: string;
24
+ readonly causationid?: string;
25
+ readonly traceparent?: string;
26
+ readonly tracestate?: string;
27
+ readonly datacontenttype: "application/json";
28
+ readonly data: JsonValue;
29
+ };
30
+ /** A message in a broker's binary mode: its attributes as headers, its data as the body. */
31
+ export type BinaryMessage = {
32
+ readonly headers: Readonly<Record<string, string | Uint8Array | null | undefined>>;
33
+ readonly body: string | JsonValue;
34
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import type { DomainEvent, EventClass } from "../../core/domain/dto.js";
1
+ import type { AnyCommand, CommandClass, CommandDescriptor, DomainEvent, EventClass, EventDescriptor, RemoteEvent } from "../../core/domain/dto.js";
2
2
  import type { Envelope } from "./envelope.js";
3
3
  /**
4
4
  * What carries a published event to whoever hears it — the port each platform implements with
@@ -15,8 +15,12 @@ import type { Envelope } from "./envelope.js";
15
15
  * every bounded context that hears it.
16
16
  */
17
17
  export interface EventQueue {
18
- put(event: DomainEvent, envelope: Envelope): void | Promise<void>;
18
+ put(event: DomainEvent | RemoteEvent, envelope: Envelope): void | Promise<void>;
19
19
  }
20
+ /** What an event of a wire name is rebuilt as: its class, or the generated entry it was registered with. */
21
+ export type EventType = EventClass<DomainEvent> | EventDescriptor;
22
+ /** What a Command of an identity is rebuilt as: its class, or the generated entry it was registered with. */
23
+ export type CommandType = CommandClass<AnyCommand> | CommandDescriptor;
20
24
  /** What one bounded context answered, or failed with, for a delivered event. */
21
25
  export type Handled = {
22
26
  readonly boundedContext: string;
@@ -30,11 +34,11 @@ export type Handled = {
30
34
  * rebuilt as, and the event executed by every subscriber.
31
35
  */
32
36
  export interface Subscribes {
33
- /** The class an event of that wire name is rebuilt as, or `undefined` when nobody hears it. */
34
- eventTypeOf(name: string): EventClass<DomainEvent> | undefined;
37
+ /** What an event of that wire name is rebuilt as, or `undefined` when nobody hears it. */
38
+ eventTypeOf(name: string): EventType | undefined;
35
39
  /**
36
40
  * The event executed in every bounded context that hears it — but those `skip` names, which
37
41
  * already have it — and what each answered.
38
42
  */
39
- handle(event: DomainEvent, envelope?: Envelope, skip?: readonly string[]): Promise<Handled[]>;
43
+ handle(event: DomainEvent | RemoteEvent, envelope?: Envelope, skip?: readonly string[]): Promise<Handled[]>;
40
44
  }
@@ -1,5 +1,5 @@
1
1
  import { BIND, type Execution } from "../../context/domain/execution.js";
2
- import type { DomainEvent } from "../../core/domain/dto.js";
2
+ import type { DomainEvent, RemoteEvent } from "../../core/domain/dto.js";
3
3
  import type { Cause } from "../../core/domain/pipeline.js";
4
4
  import { type Envelope } from "../domain/envelope.js";
5
5
  import type { EventQueue } from "../domain/queue.js";
@@ -20,7 +20,7 @@ export declare class Publisher {
20
20
  private readonly cause?;
21
21
  constructor(queue: EventQueue, cause?: Cause | undefined);
22
22
  /** `event` into the queue, with its envelope; answers the envelope once the queue took it. */
23
- publish(event: DomainEvent): Promise<Envelope>;
23
+ publish(event: DomainEvent | RemoteEvent): Promise<Envelope>;
24
24
  /** The same publisher, publishing as `execution`: its events join that run's flow. */
25
25
  [BIND](execution: Execution): Publisher;
26
26
  }
@@ -1,5 +1,6 @@
1
1
  import { BIND } from "../../context/domain/execution.js";
2
2
  import { envelopeOf, } from "../domain/envelope.js";
3
+ import { traceparentOf } from "../../observability/services/trace-context.js";
3
4
  /**
4
5
  * Puts events into a queue — what a Feature holds to tell whoever listens, as the backend's
5
6
  * `Publisher`:
@@ -27,9 +28,12 @@ export class Publisher {
27
28
  }
28
29
  /** The same publisher, publishing as `execution`: its events join that run's flow. */
29
30
  [BIND](execution) {
31
+ const traceparent = traceparentOf(execution.span);
30
32
  return new Publisher(this.queue, {
31
33
  id: execution.id,
32
34
  correlationId: execution.correlationId,
35
+ ...(traceparent ? { traceparent } : {}),
36
+ ...(execution.span.state ? { tracestate: execution.span.state } : {}),
33
37
  });
34
38
  }
35
39
  }
@@ -1,42 +1,135 @@
1
- import type { DomainEvent, EventClass } from "../../core/domain/dto.js";
2
- import type { Cause } from "../../core/domain/pipeline.js";
1
+ import type { EventConsumer, EventKey } from "../../core/domain/consumers.js";
2
+ import type { AnyCommand, CommandClass, CommandDescriptor, DomainEvent, DtoKey, EventClass, EventDescriptor, Executable, ExecutableOf, RemoteEvent } from "../../core/domain/dto.js";
3
+ import type { Cause, ExecuteOptions } from "../../core/domain/pipeline.js";
4
+ import { type HandledEvent } from "../../core/domain/services.js";
5
+ import { type HandlerRegistrar } from "../../core/services/event-handlers.js";
3
6
  import type { Envelope } from "../domain/envelope.js";
4
- import type { Handled, Subscribes } from "../domain/queue.js";
7
+ import type { BinaryMessage, Message } from "../domain/messages.js";
8
+ import type { CommandType, EventType, Handled, Subscribes } from "../domain/queue.js";
5
9
  /**
6
- * What the subscriber needs of whoever hears events — a bounded context (a use case of the event)
7
- * or `Subscriptions` (an `EventSubscriber` of it): its name, whether it hears an event, and
8
- * running it.
10
+ * What a subscriber needs of a bounded context — a bus it delivers to: what it registered, who
11
+ * consumes an event there, and running it. A `BoundedContext` is one.
9
12
  */
10
13
  export type HearingContext = {
11
14
  readonly name: string;
12
- handlerOf(dto: abstract new (...args: never[]) => DomainEvent): unknown;
13
- eventTypeOf(name: string): EventClass<DomainEvent> | undefined;
14
- execute(event: DomainEvent, cause?: Cause): Promise<unknown>;
15
+ handlerOf(dto: DtoKey): unknown;
16
+ eventTypeOf(name: string): EventType | undefined;
17
+ commandTypeOf(identity: string): CommandType | undefined;
18
+ execute(executable: Executable, options?: ExecuteOptions | Cause): Promise<unknown>;
19
+ /** Who consumes the event there: its use case and its event handlers. */
20
+ consumersOf?(event: DomainEvent | RemoteEvent): readonly EventConsumer[];
15
21
  };
22
+ /** Which bounded contexts it delivers to, and what it takes from the queue. */
23
+ export type SubscriberOptions = {
24
+ readonly boundedContexts: readonly HearingContext[];
25
+ /** The events it hears; every event its consumers registered when none is named. */
26
+ readonly events?: readonly (EventClass<DomainEvent> | EventDescriptor)[];
27
+ /**
28
+ * The Commands it consumes from the queue — none unless named, as the backend's
29
+ * `@queue.consumes`: a bus never hands all its Commands to whoever can publish on a queue.
30
+ */
31
+ readonly commands?: readonly (CommandClass<AnyCommand> | CommandDescriptor)[];
32
+ };
33
+ /** What a named subscriber takes besides its name. */
34
+ export type NamedSubscriberOptions = Omit<SubscriberOptions, "boundedContexts">;
35
+ /** A Command the queue carried that this subscriber was not told to consume. */
36
+ export declare class CommandNotConsumed extends Error {
37
+ readonly type: string;
38
+ static readonly failureKind = "permission_denied";
39
+ readonly reason = "COMMAND_NOT_CONSUMED";
40
+ constructor(type: string);
41
+ }
16
42
  /**
17
- * The door of a queue: hands an event to everyone who hears it — the backend's
18
- * `Subscriber(buses)`:
43
+ * The queue's entrypoint — the backend's `Subscriber(*buses)` and `QueueGateway`, composed: it
44
+ * pulls what the queue delivers, rebuilds it and hands it to every consumer subscribed to it —
45
+ * its own event handlers, and the buses added to it, each with its use case and its handlers.
19
46
  *
20
- * const subscriber = new Subscriber(shopBoundedContext, mailBoundedContext, ticketsSubscriptions);
21
- * const queue = syncQueue(subscriber); // or a platform's durable queue
47
+ * // entrypoints/queue/framework.ts — once, like a bounded context's framework.ts
48
+ * export const { EventHandler } = handlersWith<AppSubscriberDependencies>();
49
+ * export const appSubscriber = new Subscriber<AppSubscriberDependencies>("app");
50
+ * appSubscriber.addDependency("invoicingBoundedContext", invoicingBoundedContext); // to call it
51
+ * appSubscriber.addDependency("notifier", deviceNotifier);
52
+ * appSubscriber.addBus(salesBoundedContext); // to deliver to its use cases and handlers
22
53
  *
23
- * Context: who hears an event is a bounded context with a use case of it (a Feature or an
24
- * ApplicationService registered for its class) or `Subscriptions` with an `EventSubscriber` of
25
- * it — the entrypoint that calls use cases. Several hearing one event is several executions, one
26
- * each, in the order named; one that fails does not stop the others: each answer, or failure, is
27
- * said under its name — which a stored queue keeps, so a redelivery never runs again one that
28
- * already heard it. Two of them never share a name.
54
+ * // entrypoints/queue/sale_paid.handler.ts — events → handler
55
+ * @appSubscriber.handler([SalePaid])
56
+ * export class NotifySalePaid extends EventHandler<SalePaid> { async handle(event) { … } }
57
+ *
58
+ * storedQueue({ store, subscriber: appSubscriber });
59
+ *
60
+ * Context: one per queue, never one per handler. Two levels, never mixed: a dependency — a bus
61
+ * to call included — is what its handlers find on `this` (`addDependency`, by name); a bus added
62
+ * (`addBus`, by its own namespace) is one it delivers to. Adding the same bus again changes
63
+ * nothing; another bus of the same name is delivered to as well (`sales#2`). Every consumer is
64
+ * acknowledged apart — `app/NotifySalePaid`, `sales`, `sales/OnSalePaid` —, which a stored queue
65
+ * keeps, so a redelivery runs only who does not have it yet. A Command is a request to one bus,
66
+ * by its identity, and only when it was declared here.
29
67
  */
30
- export declare class Subscriber implements Subscribes {
31
- private readonly contexts;
32
- constructor(...contexts: HearingContext[]);
33
- eventTypeOf(name: string): EventClass<DomainEvent> | undefined;
68
+ export declare class Subscriber<D extends object = Record<never, never>> implements Subscribes {
69
+ /** Its namespace: its own handlers are said `<name>/<Handler>`. */
70
+ readonly name: string;
71
+ private readonly buses;
72
+ private readonly dependencyValues;
73
+ private readonly handlers;
74
+ private readonly instances;
75
+ private readonly events;
76
+ private readonly commands;
77
+ constructor(name: string, options?: NamedSubscriberOptions);
78
+ constructor(options: SubscriberOptions);
79
+ constructor(...boundedContexts: HearingContext[]);
80
+ /**
81
+ * What its handlers find on `this` — a bus they call, a notifier, a repository —, by name; a
82
+ * dependency carrying `BIND` (a bounded context, a publisher) is bound to the handler's run.
83
+ */
84
+ addDependency<K extends keyof D & string>(name: K, dependency: D[K], options?: {
85
+ replaces: D[K];
86
+ }): this;
87
+ /**
88
+ * A bus it delivers to: the events its use cases and handlers are registered for reach them
89
+ * through this subscriber. By its namespace — no name to write —; the same bus again changes
90
+ * nothing, another of the same name is delivered to as well. It is not a dependency: a
91
+ * handler that calls a bus has it as one (`addDependency`).
92
+ */
93
+ addBus(boundedContext: HearingContext): this;
94
+ /**
95
+ * Registers an event handler of this subscriber for one event or N — as a decorator, or
96
+ * called with the class:
97
+ *
98
+ * @appSubscriber.handler([SalePaid, SaleCancelled])
99
+ * export class OnSaleChanged extends EventHandler<SalePaid | SaleCancelled> { … }
100
+ *
101
+ * appSubscriber.handler([SalePaid, SaleCancelled])(OnSaleChanged);
102
+ */
103
+ handler<const K extends readonly EventKey[]>(events: K): HandlerRegistrar<ExecutableOf<K[number]> & HandledEvent>;
104
+ handler<E extends DomainEvent>(event: EventClass<E>): HandlerRegistrar<E>;
105
+ handler<E extends RemoteEvent = RemoteEvent>(event: EventDescriptor): HandlerRegistrar<E>;
106
+ /** What an event of that wire name is rebuilt as, or `undefined` when nobody here hears it. */
107
+ eventTypeOf(name: string): EventType | undefined;
108
+ /** What a consumed Command of that identity is rebuilt as, or `undefined` when it is not consumed here. */
109
+ commandTypeOf(identity: string): CommandType | undefined;
110
+ /**
111
+ * 1. Who consumes the event here: its own handlers, then each bus added, in the order added —
112
+ * the bus's use case, then its handlers —; none when this subscriber does not hear it.
113
+ * 2. Those `skip` names already have it; each other one consumes it — continuing the
114
+ * envelope's flow and trace when there is one —, and its answer or failure is kept: a
115
+ * failure does not stop the next.
116
+ * Final: what each consumer answered, or failed with, under its key.
117
+ */
118
+ handle(event: DomainEvent | RemoteEvent, envelope?: Envelope, skip?: readonly string[]): Promise<Handled[]>;
34
119
  /**
35
- * 1. Every bounded context registered for the event's class, in the order named — but those
36
- * `skip` names, which already have it.
37
- * 2. Each executes it — continuing the envelope's flow when there is one —, and its answer or
38
- * failure is kept; a failure does not stop the next.
39
- * Final: what each answered, or failed with.
120
+ * What a queue delivered — CloudEvents, structured or in a broker's binary mode —, executed:
121
+ *
122
+ * 1. An event this subscriber hears (by its `type`, the event's name): rebuilt as what it is
123
+ * known by here and handed to every consumer subscribed to it (`handle`).
124
+ * 2. A Command (its `type`, its identity): only when declared — `CommandNotConsumed` when a bus
125
+ * registered it but this subscriber does not consume it —, rebuilt and executed by the one
126
+ * bus that registered it.
127
+ * 3. Either continues the message's flow and trace.
128
+ * Final: what each answered, or failed with; nothing when nobody here knows it.
40
129
  */
41
- handle(event: DomainEvent, envelope?: Envelope, skip?: readonly string[]): Promise<Handled[]>;
130
+ receive(arrived: Message | BinaryMessage, skip?: readonly string[]): Promise<Handled[]>;
131
+ /** Every consumer of `event`, each under a key no other one has. */
132
+ private consumersOf;
133
+ /** One of its own handlers, as a consumer: its run, with this subscriber's dependencies. */
134
+ private consumerOf;
42
135
  }