Beacon is a signal implementation for sending and receiving events
npm install @rbxts/beaconts
type SignalParams = Parameters<
T extends unknown[] ? (...args: T) => never : T extends unknown ? (arg: T) => never : () => never
>;type SignalCallback = (...args: SignalParams) => unknown;
type SignalWait = T extends unknown[] ? LuaTuple : T;
`$3
`ts
const signal = new Signal();
`$3
`ts
public Connect(callback: SignalCallback): Connection
`
Connects a callback, which will be called each time the signal is fired. The returned connection can be disconnected to stop receiving events.$3
`ts
public Once(callback: SignalCallback): Connection
`
Same as Signal.Connect, but disconnects itself after the first time it is triggered.$3
`ts
public Fire(...args: SignalParams): void
`
Fires the signal with the given event. All connected callbacks will receive the event. Internally, this uses task.spawn to fire each callback.$3
`ts
public FireDeferred(...args: SignalParams): void
`
Same as Signal.Fire, except uses task.defer internally.$3
`ts
public Wait(): SignalWait
`
Yields the calling thread until the signal is next fired. Returns the fired event.$3
`ts
public DisconnectAll(): void
`
Disconnects all connections on the signal.$3
`ts
public Destroy(): void
`
Alias for Signal.DisconnectAll.Connection API
$3
`ts
public Connected: boolean
`
Indicates if the connection is currently connected.$3
`ts
public Disconnect(): void
`
Disconnects the connection.$3
`ts
public Destroy(): void
`
Alias for Connection.Disconnect.Example
`ts
const messenger = new Signal();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();
`$3
`ts
// No args:
const signal = new Signal();
signal.Connect(() => {});
signal.Fire();// One arg:
const signal = new Signal();
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);
``