UNPKG

2.71 kBJavaScriptView Raw
1/*
2 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5 * the License. A copy of the License is located at
6 *
7 * http://aws.amazon.com/apache2.0/
8 *
9 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11 * and limitations under the License.
12 */
13var __read = (this && this.__read) || function (o, n) {
14 var m = typeof Symbol === "function" && o[Symbol.iterator];
15 if (!m) return o;
16 var i = m.call(o), r, ar = [], e;
17 try {
18 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19 }
20 catch (error) { e = { error: error }; }
21 finally {
22 try {
23 if (r && !r.done && (m = i["return"])) m.call(i);
24 }
25 finally { if (e) throw e.error; }
26 }
27 return ar;
28};
29var __spread = (this && this.__spread) || function () {
30 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
31 return ar;
32};
33var lists = [];
34var MethodEmbed = /** @class */ (function () {
35 function MethodEmbed(context, methodName) {
36 this.context = context;
37 this.methodName = methodName;
38 this._originalMethod = context[methodName].bind(context);
39 }
40 MethodEmbed.add = function (context, methodName, methodOverride) {
41 getInstance(context, methodName).set(methodOverride);
42 };
43 MethodEmbed.remove = function (context, methodName) {
44 getInstance(context, methodName).remove();
45 };
46 MethodEmbed.prototype.set = function (methodOverride) {
47 var _this = this;
48 this.context[this.methodName] = function () {
49 var args = [];
50 for (var _i = 0; _i < arguments.length; _i++) {
51 args[_i] = arguments[_i];
52 }
53 return methodOverride(_this._originalMethod.apply(_this, __spread(args)));
54 };
55 };
56 MethodEmbed.prototype.remove = function () {
57 this.context[this.methodName] = this._originalMethod;
58 };
59 return MethodEmbed;
60}());
61export { MethodEmbed };
62function getInstance(context, methodName) {
63 var instance = lists.filter(function (h) { return h.context === context && h.methodName === methodName; })[0];
64 if (!instance) {
65 instance = new MethodEmbed(context, methodName);
66 lists.push(instance);
67 }
68 return instance;
69}
70/**
71 * @deprecated use named import
72 */
73export default MethodEmbed;
74//# sourceMappingURL=MethodEmbed.js.map
\No newline at end of file