-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.d.ts
85 lines (77 loc) · 3.82 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
export enum Role {
event = "event",
notify = "notify",
done = "done",
catchException = "catch",
observe = "obsereve",
}
export interface Property<Name extends string, Value = any> {
name: Name;
role: Role.event | Role.notify;
sticky?: boolean;
stickyLast?: boolean;
emittedType?: Value;
}
type Omit<T, K> = { [key in Exclude<keyof T, K>]: T[key] };
export interface DefaultInterfaces<DoneType = void, ErrorType = Error> {
done: DoneType;
error: ErrorType;
catch: Error;
always: DoneType | ErrorType | Error;
event: any;
notify: any
tap: {
name: string;
role?: Role;
data: any[];
}
}
type Registerer<Interfaces, K extends keyof Interfaces, J extends JarvisEmitter<any, any, Interfaces>> = (listener: (arg: Interfaces[K]) => void) => J;
type Resolver<Interfaces, K extends keyof Interfaces, J extends JarvisEmitter<any, any, Interfaces>> = (arg: Interfaces[K]) => J;
type Remover<Interfaces, K extends keyof Interfaces, J extends JarvisEmitter<any, any, Interfaces>> = (listener?: (arg: Interfaces[K]) => void) => J;
type Middleware<Interfaces, K extends keyof Interfaces, J extends JarvisEmitter<any, any, Interfaces>> = <ChangedArg extends any>(listener: (next: (arg: ChangedArg) => void, arg: Interfaces[K]) => void) =>
JarvisEmitter<
K extends "done" ?
ChangedArg :
Interfaces extends DefaultInterfaces ? Interfaces["done"] : void,
K extends "error" ?
ChangedArg :
Interfaces extends DefaultInterfaces ? Interfaces["error"] : void,
Omit<Interfaces, K> & { [key in K]: ChangedArg }
>;
interface InterfaceEntry<Interfaces, K extends keyof Interfaces, J extends JarvisEmitter<any, any, Interfaces>> {
registerer: Registerer<Interfaces, K, J>;
resolver: Resolver<Interfaces, K, J>;
remover: Remover<Interfaces, K, J>;
middleware: Middleware<Interfaces, K, J>;
name: K;
role: Role;
purge: Function;
}
type ExtractJarvisEmitterDoneType<J extends JarvisEmitter<any, any>> = J extends JarvisEmitter<infer DoneType, any> ? DoneType : void;
type ExtractJarvisEmitterErrorType<J extends JarvisEmitter<any, any>> = J extends JarvisEmitter<any, infer ErrorType> ? ErrorType : Error;
declare class JarvisEmitter<DoneType = void, ErrorType = Error, Interfaces = DefaultInterfaces<DoneType, ErrorType>> {
constructor();
on: {
[key in keyof Interfaces]: Registerer<Interfaces, key, JarvisEmitter<DoneType, ErrorType, Interfaces>>
};
call: {
[key in keyof Interfaces]: Resolver<Interfaces, key, JarvisEmitter<DoneType, ErrorType, Interfaces>>
};
off: {
[key in keyof Interfaces]: Remover<Interfaces, key, JarvisEmitter<DoneType, ErrorType, Interfaces>>
};
middleware: {
[key in keyof Interfaces]: Middleware<Interfaces, key, JarvisEmitter<DoneType, ErrorType, Interfaces>>
};
extend<K extends string, V extends any, T extends Property<K, V>>(interfaceProps: T): JarvisEmitter<DoneType, ErrorType, Interfaces & { [key in T["name"]]: T["emittedType"] }>;
promise(): Promise<DoneType>;
pipe<T extends JarvisEmitter<any, any, any>>(emitter: T): JarvisEmitter<DoneType, ErrorType, Interfaces>;
getRolesHandlers(role: Role): InterfaceEntry<Interfaces, keyof Interfaces, JarvisEmitter<DoneType, ErrorType, Interfaces>>[];
getHandlersForName<T extends keyof Interfaces>(name: T, role?: string): InterfaceEntry<Interfaces, T, JarvisEmitter<DoneType, ErrorType, Interfaces>>;
destroy(): void;
static some<J extends Array<JarvisEmitter<any, any>>>(...emitters: J): JarvisEmitter<Array<ExtractJarvisEmitterDoneType<J[number]> | undefined>, never>;
static all<J extends JarvisEmitter<any, any>[]>(...emitters: J): JarvisEmitter<Array<ExtractJarvisEmitterDoneType<J[number]>>, ExtractJarvisEmitterErrorType<J[number]>>;
static emitifyFromAsync<I extends any[], O>(fn: (...args: I) => Promise<O>): (...callArgs: I) => JarvisEmitter<O, any>;
}
export default JarvisEmitter;