工厂选择器不会通过状态更新触发订阅
Factory selector does not fire subscription with state update
引用 this NGRX ticket,我看到一个奇怪的现象,即基于代码的工厂选择器订阅不会随着状态更新而触发,但是,async
管道 会工作。
澄清一下,如果我在组件上设置了一个 public observable 属性 并使用 async
管道将 HTML 绑定到它,该值将更新随着状态更新。
component.ts
public test$: Observable<ChatInteraction>;
[...]
this.test$ = this.store
.select(getNewMessageForChat(this.chat.chatId));
component.html
test: {{(test$ | async).message}}
然后我得到像
这样的输出
test: test1
test: test2
etc etc
但是,如果我尝试订阅同一个可观察对象,它会在创建订阅时触发一次,但不会在状态更新时再次触发。
component.ts
this.store
.select(getNewMessageForChat(this.chat.chatId))
.subscribe(() => {
if (this._calculateScrollDistanceFromBottom(this.chatWindowElem.nativeElement) <= 20) {
this._scrollToBottom();
}
});
如果我在那个 if
语句上放置一个断点,它不会在我更新状态时被击中。
知道为什么吗?
想通了。结果我的订阅中有一个未定义的对象引用,这导致订阅在其第一个 运行 处中断并导致它不再触发。这很难捕捉到,因为事实证明 subscription
代码中有一个 try/crush,除非设置了设置。
__tryOrUnsub(fn, value) {
try {
fn.call(this._context, value);
}
catch (err) {
this.unsubscribe();
if (config.useDeprecatedSynchronousErrorHandling) {
throw err;
}
else {
hostReportError(err);
}
}
}
hostReportError
没有冒出任何东西,所以对我来说它看起来好像不再开火了。
引用 this NGRX ticket,我看到一个奇怪的现象,即基于代码的工厂选择器订阅不会随着状态更新而触发,但是,async
管道 会工作。
澄清一下,如果我在组件上设置了一个 public observable 属性 并使用 async
管道将 HTML 绑定到它,该值将更新随着状态更新。
component.ts
public test$: Observable<ChatInteraction>;
[...]
this.test$ = this.store
.select(getNewMessageForChat(this.chat.chatId));
component.html
test: {{(test$ | async).message}}
然后我得到像
这样的输出test: test1
test: test2
etc etc
但是,如果我尝试订阅同一个可观察对象,它会在创建订阅时触发一次,但不会在状态更新时再次触发。
component.ts
this.store
.select(getNewMessageForChat(this.chat.chatId))
.subscribe(() => {
if (this._calculateScrollDistanceFromBottom(this.chatWindowElem.nativeElement) <= 20) {
this._scrollToBottom();
}
});
如果我在那个 if
语句上放置一个断点,它不会在我更新状态时被击中。
知道为什么吗?
想通了。结果我的订阅中有一个未定义的对象引用,这导致订阅在其第一个 运行 处中断并导致它不再触发。这很难捕捉到,因为事实证明 subscription
代码中有一个 try/crush,除非设置了设置。
__tryOrUnsub(fn, value) {
try {
fn.call(this._context, value);
}
catch (err) {
this.unsubscribe();
if (config.useDeprecatedSynchronousErrorHandling) {
throw err;
}
else {
hostReportError(err);
}
}
}
hostReportError
没有冒出任何东西,所以对我来说它看起来好像不再开火了。