@decohere/core 0.0.1-dev.20261110.287 → 0.0.1-dev.20261110.311

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.
@@ -15,7 +15,7 @@ function _ts_param(paramIndex, decorator) {
15
15
  import { scribe } from "@decohere/scribe";
16
16
  import { inject, LifetimeScopeToken, optional } from "../composition/index.js";
17
17
  import { ConfigurationServiceToken } from "../configuration/index.js";
18
- import { AppServerToken, HostedServiceHostToken } from "./const.js";
18
+ import { AppServerToken, HostedServiceHostToken, StartStepsToken } from "./const.js";
19
19
  const DEFAULT_PORT = 3000;
20
20
  const DEFAULT_SHUTDOWN_SIGNALS = [
21
21
  "SIGTERM",
@@ -41,23 +41,35 @@ export class App {
41
41
  configurationService;
42
42
  server;
43
43
  hostedServices;
44
+ startSteps;
44
45
  _addresses = Array.of();
45
46
  logger = scribe.logger(App.name);
46
47
  disposing;
47
48
  disposingContainer = false;
48
49
  signalHandler;
49
50
  shutdownSignals = [];
50
- constructor(container, configurationService, server, hostedServices){
51
+ constructor(container, configurationService, server, hostedServices, startSteps){
51
52
  this.container = container;
52
53
  this.configurationService = configurationService;
53
54
  this.server = server;
54
55
  this.hostedServices = hostedServices;
56
+ this.startSteps = startSteps;
57
+ }
58
+ /**
59
+ * The app's root container, for a tool or a test that builds the app to use its services
60
+ * without starting it: `decohere`'s db commands resolve its data sources' migrations here.
61
+ */ get services() {
62
+ return this.container;
55
63
  }
56
64
  get addresses() {
57
65
  return this._addresses;
58
66
  }
59
67
  async start(options) {
60
68
  this.logger.info("Starting app");
69
+ for (const step of this.startSteps?.steps ?? []){
70
+ await step(this.container);
71
+ if (this.disposing != null) return undefined;
72
+ }
61
73
  // Before the server, so a request never reaches a service that has not started.
62
74
  await this.hostedServices?.start(this.container);
63
75
  // The app was stopped while a hosted service was still starting.
@@ -148,11 +160,14 @@ App = _ts_decorate([
148
160
  _ts_param(2, optional()),
149
161
  _ts_param(3, inject(HostedServiceHostToken)),
150
162
  _ts_param(3, optional()),
163
+ _ts_param(4, inject(StartStepsToken)),
164
+ _ts_param(4, optional()),
151
165
  _ts_metadata("design:type", Function),
152
166
  _ts_metadata("design:paramtypes", [
153
167
  typeof LifetimeScope === "undefined" ? Object : LifetimeScope,
154
168
  typeof IConfigurationService === "undefined" ? Object : IConfigurationService,
155
169
  typeof AppServer === "undefined" ? Object : AppServer,
156
- typeof HostedServiceHost === "undefined" ? Object : HostedServiceHost
170
+ typeof HostedServiceHost === "undefined" ? Object : HostedServiceHost,
171
+ typeof StartSteps === "undefined" ? Object : StartSteps
157
172
  ])
158
173
  ], App);
@@ -1,7 +1,7 @@
1
1
  import { scribe } from "@decohere/scribe";
2
2
  import { ConfigurationServiceToken } from "../configuration/index.js";
3
3
  import { App } from "./app.js";
4
- import { HostedServiceHostToken } from "./const.js";
4
+ import { HostedServiceHostToken, StartStepsToken } from "./const.js";
5
5
  import { HostedServiceHost } from "./hosted-service-host.js";
6
6
  export class Builder {
7
7
  /**
@@ -16,6 +16,7 @@ export class Builder {
16
16
  userServices = [];
17
17
  preBuildSteps = [];
18
18
  postBuildSteps = [];
19
+ startSteps = [];
19
20
  hostedServices = new HostedServiceHost();
20
21
  constructor(containerBuilder, options){
21
22
  this.containerBuilder = containerBuilder;
@@ -44,6 +45,14 @@ export class Builder {
44
45
  registerPostBuildStep(step) {
45
46
  this.postBuildSteps.push(step);
46
47
  }
48
+ /**
49
+ * Run `step` when the app starts, in registration order, before its hosted services start and
50
+ * its server listens. For work that changes the world outside the process, such as bringing a
51
+ * database's schema up to date: an app that is only built, by a tool or a test, does none of it.
52
+ * A throw fails `app.start()`.
53
+ */ registerStartStep(step) {
54
+ this.startSteps.push(step);
55
+ }
47
56
  async buildAndStart(type) {
48
57
  const app = await this.build(type);
49
58
  await app.start();
@@ -82,6 +91,9 @@ export class Builder {
82
91
  }
83
92
  configureApp(type) {
84
93
  this.containerBuilder.registerSingleton(HostedServiceHostToken, this.hostedServices);
94
+ this.containerBuilder.registerSingleton(StartStepsToken, {
95
+ steps: this.startSteps
96
+ });
85
97
  this.containerBuilder.registerSingleton(type ?? App);
86
98
  }
87
99
  resolveApp(container, type) {
@@ -1,3 +1,4 @@
1
1
  /** The {@link AppServer} the app starts last and stops first. Registered by a plugin such as `http`. */ export const AppServerToken = Symbol("AppServer");
2
2
  export const HostedServiceHostToken = Symbol("HostedServiceHost");
3
3
  export const ReadinessCheckToken = Symbol("ReadinessCheck");
4
+ /** The {@link StartSteps} `App.start` runs before hosted services: `builder.registerStartStep`. */ export const StartStepsToken = Symbol("StartSteps");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decohere/core",
3
- "version": "0.0.1-dev.20261110.287",
3
+ "version": "0.0.1-dev.20261110.311",
4
4
  "description": "The runtime base every Decohere package builds on: reflection, composition, configuration types, extensibility, collections, objects, caching on a Map, and basic file IO. No side effects on import.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -68,7 +68,7 @@
68
68
  "test": "echo \"Error: no test specified\" && exit 1"
69
69
  },
70
70
  "dependencies": {
71
- "@decohere/scribe": "0.0.1-dev.20261110.287",
71
+ "@decohere/scribe": "0.0.1-dev.20261110.311",
72
72
  "reflect-metadata": "^0.2.2"
73
73
  },
74
74
  "devDependencies": {}
@@ -1,5 +1,6 @@
1
- import { type LifetimeScope } from "../composition/index.js";
1
+ import { type IContainer, type LifetimeScope } from "../composition/index.js";
2
2
  import { type IConfigurationService } from "../configuration/index.js";
3
+ import { type StartSteps } from "./const.js";
3
4
  import type { HostedServiceHost } from "./hosted-service-host.js";
4
5
  export declare class ApiConfiguration {
5
6
  static schema: {
@@ -44,13 +45,19 @@ export declare class App {
44
45
  private configurationService?;
45
46
  private server?;
46
47
  private hostedServices?;
48
+ private startSteps?;
47
49
  private _addresses;
48
50
  private logger;
49
51
  private disposing;
50
52
  private disposingContainer;
51
53
  private signalHandler;
52
54
  private shutdownSignals;
53
- constructor(container: LifetimeScope, configurationService?: IConfigurationService, server?: AppServer, hostedServices?: HostedServiceHost);
55
+ constructor(container: LifetimeScope, configurationService?: IConfigurationService, server?: AppServer, hostedServices?: HostedServiceHost, startSteps?: StartSteps);
56
+ /**
57
+ * The app's root container, for a tool or a test that builds the app to use its services
58
+ * without starting it: `decohere`'s db commands resolve its data sources' migrations here.
59
+ */
60
+ get services(): IContainer;
54
61
  get addresses(): readonly string[];
55
62
  start(options?: StartOptions): Promise<AppServerStartResponse | undefined>;
56
63
  [Symbol.asyncDispose](): Promise<void>;
@@ -2,6 +2,7 @@ import type { ContainerBuilder, IContainer } from "../composition/index.js";
2
2
  import { type IConfigurationService } from "../configuration/index.js";
3
3
  import type { Constructable } from "../reflection/index.js";
4
4
  import { App } from "./app.js";
5
+ import { type StartStep } from "./const.js";
5
6
  import type { HostedService } from "./hosted-service.js";
6
7
  export type BuilderOptions = {
7
8
  applicationName?: string;
@@ -23,6 +24,7 @@ export declare class Builder<T extends App = App> {
23
24
  private userServices;
24
25
  private preBuildSteps;
25
26
  private postBuildSteps;
27
+ private readonly startSteps;
26
28
  private readonly hostedServices;
27
29
  constructor(containerBuilder: ContainerBuilder, options?: BuilderOptions);
28
30
  use(plugin: BuilderPlugin): this;
@@ -35,6 +37,13 @@ export declare class Builder<T extends App = App> {
35
37
  withHostedService(...types: Constructable<HostedService>[]): Builder<T>;
36
38
  registerPreBuildStep(step: (config: IConfigurationService, containerBuilder: ContainerBuilder) => Promise<void> | void): void;
37
39
  registerPostBuildStep(step: (container: IContainer) => Promise<void> | void): void;
40
+ /**
41
+ * Run `step` when the app starts, in registration order, before its hosted services start and
42
+ * its server listens. For work that changes the world outside the process, such as bringing a
43
+ * database's schema up to date: an app that is only built, by a tool or a test, does none of it.
44
+ * A throw fails `app.start()`.
45
+ */
46
+ registerStartStep(step: StartStep): void;
38
47
  buildAndStart<T extends App>(type?: Constructable<T>): Promise<T | App>;
39
48
  build<T extends App = App>(type?: Constructable<T>): Promise<T | App>;
40
49
  private configureUserServices;
@@ -1,4 +1,12 @@
1
+ import type { IContainer } from "../composition/index.js";
1
2
  /** The {@link AppServer} the app starts last and stops first. Registered by a plugin such as `http`. */
2
3
  export declare const AppServerToken: unique symbol;
3
4
  export declare const HostedServiceHostToken: unique symbol;
4
5
  export declare const ReadinessCheckToken: unique symbol;
6
+ /** The {@link StartSteps} `App.start` runs before hosted services: `builder.registerStartStep`. */
7
+ export declare const StartStepsToken: unique symbol;
8
+ /** Work `App.start` does before hosted services start and the server listens. */
9
+ export type StartStep = (container: IContainer) => Promise<void> | void;
10
+ export type StartSteps = {
11
+ readonly steps: readonly StartStep[];
12
+ };