74 lines
9.2 KiB
JavaScript
74 lines
9.2 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.delegateAPI = exports.getDelegatedAPIList = void 0;
|
|
const API_IMPLEMENTATION_METHOD_RE = /^_(\S+)\$(getter|setter)?$/;
|
|
function isTestController(obj) {
|
|
var _a;
|
|
return ((_a = obj === null || obj === void 0 ? void 0 : obj.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'TestController';
|
|
}
|
|
function getTestController(obj) {
|
|
if (isTestController(obj))
|
|
return obj;
|
|
else if (isTestController(obj._testController))
|
|
return obj._testController;
|
|
return null;
|
|
}
|
|
function getDelegatedAPIList(src) {
|
|
return Object
|
|
.getOwnPropertyNames(src)
|
|
.map(prop => {
|
|
const match = prop.match(API_IMPLEMENTATION_METHOD_RE);
|
|
if (match) {
|
|
return {
|
|
srcProp: prop,
|
|
apiProp: match[1],
|
|
accessor: match[2],
|
|
};
|
|
}
|
|
return null;
|
|
})
|
|
.filter(item => !!item);
|
|
}
|
|
exports.getDelegatedAPIList = getDelegatedAPIList;
|
|
function delegateAPI(dest, apiList, opts) {
|
|
apiList.forEach(({ srcProp, apiProp, accessor }) => {
|
|
let fn = function (...args) {
|
|
if (opts.proxyMethod)
|
|
opts.proxyMethod();
|
|
let handler = null;
|
|
if (opts.useCurrentCtxAsHandler)
|
|
handler = this;
|
|
else if (opts.getHandler)
|
|
handler = opts.getHandler(apiProp, accessor);
|
|
else
|
|
handler = opts.handler;
|
|
return handler[srcProp](...args);
|
|
};
|
|
// NOTE: need to create named function to process possible err.stack correctly
|
|
const createNamedFunction = new Function('srcProp', 'apiProp', 'accessor', 'opts', `
|
|
return ${fn.toString().replace('function', 'function ' + apiProp)}
|
|
`);
|
|
fn = createNamedFunction(srcProp, apiProp, accessor, opts);
|
|
if (accessor === 'getter')
|
|
Object.defineProperty(dest, apiProp, { get: fn, configurable: true });
|
|
else if (accessor === 'setter')
|
|
Object.defineProperty(dest, apiProp, { set: fn, configurable: true });
|
|
else {
|
|
// NOTE: need to create `property` but not a `function` to stop on `debugger`
|
|
// before the action is called
|
|
Object.defineProperty(dest, apiProp, {
|
|
get() {
|
|
const testController = getTestController(this);
|
|
if (testController && testController.shouldStop(apiProp)) {
|
|
// eslint-disable-next-line no-debugger
|
|
debugger;
|
|
}
|
|
return fn;
|
|
},
|
|
configurable: true,
|
|
});
|
|
}
|
|
});
|
|
}
|
|
exports.delegateAPI = delegateAPI;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVsZWdhdGVkLWFwaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy91dGlscy9kZWxlZ2F0ZWQtYXBpLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBLE1BQU0sNEJBQTRCLEdBQUcsNEJBQTRCLENBQUM7QUFFbEUsU0FBUyxnQkFBZ0IsQ0FBRSxHQUFHOztJQUMxQixPQUFPLENBQUEsTUFBQSxHQUFHLGFBQUgsR0FBRyx1QkFBSCxHQUFHLENBQUUsV0FBVywwQ0FBRSxJQUFJLE1BQUssZ0JBQWdCLENBQUM7QUFDdkQsQ0FBQztBQUVELFNBQVMsaUJBQWlCLENBQUUsR0FBRztJQUMzQixJQUFJLGdCQUFnQixDQUFDLEdBQUcsQ0FBQztRQUNyQixPQUFPLEdBQUcsQ0FBQztTQUVWLElBQUksZ0JBQWdCLENBQUMsR0FBRyxDQUFDLGVBQWUsQ0FBQztRQUMxQyxPQUFPLEdBQUcsQ0FBQyxlQUFlLENBQUM7SUFFL0IsT0FBTyxJQUFJLENBQUM7QUFDaEIsQ0FBQztBQUVELFNBQWdCLG1CQUFtQixDQUFFLEdBQUc7SUFDcEMsT0FBTyxNQUFNO1NBQ1IsbUJBQW1CLENBQUMsR0FBRyxDQUFDO1NBQ3hCLEdBQUcsQ0FBQyxJQUFJLENBQUMsRUFBRTtRQUNSLE1B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b3ApKSB7XG4gICAgICAgICAgICAgICAgICAgICAgICAvLyBlc2xpbnQtZGlzYWJsZS1uZXh0LWxpbmUgbm8tZGVidWdnZXJcbiAgICAgICAgICAgICAgICAgICAgICAgIGRlYnVnZ2VyO1xuICAgICAgICAgICAgICAgICAgICB9XG5cbiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGZuO1xuICAgICAgICAgICAgICAgIH0sXG4gICAgICAgICAgICAgICAgY29uZmlndXJhYmxlOiB0cnVlLFxuICAgICAgICAgICAgfSk7XG4gICAgICAgIH1cbiAgICB9KTtcbn1cbiJdfQ==
|