/
universal-mixin.node.js
190 lines (171 loc) · 6.55 KB
/
universal-mixin.node.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*!
Copyright (C) 2015 by Andrea Giammarchi @WebReflection
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var mixin = (function (O) {'use strict';
var
// feature detection: try to use Symbol
// to avoid name clashing as much as possible.
// Also verify if Symbol.hasInstance is present
// Use a basic Symbol fall-back and eventually
// ignore setting the Symbol.hasInstance method
notDefined = typeof notDefined,
hasSymbol = typeof Symbol !== notDefined,
hasInstanceOf = hasSymbol && !!Symbol.hasInstance,
i = 0, // will make Symbol fall-back less name-clash prone
createSymbol = hasSymbol ? Symbol : function (name) {
return '@@:' + (++i) + ':' + name + Math.random();
},
// unique ID used to set the init list Array
uid = createSymbol('mixin:init'),
// shortcut implicitly borrowed from Object.prototype
hOP = O.hasOwnProperty,
// shortcuts for common Object methods
// partially polyfilled for this closure purpose only
// if you are targeting IE8 don't forget to include
// ES5 shim+sham upfront or this script would fail
// because IE8 has a dumb implementation of defineProperty
defineProperty = O.defineProperty ||
function (o, k, d) {o[k] = d.value;},
gOPN = O.getOwnPropertyNames || O.keys ||
function (o, a, k) {
a = [];
for (k in o) if (hOP.call(o, k)) a.push(k);
return a;
},
gOPD = O.getOwnPropertyDescriptor ||
function (o, k) {return {value: o[k]};},
gOPS = O.getOwnPropertySymbols ||
function () { return []; },
// shortcut or fall-back for Reflect.ownKeys
ownKeys = typeof Reflect === notDefined || !Reflect.ownKeys ?
function (obj) {
return gOPN(obj).concat(
gOPS(obj)
);
} :
Reflect.ownKeys,
// will define shared "public static" properties
addStaticProperties = function (compose, sharedBehaviour) {
for (var
property, descriptor,
sharedKeys = ownKeys(sharedBehaviour),
i = 0, l = sharedKeys.length; i < l; i++
) {
descriptor = gOPD(sharedBehaviour, (property = sharedKeys[i]));
// ensure these are not configurable
descriptor.configurable = false;
// if not a getter/setter, ensure these are not writable
if (hOP.call(descriptor, 'writable')) {
descriptor.writable = false;
}
// finally define it as callback property
defineProperty(compose, property, descriptor);
}
},
// objects eventually recycled per every mixin
tagDescriptor = hasInstanceOf && {value: true},
initDescriptor = {value: function init() {
for (var
notDefined,
initializers = this[uid],
i = 0, l = initializers.length; i < l; i++
) {
// [WARNING]
// passing arguments to possibly
// unknown initializers is an anti pattern!
// You don't know what kind of parameters
// every mixin.init() might accept so please invoke
// the init method without arguments or use them
// as last possible solution/hack to your problem.
// It is also wrong to return any value
// since init is a special method that could
// invoke potentially many `init()` at once.
// Accordingly, returning anything different
// from `undefined` will throw an Error
if (initializers[i].apply(this, arguments) !== notDefined) {
throw new Error('mixin.init() must not return a value');
}
}
}}
;
return function mixin(behaviour, sharedBehaviour) {
var
instanceKeys = ownKeys(behaviour),
typeTag = hasInstanceOf && createSymbol('isa')
;
function compose(target) {
for (var
property, descriptor,
proto = target.prototype || target,
i = 0, l = instanceKeys.length; i < l; i++
) {
property = instanceKeys[i];
if (property === 'init') {
if (!hOP.call(proto, uid)) {
defineProperty(proto, uid, {
// polyfilled Symbol causes shenanigans here
value: uid in proto ? (proto[uid] || []).slice(0) : []
});
defineProperty(proto, property, initDescriptor);
}
proto[uid].push(behaviour[property]);
} else if(!hOP.call(proto, property)) {
descriptor = gOPD(behaviour, property);
descriptor.enumerable = false;
defineProperty(proto, property, descriptor);
}
}
if (hasInstanceOf) defineProperty(proto, typeTag, tagDescriptor);
return target;
}
// assign this composer shared properties
if (sharedBehaviour) addStaticProperties(
compose,
sharedBehaviour
);
// if possible, ensure that
// obj instanceof MyMixin
// will return true
if (hasInstanceOf) defineProperty(
compose,
Symbol.hasInstance,
{value: function hasInstance(obj) {
return !!obj[typeTag];
}}
);
// provide access to methods for explicit invoke
// particularly useful to workaround classes overrides
for (var
property, descriptor,
i = 0, l = instanceKeys.length; i < l; i++
) {
property = instanceKeys[i];
descriptor = gOPD(behaviour, property);
if (
hOP.call(descriptor, 'value') &&
typeof descriptor.value === 'function'
) {
defineProperty(compose, property, descriptor);
}
}
// return the function that could be used
// as both decorator, or to enrich any sort of object
return compose;
};
}(Object));
module.exports = mixin;