行为主题退订重构
Behavior Subject unsubscribe refactor
这不是取消行为主题的唯一方法:
maxSub;
fromSub;
toSub;
pageNumberSub;
originalMaxSub;
selectionSub;
selectionOfInterestSub;
constructor(private store: StoreService) {}
ngAfterContentChecked(){
this.maxSub = this.store.max$.subscribe((max) => {
if(max !== null && max !== undefined){
this.max = max;
this.maxPages = Math.floor(this.max / Variables.to);
this.pages = Array.from(Array(this.maxPages), (x, i) => i + 1);
}
}, (error) => console.log(error), () => {});
this.fromSub = this.store.from$.subscribe((from) => {
if(from !== null && from !== undefined) {this.from = from; this.split = this.to - (this.from - 1)}
}, (error) => console.log(error), () => {});
this.toSub = this.store.to$.subscribe((to) => {
if(to !== null && to !== undefined) {this.to = to; this.split = this.to - (this.from - 1)}
}, (error) => console.log(error), () => {});
this.pageNumberSub = this.store.pageNumber$.subscribe((pageNumber) => {
if(pageNumber !== null && pageNumber !== undefined) {this.page = pageNumber;}
}, (error) => console.log(error), () => {});
this.originalMaxSub = this.store.originalMax$.subscribe((originalMax) => {
if(originalMax !== null && originalMax !== undefined) {this.originalMax = originalMax;}
}, (error) => console.log(error), () => {});
this.selectionSub = this.store.selection$.subscribe((selection) => {
if(selection !== null && selection !== undefined) this.selectedAmount = selection.length;
}, (error) => console.log(error), () => {});
this.selectionOfInterestSub = this.store.selectionOfInterest$.subscribe((selectionOfInterest) => {
if(selectionOfInterest !== null && selectionOfInterest !== undefined) this.selectionOfInterest = selectionOfInterest;
}, (error) => console.log(error), () => {});
}
ngOnDestroy(){
this.maxSub.unsubscribe();
this.fromSub.unsubscribe();
this.toSub.unsubscribe();
this.pageNumberSub.unsubscribe();
this.originalMaxSub.unsubscribe();
this.selectionSub.unsubscribe();
this.selectionOfInterestSub.unsubscribe();
}
在常规的旧 Observable 中,您可以:
alive = true;
constructor(private store: StoreService) {}
ngAfterContentChecked(){
this.store.thing.subscribe().takeWhile(this.alive)...
}
ngOnDestroy(){
this.alive = false;
}
这样就简单多了。
有没有办法对行为主体做同样的事情?
另一件事:我是否遗漏了一个基本的 属性 行为主题,无论订阅被调用多少次,他们只在 class 中订阅一次,这就是为什么没有这样的东西takeWhile()
关于行为主题?
1 - 实际上,您所说的更简单的方法根本不会取消订阅,它只是停止阅读来自可观察对象的内容。所以要么使用退订要么完成。
2 - 由于您同时取消订阅,并且您显然不会将 Subject 用于任何其他用途,因此可以为所有订阅使用相同的 Subject 变量,然后您可以使用一行代码取消订阅而且您不需要初始化所有这些 Subject 实例。
这不是取消行为主题的唯一方法:
maxSub;
fromSub;
toSub;
pageNumberSub;
originalMaxSub;
selectionSub;
selectionOfInterestSub;
constructor(private store: StoreService) {}
ngAfterContentChecked(){
this.maxSub = this.store.max$.subscribe((max) => {
if(max !== null && max !== undefined){
this.max = max;
this.maxPages = Math.floor(this.max / Variables.to);
this.pages = Array.from(Array(this.maxPages), (x, i) => i + 1);
}
}, (error) => console.log(error), () => {});
this.fromSub = this.store.from$.subscribe((from) => {
if(from !== null && from !== undefined) {this.from = from; this.split = this.to - (this.from - 1)}
}, (error) => console.log(error), () => {});
this.toSub = this.store.to$.subscribe((to) => {
if(to !== null && to !== undefined) {this.to = to; this.split = this.to - (this.from - 1)}
}, (error) => console.log(error), () => {});
this.pageNumberSub = this.store.pageNumber$.subscribe((pageNumber) => {
if(pageNumber !== null && pageNumber !== undefined) {this.page = pageNumber;}
}, (error) => console.log(error), () => {});
this.originalMaxSub = this.store.originalMax$.subscribe((originalMax) => {
if(originalMax !== null && originalMax !== undefined) {this.originalMax = originalMax;}
}, (error) => console.log(error), () => {});
this.selectionSub = this.store.selection$.subscribe((selection) => {
if(selection !== null && selection !== undefined) this.selectedAmount = selection.length;
}, (error) => console.log(error), () => {});
this.selectionOfInterestSub = this.store.selectionOfInterest$.subscribe((selectionOfInterest) => {
if(selectionOfInterest !== null && selectionOfInterest !== undefined) this.selectionOfInterest = selectionOfInterest;
}, (error) => console.log(error), () => {});
}
ngOnDestroy(){
this.maxSub.unsubscribe();
this.fromSub.unsubscribe();
this.toSub.unsubscribe();
this.pageNumberSub.unsubscribe();
this.originalMaxSub.unsubscribe();
this.selectionSub.unsubscribe();
this.selectionOfInterestSub.unsubscribe();
}
在常规的旧 Observable 中,您可以:
alive = true;
constructor(private store: StoreService) {}
ngAfterContentChecked(){
this.store.thing.subscribe().takeWhile(this.alive)...
}
ngOnDestroy(){
this.alive = false;
}
这样就简单多了。
有没有办法对行为主体做同样的事情?
另一件事:我是否遗漏了一个基本的 属性 行为主题,无论订阅被调用多少次,他们只在 class 中订阅一次,这就是为什么没有这样的东西takeWhile()
关于行为主题?
1 - 实际上,您所说的更简单的方法根本不会取消订阅,它只是停止阅读来自可观察对象的内容。所以要么使用退订要么完成。
2 - 由于您同时取消订阅,并且您显然不会将 Subject 用于任何其他用途,因此可以为所有订阅使用相同的 Subject 变量,然后您可以使用一行代码取消订阅而且您不需要初始化所有这些 Subject 实例。