init commit
This commit is contained in:
40
js/core/BO3ScriptSignal.js
Normal file
40
js/core/BO3ScriptSignal.js
Normal file
@@ -0,0 +1,40 @@
|
||||
// A simple event system that mimics Roblox's RBXScriptSignal.
|
||||
|
||||
export class BO3ScriptSignal {
|
||||
constructor() {
|
||||
this._connections = [];
|
||||
}
|
||||
|
||||
// Connect a function to the signal.
|
||||
Connect(fn) {
|
||||
const connection = { fn, connected: true };
|
||||
this._connections.push(connection);
|
||||
return {
|
||||
Disconnect: () => {
|
||||
connection.connected = false;
|
||||
this._connections = this._connections.filter(c => c !== connection);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Fire all connected callbacks with arguments.
|
||||
Fire(...args) {
|
||||
for (const c of this._connections) {
|
||||
if (c.connected) {
|
||||
try {
|
||||
c.fn(...args);
|
||||
} catch (err) {
|
||||
console.error("[BO3ScriptSignal] Error in connected function:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Disconnect all listeners.
|
||||
DisconnectAll() {
|
||||
this._connections = [];
|
||||
}
|
||||
get [Symbol.toStringTag]() {
|
||||
return "BO3ScriptSignal";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user