auto-bind-js 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +175 -154
- package/dist/index.cjs +226 -226
- package/dist/index.d.ts +80 -80
- package/dist/index.mjs +288 -288
- package/package.json +59 -59
package/dist/index.mjs
CHANGED
|
@@ -1,288 +1,288 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* auto-bind-js
|
|
3
|
-
* Automatically bind class methods to their instance.
|
|
4
|
-
*
|
|
5
|
-
* Features:
|
|
6
|
-
* - Bind all own and inherited prototype methods
|
|
7
|
-
* - Include/exclude specific methods
|
|
8
|
-
* - Regex pattern matching for method names
|
|
9
|
-
* - Lazy binding (bind on first access via getter)
|
|
10
|
-
* - React-aware variant (skips lifecycle methods)
|
|
11
|
-
* - Class & method decorator support
|
|
12
|
-
* - Full TypeScript support
|
|
13
|
-
* - Zero dependencies
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
const BUILTIN_OBJECT_METHODS = new Set([
|
|
17
|
-
'constructor',
|
|
18
|
-
'toString',
|
|
19
|
-
'toLocaleString',
|
|
20
|
-
'valueOf',
|
|
21
|
-
'hasOwnProperty',
|
|
22
|
-
'isPrototypeOf',
|
|
23
|
-
'propertyIsEnumerable',
|
|
24
|
-
'__defineGetter__',
|
|
25
|
-
'__defineSetter__',
|
|
26
|
-
'__lookupGetter__',
|
|
27
|
-
'__lookupSetter__',
|
|
28
|
-
]);
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Get all method names from the prototype chain (excluding Object.prototype)
|
|
32
|
-
*/
|
|
33
|
-
function getAllMethodNames(obj) {
|
|
34
|
-
const methods = new Set();
|
|
35
|
-
let proto = Object.getPrototypeOf(obj);
|
|
36
|
-
|
|
37
|
-
while (proto && proto !== Object.prototype) {
|
|
38
|
-
const keys = Object.getOwnPropertyNames(proto);
|
|
39
|
-
for (const key of keys) {
|
|
40
|
-
if (key === 'constructor') continue;
|
|
41
|
-
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
|
|
42
|
-
if (descriptor && typeof descriptor.value === 'function') {
|
|
43
|
-
methods.add(key);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Also handle Symbol-keyed methods
|
|
48
|
-
const symbols = Object.getOwnPropertySymbols(proto);
|
|
49
|
-
for (const sym of symbols) {
|
|
50
|
-
const descriptor = Object.getOwnPropertyDescriptor(proto, sym);
|
|
51
|
-
if (descriptor && typeof descriptor.value === 'function') {
|
|
52
|
-
methods.add(sym);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
proto = Object.getPrototypeOf(proto);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return methods;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Filter methods based on options
|
|
64
|
-
*/
|
|
65
|
-
function filterMethods(methods, options = {}) {
|
|
66
|
-
let filtered = [...methods];
|
|
67
|
-
|
|
68
|
-
// Remove built-in object methods
|
|
69
|
-
filtered = filtered.filter((m) => typeof m === 'symbol' || !BUILTIN_OBJECT_METHODS.has(m));
|
|
70
|
-
|
|
71
|
-
// Include only specific methods
|
|
72
|
-
if (options.include) {
|
|
73
|
-
const includeSet = new Set(options.include);
|
|
74
|
-
filtered = filtered.filter((m) => includeSet.has(m));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Exclude specific methods
|
|
78
|
-
if (options.exclude) {
|
|
79
|
-
const excludeSet = new Set(options.exclude);
|
|
80
|
-
filtered = filtered.filter((m) => !excludeSet.has(m));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Match pattern
|
|
84
|
-
if (options.pattern) {
|
|
85
|
-
filtered = filtered.filter(
|
|
86
|
-
(m) => typeof m === 'string' && options.pattern.test(m)
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return filtered;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Bind methods eagerly (standard mode)
|
|
95
|
-
*/
|
|
96
|
-
function bindEager(self, methods) {
|
|
97
|
-
for (const method of methods) {
|
|
98
|
-
const val = self[method];
|
|
99
|
-
if (typeof val === 'function') {
|
|
100
|
-
Object.defineProperty(self, method, {
|
|
101
|
-
value: val.bind(self),
|
|
102
|
-
writable: true,
|
|
103
|
-
configurable: true,
|
|
104
|
-
enumerable: false,
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Bind methods lazily (bind on first access)
|
|
112
|
-
*/
|
|
113
|
-
function bindLazy(self, methods) {
|
|
114
|
-
for (const method of methods) {
|
|
115
|
-
const proto = Object.getPrototypeOf(self);
|
|
116
|
-
const descriptor = Object.getOwnPropertyDescriptor(proto, method) ||
|
|
117
|
-
findDescriptorInChain(self, method);
|
|
118
|
-
|
|
119
|
-
if (!descriptor || typeof descriptor.value !== 'function') continue;
|
|
120
|
-
|
|
121
|
-
const originalFn = descriptor.value;
|
|
122
|
-
|
|
123
|
-
Object.defineProperty(self, method, {
|
|
124
|
-
configurable: true,
|
|
125
|
-
enumerable: false,
|
|
126
|
-
get() {
|
|
127
|
-
const boundFn = originalFn.bind(self);
|
|
128
|
-
// Replace getter with the bound value on first access
|
|
129
|
-
Object.defineProperty(self, method, {
|
|
130
|
-
value: boundFn,
|
|
131
|
-
writable: true,
|
|
132
|
-
configurable: true,
|
|
133
|
-
enumerable: false,
|
|
134
|
-
});
|
|
135
|
-
return boundFn;
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function findDescriptorInChain(obj, key) {
|
|
142
|
-
let proto = Object.getPrototypeOf(obj);
|
|
143
|
-
while (proto && proto !== Object.prototype) {
|
|
144
|
-
const desc = Object.getOwnPropertyDescriptor(proto, key);
|
|
145
|
-
if (desc) return desc;
|
|
146
|
-
proto = Object.getPrototypeOf(proto);
|
|
147
|
-
}
|
|
148
|
-
return undefined;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Main autoBind function
|
|
153
|
-
*
|
|
154
|
-
* @param {object} self - The class instance (usually `this`)
|
|
155
|
-
* @param {object} [options] - Configuration options
|
|
156
|
-
* @param {string[]} [options.include] - Only bind these methods
|
|
157
|
-
* @param {string[]} [options.exclude] - Don't bind these methods
|
|
158
|
-
* @param {RegExp} [options.pattern] - Only bind methods matching this regex
|
|
159
|
-
* @param {boolean} [options.lazy] - Use lazy binding (bind on first access)
|
|
160
|
-
* @returns {object} The instance (for chaining)
|
|
161
|
-
*/
|
|
162
|
-
function autoBind(self, options) {
|
|
163
|
-
// Support autoBind(this, 'method1', 'method2') shorthand
|
|
164
|
-
if (typeof options === 'string') {
|
|
165
|
-
const methodNames = [options, ...Array.from(arguments).slice(2)];
|
|
166
|
-
options = { include: methodNames };
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const allMethods = getAllMethodNames(self);
|
|
170
|
-
const methods = filterMethods(allMethods, options);
|
|
171
|
-
|
|
172
|
-
if (options && options.lazy) {
|
|
173
|
-
bindLazy(self, methods);
|
|
174
|
-
} else {
|
|
175
|
-
bindEager(self, methods);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return self;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// ─── React-aware variant ────────────────────────────────────────────────────
|
|
182
|
-
|
|
183
|
-
const REACT_LIFECYCLE_METHODS = new Set([
|
|
184
|
-
'render',
|
|
185
|
-
'componentDidMount',
|
|
186
|
-
'componentDidUpdate',
|
|
187
|
-
'componentWillUnmount',
|
|
188
|
-
'shouldComponentUpdate',
|
|
189
|
-
'getSnapshotBeforeUpdate',
|
|
190
|
-
'getDerivedStateFromProps',
|
|
191
|
-
'getDerivedStateFromError',
|
|
192
|
-
'componentDidCatch',
|
|
193
|
-
'UNSAFE_componentWillMount',
|
|
194
|
-
'UNSAFE_componentWillReceiveProps',
|
|
195
|
-
'UNSAFE_componentWillUpdate',
|
|
196
|
-
'getDefaultProps',
|
|
197
|
-
'getInitialState',
|
|
198
|
-
'componentWillMount',
|
|
199
|
-
'componentWillReceiveProps',
|
|
200
|
-
'componentWillUpdate',
|
|
201
|
-
]);
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* React-aware autoBind. Skips React lifecycle methods.
|
|
205
|
-
*
|
|
206
|
-
* @param {object} self - The component instance
|
|
207
|
-
* @param {object} [options] - Same options as autoBind (exclude is merged)
|
|
208
|
-
* @returns {object} The instance
|
|
209
|
-
*/
|
|
210
|
-
function autoBindReact(self, options = {}) {
|
|
211
|
-
const exclude = [
|
|
212
|
-
...(options.exclude || []),
|
|
213
|
-
...REACT_LIFECYCLE_METHODS,
|
|
214
|
-
];
|
|
215
|
-
return autoBind(self, { ...options, exclude });
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// ─── Decorator ──────────────────────────────────────────────────────────────
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Class decorator: @boundClass
|
|
222
|
-
* Method decorator: @bound
|
|
223
|
-
*
|
|
224
|
-
* Usage:
|
|
225
|
-
* @boundClass
|
|
226
|
-
* class Foo { ... }
|
|
227
|
-
*
|
|
228
|
-
* class Foo {
|
|
229
|
-
* @bound
|
|
230
|
-
* handleClick() { ... }
|
|
231
|
-
* }
|
|
232
|
-
*/
|
|
233
|
-
function boundClass(target) {
|
|
234
|
-
const original = target;
|
|
235
|
-
|
|
236
|
-
const wrapped = function (...args) {
|
|
237
|
-
const instance = new original(...args);
|
|
238
|
-
autoBind(instance);
|
|
239
|
-
return instance;
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
wrapped.prototype = original.prototype;
|
|
243
|
-
Object.defineProperty(wrapped, 'name', { value: original.name });
|
|
244
|
-
Object.setPrototypeOf(wrapped, original);
|
|
245
|
-
|
|
246
|
-
// Copy static properties
|
|
247
|
-
for (const key of Object.getOwnPropertyNames(original)) {
|
|
248
|
-
if (['length', 'name', 'prototype', 'arguments', 'caller'].includes(key)) continue;
|
|
249
|
-
const desc = Object.getOwnPropertyDescriptor(original, key);
|
|
250
|
-
if (desc) Object.defineProperty(wrapped, key, desc);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return wrapped;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Method decorator
|
|
258
|
-
*/
|
|
259
|
-
function bound(target, key, descriptor) {
|
|
260
|
-
if (!descriptor || typeof descriptor.value !== 'function') {
|
|
261
|
-
throw new TypeError('@bound can only be applied to methods');
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
const fn = descriptor.value;
|
|
265
|
-
|
|
266
|
-
return {
|
|
267
|
-
configurable: true,
|
|
268
|
-
enumerable: false,
|
|
269
|
-
get() {
|
|
270
|
-
// Only define on the instance, not the prototype
|
|
271
|
-
if (this === target) {
|
|
272
|
-
return fn;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const boundFn = fn.bind(this);
|
|
276
|
-
Object.defineProperty(this, key, {
|
|
277
|
-
value: boundFn,
|
|
278
|
-
writable: true,
|
|
279
|
-
configurable: true,
|
|
280
|
-
enumerable: false,
|
|
281
|
-
});
|
|
282
|
-
return boundFn;
|
|
283
|
-
},
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
export default autoBind;
|
|
288
|
-
export { autoBind, autoBindReact, boundClass, bound };
|
|
1
|
+
/**
|
|
2
|
+
* auto-bind-js
|
|
3
|
+
* Automatically bind class methods to their instance.
|
|
4
|
+
*
|
|
5
|
+
* Features:
|
|
6
|
+
* - Bind all own and inherited prototype methods
|
|
7
|
+
* - Include/exclude specific methods
|
|
8
|
+
* - Regex pattern matching for method names
|
|
9
|
+
* - Lazy binding (bind on first access via getter)
|
|
10
|
+
* - React-aware variant (skips lifecycle methods)
|
|
11
|
+
* - Class & method decorator support
|
|
12
|
+
* - Full TypeScript support
|
|
13
|
+
* - Zero dependencies
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const BUILTIN_OBJECT_METHODS = new Set([
|
|
17
|
+
'constructor',
|
|
18
|
+
'toString',
|
|
19
|
+
'toLocaleString',
|
|
20
|
+
'valueOf',
|
|
21
|
+
'hasOwnProperty',
|
|
22
|
+
'isPrototypeOf',
|
|
23
|
+
'propertyIsEnumerable',
|
|
24
|
+
'__defineGetter__',
|
|
25
|
+
'__defineSetter__',
|
|
26
|
+
'__lookupGetter__',
|
|
27
|
+
'__lookupSetter__',
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get all method names from the prototype chain (excluding Object.prototype)
|
|
32
|
+
*/
|
|
33
|
+
function getAllMethodNames(obj) {
|
|
34
|
+
const methods = new Set();
|
|
35
|
+
let proto = Object.getPrototypeOf(obj);
|
|
36
|
+
|
|
37
|
+
while (proto && proto !== Object.prototype) {
|
|
38
|
+
const keys = Object.getOwnPropertyNames(proto);
|
|
39
|
+
for (const key of keys) {
|
|
40
|
+
if (key === 'constructor') continue;
|
|
41
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
|
|
42
|
+
if (descriptor && typeof descriptor.value === 'function') {
|
|
43
|
+
methods.add(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Also handle Symbol-keyed methods
|
|
48
|
+
const symbols = Object.getOwnPropertySymbols(proto);
|
|
49
|
+
for (const sym of symbols) {
|
|
50
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, sym);
|
|
51
|
+
if (descriptor && typeof descriptor.value === 'function') {
|
|
52
|
+
methods.add(sym);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
proto = Object.getPrototypeOf(proto);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return methods;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Filter methods based on options
|
|
64
|
+
*/
|
|
65
|
+
function filterMethods(methods, options = {}) {
|
|
66
|
+
let filtered = [...methods];
|
|
67
|
+
|
|
68
|
+
// Remove built-in object methods
|
|
69
|
+
filtered = filtered.filter((m) => typeof m === 'symbol' || !BUILTIN_OBJECT_METHODS.has(m));
|
|
70
|
+
|
|
71
|
+
// Include only specific methods
|
|
72
|
+
if (options.include) {
|
|
73
|
+
const includeSet = new Set(options.include);
|
|
74
|
+
filtered = filtered.filter((m) => includeSet.has(m));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Exclude specific methods
|
|
78
|
+
if (options.exclude) {
|
|
79
|
+
const excludeSet = new Set(options.exclude);
|
|
80
|
+
filtered = filtered.filter((m) => !excludeSet.has(m));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Match pattern
|
|
84
|
+
if (options.pattern) {
|
|
85
|
+
filtered = filtered.filter(
|
|
86
|
+
(m) => typeof m === 'string' && options.pattern.test(m)
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return filtered;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Bind methods eagerly (standard mode)
|
|
95
|
+
*/
|
|
96
|
+
function bindEager(self, methods) {
|
|
97
|
+
for (const method of methods) {
|
|
98
|
+
const val = self[method];
|
|
99
|
+
if (typeof val === 'function') {
|
|
100
|
+
Object.defineProperty(self, method, {
|
|
101
|
+
value: val.bind(self),
|
|
102
|
+
writable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
enumerable: false,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Bind methods lazily (bind on first access)
|
|
112
|
+
*/
|
|
113
|
+
function bindLazy(self, methods) {
|
|
114
|
+
for (const method of methods) {
|
|
115
|
+
const proto = Object.getPrototypeOf(self);
|
|
116
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, method) ||
|
|
117
|
+
findDescriptorInChain(self, method);
|
|
118
|
+
|
|
119
|
+
if (!descriptor || typeof descriptor.value !== 'function') continue;
|
|
120
|
+
|
|
121
|
+
const originalFn = descriptor.value;
|
|
122
|
+
|
|
123
|
+
Object.defineProperty(self, method, {
|
|
124
|
+
configurable: true,
|
|
125
|
+
enumerable: false,
|
|
126
|
+
get() {
|
|
127
|
+
const boundFn = originalFn.bind(self);
|
|
128
|
+
// Replace getter with the bound value on first access
|
|
129
|
+
Object.defineProperty(self, method, {
|
|
130
|
+
value: boundFn,
|
|
131
|
+
writable: true,
|
|
132
|
+
configurable: true,
|
|
133
|
+
enumerable: false,
|
|
134
|
+
});
|
|
135
|
+
return boundFn;
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function findDescriptorInChain(obj, key) {
|
|
142
|
+
let proto = Object.getPrototypeOf(obj);
|
|
143
|
+
while (proto && proto !== Object.prototype) {
|
|
144
|
+
const desc = Object.getOwnPropertyDescriptor(proto, key);
|
|
145
|
+
if (desc) return desc;
|
|
146
|
+
proto = Object.getPrototypeOf(proto);
|
|
147
|
+
}
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Main autoBind function
|
|
153
|
+
*
|
|
154
|
+
* @param {object} self - The class instance (usually `this`)
|
|
155
|
+
* @param {object} [options] - Configuration options
|
|
156
|
+
* @param {string[]} [options.include] - Only bind these methods
|
|
157
|
+
* @param {string[]} [options.exclude] - Don't bind these methods
|
|
158
|
+
* @param {RegExp} [options.pattern] - Only bind methods matching this regex
|
|
159
|
+
* @param {boolean} [options.lazy] - Use lazy binding (bind on first access)
|
|
160
|
+
* @returns {object} The instance (for chaining)
|
|
161
|
+
*/
|
|
162
|
+
function autoBind(self, options) {
|
|
163
|
+
// Support autoBind(this, 'method1', 'method2') shorthand
|
|
164
|
+
if (typeof options === 'string') {
|
|
165
|
+
const methodNames = [options, ...Array.from(arguments).slice(2)];
|
|
166
|
+
options = { include: methodNames };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const allMethods = getAllMethodNames(self);
|
|
170
|
+
const methods = filterMethods(allMethods, options);
|
|
171
|
+
|
|
172
|
+
if (options && options.lazy) {
|
|
173
|
+
bindLazy(self, methods);
|
|
174
|
+
} else {
|
|
175
|
+
bindEager(self, methods);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return self;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ─── React-aware variant ────────────────────────────────────────────────────
|
|
182
|
+
|
|
183
|
+
const REACT_LIFECYCLE_METHODS = new Set([
|
|
184
|
+
'render',
|
|
185
|
+
'componentDidMount',
|
|
186
|
+
'componentDidUpdate',
|
|
187
|
+
'componentWillUnmount',
|
|
188
|
+
'shouldComponentUpdate',
|
|
189
|
+
'getSnapshotBeforeUpdate',
|
|
190
|
+
'getDerivedStateFromProps',
|
|
191
|
+
'getDerivedStateFromError',
|
|
192
|
+
'componentDidCatch',
|
|
193
|
+
'UNSAFE_componentWillMount',
|
|
194
|
+
'UNSAFE_componentWillReceiveProps',
|
|
195
|
+
'UNSAFE_componentWillUpdate',
|
|
196
|
+
'getDefaultProps',
|
|
197
|
+
'getInitialState',
|
|
198
|
+
'componentWillMount',
|
|
199
|
+
'componentWillReceiveProps',
|
|
200
|
+
'componentWillUpdate',
|
|
201
|
+
]);
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* React-aware autoBind. Skips React lifecycle methods.
|
|
205
|
+
*
|
|
206
|
+
* @param {object} self - The component instance
|
|
207
|
+
* @param {object} [options] - Same options as autoBind (exclude is merged)
|
|
208
|
+
* @returns {object} The instance
|
|
209
|
+
*/
|
|
210
|
+
function autoBindReact(self, options = {}) {
|
|
211
|
+
const exclude = [
|
|
212
|
+
...(options.exclude || []),
|
|
213
|
+
...REACT_LIFECYCLE_METHODS,
|
|
214
|
+
];
|
|
215
|
+
return autoBind(self, { ...options, exclude });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ─── Decorator ──────────────────────────────────────────────────────────────
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Class decorator: @boundClass
|
|
222
|
+
* Method decorator: @bound
|
|
223
|
+
*
|
|
224
|
+
* Usage:
|
|
225
|
+
* @boundClass
|
|
226
|
+
* class Foo { ... }
|
|
227
|
+
*
|
|
228
|
+
* class Foo {
|
|
229
|
+
* @bound
|
|
230
|
+
* handleClick() { ... }
|
|
231
|
+
* }
|
|
232
|
+
*/
|
|
233
|
+
function boundClass(target) {
|
|
234
|
+
const original = target;
|
|
235
|
+
|
|
236
|
+
const wrapped = function (...args) {
|
|
237
|
+
const instance = new original(...args);
|
|
238
|
+
autoBind(instance);
|
|
239
|
+
return instance;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
wrapped.prototype = original.prototype;
|
|
243
|
+
Object.defineProperty(wrapped, 'name', { value: original.name });
|
|
244
|
+
Object.setPrototypeOf(wrapped, original);
|
|
245
|
+
|
|
246
|
+
// Copy static properties
|
|
247
|
+
for (const key of Object.getOwnPropertyNames(original)) {
|
|
248
|
+
if (['length', 'name', 'prototype', 'arguments', 'caller'].includes(key)) continue;
|
|
249
|
+
const desc = Object.getOwnPropertyDescriptor(original, key);
|
|
250
|
+
if (desc) Object.defineProperty(wrapped, key, desc);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return wrapped;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Method decorator
|
|
258
|
+
*/
|
|
259
|
+
function bound(target, key, descriptor) {
|
|
260
|
+
if (!descriptor || typeof descriptor.value !== 'function') {
|
|
261
|
+
throw new TypeError('@bound can only be applied to methods');
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const fn = descriptor.value;
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
configurable: true,
|
|
268
|
+
enumerable: false,
|
|
269
|
+
get() {
|
|
270
|
+
// Only define on the instance, not the prototype
|
|
271
|
+
if (this === target) {
|
|
272
|
+
return fn;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const boundFn = fn.bind(this);
|
|
276
|
+
Object.defineProperty(this, key, {
|
|
277
|
+
value: boundFn,
|
|
278
|
+
writable: true,
|
|
279
|
+
configurable: true,
|
|
280
|
+
enumerable: false,
|
|
281
|
+
});
|
|
282
|
+
return boundFn;
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export default autoBind;
|
|
288
|
+
export { autoBind, autoBindReact, boundClass, bound };
|