npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rbxts/beacon

v2.1.1

Published

Beacon is a signal implementation for sending and receiving events

Downloads

893

Readme

Beacon

Beacon is a signal implementation for sending and receiving events.

Signal API

Types

type SignalParams<T> = Parameters<
	T extends unknown[] ? (...args: T) => never : T extends unknown ? (arg: T) => never : () => never
>;

type SignalCallback<T> = (...args: SignalParams<T>) => unknown;

type SignalWait<T> = T extends unknown[] ? LuaTuple<T> : T;

Constructor

const signal = new Signal<T>();

Signal.Connect

public Connect(callback: SignalCallback<T>): Connection<T>

Connects a callback, which will be called each time the signal is fired. The returned connection can be disconnected to stop receiving events.

Signal.Once

public Once(callback: SignalCallback<T>): Connection<T>

Same as Signal.Connect, but disconnects itself after the first time it is triggered.

Signal.Fire

public Fire(...args: SignalParams<T>): void

Fires the signal with the given event. All connected callbacks will receive the event. Internally, this uses task.spawn to fire each callback.

Signal.FireDeferred

public FireDeferred(...args: SignalParams<T>): void

Same as Signal.Fire, except uses task.defer internally.

Signal.Wait

public Wait(): SignalWait<T>

Yields the calling thread until the signal is next fired. Returns the fired event.

Signal.DisconnectAll

public DisconnectAll(): void

Disconnects all connections on the signal.

Signal.Destroy

public Destroy(): void

Alias for Signal.DisconnectAll.

Connection API

Connection.Connected

public Connected: boolean

Indicates if the connection is currently connected.

Connection.Disconnect

public Disconnect(): void

Disconnects the connection.

Connection.Destroy

public Destroy(): void

Alias for Connection.Disconnect.

Example

const messenger = new Signal<string>();

const connection = messenger.Connect((msg) => {
	print(`Got message: ${msg}`);
});

messenger.Fire("Hello world!");
connection.Disconnect();
messenger.Fire("No one will see this");

// The spawned thread will wait indefinitely until the
// signal is fired. If all connections are disconnected
// using signal.Destroy() or signal.DisconnectAll(), then
// the waiting thread will be closed.
task.spawn(() => {
	const msg = messenger.Wait();
	print(`Got message from waiting: ${msg}`);
});
task.wait(2);
messenger.Fire("Hello to the waiting thread");

// Destroying isn't necessary for cleanup, but is nice when
// using signals in OOP environments for quick cleanup.
messenger.Destroy();

Different number of arguments

// No args:
const signal = new Signal<void>();
signal.Connect(() => {});
signal.Fire();

// One arg:
const signal = new Signal<number>();
signal.Connect((n) => print(n));
signal.Fire(32);

// One arg, named (preferred over the above):
const signal = new Signal<[points: number]>();
signal.Connect((points) => print(points));
signal.Fire(64);

// Multiple args:
const signal = new Signal<[msg: string, value: number, cool: boolean]>();
signal.Connect((msg, value, cool) => print(msg, cool, value));
signal.Fire("hello", 10, true);