@blixis-io/events 0.0.0 → 0.1.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Michael Voeten
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1 +1,33 @@
1
- placeholder
1
+ # `@blixis-io/events`
2
+
3
+ An in-process domain event bus (`defineEventsModule`, `EventBus.emit`/`on`). Depends only on `@blixis-io/core` — usable in any app, HTTP or not.
4
+
5
+ ```bash
6
+ npm install @blixis-io/events @blixis-io/core @blixis-io/di
7
+ ```
8
+
9
+ ```ts
10
+ // events.ts
11
+ type AppEvents = {
12
+ "post.created": { postId: string; title: string };
13
+ };
14
+
15
+ export const { EventsModule, EVENT_BUS } = defineEventsModule<AppEvents>();
16
+ ```
17
+
18
+ ```ts
19
+ @Injectable()
20
+ class PostsService {
21
+ constructor(@Inject(EVENT_BUS) private readonly events: EventBus<AppEvents>) {}
22
+
23
+ async create(input: CreatePostInput) {
24
+ const post = await this.repo.insert(input);
25
+ await this.events.emit("post.created", { postId: post.id, title: post.title });
26
+ return post;
27
+ }
28
+ }
29
+ ```
30
+
31
+ Runs every handler for an event concurrently, in-process — no persistence, no delivery guarantee across a crash. Deliberately the only implementation for now; see the docs for why a transactional outbox was deferred rather than built ahead of a real need.
32
+
33
+ Part of [Blixis Framework](https://github.com/blixis-io/framework) — full docs: [Events](https://blixis-io.github.io/framework/concepts/events/) · [API reference](https://blixis-io.github.io/framework/reference/blixis-events/).
@@ -0,0 +1,28 @@
1
+ import { DynamicModule } from "@blixis-io/core";
2
+ import { InjectionToken } from "@blixis-io/di";
3
+ //#region src/module.d.ts
4
+ type EventHandler<Payload> = (payload: Payload) => void | Promise<void>;
5
+ interface EventBus<Events extends Record<string, unknown>> {
6
+ emit<K extends keyof Events & string>(type: K, payload: Events[K]): Promise<void>;
7
+ /** Registers `handler` for `type`, returning a function that unsubscribes just this handler. */
8
+ on<K extends keyof Events & string>(type: K, handler: EventHandler<Events[K]>): () => void;
9
+ }
10
+ interface EventsForRootOptions {
11
+ /** Makes `EVENT_BUS` visible to every module without each one importing this one directly. Defaults to `false`. */
12
+ global?: boolean;
13
+ }
14
+ /**
15
+ * Builds an `EVENT_BUS` token typed to one app's event map. Same
16
+ * factory-closure shape as `defineConfigModule`/`defineAuthModule` — the
17
+ * event-name-to-payload map is app-specific, so it's a generic parameter
18
+ * fixed once per app, not baked into the package.
19
+ */
20
+ export declare function defineEventsModule<Events extends Record<string, unknown>>(): {
21
+ EventsModule: {
22
+ forRoot(options?: EventsForRootOptions): DynamicModule;
23
+ };
24
+ EVENT_BUS: InjectionToken<EventBus<Events>>;
25
+ };
26
+ //#endregion
27
+ export type { EventBus, EventHandler, EventsForRootOptions };
28
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/module.ts"],"mappings":";;;KAGY,aAAa,YAAY,SAAS,mBAAmB;UAEhD,SAAS,eAAe;EACvC,KAAK,gBAAgB,iBAAiB,MAAM,GAAG,SAAS,OAAO,KAAK;;EAEpE,GAAG,gBAAgB,iBAAiB,MAAM,GAAG,SAAS,aAAa,OAAO;;UAG3D;;EAEf;;;;;;;;wBAuDc,mBAAmB,eAAe;EAChD;IAAgB,QAAQ,UAAU,uBAAuB;;EACzD,WAAW,eAAe,SAAS"}
package/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ import { Module } from "@blixis-io/core";
2
+ import { Injectable, InjectionToken } from "@blixis-io/di";
3
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/decorate.js
4
+ function __decorate(decorators, target, key, desc) {
5
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
6
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
7
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
8
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
9
+ }
10
+ //#endregion
11
+ //#region src/module.ts
12
+ /**
13
+ * Runs every handler for an event concurrently and in-process — no
14
+ * persistence, no delivery guarantee across a crash. Deliberately the only
15
+ * implementation for now; see `concepts/events.md` for why a transactional
16
+ * outbox was deferred rather than built ahead of a real consumer.
17
+ */
18
+ let InProcessEventBus = class InProcessEventBus {
19
+ #handlers = /* @__PURE__ */ new Map();
20
+ on(type, handler) {
21
+ let handlers = this.#handlers.get(type);
22
+ if (!handlers) {
23
+ handlers = /* @__PURE__ */ new Set();
24
+ this.#handlers.set(type, handlers);
25
+ }
26
+ handlers.add(handler);
27
+ return () => handlers.delete(handler);
28
+ }
29
+ async emit(type, payload) {
30
+ const handlers = this.#handlers.get(type);
31
+ if (!handlers || handlers.size === 0) return;
32
+ await Promise.all([...handlers].map(async (handler) => {
33
+ try {
34
+ await handler(payload);
35
+ } catch (error) {
36
+ console.error(`event handler for "${type}" failed:`, error);
37
+ }
38
+ }));
39
+ }
40
+ };
41
+ InProcessEventBus = __decorate([Injectable()], InProcessEventBus);
42
+ /**
43
+ * Builds an `EVENT_BUS` token typed to one app's event map. Same
44
+ * factory-closure shape as `defineConfigModule`/`defineAuthModule` — the
45
+ * event-name-to-payload map is app-specific, so it's a generic parameter
46
+ * fixed once per app, not baked into the package.
47
+ */
48
+ function defineEventsModule() {
49
+ var _EventsModule;
50
+ const EVENT_BUS = new InjectionToken("blixis.events.bus");
51
+ let EventsModule = _EventsModule = class EventsModule {
52
+ static forRoot(options = {}) {
53
+ return {
54
+ module: _EventsModule,
55
+ providers: [{
56
+ provide: EVENT_BUS,
57
+ useClass: InProcessEventBus
58
+ }],
59
+ exports: [EVENT_BUS],
60
+ global: options.global ?? false
61
+ };
62
+ }
63
+ };
64
+ EventsModule = _EventsModule = __decorate([Module()], EventsModule);
65
+ return {
66
+ EventsModule,
67
+ EVENT_BUS
68
+ };
69
+ }
70
+ //#endregion
71
+ export { defineEventsModule };
72
+
73
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/module.ts"],"sourcesContent":["import { Module, type DynamicModule } from \"@blixis-io/core\";\nimport { Injectable, InjectionToken } from \"@blixis-io/di\";\n\nexport type EventHandler<Payload> = (payload: Payload) => void | Promise<void>;\n\nexport interface EventBus<Events extends Record<string, unknown>> {\n emit<K extends keyof Events & string>(type: K, payload: Events[K]): Promise<void>;\n /** Registers `handler` for `type`, returning a function that unsubscribes just this handler. */\n on<K extends keyof Events & string>(type: K, handler: EventHandler<Events[K]>): () => void;\n}\n\nexport interface EventsForRootOptions {\n /** Makes `EVENT_BUS` visible to every module without each one importing this one directly. Defaults to `false`. */\n global?: boolean;\n}\n\n/**\n * Runs every handler for an event concurrently and in-process — no\n * persistence, no delivery guarantee across a crash. Deliberately the only\n * implementation for now; see `concepts/events.md` for why a transactional\n * outbox was deferred rather than built ahead of a real consumer.\n */\n@Injectable()\nclass InProcessEventBus<Events extends Record<string, unknown>> implements EventBus<Events> {\n readonly #handlers = new Map<string, Set<EventHandler<never>>>();\n\n on<K extends keyof Events & string>(type: K, handler: EventHandler<Events[K]>): () => void {\n let handlers = this.#handlers.get(type);\n if (!handlers) {\n handlers = new Set();\n this.#handlers.set(type, handlers);\n }\n handlers.add(handler);\n return () => handlers.delete(handler);\n }\n\n async emit<K extends keyof Events & string>(type: K, payload: Events[K]): Promise<void> {\n const handlers = this.#handlers.get(type);\n if (!handlers || handlers.size === 0) {\n return;\n }\n\n await Promise.all(\n [...handlers].map(async (handler) => {\n try {\n // Handlers for every event type share one Set<EventHandler<never>>\n // collection (indexed by the string `type`, not a type-level key),\n // so there's no way for TS to correlate a specific handler back to\n // the K it was registered with — this cast is the one place that\n // trusts `type` actually matches, same as `on()`'s own erasure.\n await handler(payload as never);\n } catch (error) {\n // A handler's own failure never blocks sibling handlers or the\n // caller — same self-contained fallback @blixis-io/logging's own\n // transport-failure handling already uses.\n console.error(`event handler for \"${type}\" failed:`, error);\n }\n }),\n );\n }\n}\n\n/**\n * Builds an `EVENT_BUS` token typed to one app's event map. Same\n * factory-closure shape as `defineConfigModule`/`defineAuthModule` — the\n * event-name-to-payload map is app-specific, so it's a generic parameter\n * fixed once per app, not baked into the package.\n */\nexport function defineEventsModule<Events extends Record<string, unknown>>(): {\n EventsModule: { forRoot(options?: EventsForRootOptions): DynamicModule };\n EVENT_BUS: InjectionToken<EventBus<Events>>;\n} {\n const EVENT_BUS = new InjectionToken<EventBus<Events>>(\"blixis.events.bus\");\n\n @Module()\n class EventsModule {\n static forRoot(options: EventsForRootOptions = {}): DynamicModule {\n return {\n module: EventsModule,\n providers: [{ provide: EVENT_BUS, useClass: InProcessEventBus }],\n exports: [EVENT_BUS],\n global: options.global ?? false,\n };\n }\n }\n\n return { EventsModule, EVENT_BUS };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAsBA,IACM,oBADN,MACM,kBAAsF;CAC1F,4BAAqB,IAAI,IAAsC;CAE/D,GAAoC,MAAS,SAA8C;EACzF,IAAI,WAAW,KAAK,UAAU,IAAI,IAAI;EACtC,IAAI,CAAC,UAAU;GACb,2BAAW,IAAI,IAAI;GACnB,KAAK,UAAU,IAAI,MAAM,QAAQ;EACnC;EACA,SAAS,IAAI,OAAO;EACpB,aAAa,SAAS,OAAO,OAAO;CACtC;CAEA,MAAM,KAAsC,MAAS,SAAmC;EACtF,MAAM,WAAW,KAAK,UAAU,IAAI,IAAI;EACxC,IAAI,CAAC,YAAY,SAAS,SAAS,GACjC;EAGF,MAAM,QAAQ,IACZ,CAAC,GAAG,QAAQ,CAAC,CAAC,IAAI,OAAO,YAAY;GACnC,IAAI;IAMF,MAAM,QAAQ,OAAgB;GAChC,SAAS,OAAO;IAId,QAAQ,MAAM,sBAAsB,KAAK,YAAY,KAAK;GAC5D;EACF,CAAC,CACH;CACF;AACF;AAtCC,oBAAA,WAAA,CAAA,WAAW,CAAA,GAAA,iBAAA;;;;;;;AA8CZ,SAAgB,qBAGd;;CACA,MAAM,YAAY,IAAI,eAAiC,mBAAmB;CAE1E,IACM,eAAA,gBADN,MACM,aAAa;EACjB,OAAO,QAAQ,UAAgC,CAAC,GAAkB;GAChE,OAAO;IACL,QAAA;IACA,WAAW,CAAC;KAAE,SAAS;KAAW,UAAU;IAAkB,CAAC;IAC/D,SAAS,CAAC,SAAS;IACnB,QAAQ,QAAQ,UAAU;GAC5B;EACF;CACF;CAVC,eAAA,gBAAA,WAAA,CAAA,OAAO,CAAA,GAAA,YAAA;CAYR,OAAO;EAAE;EAAc;CAAU;AACnC"}
package/package.json CHANGED
@@ -1 +1,44 @@
1
- { "name": "@blixis-io/events", "version": "0.0.0", "description": "placeholder" }
1
+ {
2
+ "name": "@blixis-io/events",
3
+ "version": "0.1.2",
4
+ "description": "An in-process domain event bus (defineEventsModule, EventBus.emit/on). Depends only on @blixis-io/core — usable in any app, HTTP or not.",
5
+ "license": "MIT",
6
+ "author": "Michael Voeten <michael@voeten.online>",
7
+ "homepage": "https://blixis-io.github.io/framework/",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/blixis-io/framework.git",
11
+ "directory": "packages/events"
12
+ },
13
+ "type": "module",
14
+ "engines": {
15
+ "node": ">=24"
16
+ },
17
+ "keywords": [
18
+ "events",
19
+ "pub-sub",
20
+ "event-bus",
21
+ "typescript"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "@blixis-io/source": "./src/index.ts",
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.js"
34
+ }
35
+ },
36
+ "dependencies": {
37
+ "@blixis-io/core": "0.2.1",
38
+ "@blixis-io/di": "0.1.1"
39
+ },
40
+ "scripts": {
41
+ "typecheck": "tsc -p tsconfig.json",
42
+ "build": "rolldown -c"
43
+ }
44
+ }