-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (110 loc) · 3.27 KB
/
index.js
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
"use strict";
/*
change from https://github.com/Microsoft/TypeScript/pull/13743#issuecomment-299540915
*/
Object.defineProperty(exports, "__esModule", { value: true });
/* 为了推断 typeof T ,防止tsc报错 */
function mix(superclass) {
if (superclass === void 0) { superclass = /** @class */ (function () {
function class_1() {
}
return class_1;
}()); }
return new MixinBuilder(superclass);
}
exports.mix = mix;
var MixinBuilder = /** @class */ (function () {
function MixinBuilder(superclass) {
this.superclass = superclass;
}
MixinBuilder.prototype.with = function () {
var mixins = [];
for (var _i = 0; _i < arguments.length; _i++) {
mixins[_i] = arguments[_i];
}
return compose.apply(void 0, [this.superclass].concat(mixins));
};
return MixinBuilder;
}());
exports.MixinBuilder = MixinBuilder;
function compose(target) {
var traits = [];
for (var _i = 1; _i < arguments.length; _i++) {
traits[_i - 1] = arguments[_i];
}
var classes = [target].concat(traits);
var superClass = function ComposeClass() {
var _this = this;
var arg = arguments;
skipBabelClassCheck(function () {
var len = classes.length;
while (len--) {
classes[len].apply(_this, arg);
}
});
};
superClass.prototype = Object.create(target.prototype, {
constructor: {
value: target,
writable: true,
enumerable: false,
configurable: false
}
});
classes.forEach(function (baseCtor) {
applyProtoMixins(superClass.prototype, baseCtor.prototype);
});
mixinsStatic(superClass, classes);
return superClass;
}
function applyProtoMixins(proto, baseProto) {
if (baseProto !== Object.prototype && baseProto !== null) {
mixinsProto(proto, baseProto);
var superProto = Object.getPrototypeOf(baseProto);
applyProtoMixins(proto, superProto);
}
}
function mixinsProto(proto, baseProto) {
var keys = Object.getOwnPropertyNames(baseProto);
var len = keys.length, key;
while (len--) {
key = keys[len];
/*前面参数的优先级大于后面的*/
if (!proto.hasOwnProperty(key)) {
var desc = Object.getOwnPropertyDescriptor(baseProto, key);
if (desc) {
Object.defineProperty(proto, key, desc);
}
}
}
}
function mixinsStatic(target, bases) {
bases.forEach(function (base) {
for (var key in base) {
if (!target.hasOwnProperty(key)) {
target[key] = base[key];
}
}
});
}
/**
* 跳过babel编译器的instanceof检查
* @param fn
*/
var skipBabelClassCheck = process.env.MIX_ENV !== 'babel'
?
function (fn) { return fn(); }
:
function (fn) {
var babelCheck = require('babel-runtime/helpers/classCallCheck.js');
var checkFn = babelCheck.default;
try {
// tslint:disable-next-line:no-empty
babelCheck.default = function () { };
return fn();
}
finally {
babelCheck.default = checkFn;
}
};
exports.default = mix;