-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrickyard.ts
221 lines (201 loc) · 5.6 KB
/
brickyard.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
import { is_obj } from "./is-obj.util.ts";
/**
* @description
* ### How to use:
* #### Minimal example:
* ```ts
* // bricks.ts
* import { some_fn } from "./some_fn.ts";
* import { another_fn } from "./another_fn.ts";
*
* export const bircks = Brickyard
* .init(Brickyard.pre_init().complete())
* .enroll({ some_fn, another_fn });
*
* // main.ts
* import { bricks } from "./bricks.ts";
*
* bricks.some_fn();
* ```
*
* > but actualy example above works exactly as a simple reexport.
*
* #### Usefull example:
*
* ```ts
* // bricks.ts
* import { some_fn } from "./some_fn.ts";
* import { another_fn } from "./another_fn.ts";
*
* const interceptor = Brickyard
* .intercept('some_fn', { fn: () => 'intercepted some_fn' })
* .pre_init()
* .complete();
*
* export const bircks = Brickyard
* .init(interceptor)
* .enroll({ some_fn, another_fn });
*
* // main.ts
* import { bricks } from "./bricks.ts";
*
* bricks.some_fn();
* ```
*
* #### Advanced example:
* > the same as above except the interceptor is placed in the separate file and ignored by git.
* > but you can change the implementation of `some_fn` and `another_fn` without changing the code.
*
* ```ts
* // .interceptor.ts
* import { some_fn } from "./some_fn.ts";
* import { another_fn } from "./another_fn.ts";
*
* export const interceptor = Brickyard
* .pre_init()
* .intercept('some_fn', { fn: () => 'intercepted some_fn' })
* .complete();
*
* // bricks.ts
* import { interceptor } from "./.interceptor.ts";
*
* export const bircks = Brickyard
* .init(interceptor)
* .enroll({ some_fn, another_fn });
*
* // main.ts
* import { bricks } from "./bricks.ts";
*
* bricks.some_fn();
* ```
*
* #### P.S. if you hide your interceptor file with `.gitignore` you should create this file manually (even if you don't want to do interceptions)
* > so it can be as possible simple:
*
* ```ts
* // .interceptor.ts, ignored by git
* export const interceptor = Brickyard.pre_init().complete();
* ```
*/
export class Brickyard {
private constructor() {
}
/**
* @description
* To start register your interceptors
*/
public static pre_init(): {
intercept: Brickyard["intercept"];
complete: () => BricksInterceptor;
} {
const brickyard = new Brickyard();
return {
intercept: brickyard.intercept.bind(brickyard),
complete: () => brickyard.#interceptor,
};
}
#interceptor = new BricksInterceptor(this);
/**
* @description
* To start register your functions for reexport
*/
public static init(
interceptor: BricksInterceptor,
): Pick<Brickyard, "enroll"> {
return interceptor.brickyard as Pick<Brickyard, "enroll">;
}
#stuff = new Map<string, InterceptorType>();
/**
* @description
* Register interceptor for the function with `id`.
* (will be ignored if no such `id` and throw error if `id` is already the member of interceptor)
*/
public intercept<T extends Fn>(
id: string,
interceptor: InterceptorType<T>,
): {
intercept: Brickyard["intercept"];
complete: () => BricksInterceptor;
} {
if (this.#stuff.has(id)) {
throw new Error(`This id (${id}) is already the member of Brickyard`);
}
this.#stuff.set(id, interceptor);
return {
intercept: this.intercept.bind(this),
complete: () => new BricksInterceptor(this),
};
}
/**
* @description
* Pass the object with functions to enroll them.
* And use returned result as reexport of the original stuff for your project.
*/
public enroll<T extends Record<string, Fn>>(candidates: T): T {
if (this.#stuff.size === 0) {
console.warn("No interceptors found");
console.info("Be sure to call .intercept() method before .enroll()");
}
const reexport = Object.fromEntries(
Object.entries(candidates).map(([id, origin]) => {
const enrolled = this._enroll(origin, id);
return [id, enrolled];
}),
);
return reexport as T;
}
private _enroll<T extends Fn>(cb: T, id: string) {
const interceptor = this.#stuff.get(id);
if (!interceptor) {
return cb;
}
const final_fn = interceptor.fn || cb;
const intercepted_args = interceptor.args;
if (intercepted_args) {
if (interceptor.args_strategy === "merge") {
return (...args: Parameters<T>) => {
const merged =
(args.length > intercepted_args.length ? args : intercepted_args)
.map((
_,
i,
) => {
const intercepted_a = intercepted_args[i];
const a = args[i];
if (is_obj(a) && is_obj(intercepted_a)) {
for (const key in intercepted_a) {
a[key] = intercepted_a[key];
}
return a;
}
return intercepted_a ?? a;
});
return final_fn(...merged);
};
} else {
return () => final_fn(...intercepted_args);
}
}
return final_fn;
}
}
type InterceptorType<T extends Fn = Fn> = {
fn: T;
args?: never;
args_strategy?: never;
} | {
fn?: T;
args: Parameters<T>;
args_strategy: "merge" | "replace";
};
type Fn = (...args: any[]) => any;
/**
* @description
* This is mostly util-helper class.
* So it is a kind of a trick to make you call pre_init() method before init() method.
* It's purpose is to enforce you place it into the Brickyard.init() method.
* But because of to get it your should first call Brickyard.pre_init() method.
*/
class BricksInterceptor {
constructor(public brickyard: Brickyard) {}
}