Angular ngrx效果-每隔几秒钟有条件地调用/停止效果时出现问题
我正在尽我最大的努力,使用计时器以以下方式每2秒有条件地启用ngrx效果:Angular ngrx效果-每隔几秒钟有条件地调用/停止效果时出现问题,angular,ngrx,ngrx-effects,Angular,Ngrx,Ngrx Effects,我正在尽我最大的努力,使用计时器以以下方式每2秒有条件地启用ngrx效果: @Effect() loadSummary$ = this.actions$ .pipe( ofType(SummaryTypes.SAVE_SUMMARY), switchMap((action:any) => (action.running ? timer(0, 2000) : NEVER)), switchMap((action: any) =
@Effect()
loadSummary$ = this.actions$
.pipe(
ofType(SummaryTypes.SAVE_SUMMARY),
switchMap((action:any) => (action.running ? timer(0, 2000) : NEVER)),
switchMap((action: any) => {
console.log(action); /* prints 0 (how is this possible?!) */
return this.analyticsService.getData(action.fetchArr, action.startAndEndDate).pipe(
catchError((err) => {
/* log error */
}),
)
}),
)
所以我每2秒调用第二个switchMapaction.running
在第一次switchMap调用中也会正确返回true或false,但是console.log(action)
返回0,类似地,action.fetchArr
和action.startAndDate
返回未定义
这是因为我无法将第一个操作返回到第二个操作。有人能告诉我我做错了什么吗
我想要什么:
我希望效果每2秒调用一次,如果action.running
返回false,则必须立即停止<代码>控制台日志(操作)代码>应在第二个开关映射中正确返回传递的对象,而不是0
我的尝试:
尝试阅读过去几个小时的效果,甚至尝试使用takeUntil
,但找不到任何特别的方法。嗨,我试过这样的方法,效果很好
public someSubject = new Subject<boolean>();
public someObes = from([1, 2, 3, 4]);
ngOnInit() {
this.someSubject
.pipe(
switchMap(
(value: boolean): any =>
timer(0, 200).pipe(
filter(() => value),
switchMap((): any => this.someObes)
)
)
)
.subscribe(value => console.log(value));
this.someSubject.next(true);
setTimeout(() => this.someSubject.next(false), 3 * 1000);
setTimeout(() => this.someSubject.next(true), 5 * 1000);
setTimeout(() => this.someSubject.next(false), 8 * 1000);
}
}
public someSubject=新主题();
公共someObes=from([1,2,3,4]);
恩戈尼尼特(){
这个,某个主题
.烟斗(
开关图(
(值:布尔):任意=>
计时器(0,200)。管道(
过滤器(()=>值),
switchMap(():any=>this.someObes)
)
)
)
.subscribe(值=>console.log(值));
这个。某个主题。下一个(对);
setTimeout(()=>this.someSubject.next(false),3*1000);
setTimeout(()=>this.someSubject.next(true),5*1000);
setTimeout(()=>this.someSubject.next(false),8*1000);
}
}
我想你应该pipe
timer(02000)
嗨,我试过这样的东西,工作了
public someSubject = new Subject<boolean>();
public someObes = from([1, 2, 3, 4]);
ngOnInit() {
this.someSubject
.pipe(
switchMap(
(value: boolean): any =>
timer(0, 200).pipe(
filter(() => value),
switchMap((): any => this.someObes)
)
)
)
.subscribe(value => console.log(value));
this.someSubject.next(true);
setTimeout(() => this.someSubject.next(false), 3 * 1000);
setTimeout(() => this.someSubject.next(true), 5 * 1000);
setTimeout(() => this.someSubject.next(false), 8 * 1000);
}
}
public someSubject=新主题();
公共someObes=from([1,2,3,4]);
恩戈尼尼特(){
这个,某个主题
.烟斗(
开关图(
(值:布尔):任意=>
计时器(0,200)。管道(
过滤器(()=>值),
switchMap(():any=>this.someObes)
)
)
)
.subscribe(值=>console.log(值));
这个。某个主题。下一个(对);
setTimeout(()=>this.someSubject.next(false),3*1000);
setTimeout(()=>this.someSubject.next(true),5*1000);
setTimeout(()=>this.someSubject.next(false),8*1000);
}
}
我认为你应该用管道定时器(0,2000)
来解决你的误解:
.console.log(操作);/*在switchMap中打印0(这怎么可能?!)*/
在此步骤之前,您将切换到可观察的计时器,该计时器返回一个运行数字。检查此处的操作类型。这不是你所期望的
我认为这应该起作用:
switchMap((action: any) =>
(action.running ? timer(0, 2000) : NEVER)
.pipe(map(() => action))),
switchMap((action: any) => { // now this is actually of type 'Action'
console.log(action);
要真正解决您的误解:
.console.log(操作);/*在switchMap中打印0(这怎么可能?!)*/
在此步骤之前,您将切换到可观察的计时器,该计时器返回一个运行数字。检查此处的操作类型。这不是你所期望的
我认为这应该起作用:
switchMap((action: any) =>
(action.running ? timer(0, 2000) : NEVER)
.pipe(map(() => action))),
switchMap((action: any) => { // now this is actually of type 'Action'
console.log(action);
好吧,我明白这一点,只是无法得到如何在这种情况下有条件地启用和禁用计时器?在我的例子中,我有
value.running
参数可以跟踪。所以,若为真,那个么计时器将继续运行,若为假,那个么计时器将停止。您将如何将其纳入此代码中?另外,您的代码在组件文件中,但我编写的代码在ngrx效果文件中运行,在这种情况下,需要有人向效果文件传递计时器停止操作。ngrx效果工作链接我编写的主题。我会写下效果并更新我的答案。我不理解的是:在你的代码动作中。running
runningproperty是你的动作的payload
,对吗?或者操作。运行是可观察的吗?是的,是有效负载,而不是可观察的。it:感谢ALGDB,我已经按照这里的接受答案中提到的内容解决了这个问题。好的,我理解这一点,只是无法得到在这种情况下我如何有条件地启用和禁用计时器?在我的例子中,我有value.running
参数可以跟踪。所以,若为真,那个么计时器将继续运行,若为假,那个么计时器将停止。您将如何将其纳入此代码中?另外,您的代码在组件文件中,但我编写的代码在ngrx效果文件中运行,在这种情况下,需要有人向效果文件传递计时器停止操作。ngrx效果工作链接我编写的主题。我会写下效果并更新我的答案。我不理解的是:在你的代码动作中。running
running
property是你的动作的payload
,对吗?或者操作。运行是可观察的吗?是的,它是有效载荷,而不是可观察的。it:谢谢ALGDB,我已经根据此处接受的答案中提到的内容解决了这个问题。谢谢。这是可行的,从最近几天开始,我一直在努力解决这个问题.pipe(map(()=>action))
这是我唯一缺少的一块,我从未想过通过MapThank返回action。这是可行的,从最近几天开始,我一直在努力解决这个问题.pipe(map(()=>action))
这是我唯一缺少的一块,我从未想过通过map返回action