forked from thepeacockproject/Peacock
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhooksImpl.ts
More file actions
175 lines (151 loc) · 4.79 KB
/
hooksImpl.ts
File metadata and controls
175 lines (151 loc) · 4.79 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* The Peacock Project - a HITMAN server replacement.
* Copyright (C) 2021-2026 The Peacock Project Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* A HookMap is a helper class for a Map with Hooks.
*
* @example
* const myHookMap = new HookMap(key => new SyncHook())
*
* @example
* hookMap.for("some-key").tap("MyPlugin", (arg) => { })
*
* @example
* const hook = hookMap.for("some-key")
* hook.call("some value", 123456)
*/
export class HookMap<Hook> {
private readonly _map: Map<string, Hook>
public constructor(private readonly _createFunc: (key: string) => Hook) {
this._map = new Map()
}
/**
* Get a hook for the given key.
*
* @param key The hook to get.
* @returns The hook.
*/
public for(key: string): Hook {
if (this._map.has(key)) {
return this._map.get(key)!
}
const hook = this._createFunc(key)
this._map.set(key, hook)
return hook
}
}
/**
* The options for a hook. Will either be just the name (as a string), or an object containing the additional options.
*/
export type TapOptions = string | { name: string; context: boolean }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AsArray<T> = T extends any[] ? T : [T]
/**
* An internal interface containing the properties held by a single taps' container object.
*/
interface Tap<T, R> {
name: string
func: (...args: AsArray<T>) => R
enableContext: boolean
}
/**
* The base for a hook.
*
* @see SyncHook
* @see SyncBailHook
* @see AsyncSeriesHook
*/
export abstract class BaseImpl<Params, Return = void> {
protected _taps: Tap<Params, Return>[] = []
/**
* Tap the hook.
*
* @param nameOrOptions A string containing the tap's name, or an object containing the tap's details.
* @param consumer The function that will be called when the hook is.
* @see TapOptions
*/
public tap(
nameOrOptions: TapOptions,
consumer: (...args: AsArray<Params>) => Return,
): void {
const name =
typeof nameOrOptions === "string"
? nameOrOptions
: nameOrOptions.name
const enableContext =
typeof nameOrOptions === "string" ? false : nameOrOptions.context
this._taps.push({
name,
func: consumer,
enableContext,
})
}
/**
* Destructively remove all taps from the hook.
* Are you SURE you know what you're doing if you want to use this?
*/
public resetTaps(): void {
this._taps = []
}
public get allTapNames(): string[] {
return this._taps.map((t) => t.name)
}
}
/**
* A hook that runs each tap one-by-one.
*/
export class SyncHook<Params> extends BaseImpl<Params> {
public call(...params: AsArray<Params>): void {
const context = {}
for (const tap of this._taps) {
const args = tap.enableContext ? [context, ...params] : [...params]
// @ts-expect-error TypeScript things.
tap.func(...args)
}
return
}
}
/**
* A hook that runs each tap one-by-one until one returns a result.
*/
export class SyncBailHook<Params, Return> extends BaseImpl<Params, Return> {
public call(...params: AsArray<Params>): Return | null {
const context = {}
for (const tap of this._taps) {
const args = tap.enableContext ? [context, ...params] : [...params]
// @ts-expect-error TypeScript things.
const result = tap.func(...args)
if (result) {
return result
}
}
return null
}
}
/**
* A hook that runs each tap, one-by-one, in an async context (each tap may be an async function).
*/
export class AsyncSeriesHook<Params> extends BaseImpl<Params, Promise<void>> {
public async callAsync(...params: AsArray<Params>): Promise<void> {
const context = {}
for (const tap of this._taps) {
const args = tap.enableContext ? [context, ...params] : [...params]
// @ts-expect-error TypeScript things.
await tap.func(...args)
}
}
}