0.2.0 • Published 2 months ago

signal-async v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

signal-async

NPM Version

distinct utilities for signaling in async contexts.

Installation

npm install signal-async

Usage

Dirty

Dirty signals are signals that can be marked and listened to. They trigger active listeners when they are emitted to.

import { dirty } from 'signal-async';

const { emit, signal } = dirty();

signal().then(() => {
	console.log('signal received');
});

emit();

However, if a signal is emitted before a listener is attached, the listener will not be triggered.

emit();

signal().then(() => {
	console.log('signal received'); // not triggered
});

Events

Events build on top of dirty signals. They allow for enqueuing data in a FILO queue and applying async iteration over the queue.

import { event } from 'signal-async';

const { iterator, enqueue } = event<number>();

enqueue(1);
enqueue(2);

for await (const item of iterator) {
	console.log(item);	// 1, 2
}

State

State allows for listening for a certain value, perfect for waiting for a 'ready' state or transmitting IO between async contexts.

import { state } from 'signal-async';

const { waitFor, set } = state<number>(1);

waitFor(2).then(() => console.log("this will print!"));

set(2);

Unlike dirty, this works even if the value is set before the listener is attached.

set(2);

waitFor(2).then(() => console.log("this will print!"));
0.2.0

2 months ago

0.1.3

2 months ago

0.1.2

2 months ago

0.1.1

2 months ago

0.1.0

2 months ago