GithubHelp home page GithubHelp logo

scyfren / rbxts-beacon Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sleitnick/rbxts-beacon

0.0 0.0 0.0 73 KB

Beacon is a signal implementation for sending and receiving events

Home Page: https://www.npmjs.com/package/@rbxts/beacon

License: MIT License

TypeScript 100.00%

rbxts-beacon's Introduction

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.

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);

rbxts-beacon's People

Contributors

sleitnick avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.