在ES2015中如何确保所有方法等待对象初始化?与ES7装饰? [英] In ES2015 how can I ensure all methods wait for object to initialize ? With ES7 decorators?

查看:94
本文介绍了在ES2015中如何确保所有方法等待对象初始化?与ES7装饰?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到远程服务的ES2015类。



问题是我的代码尝试在对象连接到远程对象之前访问此类服务器。



如果对象尚未完成初始化,我想确保方法不会发生错误。



我将在我的类中有很多依赖于连接正在运行的方法,所以如果有一个单一的,容易理解的机制可以应用于所有方法,如@ensureConnected装饰器,这将是很好的。



这里的小提琴: https://jsfiddle.net / mct6ss19 / 2 /

 'use strict'; 

class Server {
helloWorld(){
returnHello world
}
}

class Client {
构造函数(){
this.connection = null
this.establishConnection()
}

建立连接(){
//模拟慢连接设置通过在2秒后初始化
setTimeout(()=> {this.connection = new Server()},2000)
}

doSomethingRemote(){
console.log(this.connection.helloWorld())
}

}

let test = new Client();
//不起作用,因为我们在对象初始化后立即尝试
test.doSomethingRemote();
//因为对象有时间初始化
setTimeout(()=> {test.doSomethingRemote()},3000)

我正在使用ES7装饰器进行成像,以实现连接建立,但无法看到如何执行此测试。

解决方案

我不会在构造函数中启动连接。构造函数更加设计用于初始化变量等而不是程序逻辑。我会从客户端代码调用 establishConnection



如果你想在构造函数中这样做,结果在一个实例变量,然后等待它在 doSomethingRemote ,如:

  class Client {
constructor(){
this.connection = this.establishConnection();
}

建立连接(){
//在2秒后通过初始化模拟慢连接设置
返回新的Promise(resolve => setTimeout(()=> ;
resolve(new Server()),2000));
}

doSomethingRemote(){
this.connection.then(connection => connection.helloWorld());
}

}


I have an ES2015 class that connects to a remote service.

The problem is that my code tries to access this class before its object has finished connecting to the remote server.

I want to ensure that methods don't just give an error if the object has not finished initializing.

I'll have alot of methods in my class that depend on the connection being up and running, so it would be good if there was a single, easy to understand mechanism that could be applied to all methods like an @ensureConnected decorator.

Fiddle here: https://jsfiddle.net/mct6ss19/2/

'use strict';

class Server {
    helloWorld() {
        return "Hello world"
    }
}

class Client {
    constructor() {
            this.connection = null
            this.establishConnection()
    }

    establishConnection() {
        // simulate slow connection setup by initializing after 2 seconds
        setTimeout(() => {this.connection= new Server()}, 2000)
    }

    doSomethingRemote() {
            console.log(this.connection.helloWorld())
    }

}

let test = new Client();
// doesn't work because we try immediately after object initialization
test.doSomethingRemote();
// works because the object has had time to initialize
setTimeout(() => {test.doSomethingRemote()}, 3000)

I was imaging using ES7 decorators to implement a test to see if the connection is established but I can't see how to do so.

解决方案

I would not initiate the connection in the constructor. Constructors are more designed for initializing variables, etc., rather than program logic. I would instead call establishConnection yourself from your client code.

If you want to do this in the constructor, store the result in an instance variable, and then wait for it in doSomethingRemote, as in:

class Client {
    constructor() {
        this.connection = this.establishConnection();
    }

    establishConnection() {
        // simulate slow connection setup by initializing after 2 seconds
        return new Promise(resolve => setTimeout(() =>
          resolve(new Server()), 2000));
    }

    doSomethingRemote() {
        this.connection.then(connection => connection.helloWorld());
    }

}

这篇关于在ES2015中如何确保所有方法等待对象初始化?与ES7装饰?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆