-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin.ts
74 lines (66 loc) · 2.23 KB
/
plugin.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
import { IEmitterDictionary, IMiddleware, IWebcheck } from "./webcheck";
export interface IPlugin {
enable(): this;
disable(): this;
register(handle: IWebcheck): this;
// protected handle?: IWebcheck;
// protected on?: IEmitterDictionary;
// protected once?: IEmitterDictionary;
// protected middleware?: IMiddleware;
// protected init?: Function;
}
export abstract class Plugin implements IPlugin {
protected handle?: IWebcheck;
protected on?: IEmitterDictionary = {};
protected once?: IEmitterDictionary = {};
protected middleware?: IMiddleware;
protected init?: () => void;
public enable(...args: any[]): this {
if (!this.handle) {
throw new Error("You have to register the plugin in Webcheck first");
}
this.handle.emit("enablePlugin", this);
for (const hash in this.on) {
if (this.on.hasOwnProperty(hash)) {
this.handle.on(hash, this.on[hash]);
}
}
for (const hash in this.once) {
if (this.once.hasOwnProperty(hash)) {
this.handle.once(hash, this.once[hash]);
}
}
if (this.middleware) {
this.handle.middlewares.push(this.middleware);
}
if (typeof this.init === "function") {
this.init.apply(this, args as any);
}
return this;
}
public disable(): this {
if (!this.handle) {
throw new Error("You have to register the plugin in Webcheck first");
}
this.handle.emit("disablePlugin", this);
for (const hash in this.on) {
if (this.on.hasOwnProperty(hash)) {
this.handle.removeListener(hash, this.on[hash]);
}
}
for (const hash in this.once) {
if (this.once.hasOwnProperty(hash)) {
this.handle.removeListener(hash, this.once[hash]);
}
}
if (this.middleware) {
this.handle.middlewares.splice(this.handle.middlewares.indexOf(this.middleware), 1);
}
return this;
}
public register(handle: IWebcheck): this {
this.handle = handle;
this.handle.emit("registerPlugin", this);
return this;
}
}