-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
plugin-store.ts
60 lines (48 loc) · 1.7 KB
/
plugin-store.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
import { EventEmitter } from 'node:events';
import type {
SimpleGitPlugin,
SimpleGitPluginType,
SimpleGitPluginTypes,
} from './simple-git-plugin';
import { append, asArray } from '../utils';
import type { SimpleGitPluginConfig } from '../types';
export class PluginStore {
private plugins: Set<SimpleGitPlugin<SimpleGitPluginType>> = new Set();
private events = new EventEmitter();
on<K extends keyof SimpleGitPluginConfig>(
type: K,
listener: (data: SimpleGitPluginConfig[K]) => void
) {
this.events.on(type, listener);
}
reconfigure<K extends keyof SimpleGitPluginConfig>(type: K, data: SimpleGitPluginConfig[K]) {
this.events.emit(type, data);
}
public append<T extends SimpleGitPluginType>(type: T, action: SimpleGitPlugin<T>['action']) {
const plugin = append(this.plugins, { type, action });
return () => this.plugins.delete(plugin);
}
public add<T extends SimpleGitPluginType>(
plugin: void | SimpleGitPlugin<T> | SimpleGitPlugin<T>[]
) {
const plugins: SimpleGitPlugin<T>[] = [];
asArray(plugin).forEach((plugin) => plugin && this.plugins.add(append(plugins, plugin)));
return () => {
plugins.forEach((plugin) => this.plugins.delete(plugin));
};
}
public exec<T extends SimpleGitPluginType>(
type: T,
data: SimpleGitPluginTypes[T]['data'],
context: SimpleGitPluginTypes[T]['context']
): typeof data {
let output = data;
const contextual = Object.freeze(Object.create(context));
for (const plugin of this.plugins) {
if (plugin.type === type) {
output = plugin.action(output, contextual);
}
}
return output;
}
}