forked from inikulin/parse5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMixin.ts
More file actions
46 lines (38 loc) · 1.46 KB
/
Mixin.ts
File metadata and controls
46 lines (38 loc) · 1.46 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
export abstract class Mixin<T> {
protected abstract _getOverriddenMethods(originalMethods: Partial<T>): Partial<T>;
public constructor(host: T) {
const originalMethods: Partial<T> = {};
const overriddenMethods = this._getOverriddenMethods(originalMethods);
for (const key in overriddenMethods) {
if (Object.prototype.hasOwnProperty.call(overriddenMethods, key)) {
const override = overriddenMethods[key];
const hostMethod = host[key];
if (override !== undefined) {
originalMethods[key] = hostMethod;
// TODO (43081j): no clue why i need this nasty cast,
// typescript refuses to drop the `undefined` from the type
host[key] = override as NonNullable<T[typeof key]>;
}
}
}
}
}
export function install<THost, TOpts, TMixin extends object>(
host: THost,
Ctor: { new (host: THost, opts?: TOpts): TMixin },
opts?: TOpts
): TMixin {
const mutableHost = host as THost & { __mixins?: Array<TMixin> };
if (!mutableHost.__mixins) {
mutableHost.__mixins = [];
}
for (let i = 0; i < mutableHost.__mixins.length; i++) {
const cached = mutableHost.__mixins[i];
if (cached?.constructor === Ctor) {
return cached;
}
}
const mixin = new Ctor(host, opts);
mutableHost.__mixins.push(mixin);
return mixin;
}