[英]TypeScript Javascript Class, two instances this get mixed when returning Promise
I have a typescript app that it is mixing 'this' context inside a class. 我有一个打字稿应用程序,它正在类中混合“此”上下文。
This class is responsible to setup new Express server instance. 此类负责设置新的Express服务器实例。
If I track 'this' with debugger and node --inspect-brk
we can see 'this' is undefined when serverProtocol gets resolved but it does not matter ! 如果我使用调试器和
node --inspect-brk
跟踪“ this”,则可以看到在解析serverProtocol时未定义“ this”, 但这没有关系 !
I've setup a little project to show what i'm facing. 我已经设置了一个小项目来展示我所面临的问题。
$> git clone https://github.com/AdSegura/ThisLoseContext.git
$> cd ThisLoseContext/
$> npm i
$> npm run build && node dist/
INIT: Server_ID: quijote_1111, PORT: 1111
-----------------------------------------
INIT: Server_ID: quijote_2222, PORT: 2222
-----------------------------------------
Callback: Server_ID: quijote_1111, PORT: 1111
ServerProtocol: Server_ID: quijote_1111; PORT: 1111
Callback: Server_ID: quijote_2222, PORT: 2222
ServerProtocol: Server_ID: quijote_2222; PORT: 2222
INIT: Server_ID: quijote_1111, PORT: 1111
-----------------------------------------
INIT: Server_ID: quijote_2222, PORT: 2222
-----------------------------------------
Callback: Server_ID: quijote_1111, PORT: 2222 [ERROR]
ServerProtocol: Server_ID: quijote_1111; PORT: 2222 [ERROR]
Callback: Server_ID: quijote_2222, PORT: 2222
ServerProtocol: Server_ID: quijote_2222; PORT: 2222
The problem is in Server.ts this.serverProtocol()
is mixing this.options.port
问题出在Server.ts中,
this.serverProtocol()
正在混合this.options.port
var http = require('http');
var express = require('express');
const os = require("os");
export class Server {
/** The http server.*/
public express: any;
/** express httpServer */
protected server: any;
/** id representing server instance */
protected server_id: any;
/**
* Create a new server instance.
*/
constructor(private options: any) {
this.server_id = this.generateServerId();
}
/**
* Start the Socket.io server.
*
* @return {void}
*/
init(): Promise<any> {
console.log(`INIT: Server_ID: ${this.server_id}, PORT: ${this.options.port}`);
return new Promise((resolve, reject) => {
debugger;
this.serverProtocol().then(instance => {
debugger;
console.log(`ServerProtocol: Server_ID: ${this.server_id}; PORT: ${this.options.port}`);
resolve();
}, error => reject(error));
});
}
/**
* Select the http protocol to run on.
*
* @return {Promise<any>}
*/
serverProtocol(): Promise<any> {
return this.httpServer()
}
/**
* Express socket.io server.
*/
httpServer(): Promise<any> {
return new Promise((resolve, reject) => {
this.express = express();
this.express.use((req, res, next) => {
for (var header in this.options.headers) {
res.setHeader(header, this.options.headers[header]);
}
next();
});
const httpServer = http.createServer(this.express);
function cb() {
debugger;
console.log(`Callback: Server_ID: ${this.server_id}, PORT: ${this.options.port}`)
return resolve.call(this, this)
}
this.server = httpServer.listen(this.options.port, this.options.host, () => cb.call(this));
})
}
/**
* Generate Server Id
*
* @return string hostname_port
*/
generateServerId(): string {
const hostname = os.hostname();
const port = this.options.port;
return `${hostname}_${port}`
}
}
I'm a novice with typescript, I tried multiple targets at tsconfig but same result, this.options
gets the last config {port:2222}
when you instanciate two or more objects. 我是打字稿的新手,我在tsconfig上尝试了多个目标,但结果相同,当实例化两个或多个对象时,
this.options
获得最后一个配置 {port:2222}
。
Thanks. 谢谢。
I think understand this: 我想了解一下:
There is no such thing as a "local object".
没有所谓的“本地对象”。 You do have a reference to an object.
您确实有一个对象的引用。 Two instances might have two references to the same object.
两个实例可能具有对同一对象的两个引用。
const opt = { foo: 3 };
export class MyClass {
constructor(private options: any){
this.options = Object.assign(opt, options)
}
getOptions(): any {
return this.options
}
}
export class MyClass {
constructor(private options: any){
const opt = { foo: 3 };
this.options = Object.assign(opt, options)
}
getOptions(): any {
return this.options
}
}
But as our partners said bellow, the safest methods to create new options variable are: 但是正如我们的合作伙伴所说的那样,创建新的option变量的最安全方法是:
this.options = Object.assign({}, this.defaultOptions, config);
or 要么
this.options = { ...this.defaultOptions, ...config };
And now we have a new object not a copy to an object outside the scope of the constructor method. 现在,我们有了一个新对象,而不是构造函数方法范围之外的对象的副本。
So the safest version of the code should be: 因此,最安全的代码版本应为:
export class MyClass {
constructor(private options: any){
const opt = { foo: 3 };
this.options = Object.assign({}, opt, options)
}
getOptions(): any {
return this.options
}
}
The issue is probably inside the EchoServer
class, namely here: 问题可能在
EchoServer
类内部,即在这里:
this.options = Object.assign(this.defaultOptions, config);
Thats equal to: 那等于:
this.options = this.defaultOptions;
Object.assign(this.defaultOptions, config);
I assume you actually wanted to do: 我认为您确实想这样做:
this.options = Object.assign({}, this.defaultOptions, config);
which creates an own options
object for each instance. 为每个实例创建一个自己的
options
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.