-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiMixinBuilder.ts
100 lines (91 loc) · 2.76 KB
/
MultiMixinBuilder.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
import type {
Class
} from "type-fest";
import type {
StaticAndInstance
} from "./types/StaticAndInstance.js";
import type {
SubclassDecoratorSequence,
} from "./types/SubclassDecorator.js";
import type {
ClassDecoratorFunction
} from "./types/ClassDecoratorFunction.js";
import type {
MultiMixinClass
} from "./types/MultiMixinClass.js";
/**
* Apply decorators to build the mixin class.
*
* @typeParam Interfaces - the sequence of static and instance interfaces.
* @param decorators - a sequence which creates and returns subclasses of MixinBase. This must match the ordering of Interfaces.
* @param baseClass - always `MixinBase`.
* @param context - the class decorator context to forward to each decorator.
*/
function applyAllDecorators<
Interfaces extends readonly StaticAndInstance<symbol>[],
Base extends Class<object>,
>
(
this: void,
decorators: SubclassDecoratorSequence<Interfaces, Base, false>,
baseClass: Base,
context: ClassDecoratorContext,
) : MultiMixinClass<Interfaces, Base>
{
let _class = baseClass;
for (let i = decorators.length - 1; i >= 0; i--) {
_class = decorators[i](_class, context) as Base;
}
return _class as MultiMixinClass<Interfaces, Base>;
}
/**
* Return a ClassDecorator to execute mixin decorators.
*
* @typeParam Interfaces - the sequence of static and instance interfaces.
* @param decorators - a sequence which creates and returns subclasses of MixinBase. This must match the ordering of Interfaces.
*/
function MixinBuilderInternal<
Interfaces extends readonly StaticAndInstance<symbol>[],
Base extends Class<object>,
>
(
decorators: SubclassDecoratorSequence<Interfaces, Base, false>
) : (_class: Base, context: ClassDecoratorContext) => MultiMixinClass<Interfaces, Base>
{
return function(
this: void,
_class: Base,
context: ClassDecoratorContext
) : MultiMixinClass<Interfaces, Base>
{
return applyAllDecorators(decorators, _class, context);
}
}
/**
* Build a mixin class inheriting from `MixinBase`.
*
* @typeParam Interfaces - the sequence of static and instance interfaces.
* @param decorators - a sequence which creates and returns subclasses of MixinBase. This must match the ordering of Interfaces.
*/
function MultiMixinBuilder<
Interfaces extends readonly StaticAndInstance<symbol>[],
Base extends Class<object>,
>
(
decorators: SubclassDecoratorSequence<Interfaces, Base, false>,
baseClass: Base,
) : MultiMixinClass<Interfaces, Base>
{
const decoratorFunction = MixinBuilderInternal<
Interfaces, Base
>(decorators) as unknown as ClassDecoratorFunction<Base, true, false>;
return (
@decoratorFunction
class extends baseClass {
}
) as unknown as MultiMixinClass<Interfaces, Base>;
}
export default MultiMixinBuilder;
export {
type MultiMixinClass
};