Skip to content

Notify Reference

Coverage License NPM Version Open Issues Size

πŸ“£ Minimalistic Pub/Sub implementation.

Usage

πŸ“¦ Node

Install @lou.codes/notify as a dependency:

Terminal window
1
pnpm add @lou.codes/notify
2
# or
3
npm install @lou.codes/notify
4
# or
5
yarn add @lou.codes/notify

Import it and use it:

1
import { broadcast } from "@lou.codes/notify";
2
3
const { emit, on } = broadcast<{ event: string }>();
4
5
const onEvent = on("event");
6
const offEvent = onEvent(console.log);
7
8
const emitEvent = emit("event");
9
emitEvent("Hello world 1"); // Logs "Hello world 1"
10
emitEvent("Hello world 2"); // Logs "Hello world 2"
11
offEvent();
12
emitEvent("Nope"); // Nothing happens

πŸ¦• Deno

Import @lou.codes/notify using the npm: prefix, and use it directly:

1
import { broadcast } from "npm:@lou.codes/notify";
2
3
const { emit, on } = broadcast<{ event: string }>();
4
5
const onEvent = on("event");
6
const offEvent = onEvent(console.log);
7
8
const emitEvent = emit("event");
9
emitEvent("Hello world 1"); // Logs "Hello world 1"
10
emitEvent("Hello world 2"); // Logs "Hello world 2"
11
offEvent();
12
emitEvent("Nope"); // Nothing happens

🌎 Browser

Import @lou.codes/notify using esm.sh, and use it directly:

1
<script type="module">
2
import { broadcast } from "https://esm.sh/@lou.codes/notify";
3
4
const { emit, on } = broadcast();
5
6
const onEvent = on("event");
7
const offEvent = onEvent(console.log);
8
9
const emitEvent = emit("event");
10
emitEvent("Hello world 1"); // Logs "Hello world 1"
11
emitEvent("Hello world 2"); // Logs "Hello world 2"
12
offEvent();
13
emitEvent("Nope"); // Nothing happens
14
</script>

Type Aliases

Emitter

Ζ¬ Emitter<Data>: Single<Data> extends Single<never> ? () => void : Unary<Data, void>

Emitter function (when data is never it doesn’t take any arguments).

Type parameters

NameDescription
DataData type.

View source


EventListener

Ζ¬ EventListener<Data>: Single<Data> extends Single<never> ? () => void : Unary<Data, void>

Event listener unary function.

See

Unary

Type parameters

NameDescription
DataData type.

View source


EventRegistry

Ζ¬ EventRegistry<Events>: { readonly [Event in keyof Events]?: ReadOnlyArray<EventListener<Readonly<Events[Event]>>> }

Registry of event names to array of listeners.

See

Example

1
const eventRegistry = {
2
example: [() => console.log("example called")],
3
};

Type parameters

NameTypeDescription
Eventsextends EventTypeDictionaryEvent registry.

View source


EventTypeDictionary

Ζ¬ EventTypeDictionary: ReadOnlyRecord<string>

Dictionary of event name to event types.

Example

1
const example = {
2
eventName: TypeOfEvent,
3
} satisfies EventTypeDictionary;

See

ReadOnlyRecord

View source

Functions

broadcast

β–Έ broadcast<Events>(eventRegistry?): Object

Creates a new β€œbroadcast” object, which has emit and on with a shared eventRegistry.

Type parameters

NameTypeDescription
Eventsextends Readonly<Record<string, unknown>>Event registry.

Parameters

NameTypeDescription
eventRegistryEventRegistry<Events>Optional record of event names mapped to an array of listeners.

Returns

Object

Object with emit and on functions.

NameType
emit<Event>(event: Event) => Emitter<Events[Event]>
on<Event>(event: Event) => (listener: EventListener<Events[Event]>) => () => undefined

Example

1
const { emit, on } = broadcast<{ event: string }>();
2
const off = on("event")(console.log);
3
emit("event")("Hello world"); // Logs "Hello world"
4
off();
5
emit("event")("Nope"); // Nothing happens

View source


emit

β–Έ emit<Events>(eventRegistry): <Event>(event: Event) => Emitter<Events[Event]>

Creates a curried function to emit events for listeners of the given eventRegistry.

Type parameters

NameTypeDescription
Eventsextends Readonly<Record<string, unknown>>Event registry.

Parameters

NameTypeDescription
eventRegistryEventRegistry<Events>Record of event names mapped to an array of listeners.

Returns

fn

Curried function with eventRegistry in context.

β–Έ <Event>(event): Emitter<Events[Event]>

Creates a curried function to emit an event of the eventRegistry in context.

Type parameters
NameTypeDescription
Eventextends string | number | symbolEvent name.
Parameters
NameTypeDescription
eventEventEvent name (has to be a valid key of the eventRegistry).
Returns

Emitter<Events[Event]>

Curried function with eventRegistry and event in context.

Example

1
const eventRegistry = {};
2
const emitRegistry = emit(eventRegistry);
3
const emitEvent = emitRegistry("event"); // πŸ‘ˆπŸ» You are here
4
emitEvent("data");

Example

1
const eventRegistry = {};
2
const emitRegistry = emit(eventRegistry); // πŸ‘ˆπŸ» You are here
3
const emitEvent = emitRegistry("event");
4
emitEvent("data");

View source


on

β–Έ on<Events>(eventRegistry): <Event>(event: Event) => (listener: EventListener<Events[Event]>) => () => undefined

Creates a curried function to listen for calls to an event in the passed eventRegistry.

Type parameters

NameTypeDescription
Eventsextends Readonly<Record<string, unknown>>Event registry.

Parameters

NameTypeDescription
eventRegistryEventRegistry<Events>Record of event names mapped to an array of listeners.

Returns

fn

Curried function with eventRegistry in context.

β–Έ <Event>(event): (listener: EventListener<Events[Event]>) => () => undefined

Creates a curried function to listen for calls to an event of the eventRegistry in context.

Type parameters
NameTypeDescription
Eventextends string | number | symbolEvent name.
Parameters
NameTypeDescription
eventEventEvent name (has to be a valid key of the eventRegistry).
Returns

fn

Curried function with eventRegistry and event in context.

β–Έ (listener): () => undefined

Listens for calls of the event in context of the eventRegistry in context.

Parameters
NameTypeDescription
listenerEventListener<Events[Event]>Listener to be called when the event is emitted.
Returns

fn

Function to remove the listener from the eventRegistry.

β–Έ (): undefined

Returns

undefined

Example

1
const eventRegistry = {};
2
const onRegistry = on(eventRegistry);
3
const onEvent = onRegistry("event");
4
const offEvent = onEvent(() => console.log("event called")); // πŸ‘ˆπŸ» You are here
5
emit(eventRegistry)("event")(); // Logs "event called"
6
offEvent();
7
emit(eventRegistry)("event")(); // Nothing happens

Example

1
const eventRegistry = {};
2
const onRegistry = on(eventRegistry);
3
const onEvent = onRegistry("event"); // πŸ‘ˆπŸ» You are here
4
const offEvent = onEvent(() => console.log("event called"));
5
emit(eventRegistry)("event")(); // Logs "event called"
6
offEvent();
7
emit(eventRegistry)("event")(); // Nothing happens

Example

1
const eventRegistry = {};
2
const onRegistry = on(eventRegistry); // πŸ‘ˆπŸ» You are here
3
const onEvent = onRegistry("event");
4
const offEvent = onEvent(() => console.log("event called"));
5
emit(eventRegistry)("event")(); // Logs "event called"
6
offEvent();
7
emit(eventRegistry)("event")(); // Nothing happens

View source