Skip to content

Commit 684963b

Browse files
Added documentation to symbols and JSR publish github workflow
1 parent 098c3b3 commit 684963b

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

.github/workflows/publish-jsr.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# .github/workflows/publish.yml
2+
3+
name: Publish
4+
5+
on:
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
publish:
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
id-token: write # The OIDC ID token is used for authentication with JSR.
16+
steps:
17+
- uses: actions/checkout@v4
18+
- run: npx jsr publish

index.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
/**
2+
* @module
3+
* MeBus is a type safe and end-to-end runtime validated message bus for the browser.
4+
*/
5+
16
import z from "zod";
27

8+
/**
9+
* EventSchema is a record of event types and their corresponding payload schemas.
10+
* @example
11+
* const MyEventSchema: EventSchema = {
12+
* event1: z.object({ payload: z.string(), }),
13+
* event2: z.object({ id: z.number(), }),
14+
* };
15+
*/
316
export type EventSchema = Record<string, z.Schema>;
417

18+
/**
19+
* Type guard for CustomEvent.
20+
* @param event - The event to check.
21+
* @returns true if the event is a CustomEvent, false otherwise.
22+
*/
523
const isCustomEvent = (event: Event): event is CustomEvent => {
624
return event instanceof CustomEvent;
725
};
@@ -26,6 +44,11 @@ const isCustomEvent = (event: Event): event is CustomEvent => {
2644
export class MeBus<T extends EventSchema> {
2745
private eventSchema: T;
2846

47+
/**
48+
* Creates a new instance of MeBus.
49+
* @param eventSchema - Schema with definitions for all the event types and their payloads.
50+
* @throws Error if MeBus is not used in the browser.
51+
*/
2952
constructor(eventSchema: T) {
3053
if (typeof window === "undefined") {
3154
throw new Error("[MeBus] MeBus is only available in the browser");
@@ -34,12 +57,15 @@ export class MeBus<T extends EventSchema> {
3457
}
3558

3659
/**
37-
* Subscribes to a specific event type and registers a listener function to be called when the event is triggered.
60+
* Subscribes to a specific event type and registers a listener function to be called when the event is triggered. Returns cleanup.
3861
* @param type - The event type to subscribe to.
3962
* @param listener - The listener function to be called when the event is triggered. It receives the payload of the event as a parameter.
4063
* @returns A function that can be called to unsubscribe from the event.
4164
*/
42-
public subscribe<K extends keyof T & string>(type: K, listener: (payload: z.infer<T[K]>) => void) {
65+
public subscribe<K extends keyof T & string>(
66+
type: K,
67+
listener: (payload: z.infer<T[K]>) => void
68+
): () => void {
4369
const schema = this.eventSchema[type];
4470
if (!schema) {
4571
throw new Error(`[MeBus] No schema found for event: ${type}`);
@@ -74,7 +100,10 @@ export class MeBus<T extends EventSchema> {
74100
* @returns void
75101
* @throws Error if no schema is found for the event or if the payload fails validation.
76102
*/
77-
public publish<K extends keyof T & string>(type: K, payload: z.infer<T[K]>): void {
103+
public publish<K extends keyof T & string>(
104+
type: K,
105+
payload: z.infer<T[K]>
106+
): void {
78107
const schema = this.eventSchema[type];
79108
if (!schema) {
80109
throw new Error(`[MeBus] No schema found for event: ${type}`);

jsr.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@mebus/mebus",
3+
"version": "1.0.2",
4+
"exports": "./index.ts"
5+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"email": "kirill@kcrz.dev",
66
"url": "https://kcrz.dev"
77
},
8-
"version": "1.0.1",
8+
"version": "1.0.2",
99
"description": "Type safe event bus for browser",
1010
"license": "MIT",
1111
"repository": {

0 commit comments

Comments
 (0)