UNPKG

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