小编chi*_*lad的帖子

API轮询和超时

我有一个轮询用例,其中:

  1. 我想调用一个 API,它根据业务逻辑立即(1.5-2 秒)返回数字(1-10)或错误(API 中的网络问题/异常等)。
  2. 如果 API 返回错误(API 中的网络问题/异常等),那么我想取消订阅轮询并显示错误。
  3. 如果 API 返回成功,我想检查返回值并取消订阅(如果返回值为 5)或继续轮询。
  4. 我想每 5 秒调用一次 API。
  5. 我想将轮询的最长时间(超时/阈值)保持为 3 分钟。如果我在这 3 分钟内没有得到所需的响应(数字 5),则轮询应该会出错。

这就是我目前的实现方式:

this.trackSpoke$ = interval(5000)
                .pipe(
                    timeout(250000),
                    startWith(0),
                    switchMap(() =>
                        this.sharedService.pollForAPropertyValue(
                            "newuser"
                        )
                    )
                )
                .subscribe(
                    (data: SpokeProperty) => {
                        this.CheckSpokeStatus(data);
                    },
                    error => {
                        this.trackSpoke$.unsubscribe();
                        this.createSpokeState.CdhResponseTimedOut();
                    }
                );


private CheckSpokeStatus(data) {
        if (data.PropertyValue === "5") {
            this.trackSpoke$.unsubscribe();
            //display success   
        } else {
              //keep the polling going
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是,上述实现并没有超时。

需要做什么才能让它超时并且我能够实现所有提到的用例?

polling reactive-programming observable rxjs angular

3
推荐指数
1
解决办法
1720
查看次数

子文件与猫鼬人口

我有以下senario:

用户可以登录网站。用户可以添加/删除民意调查(一个有两个选项的问题)。通过选择任何选项,任何用户都可以对民意调查发表意见。

考虑到上述情况,我有三种模型- Users Polls Options。它们如下-

var usersSchema = new Schema({
username : {
    type : String,
    required : true
},
email : {
    type : String,
    required : true,
    unique : true
},
password : String,
polls : [pollSchema]
Run Code Online (Sandbox Code Playgroud)

});

var pollSchema = new Schema({
question : {
    type : String,
    required : true
},
options : [optionSchema]
Run Code Online (Sandbox Code Playgroud)

});

var optionSchema = new Schema({
optionName : {
    type : String,
    required : true,
},
optionCount : {
    type …
Run Code Online (Sandbox Code Playgroud)

crud mongoose mongodb node.js

2
推荐指数
1
解决办法
431
查看次数