ultra-minimal pubsub module written in TypeScript
# pubsub

ultra-minimal pubsub module written in TypeScript

## install

```sh
pnpm add @rasch/pubsub
# or with bun
bun add @rasch/pubsub
```

## usage

```js
import { publish, subscribe, unsubscribe } from "@rasch/pubsub"
import { welcome, tryagain } from "./fakeCallbackFunctions.js"

subscribe("login", welcome)
subscribe("loginError", tryagain)

if (user.isLoggedIn()) {
  publish("login", user.name)
} else {
  publish("loginError", `Incorrect username "${user.name}" or password!`)
}

unsubscribe("*")
```

## api

### subscribe

```txt
subscribe :: string -> (...a -> void) -> void
```

Attach a callback function to an event.

```typescript
subscribe("event", myFunction)
```

### publish

```txt
publish :: (string, ...a) -> void
```

Run all of the functions attached to an event with the given arguments.

```typescript
publish("event", arg)
publish("event", arg1, arg2, ...)
```

### unsubscribe

```txt
unsubscribe :: (string, ((...a -> void) | undefined)) -> void
```

Stop listening for events.

```typescript
// stop listening to a single event callback function
unsubscribe("event", myFunction)

// stop listening for all callback functions on event
unsubscribe("event")

// cancel all subscriptions
unsubscribe("*")
```