Skip to content

Focused on developing a TypeScript-compatible solution to @robloxiandemo/roblox-modules/signal-plus!

License

Notifications You must be signed in to change notification settings

rbxts-libraries/rbxts-signal-plus

Repository files navigation

Signal Plus

Signal (plus) is a fairly personalized, all-in-one, and lightweight implementation served for communication between scripts across the same environment (client -> client and server -> server).

Installation

📦 — NPM:

npm i @rbxts/signal-plus

🧶 — Yarn:

yarn add @rbxts/signal-plus

📀 — pnPM:

pnpm add @rbxts/signal-plus

Signal API

Types

interface Destroyable {
	Destroy(): void;
}

type Object =
	| never
	| undefined
	| ((this: defined) => void)
	| ((_: defined) => void)
	| ExtractKeys<defined, () => void>
	| thread
	| RBXScriptConnection
	| Signal
	| Signal.Destroyable
	| Signal.Connection;

Constructor

const signal = new Signal<string>("Local_Signal");

Signal.Is

public Is(
		object?: T extends Signal.Object | Signal.Destroyable | true ? RBXScriptConnection : T | defined,
	): boolean;

Returns whether or not the specified class is a valid Signal reference.

Signal.Connect

public Connect(Callback: Callback): (Callback: Callback);

Connect to the signal while awaiting on a fire to successfully load for the specified callback.

Signal.ConnectOnce

public ConnectOnce(Callback: Callback): (Callback: Callback);

Unlike the normal connect method, this will run once.

Signal.ConnectParallel

public ConnectParallel(Callback: Callback): (Callback: Callback);

Unlike the normal connect method, this will run in parallel, resulting in zero code interference.

Signal.ConnectToOnClose

ConnectToOnClose(Callback: Callback): (Callback: Callback);

Unlike the normal connect method, this will run when specified callback when the server's closing.

Signal.Wait

public Wait(Callback: Callback): Signal.Wait;

Wait for the connection to be fired and then return any retrieved values.

Signal.Fire

public Fire(...args: Array<defined>): void;

Fire the current signal's connections.

Signal.FireUntil

FireUntil(Callback: Callback, ...args: Array<defined>): void;

Fire the current signal's connections until the specified callback is reached.

Signal.OnInvoke

OnInvoke(Callback: Callback): void;

Create a callback function that'd be activated on invoke, retrieving the function's callback.

Signal.Invoke

Invoke(...args: Array<defined>): (...args: Array<defined>);

Wait until the "OnInvoke" method exists and then invoke with the necessary arguments.

Signal.Destroy

public Destroy(): void;

Destroy and cleanup a Signal (making it unusable).

Example

// Modules
import { Signal } from "@rbxts/signal-plus";

// Functions
const Table: Array<number> = [1, 2, 3];

const TableSignal = new Signal<string>("TableSignal");

function callback(currentString: string, ...args: any[]): any | undefined {
	if (typeIs(currentString, "string") && currentString === "NewEntry") {
		for (const arg of args) {
			Table.push(arg);
		}

		print(Table);
	}
}

TableSignal.ConnectOnce(callback);

function callback() {
	while (true) {
		if (Table.includes(7)) {
			break;
		}

		task.wait(1);
	}
}

TableSignal.FireUntil(callback, "NewEntry", 1);

TableSignal.Destroy();

About

Focused on developing a TypeScript-compatible solution to @robloxiandemo/roblox-modules/signal-plus!

Resources

License

Stars

Watchers

Forks

Packages

No packages published