Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rxjs 你最喜欢什么?_Rxjs_Observable_Combinelatest - Fatal编程技术网

Rxjs 你最喜欢什么?

Rxjs 你最喜欢什么?,rxjs,observable,combinelatest,Rxjs,Observable,Combinelatest,我有一些可以观察到的东西。我需要知道是哪一个触发了订阅 Observable.combineLatest( this.tournamentsService.getUpcoming(), this.favoriteService.getFavoriteTournaments(), this.teamsService.getTeamRanking(), (tournament, favorite, team) => { //what triggered co

我有一些可以观察到的东西。我需要知道是哪一个触发了订阅

Observable.combineLatest(
      this.tournamentsService.getUpcoming(),
      this.favoriteService.getFavoriteTournaments(),
      this.teamsService.getTeamRanking(),
(tournament, favorite, team) => {
//what triggered combinelatest to run?
}).subscribe()

简单的回答是:你不知道。 您可以实现一些变通方法,但是这真的很难看,我建议您重新思考为什么需要这个用例,或者如果您可以更改架构的话。 还要记住,函数的第一次执行将在所有三个可观察对象发出至少1个值之后进行

无论如何-可能的解决方法是:

let trigger=”“;
可观察的(
this.tournamentsService.getCombing().do(()=>trigger=“tournament”),
this.favoriteService.getFavoriteTournaments().do(()=>trigger=“favTournament”),
this.teamsService.getTeamRanking().do(()=>trigger=“teamRanking”),
(锦标赛、最受欢迎的比赛、球队)=>{
log(`triggeredby${trigger}`);
}).subscribe();
如果你想执行一个特定的操作,你应该使用每个可观察的对象,并将它们作为单独的触发器,切换到一个组合触发器,它可能会稍微多一些代码,但它更干净,你不会在一个丑陋的If/else中结束,switch/case混乱了一些老套的解决方法-此外,您甚至有机会使用
async
-管道,而不是手动订阅所有内容并更新局部变量(无论如何,这是一种糟糕的做法):

下面是一些示例代码,说明这可能是什么样子:

让即将到来的$=this.tournamentsService.getuncoming();
让favorite$=this.favoriteService.getFavoriteTournaments();
让排名$=this.teamsService.getTeamRanking();
让allData$=Observable.CombineTest(
即将推出的美元、最受欢迎的美元、排名美元、,
(锦标赛、最受欢迎的比赛、球队)=>{
返回{锦标赛、最爱、球队};
}
);
//初始呼叫->这应该是多余的,
//但是因为我不知道你的代码的细节
//我已经把它放进去了-你能不能把它拿走
//取决于数据的输入顺序
所有数据$
.采取(1)
.do({锦标赛、最受欢迎的比赛、球队})=>{
这个.displayMatches(…);
这个。sortByFavorites(…);
这个。填写排名(…);
})
.subscribe();
//单个更新触发器
即将到来$
.switchMapTo(allData$.take(1))
.do(({锦标赛,收藏夹,团队})=>this.displayMatches(…)
.subscribe();
宠儿$
.switchMapTo(allData$.take(1))
.do(({锦标赛,收藏夹,团队})=>this.sortByFavorites(…)
.subscribe();
排名$
.switchMapTo(allData$.take(1))
.do(({锦标赛,收藏夹,团队})=>this.fillWithRanking(…)
.subscribe();
您可以使用操作符将发射值与任何先前发射的值进行比较,并可以包括额外的数据,指示组合可观测值的组件是否实际发生了变化。例如:

let组合=可观察
.联合测试(
this.tournamentsService.getUncoming(),
this.favoriteService.getFavoriteTournaments(),
this.teamsService.getTeamRanking()
)
.扫描((acc,值)=>[
价值观
acc[0]!==值[0],
acc[1]!==值[1],
acc[2]!==值[2]
], []);
联合订阅(
([锦标赛,收藏夹,团队,锦标赛更改,收藏夹更改,团队更改]=>{
log(`tourbank=${tourbank};changed=${tournamentChanged}`);
log(`favorite=${favorite};changed=${favoriteChanged}`);
log(`team=${team};changed=${teamChanged}`);
}
);
实现这一点的一种非常干净的“rx”方法是使用时间戳操作符

示例代码

sourceObservable
  .pipe(
    timestamp(),  // wraps the source items in object with timestamp of emit
    combineLatest( otherObservable.pipe( timestamp() ), function( source, other ) {

      if( source.timestamp > other.timestamp ) {

        // source emitted and triggered combineLatest
        return source.value;
      }
      else {

        // other emitted and triggered combineLatest
        return other.value;
      }

    } ),
  )

如果在
combineLatest()
中涉及两个以上的观测值,则通过时间戳对它们进行排序将能够检测触发
combineLatest()

的是哪一个触发了

我看到了一个函数原型,它包含一个可观察对象列表和一个实现。但是我不知道如何做到这一点,我在实现call方法时遇到了问题。请检查我的代码并做必要的操作

Stream>get cities=>\u citiesController.Stream

Stream get city=>\u cityController.Stream

Stream get agentcity=>\u agentcityController.Stream

Stream get userpackages=>\u packagesController.Stream

Stream get email=>\u emailController.Stream.transform(validateEmail)

Stream get firstName=>\u firstNameController.Stream.transform(validateFirstName)

Stream get lastName=>\u lastNameController.Stream.transform(validateLastName)

Stream get mobileNumber=>\u mobileNumberController.Stream.transform(validateMobile)

Stream get dob=>\u dobController.Stream.transform(validatedob)

Stream get appointmentdate=>\u appointmentdateController.Stream.transform(validateappointmentDate)

Stream get pincode=>\u pincodeController.Stream.transform(验证代码)

Stream get gender=>\u genderController.Stream

Stream get address=>\u addressController.Stream.transform(validateAddress)

Stream get agentname=>\u agentnameController.Stream.transform(validateAgentName)

Stream get validSubmission=>Observable.CombineRelatest9(

))


请让我知道如何在颤振代码中使用CombineTest。

解决此问题的更好方法是使用有区别的联合类型。如果您的语言没有内置区分联合,则可以通过将构造函数设为私有并公开
n
null可为空的公共静态属性来创建一个联合,每个可观察属性对应一个。用C#写这篇文章,因为我更熟悉这种语言,但它应该很容易翻译。请注意可为空的字符串。如果您的语言不支持空值,请使用其他机制指示是否设置了值

private class DiscriminatedUnion
{
    private DiscriminatedUnion(string? property1, string? property2)
    {
        Property1 = property1;
        Property2 = property2;
    }

    public string? Property1 { get; }
    public string? Property2 { get; }

    public static DiscrimintatedUnion FromObservable1(string property1)
    {
        return new DiscriminatedUnion(property1, null);
    }

    public static DiscrimintatedUnion FromObservable2(string property2)
    {
        return new DiscriminatedUnion(null, property2);
    }

}

private IObservable<DiscriminatedUnion> CreateCombination()
{
    var firstObservable = tournamentsService
        .getUpcoming()
        .Select(x => DiscriminatedUnion.FromObservable1(x));

    var secondObservable = favoriteService
        .getFavoriteTournaments()
        .Select(x => DiscriminatedUnion.FromObservable2(x));

    return Observable
        CombineLatest(firstObservable, secondObservable);
}
私有类判别联合
{
私有判别联合(字符串?属性1,字符串?属性2)
private class DiscriminatedUnion
{
    private DiscriminatedUnion(string? property1, string? property2)
    {
        Property1 = property1;
        Property2 = property2;
    }

    public string? Property1 { get; }
    public string? Property2 { get; }

    public static DiscrimintatedUnion FromObservable1(string property1)
    {
        return new DiscriminatedUnion(property1, null);
    }

    public static DiscrimintatedUnion FromObservable2(string property2)
    {
        return new DiscriminatedUnion(null, property2);
    }

}

private IObservable<DiscriminatedUnion> CreateCombination()
{
    var firstObservable = tournamentsService
        .getUpcoming()
        .Select(x => DiscriminatedUnion.FromObservable1(x));

    var secondObservable = favoriteService
        .getFavoriteTournaments()
        .Select(x => DiscriminatedUnion.FromObservable2(x));

    return Observable
        CombineLatest(firstObservable, secondObservable);
}