Notify by Lou
π£ Minimalistic Pub/Sub implementation.
Usage
π¦ Node
Install @lou.codes/notify
as a dependency:
pnpm add @lou.codes/notify# ornpm install @lou.codes/notify# oryarn 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>
Useful links
- π Documentation: TypeDoc generated documentation.
- β³ Changelog: List of changes between versions.
- β Tests Coverage: Coveralls page with tests coverage.
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
Name | Description |
---|---|
Data | Data type. |
EventListener
Ζ¬ EventListener<Data
>: Single
<Data
> extends Single
<never
> ?
Function
<[], void
> : Unary
<Data
, void
>
Event listener unary function.
See
Type parameters
Name | Description |
---|---|
Data | Data type. |
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
Name | Type | Description |
---|---|---|
Events | extends EventTypeDictionary | Event registry. |
EventTypeDictionary
Ζ¬ EventTypeDictionary: ReadOnlyRecord
<string
>
Dictionary of event name to event types.
See
Functions
broadcast
βΈ broadcast<Events
>(eventRegistry?
): Object
Creates a new βbroadcastβ object, which has emit
and on
with a shared
eventRegistry
.
Type parameters
Name | Type | Description |
---|---|---|
Events | extends Object | Event registry. |
Parameters
Name | Type | Description |
---|---|---|
eventRegistry | EventRegistry <Events > | Optional record of event names mapped to an array of listeners. |
Returns
Object
Object with emit
and on
functions.
Name | Type |
---|---|
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
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
Name | Type | Description |
---|---|---|
Events | extends Object | Event registry. |
Parameters
Name | Type | Description |
---|---|---|
eventRegistry | EventRegistry <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
Name | Type | Description |
---|---|---|
Event | extends string | Event name. |
Parameters
Name | 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
const eventRegistry = {};const emitRegistry = emit(eventRegistry);const emitEvent = emitRegistry("event"); // ππ» You are hereemitEvent("data");
Example
const eventRegistry = {};const emitRegistry = emit(eventRegistry); // ππ» You are hereconst emitEvent = emitRegistry("event");emitEvent("data");
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
Name | Type | Description |
---|---|---|
Events | extends Object | Event registry. |
Parameters
Name | Type | Description |
---|---|---|
eventRegistry | EventRegistry <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
Name | Type | Description |
---|---|---|
Event | extends string | Event name. |
Parameters
Name | Type | Description |
---|---|---|
event | Event | Event 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
Name | Type | Description |
---|---|---|
listener | EventListener <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 hereemit(eventRegistry)("event")(); // Logs "event called"offEvent();emit(eventRegistry)("event")(); // Nothing happens
Example
const eventRegistry = {};const onRegistry = on(eventRegistry);const onEvent = onRegistry("event"); // ππ» You are hereconst 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 hereconst onEvent = onRegistry("event");const offEvent = onEvent(() => console.log("event called"));emit(eventRegistry)("event")(); // Logs "event called"offEvent();emit(eventRegistry)("event")(); // Nothing happens