mirror of
https://github.com/SikongJueluo/cc-utils.git
synced 2025-11-05 03:37:50 +08:00
reconstruct ccTUI, add Button component
This commit is contained in:
28
src/lib/ccTUI/Signal.ts
Normal file
28
src/lib/ccTUI/Signal.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Signal and Slot system similar to Qt
|
||||
* Allows components to communicate with each other
|
||||
*/
|
||||
export class Signal<T = void> {
|
||||
private slots: ((data?: T) => void)[] = [];
|
||||
|
||||
connect(slot: (data?: T) => void): void {
|
||||
this.slots.push(slot);
|
||||
}
|
||||
|
||||
disconnect(slot: (data?: T) => void): void {
|
||||
const index = this.slots.indexOf(slot);
|
||||
if (index !== -1) {
|
||||
this.slots.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
emit(data?: T): void {
|
||||
for (const slot of this.slots) {
|
||||
try {
|
||||
slot(data);
|
||||
} catch (e) {
|
||||
printError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user