UNPKG

2.78 kBJavaScriptView Raw
1define(["require", "exports"], function (require, exports) {
2 "use strict";
3 Object.defineProperty(exports, "__esModule", { value: true });
4 var REACT_LIFECYCLE_EXCLUSIONS = [
5 'setState',
6 'render',
7 'componentWillMount',
8 'UNSAFE_componentWillMount',
9 'componentDidMount',
10 'componentWillReceiveProps',
11 'UNSAFE_componentWillReceiveProps',
12 'shouldComponentUpdate',
13 'componentWillUpdate',
14 'getSnapshotBeforeUpdate',
15 'UNSAFE_componentWillUpdate',
16 'componentDidUpdate',
17 'componentWillUnmount',
18 ];
19 /**
20 * Allows you to hoist methods, except those in an exclusion set from a source object into a destination object.
21 *
22 * @public
23 * @param destination - The instance of the object to hoist the methods onto.
24 * @param source - The instance of the object where the methods are hoisted from.
25 * @param exclusions - (Optional) What methods to exclude from being hoisted.
26 * @returns An array of names of methods that were hoisted.
27 */
28 function hoistMethods(
29 // eslint-disable-next-line @typescript-eslint/no-explicit-any
30 destination,
31 // eslint-disable-next-line @typescript-eslint/no-explicit-any
32 source, exclusions) {
33 if (exclusions === void 0) { exclusions = REACT_LIFECYCLE_EXCLUSIONS; }
34 var hoisted = [];
35 var _loop_1 = function (methodName) {
36 if (typeof source[methodName] === 'function' &&
37 destination[methodName] === undefined &&
38 (!exclusions || exclusions.indexOf(methodName) === -1)) {
39 hoisted.push(methodName);
40 // eslint-disable-next-line @typescript-eslint/no-explicit-any
41 destination[methodName] = function () {
42 var args = [];
43 for (var _i = 0; _i < arguments.length; _i++) {
44 args[_i] = arguments[_i];
45 }
46 source[methodName].apply(source, args);
47 };
48 }
49 };
50 for (var methodName in source) {
51 _loop_1(methodName);
52 }
53 return hoisted;
54 }
55 exports.hoistMethods = hoistMethods;
56 /**
57 * Provides a method for convenience to unhoist hoisted methods.
58 *
59 * @public
60 * @param source - The source object upon which methods were hoisted.
61 * @param methodNames - An array of method names to unhoist.
62 */
63 // eslint-disable-next-line @typescript-eslint/no-explicit-any
64 function unhoistMethods(source, methodNames) {
65 methodNames.forEach(function (methodName) { return delete source[methodName]; });
66 }
67 exports.unhoistMethods = unhoistMethods;
68});
69//# sourceMappingURL=hoist.js.map
\No newline at end of file