Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
A simple pub/sub written in Typescript
import { Subscription } from '../dist';
const mySub = Subscription.create<number>();
const unsub = mySub.subscribe(num => {
console.log('num: ' + name);
});
mySub.call(45); // num: 45
unsub();
You have two ways to subscribe
/ unsubscribe
.
const mySub = () => {
/*...*/
};
subscription.subscribe(mySub);
// later
subscription.unsubscribe(mySub);
subscription.subscribe('mySubId', () => {
/*...*/
});
// later
subscription.unsubscribe('mySubId');
In both case the subscribe
return a function that will unsubscribe:
const unsub = subscription.subscribe(/*...*/);
// later
unsub();
In other words, calling subscribe
on an already subscribed listener is the same as calling unsubscribe
then subscribe
on that listener except you get the same unsubscribe
reference back.
unsubscribe
in a listener it will have effect immediatly.If the listener you unsubscribe is supposed to run after the current listener, it will not be called.
subscribe
in a listener it will not be called immediatly.But it will be in the next call
.
call()
in a listener it will defer the call to after the current call is done.Take a look at the Examples folder.
Create a new Subscription
options.onFirstSubscription?
: A function called when the listener count goes from 0 to 1options.onLastUnsubscribe?
: A function called when the listener count goes from 1 to 0options.maxListenerCount?
: The maximum number of listeners (default is 10000
)options.maxRecursiveCall?
: The maximum number of recursive call (calling call
in a listener) (default is 1000
)Note: by default the T
type is void
meaning the Subscription
has no data.
A subscription object
Add a listener
subId
(optional): Associate an id with the listener to be able to unsubscribe
by this same id.
listener
: The function that will be called when you call
, this function receive a value as parameter (of type T
)
return Unsubscribe
: returns a function that will unsubscribe the listener.
Call all listeners in the same order they were subscribed
value
: The value (of type T
) that will be passed to the listenersNote: If T
is void
this function does not take any arguments.
Unsubscribe all listeners
Unsubscribe a listener either by reference or by id
listener
: Either an id (string
) or a reference to a listenerTest wether a listener id subscribed or not
listener
: Either an id (string
) or a reference to a listenerFAQs
A simple pub/sub written in Typescript
The npm package suub receives a total of 7 weekly downloads. As such, suub popularity was classified as not popular.
We found that suub demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.