RxJS 管道如何工作(可出租操作符)
How RxJS pipe works (lettable operators)
这实际上是给自己的笔记,但可能对其他人有用。
所以,这是 2 段代码和我在 gitter 中的问题:
这两者有什么区别?:
@Effect()
loadRegistrationsFailed$: Observable<Action> = this.actions$
.ofType(registrations.LOAD_FAIL)
.pipe(
map(
action =>
new ShowErrorDialogAction({
correlationId: new Guid(),
title: "Server is unreachable",
message:
"Can't load user registrations. Can't connect to the server"
})
)
);
```
and
```
@Effect()
loadRegistrationsFailed$: Observable<Action> = this.actions$
.ofType(registrations.LOAD_FAIL)
.pipe(action =>
of(
new ShowErrorDialogAction({
correlationId: new Guid(),
title: "Server is unreachable",
message:
"Can't load user registrations. Can't connect to the server"
})
)
);
感谢 Brandt B.,这是答案:
its related to how the pipe function works. pipe reduces the array of functions passed to it. In the previous value it executes the map function which stores the function you passed to the map internally. In the second example it executes the action=> of immediatly and returns it as the result of the pipe. Therefore the result of the entire observable is of(action) which gets subscribed to by the effect library which produces the value immediately
Dorus 对同一问题的回答:
这两个示例之间的区别在于第一个示例将映射值,而第二个示例仅将整个内容替换为 of 并忽略源发出的任何内容。
第二个的正确写法是
@Effect()
loadRegistrationsFailed$: Observable<Action> = this.actions$
.ofType(registrations.LOAD_FAIL)
.pipe(ob => ob.mergeMap(action =>
of(
new ShowErrorDialogAction({
correlationId: new Guid(),
title: "Server is unreachable",
message:
"Can't load user registrations. Can't connect to the server"
})
))
);
因为你不使用动作,你也可以使用 mergeMapTo 或 mepTo:
@Effect()
loadRegistrationsFailed$: Observable<Action> = this.actions$
.ofType(registrations.LOAD_FAIL)
.pipe(ob => ob.mergeMapTo(of(
new ShowErrorDialogAction({
correlationId: new Guid(),
title: "Server is unreachable",
message:
"Can't load user registrations. Can't connect to the server"
})
))
);
唯一可让运算符添加的是您可以编写 .pipe(map()) 而不是 .pipe(ob => ob.map())
这实际上是给自己的笔记,但可能对其他人有用。
所以,这是 2 段代码和我在 gitter 中的问题:
这两者有什么区别?:
@Effect()
loadRegistrationsFailed$: Observable<Action> = this.actions$
.ofType(registrations.LOAD_FAIL)
.pipe(
map(
action =>
new ShowErrorDialogAction({
correlationId: new Guid(),
title: "Server is unreachable",
message:
"Can't load user registrations. Can't connect to the server"
})
)
);
```
and
```
@Effect()
loadRegistrationsFailed$: Observable<Action> = this.actions$
.ofType(registrations.LOAD_FAIL)
.pipe(action =>
of(
new ShowErrorDialogAction({
correlationId: new Guid(),
title: "Server is unreachable",
message:
"Can't load user registrations. Can't connect to the server"
})
)
);
感谢 Brandt B.,这是答案:
its related to how the pipe function works. pipe reduces the array of functions passed to it. In the previous value it executes the map function which stores the function you passed to the map internally. In the second example it executes the action=> of immediatly and returns it as the result of the pipe. Therefore the result of the entire observable is of(action) which gets subscribed to by the effect library which produces the value immediately
Dorus 对同一问题的回答:
这两个示例之间的区别在于第一个示例将映射值,而第二个示例仅将整个内容替换为 of 并忽略源发出的任何内容。
第二个的正确写法是
@Effect()
loadRegistrationsFailed$: Observable<Action> = this.actions$
.ofType(registrations.LOAD_FAIL)
.pipe(ob => ob.mergeMap(action =>
of(
new ShowErrorDialogAction({
correlationId: new Guid(),
title: "Server is unreachable",
message:
"Can't load user registrations. Can't connect to the server"
})
))
);
因为你不使用动作,你也可以使用 mergeMapTo 或 mepTo:
@Effect()
loadRegistrationsFailed$: Observable<Action> = this.actions$
.ofType(registrations.LOAD_FAIL)
.pipe(ob => ob.mergeMapTo(of(
new ShowErrorDialogAction({
correlationId: new Guid(),
title: "Server is unreachable",
message:
"Can't load user registrations. Can't connect to the server"
})
))
);
唯一可让运算符添加的是您可以编写 .pipe(map()) 而不是 .pipe(ob => ob.map())