1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.mix = exports.Mixin = void 0;
|
---|
4 | const proxy_1 = require("./proxy");
|
---|
5 | const settings_1 = require("./settings");
|
---|
6 | const util_1 = require("./util");
|
---|
7 | const decorator_1 = require("./decorator");
|
---|
8 | const mixin_tracking_1 = require("./mixin-tracking");
|
---|
9 | function Mixin(...constructors) {
|
---|
10 | var _a, _b, _c;
|
---|
11 | const prototypes = constructors.map(constructor => constructor.prototype);
|
---|
12 | // Here we gather up the init functions of the ingredient prototypes, combine them into one init function, and
|
---|
13 | // attach it to the mixed class prototype. The reason we do this is because we want the init functions to mix
|
---|
14 | // similarly to constructors -- not methods, which simply override each other.
|
---|
15 | const initFunctionName = settings_1.settings.initFunction;
|
---|
16 | if (initFunctionName !== null) {
|
---|
17 | const initFunctions = prototypes
|
---|
18 | .map(proto => proto[initFunctionName])
|
---|
19 | .filter(func => typeof func === 'function');
|
---|
20 | const combinedInitFunction = function (...args) {
|
---|
21 | for (let initFunction of initFunctions)
|
---|
22 | initFunction.apply(this, args);
|
---|
23 | };
|
---|
24 | const extraProto = { [initFunctionName]: combinedInitFunction };
|
---|
25 | prototypes.push(extraProto);
|
---|
26 | }
|
---|
27 | function MixedClass(...args) {
|
---|
28 | for (const constructor of constructors)
|
---|
29 | // @ts-ignore: potentially abstract class
|
---|
30 | (0, util_1.copyProps)(this, new constructor(...args));
|
---|
31 | if (initFunctionName !== null && typeof this[initFunctionName] === 'function')
|
---|
32 | this[initFunctionName].apply(this, args);
|
---|
33 | }
|
---|
34 | MixedClass.prototype = settings_1.settings.prototypeStrategy === 'copy'
|
---|
35 | ? (0, util_1.hardMixProtos)(prototypes, MixedClass)
|
---|
36 | : (0, proxy_1.softMixProtos)(prototypes, MixedClass);
|
---|
37 | Object.setPrototypeOf(MixedClass, settings_1.settings.staticsStrategy === 'copy'
|
---|
38 | ? (0, util_1.hardMixProtos)(constructors, null, ['prototype'])
|
---|
39 | : (0, proxy_1.proxyMix)(constructors, Function.prototype));
|
---|
40 | let DecoratedMixedClass = MixedClass;
|
---|
41 | if (settings_1.settings.decoratorInheritance !== 'none') {
|
---|
42 | const classDecorators = settings_1.settings.decoratorInheritance === 'deep'
|
---|
43 | ? (0, decorator_1.deepDecoratorSearch)(...constructors)
|
---|
44 | : (0, decorator_1.directDecoratorSearch)(...constructors);
|
---|
45 | for (let decorator of (_a = classDecorators === null || classDecorators === void 0 ? void 0 : classDecorators.class) !== null && _a !== void 0 ? _a : []) {
|
---|
46 | const result = decorator(DecoratedMixedClass);
|
---|
47 | if (result) {
|
---|
48 | DecoratedMixedClass = result;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | applyPropAndMethodDecorators((_b = classDecorators === null || classDecorators === void 0 ? void 0 : classDecorators.static) !== null && _b !== void 0 ? _b : {}, DecoratedMixedClass);
|
---|
52 | applyPropAndMethodDecorators((_c = classDecorators === null || classDecorators === void 0 ? void 0 : classDecorators.instance) !== null && _c !== void 0 ? _c : {}, DecoratedMixedClass.prototype);
|
---|
53 | }
|
---|
54 | (0, mixin_tracking_1.registerMixins)(DecoratedMixedClass, constructors);
|
---|
55 | return DecoratedMixedClass;
|
---|
56 | }
|
---|
57 | exports.Mixin = Mixin;
|
---|
58 | const applyPropAndMethodDecorators = (propAndMethodDecorators, target) => {
|
---|
59 | const propDecorators = propAndMethodDecorators.property;
|
---|
60 | const methodDecorators = propAndMethodDecorators.method;
|
---|
61 | if (propDecorators)
|
---|
62 | for (let key in propDecorators)
|
---|
63 | for (let decorator of propDecorators[key])
|
---|
64 | decorator(target, key);
|
---|
65 | if (methodDecorators)
|
---|
66 | for (let key in methodDecorators)
|
---|
67 | for (let decorator of methodDecorators[key])
|
---|
68 | decorator(target, key, Object.getOwnPropertyDescriptor(target, key));
|
---|
69 | };
|
---|
70 | /**
|
---|
71 | * A decorator version of the `Mixin` function. You'll want to use this instead of `Mixin` for mixing generic classes.
|
---|
72 | */
|
---|
73 | const mix = (...ingredients) => decoratedClass => {
|
---|
74 | // @ts-ignore
|
---|
75 | const mixedClass = Mixin(...ingredients.concat([decoratedClass]));
|
---|
76 | Object.defineProperty(mixedClass, 'name', {
|
---|
77 | value: decoratedClass.name,
|
---|
78 | writable: false,
|
---|
79 | });
|
---|
80 | return mixedClass;
|
---|
81 | };
|
---|
82 | exports.mix = mix;
|
---|