UNPKG

1.55 kBJavaScriptView Raw
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3import { __read, __spread } from "tslib";
4var lists = [];
5var MethodEmbed = /** @class */ (function () {
6 function MethodEmbed(context, methodName) {
7 this.context = context;
8 this.methodName = methodName;
9 this._originalMethod = context[methodName].bind(context);
10 }
11 MethodEmbed.add = function (context, methodName, methodOverride) {
12 getInstance(context, methodName).set(methodOverride);
13 };
14 MethodEmbed.remove = function (context, methodName) {
15 getInstance(context, methodName).remove();
16 };
17 MethodEmbed.prototype.set = function (methodOverride) {
18 var _this = this;
19 this.context[this.methodName] = function () {
20 var args = [];
21 for (var _i = 0; _i < arguments.length; _i++) {
22 args[_i] = arguments[_i];
23 }
24 return methodOverride(_this._originalMethod.apply(_this, __spread(args)));
25 };
26 };
27 MethodEmbed.prototype.remove = function () {
28 this.context[this.methodName] = this._originalMethod;
29 };
30 return MethodEmbed;
31}());
32export { MethodEmbed };
33function getInstance(context, methodName) {
34 var instance = lists.filter(function (h) { return h.context === context && h.methodName === methodName; })[0];
35 if (!instance) {
36 instance = new MethodEmbed(context, methodName);
37 lists.push(instance);
38 }
39 return instance;
40}
41//# sourceMappingURL=MethodEmbed.js.map
\No newline at end of file