-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
230 lines (171 loc) · 4.97 KB
/
index.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//@ts-nocheck
export type Promisable<T = unknown> = T | Promise<T>;
export type Next<I = unknown, O = unknown> = (
input?: I
) => Promisable<O>;
export type Middleware<I = unknown, O = unknown> = (
input: I,
next: Next<I, O>,
) => Promisable<O>;
export type Middlewares<I = unknown, O = unknown> =
Middleware<I, O>[];
export type PipelineLike<I = unknown, O = unknown> =
{ middlewares: Middlewares<I, O> };
export type MiddlewareInput<I = unknown, O = unknown> =
| Middleware<I, O>
| Middlewares<I, O>
| PipelineLike<I, O>;
export interface Plugin {
name: string;
}
const PipelineSymbol = Symbol.for('PLUGGABLE_PIPELINE');
export class Context<I> {
private currentValue: I;
public constructor(input: I) {
this.currentValue = input;
}
public use() {
return new Proxy(this as Context<I>, {
get(target, prop) {
if (prop === 'value') {
return target.get();
}
return Reflect.get(target, prop);
},
set(target, prop, value) {
if (prop === 'value') {
target.set(value as I);
return true
}
return Reflect.set(target, prop, value);
},
});
}
public get(): I {
return this.currentValue;
}
public set(input: I): I {
return this.currentValue = input;
}
}
export class Pipeline<I, O> implements PipelineLike<I, O> {
public readonly middlewares: Middlewares<I, O> = [];
public use(...inputs: MiddlewareInput<I, O>[]): Pipeline<I, O> {
this.middlewares.push(...inputs.flatMap(getMiddlewares));
return this as Pipeline<I, O>;
}
public run(input: I) {
return this.dispatch(0, input)
}
public dispatch(index: number, input: I) {
const callback = this.middlewares[index]
const next: Next<I, O> = (_input: I = input) => this.dispatch(index + 1, _input)
return callback(input, next)
}
public readonly [PipelineSymbol] = true;
}
export abstract class Pluggable {
public readonly plugins: Map<string, Plugin> = new Map()
public reset() {
this.plugins.clear()
}
}
export class Beacon extends Pluggable { }
export function isPipeline<I, O>(input: unknown): input is Pipeline<I, O> {
return Boolean((input as Pipeline<I, O>)?.[PipelineSymbol]);
}
export function isMiddleware<I, O>(input: unknown): input is Middleware<I, O> {
return typeof input === 'function';
}
export function isMiddlewares<I, O>(input: unknown): input is Middlewares<I, O> {
return Array.isArray(input);
}
export function isPipelineLike<I, O>(input: unknown): input is PipelineLike<I, O> {
return typeof input === 'object' && input !== null && 'middlewares' in input;
}
export function getMiddlewares<I, O>(
input: MiddlewareInput<I, O>
): Middlewares<I, O> {
if (isMiddleware<I, O>(input))
return [input]
if (isMiddlewares<I, O>(input))
return input.flatMap(middleware => getMiddlewares(middleware));
if (isPipelineLike<I, O>(input))
return getMiddlewares(input.middlewares);
throw new Error(`The provided input is of type ${input}, but a Middleware, Middlewares array,
or PipelineLike object was expected.`);
}
export function createContext<I>(input: I) {
const context = new Context<I>(input);
return context
}
export function createPipeline<I, O>() {
const pipeline = new Pipeline<I, O>()
return pipeline
}
export function createHooks() { }
export function createApis() { }
export function defineConfig() {
const beacon = new Beacon()
return beacon
}
function pluginA() {
return {
name: "PluginA",
setup(api, context) {
api.useConfigContext()
api.useAppContext()
return {
beforeCreated() { },
created() { }
}
}
}
}
export type Hook = () => void;
export type AnyHook = Hook
export type Hooks = Record<string, AnyHook>
export type Model<I, O> = Pipeline<I, O>
export type AnyModel = Model<any, any>
export type Models = Record<string, AnyModel>
function makeRunner<H extends Hooks, M extends Models>(
hooksList: H[],
models: M
) {
const runner = Object.create(null)
if (models) {
for (const key in models) {
hooksList.forEach(hooks => {
if (hooks?.[key]) {
models[key].use(hooks[key])
}
})
runner[key] = () =>
(models[key] as any).run();
}
}
return runner
}
import { SyncHook } from "tapable"
export class Pipeline<I, O> implements PipelineLike<I, O> {
public readonly middlewares: Middlewares<I, O> = [];
public use(...inputs: MiddlewareInput<I, O>[]): Pipeline<I, O> {
this.middlewares.push(...inputs.flatMap(getMiddlewares));
return this as Pipeline<I, O>;
}
public run(input: I) {
return this.dispatch(0, input)
}
public dispatch(index: number, input: I) {
const callback = this.middlewares[index]
const next: Next<I, O> = (_input: I = input) => this.dispatch(index + 1, _input)
return callback(input, next)
}
public readonly [PipelineSymbol] = true;
}
class SyncHook extends Pipeline {
public call(input) {
}
}
class SyncBailHook extends Pipeline {
}