@nwire/app 0.11.1 → 0.12.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/README.md CHANGED
@@ -69,15 +69,17 @@ await endpoint("monolith", { port: 3000 }).use(httpKoa()).mount(monolith).run();
69
69
  ## Framework events
70
70
 
71
71
  The runtime fires typed events at lifecycle transitions —
72
- `AppBooting`, `AppBooted`, `PluginBooting`, `PluginBooted`,
73
- `AppStopping`, `AppStopped`. Subscribe via the App's `FrameworkEventBus`
74
- or register a `FrameworkEventBus` hook from a plugin.
72
+ `AppBooting`, `AppBooted`, `AppReady`, `PluginBooting`, `PluginBooted`,
73
+ `AppShuttingDown`, `AppShutdown`. Observe them from a plugin's ctx `on`
74
+ the payload is typed from the named hook.
75
75
 
76
76
  ```ts
77
- import { AppBooted } from "@nwire/app";
77
+ import { definePlugin } from "@nwire/app";
78
78
 
79
- app.frameworkBus.on(AppBooted, ({ appName, bootedAt }) => {
80
- console.log(`${appName} booted at ${bootedAt}`);
79
+ const readyLog = definePlugin("ready-log", ({ on }) => {
80
+ on("AppBooted", ({ appName, bootedAt }) => {
81
+ console.log(`${appName} booted at ${bootedAt}`);
82
+ });
81
83
  });
82
84
  ```
83
85
 
@@ -20,8 +20,8 @@
20
20
  import { type Container } from "@nwire/container/awilix";
21
21
  import { type Logger } from "@nwire/logger";
22
22
  import { type Binding, type HandlerDef, type Interface, type WireCtxBuilder } from "@nwire/wires";
23
- import type { EventDefinition } from "@nwire/messages";
24
- import type { MessageEnvelope } from "@nwire/envelope";
23
+ import type { EventDefinition, EventPayload } from "@nwire/messages";
24
+ import type { ListenerContext } from "@nwire/runtime";
25
25
  import { type PluginDefinition, type Runtime } from "./runtime/index.js";
26
26
  import { type Subscription } from "./subscribe.js";
27
27
  export interface CreateAppOptions {
@@ -87,12 +87,12 @@ export interface App<TCaps = {}> {
87
87
  * pulling a dedicated listener file feels heavy. For organised codebases,
88
88
  * prefer `.subscribe([...])` from `app/listeners/`.
89
89
  */
90
- when<TPayload>(event: EventDefinition & {
90
+ when<E extends EventDefinition & {
91
91
  readonly name: string;
92
92
  readonly schema: {
93
93
  parse(input: unknown): unknown;
94
94
  };
95
- }, fn: (payload: TPayload, envelope: MessageEnvelope) => Promise<void> | void): this;
95
+ }>(event: E, fn: (payload: EventPayload<E>, ctx: ListenerContext) => Promise<void> | void): this;
96
96
  /** Sugar for `app.interface.wire(b, h)`. Returns the app for chaining. */
97
97
  wire(binding: Binding, handler: HandlerDef): this;
98
98
  /** Sugar for `app.interface.provide(builder)`. Returns the app for chaining. */
@@ -12,21 +12,24 @@
12
12
  * import { UserRegistered } from "../events";
13
13
  *
14
14
  * export default subscribe((when) => {
15
- * when(UserRegistered, async (payload, env) => {
16
- * await sendWelcomeEmail(payload.email);
15
+ * when(UserRegistered, async (user, ctx) => {
16
+ * await ctx.send(sendWelcome({ email: user.email }));
17
17
  * });
18
18
  * });
19
19
  *
20
20
  * Subscriptions are registered through `app.subscribe(sub | subs)`.
21
21
  */
22
- import type { EventDefinition } from "@nwire/messages";
23
- import type { MessageEnvelope } from "@nwire/envelope";
22
+ import type { EventDefinition, EventPayload } from "@nwire/messages";
23
+ import type { ListenerContext } from "@nwire/runtime";
24
24
  /**
25
- * Late-bound listener registration handed to a subscribe closure.
25
+ * Late-bound listener registration handed to a subscribe closure. The
26
+ * handler receives the event payload and a {@link ListenerContext} — the
27
+ * subset of the workflow `when` ctx, so the same block moves into a
28
+ * `defineWorkflow` unchanged.
26
29
  */
27
- export type WhenFn = <TPayload>(event: EventDefinition & {
30
+ export type WhenFn = <E extends EventDefinition & {
28
31
  readonly name: string;
29
- }, fn: (payload: TPayload, envelope: MessageEnvelope) => Promise<void> | void) => void;
32
+ }>(event: E, fn: (payload: EventPayload<E>, ctx: ListenerContext) => Promise<void> | void) => void;
30
33
  /**
31
34
  * A subscription is a closure waiting for an App's `when` to be handed in.
32
35
  * `install(when)` is the contract: the App passes its runtime's `when` and
package/dist/subscribe.js CHANGED
@@ -12,8 +12,8 @@
12
12
  * import { UserRegistered } from "../events";
13
13
  *
14
14
  * export default subscribe((when) => {
15
- * when(UserRegistered, async (payload, env) => {
16
- * await sendWelcomeEmail(payload.email);
15
+ * when(UserRegistered, async (user, ctx) => {
16
+ * await ctx.send(sendWelcome({ email: user.email }));
17
17
  * });
18
18
  * });
19
19
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nwire/app",
3
- "version": "0.11.1",
3
+ "version": "0.12.0",
4
4
  "description": "Nwire — managed Container with plugin lifecycle, framework events, and DI hooks. Composes modules + plugins, boots in order, exposes a Container, fires framework events at every lifecycle transition.",
5
5
  "keywords": [
6
6
  "app",
@@ -29,14 +29,14 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@nwire/container": "0.11.1",
33
- "@nwire/envelope": "0.11.1",
34
- "@nwire/handler": "0.11.1",
35
- "@nwire/hooks": "0.11.1",
36
- "@nwire/logger": "0.11.1",
37
- "@nwire/messages": "0.11.1",
38
- "@nwire/runtime": "0.11.1",
39
- "@nwire/wires": "0.11.1"
32
+ "@nwire/container": "0.12.0",
33
+ "@nwire/envelope": "0.12.0",
34
+ "@nwire/hooks": "0.12.0",
35
+ "@nwire/logger": "0.12.0",
36
+ "@nwire/messages": "0.12.0",
37
+ "@nwire/handler": "0.12.0",
38
+ "@nwire/wires": "0.12.0",
39
+ "@nwire/runtime": "0.12.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/node": "^22.19.9",