在使用之前,请等待提供者构造函数准备就绪

问题描述 投票:1回答:1

在我的应用程序中,我使用提供程序进行身份验证,一个用于加载应用程序配置文件。然而,auth提供商使用来自config提供商的数据。因此,我希望auth提供程序在调用它的config方法之前等待getSelectedConfig()提供程序构造函数准备就绪。现在我实现了以下机制

// AuthProvider
@Injectable()
export class AuthProvider {

  private url:string;
  private token:string;

  constructor(
    public http: HttpClient,
    public config: ConfigProvider
  ) { 
    this.loadParams();
  }

  private loadParams() {
    this.config.ready().subscribe(
      ready => {
        this.config.getSelectedConfig().subscribe(
          config => { 
            this.url = config.endpoint;
            this.token = config.token;
          }
        );
      }
    )
  } 
}

// ConfigProvider (in seperate file, of course)
@Injectable()
export class ConfigProvider {

  private selectedConfig:Config = null;
  private configStorageKey:string = "selected_config";
  public readyObservable:Observable<boolean>;

  constructor(
      public storage: Storage,
      public http: HttpClient
    ) {
    this.checkStorageForConfig();
  }

  public ready(){
    return this.readyObservable;
  }

  public checkStorageForConfig() {
    this.readyObservable = Observable.create(
      observer => {
        this.storage.get(this.configStorageKey).then(
          config => {
            if(config){
              this.selectedConfig = config;
            }
            observer.next();
          }
        )
      }
    )
  }
}

所以我创建了一个Observable,可以给auth知道是否可以使用config提供程序。

这种方法有效,但我不认为这是一个很好的解决方案。但我无法想出更好的一个。我一般不知道如何处理这种情况。

在我看来,我想简单地称之为this.config.ready().then( ... ),而不引入其他Observables。但也许这是正确的方法。

angular typescript asynchronous ionic-framework storage
1个回答
1
投票

您可以通过使用等待async / await来简化它,如果逻辑开始进一步增加,我建议使用单独的服务来处理onReady问题。这只是一个简单的返工:

// AuthProvider
@Injectable()
export class AuthProvider {

private url: string;
private token: string;

constructor(
    public http: HttpClient,
    public config: ConfigProvider
) {
    this.loadParams();
}

private loadParams() {
    this.config.readyObservable.subscribe(
        ready => {
            if (ready) {
                this.config.getSelectedConfig().subscribe(
                    config => {
                        this.url = config.endpoint;
                        this.token = config.token;
                    }
                );
            }
        }
    )
}

// ConfigProvider (in seperate file, of course)

@Injectable()
export class ConfigProvider {

private selectedConfig: Config = null;
private configStorageKey: string = "selected_config";
public readyObservable: ReplaySubject<boolean> = new 
ReplaySubject(1);

constructor(
    public storage: Storage,
    public http: HttpClient
) {
    waitForStuffInConstructor()
}

public ready() {
    return this.readyObservable;
}
public async waitForStuffInConstructor() {
    await this.checkStorageForConfig();
    this.readyObservable.next(true);
}
public checkStorageForConfig() {
    this.storage.get(this.configStorageKey).then(
        config => {
            if (config) {
                this.selectedConfig = config;
            }
            observer.next();
        }
    )
}
© www.soinside.com 2019 - 2024. All rights reserved.