source: node_modules/ts-mixer/dist/cjs/mixins.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.2 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.mix = exports.Mixin = void 0;
4const proxy_1 = require("./proxy");
5const settings_1 = require("./settings");
6const util_1 = require("./util");
7const decorator_1 = require("./decorator");
8const mixin_tracking_1 = require("./mixin-tracking");
9function 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}
57exports.Mixin = Mixin;
58const 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 */
73const 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};
82exports.mix = mix;
Note: See TracBrowser for help on using the repository browser.