Skip to content

Notify by Lou

Coverage License NPM Version Open Issues Size

πŸ“£ Minimalistic Pub/Sub implementation.

Usage

πŸ“¦ Node

Install @lou.codes/notify as a dependency:

Terminal window
pnpm add @lou.codes/notify
# or
npm install @lou.codes/notify
# or
yarn add @lou.codes/notify

Import it and use it:

import { broadcast } from "@lou.codes/notify";
const { emit, on } = broadcast<{ event: string }>();
const onEvent = on("event");
const offEvent = onEvent(console.log);
const emitEvent = emit("event");
emitEvent("Hello world 1"); // Logs "Hello world 1"
emitEvent("Hello world 2"); // Logs "Hello world 2"
offEvent();
emitEvent("Nope"); // Nothing happens

πŸ¦• Deno

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

import { broadcast } from "npm:@lou.codes/notify";
const { emit, on } = broadcast<{ event: string }>();
const onEvent = on("event");
const offEvent = onEvent(console.log);
const emitEvent = emit("event");
emitEvent("Hello world 1"); // Logs "Hello world 1"
emitEvent("Hello world 2"); // Logs "Hello world 2"
offEvent();
emitEvent("Nope"); // Nothing happens

🌎 Browser

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

<script type="module">
import { broadcast } from "https://esm.sh/@lou.codes/notify";
const { emit, on } = broadcast();
const onEvent = on("event");
const offEvent = onEvent(console.log);
const emitEvent = emit("event");
emitEvent("Hello world 1"); // Logs "Hello world 1"
emitEvent("Hello world 2"); // Logs "Hello world 2"
offEvent();
emitEvent("Nope"); // Nothing happens
</script>

Type Aliases

Emitter

Ζ¬ Emitter<Data>: Single<Data> extends Single<never> ? Function<[], 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> ? Function<[], 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

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

Type parameters

NameTypeDescription
Eventsextends EventTypeDictionaryEvent registry.

View source


EventTypeDictionary

Ζ¬ EventTypeDictionary: ReadOnlyRecord<string>

Dictionary of event name to event types.

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 ObjectEvent 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

const { emit, on } = broadcast<{ event: string }>();
const off = on("event")(console.log);
emit("event")("Hello world"); // Logs "Hello world"
off();
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 ObjectEvent 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 stringEvent 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

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

Example

const eventRegistry = {};
const emitRegistry = emit(eventRegistry); // πŸ‘ˆπŸ» You are here
const emitEvent = emitRegistry("event");
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 ObjectEvent 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 stringEvent 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

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

Example

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

Example

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

View source