【发布时间】:2016-08-15 07:32:27
【问题描述】:
我正在尝试拦截 ES6 代理上的方法调用,以便能够使用从代理获得的信息在两者之间进行处理。现在就我而言,在从某种工厂创建和返回代理之前,有很多事情要做。由于所有这些东西,我决定将先决条件包装到一个承诺函数中,以便我可以将代理创建直接链接到它并通过承诺链返回生成的代理。这是重现问题的代码:
proxy_factory.min.js
'use strict';
// require('harmony-reflect');
class ProxyFactory {
create(options) {
const self = this;
const handler = {
get(target, propertyKey, receiver) {
if (propertyKey === 'then') {
return function proxyPromiseWrapper(thenCallback) {
const innerProxy = self.create(options);
return thenCallback(innerProxy);
};
}
return function resourceFunctionProxy() {
const callContext = {
target: target,
method: propertyKey,
args: arguments,
thisContext: this
};
const resourceInstanceMethod = Reflect.get(options.originalObject, callContext.method);
return resourceInstanceMethod.apply(callContext.thisContext, callContext.arguments);
};
}
};
return new Proxy(options.originalObject, handler);
}
}
module.exports = ProxyFactory;
test.js
'use strict';
const Promise = require('bluebird');
const ProxyFactory = require('./proxy_factory.min.js');
const proxyFactory = new ProxyFactory();
function createProxyWithPromise() {
const TestClass = class {
doSomething() {
return Promise.resolve('promise return value');
}
};
const options = {
originalObject: new TestClass()
};
return Promise.resolve()
.then(() => {
return proxyFactory.create(options);
});
}
function test() {
createProxyWithPromise()
.then((proxy) => {
const result = proxy.doSomething();
console.log(result); // should output 'promisereturnvalue'
});
}
test();
在代理上调用 doSomething() 之前,会一遍又一遍地调用 then() 函数,从而导致堆栈溢出。 我已经在 node.js github 问题中问过这个问题,你可以在这里找到之前的对话:https://github.com/nodejs/node/issues/8082 也许它可以帮助某人帮助我;)
【问题讨论】:
-
嗯,是的,
proxyPromiseWrapper将立即使用另一个 thenable 调用回调,而不是等待结果。我不知道你想做什么,也不知道为什么要在那里特别对待承诺。 -
你的代理很奇怪。无论什么属性,它总是返回一个函数。
-
您不应该将
ProxyFactory设为一个类。它不持有任何状态,所以不要创建它的实例。只需导出createProxy(option)工厂函数即可。 -
是的
proxyPromiseWrapper是我试图处理“干扰”我的拦截的承诺。我的问题是我必须通过承诺链返回代理,因此只能在代理内部调用then。我想要实现的是假装 then 没有在我的代理上调用,这样我就可以获得在我通过承诺链返回代理后发生的实际方法调用。我用它做了一堂课,因为当一个电话被拦截时会有更多的事情发生。 -
我想你需要做的就是
return undefined。如果您的代理不包含承诺,则它不应该是 thenable。
标签: javascript node.js proxy ecmascript-6 es6-promise