Notify Reference
๐ฃ Minimalistic Pub/Sub implementation.
Usage
๐ฆ Node
Install @lou.codes/notify
as a dependency:
1pnpm add @lou.codes/notify2# or3npm install @lou.codes/notify4# or5yarn add @lou.codes/notify
Import it and use it:
1import { broadcast } from "@lou.codes/notify";2
3const { emit, on } = broadcast<{ event: string }>();4
5const onEvent = on("event");6const offEvent = onEvent(console.log);7
8const emitEvent = emit("event");9emitEvent("Hello world 1"); // Logs "Hello world 1"10emitEvent("Hello world 2"); // Logs "Hello world 2"11offEvent();12emitEvent("Nope"); // Nothing happens
๐ฆ Deno
Import @lou.codes/notify
using the npm:
prefix, and use it directly:
1import { broadcast } from "npm:@lou.codes/notify";2
3const { emit, on } = broadcast<{ event: string }>();4
5const onEvent = on("event");6const offEvent = onEvent(console.log);7
8const emitEvent = emit("event");9emitEvent("Hello world 1"); // Logs "Hello world 1"10emitEvent("Hello world 2"); // Logs "Hello world 2"11offEvent();12emitEvent("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 happens14</script>
Useful links
- ๐ Documentation: TypeDoc generated documentation.
- โณ Changelog: List of changes between versions.
- โ Tests Coverage: Coveralls page with tests coverage.
Type Aliases
Emitter<Data>
1type 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
Type parameter | Description |
---|---|
Data | Data type. |
EventListener<Data>
1type EventListener<Data>: Single<Data> extends Single<never> ? () => void : Unary<Data, void>;
Event listener unary function.
See
Type parameters
Type parameter | Description |
---|---|
Data | Data type. |
EventRegistry<Events>
1type EventRegistry<Events>: { readonly [Event in keyof Events]?: ReadOnlyArray<EventListener<Readonly<Events[Event]>>> };
Registry of event names to array of listeners.
See
Example
1const eventRegistry = {2 example: [() => console.log("example called")],3};
Type parameters
Type parameter | Description |
---|---|
Events extends EventTypeDictionary | Event registry. |
EventTypeDictionary
1type EventTypeDictionary: ReadOnlyRecord<string>;
Dictionary of event name to event types.
Example
1const example = {2 eventName: TypeOfEvent,3} satisfies EventTypeDictionary;
See
Functions
broadcast()
1function broadcast<Events>(eventRegistry: EventRegistry<Events>): object;
Creates a new โbroadcastโ object, which has emit
and on
with a shared
eventRegistry
.
Type parameters
Type parameter | Description |
---|---|
Events extends Readonly <Record <string , unknown >> | Event registry. |
Parameters
Parameter | Type | Description |
---|---|---|
eventRegistry | EventRegistry <Events > | Optional record of event names mapped to an array of listeners. |
Returns
object
Object with emit
and on
functions.
Member | Type | Value |
---|---|---|
emit | <Event >(event : Event ) => Emitter <Events [Event ]> | โฆ |
on | <Event >(event : Event ) => (listener : EventListener <Events [Event ]>) => () => undefined | โฆ |
Example
1const { emit, on } = broadcast<{ event: string }>();2const off = on("event")(console.log);3emit("event")("Hello world"); // Logs "Hello world"4off();5emit("event")("Nope"); // Nothing happens
emit()
1function emit<Events>(2 eventRegistry: EventRegistry<Events>,3): <Event>(event: Event) => Emitter<Events[Event]>;
Creates a curried function to emit events for listeners of the given
eventRegistry
.
Type parameters
Type parameter | Description |
---|---|
Events extends Readonly <Record <string , unknown >> | Event registry. |
Parameters
Parameter | Type | Description |
---|---|---|
eventRegistry | EventRegistry <Events > | Record of event names mapped to an array of listeners. |
Returns
Function
Curried function with eventRegistry
in context.
Creates a curried function to emit an event of the eventRegistry
in context.
Type parameters
Type parameter | Description |
---|---|
Event extends string | number | symbol | Event name. |
Parameters
Parameter | Type | Description |
---|---|---|
event | Event | Event name (has to be a valid key of the eventRegistry ). |
Returns
Emitter
<Events
[Event
]>
Curried function with eventRegistry
and event
in context.
Example
1const eventRegistry = {};2const emitRegistry = emit(eventRegistry);3const emitEvent = emitRegistry("event"); // ๐๐ป You are here4emitEvent("data");
Example
1const eventRegistry = {};2const emitRegistry = emit(eventRegistry); // ๐๐ป You are here3const emitEvent = emitRegistry("event");4emitEvent("data");
on()
1function on<Events>(2 eventRegistry: EventRegistry<Events>,3): <Event>(4 event: Event,5) => (listener: EventListener<Events[Event]>) => () => undefined;
Creates a curried function to listen for calls to an event in the passed
eventRegistry
.
Type parameters
Type parameter | Description |
---|---|
Events extends Readonly <Record <string , unknown >> | Event registry. |
Parameters
Parameter | Type | Description |
---|---|---|
eventRegistry | EventRegistry <Events > | Record of event names mapped to an array of listeners. |
Returns
Function
Curried function with eventRegistry
in context.
Creates a curried function to listen for calls to an event of the
eventRegistry
in context.
Type parameters
Type parameter | Description |
---|---|
Event extends string | number | symbol | Event name. |
Parameters
Parameter | Type | Description |
---|---|---|
event | Event | Event name (has to be a valid key of the eventRegistry ). |
Returns
Function
Curried function with eventRegistry
and event
in context.
Parameters
Parameter | Type |
---|---|
listener | EventListener <Events [Event ]> |
Returns
Function
Returns
undefined
Example
1const eventRegistry = {};2const onRegistry = on(eventRegistry);3const onEvent = onRegistry("event"); // ๐๐ป You are here4const offEvent = onEvent(() => console.log("event called"));5emit(eventRegistry)("event")(); // Logs "event called"6offEvent();7emit(eventRegistry)("event")(); // Nothing happens
Example
1const eventRegistry = {};2const onRegistry = on(eventRegistry); // ๐๐ป You are here3const onEvent = onRegistry("event");4const offEvent = onEvent(() => console.log("event called"));5emit(eventRegistry)("event")(); // Logs "event called"6offEvent();7emit(eventRegistry)("event")(); // Nothing happens