Skip to content

Instantly share code, notes, and snippets.

@sccolbert
Created February 19, 2016 18:20
Show Gist options
  • Save sccolbert/11a3fb4d2c3ba6f10dcc to your computer and use it in GitHub Desktop.
Save sccolbert/11a3fb4d2c3ba6f10dcc to your computer and use it in GitHub Desktop.
interface ISignal {
_ISignalStructuralProperty: void;
}
interface ISignal1<T1> {
_ISignal1StructuralPropertyT1: T1;
}
interface ISignal2<T1, T2> {
_ISignal2StructuralPropertyT1: T1;
_ISignal2StructuralPropertyT2: T2;
}
interface ISignal3<T1, T2, T3> {
_ISignal3StructuralPropertyT1: T1;
_ISignal3StructuralPropertyT2: T2;
_ISignal3StructuralPropertyT3: T3;
}
interface ISignal4<T1, T2, T3, T4> {
_ISignal4StructuralPropertyT1: T1;
_ISignal4StructuralPropertyT2: T2;
_ISignal4StructuralPropertyT3: T3;
_ISignal4StructuralPropertyT4: T4;
}
function createSignal(): ISignal;
function createSignal<T1>(): ISignal1<T1>;
function createSignal<T1, T2>(): ISignal2<T1, T2>;
function createSignal<T1, T2, T3>(): ISignal3<T1, T2, T3>;
function createSignal<T1, T2, T3, T4>(): ISignal4<T1, T2, T3, T4>;
function createSignal(): any {
return Object.create(null);
}
interface ISlot<T> {
(sender: T): void;
}
interface ISlot1<T, A1> {
(sender: T, a1: A1): void;
}
interface ISlot2<T, A1, A2> {
(sender: T, a1: A1, a2: A2): void;
}
interface ISlot3<T, A1, A2, A3> {
(sender: T, a1: A1, a2: A2, a3: A3): void;
}
interface ISlot4<T, A1, A2, A3, A4> {
(sender: T, a1: A1, a2: A2, a3: A3, a4: A4): void;
}
class Emitter {
connect(signal: ISignal, slot: ISlot<this>): void;
connect<A1>(signal: ISignal1<A1>, slot: ISlot1<this, A1>): void;
connect<A1, A2>(signal: ISignal2<A1, A2>, slot: ISlot2<this, A1, A2>): void;
connect<A1, A2, A3>(signal: ISignal3<A1, A2, A3>, slot: ISlot3<this, A1, A2, A3>): void;
connect<A1, A2, A3, A4>(signal: ISignal4<A1, A2, A3, A4>, slot: ISlot4<this, A1, A2, A3, A4>): void;
connect(signal: any, slot: any): void {
}
disconnect(signal: ISignal, slot: ISlot<this>): void;
disconnect<A1>(signal: ISignal1<A1>, slot: ISlot1<this, A1>): void;
disconnect<A1, A2>(signal: ISignal2<A1, A2>, slot: ISlot2<this, A1, A2>): void;
disconnect<A1, A2, A3>(signal: ISignal3<A1, A2, A3>, slot: ISlot3<this, A1, A2, A3>): void;
disconnect<A1, A2, A3, A4>(signal: ISignal4<A1, A2, A3, A4>, slot: ISlot4<this, A1, A2, A3, A4>): void;
disconnect(signal: any, slot: any): void {
}
protected emit(signal: ISignal): void;
protected emit<A1>(signal: ISignal1<A1>, a1: A1): void;
protected emit<A1, A2>(signal: ISignal2<A1, A2>, a1: A1, a2: A2): void;
protected emit<A1, A2, A3>(signal: ISignal3<A1, A2, A3>, a1: A1, a2: A2, a3: A3): void;
protected emit<A1, A2, A3, A4>(signal: ISignal4<A1, A2, A3, A4>, a1: A1, a2: A2, a3: A3, a4: A4): void;
protected emit(signal: any, a1?: any, a2?: any, a3?: any, a4?: any): void {
}
}
// Example
class MyClass extends Emitter {
static changed = createSignal();
static textChanged = createSignal<string>();
static tabMoved = createSignal<number, number>();
doThing(): void {
// ...
this.emit(MyClass.changed);
}
setText(text: string): void {
// ...
this.emit(MyClass.textChanged, text);
}
moveTab(fromIndex: number, toIndex: number) {
// ...
this.emit(MyClass.tabMoved, fromIndex); // error - missing argument
this.emit(MyClass.tabMoved, fromIndex, toIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment