如何结合运营商RXJS?
How to combine operators RXJS?
我有以下四个听众:
this.reportPropertiesChanges$.pipe(filter(() => this.loaded)).subscribe((v) => {
this.registryReportSettings.reportProperties = v;
this.registryReportSettings$.next(this.registryReportSettings);
});
this.objectsProperties$.pipe(filter(() => this.loaded)).subscribe((v) => {
this.registryReportSettings.reportObjectsProperties = v;
this.registryReportSettings$.next(this.registryReportSettings);
});
this.textPropertiesChanges$.pipe(filter(() => this.loaded)).subscribe((v) => {
this.registryReportSettings.textProperties = v;
this.registryReportSettings$.next(this.registryReportSettings);
});
this.toggleLabels$.pipe(filter(() => this.loaded)).subscribe((v) => {
this.registryReportSettings.visibleText = v;
this.registryReportSettings$.next(this.registryReportSettings);
});
如何将它们合并为一个运算符?我无法使用 combineLatest,因为并非所有流都可以启动。
您可以使用 merge,如果任何 Observable 将发出一个值,它将触发
this.reportPropertiesChanges$.pipe(map(reportProperties => ({reportProperties})));
this.objectsProperties$.pipe(map(reportObjectsProperties => ({reportObjectsProperties})));
this.textPropertiesChanges$.pipe(map(textProperties => ({textProperties})))
this.toggleLabels$.pipe(map(visibleText => ({visibleText})));
merge(
this.reportPropertiesChanges$,
this.objectsProperties$,
this.textPropertiesChanges$,
this.toggleLabels$
)
.pipe(
filter(() => this.loaded)
).subscribe((p) => {
this.registryReportSettings = {...this.registryReportSettings, ...p };
this.registryReportSettings$.next(this.registryReportSettings);
});
我会将其重构为:
import { merge } from 'rxjs';
import { map } from 'rxjs/operators';
...
const forwardProp = prop => value => ({prop, value});
const obs = [
this.reportPropertiesChanges$.pipe(map(forwardProp('reportProperties'))),
this.objectsProperties$.pipe(map(forwardProp('reportObjectsProperties'))),
this.textPropertiesChanges$.pipe(map(forwardProp('textProperties'))),
this.toggleLabels$.pipe(map(forwardProp('visibleText')))
];
merge(...obs).pipe(filter(() => this.loaded)).subscribe(({prop, value}) => {
this.registryReportSettings[prop] = value;
this.registryReportSettings$.next(this.registryReportSettings);
});
您也可以使用 rxjs 中的 combineLatest 运算符。它将使您能够访问一个操作员中的所有 4 个可订阅对象。像这样:
const reportProps = this.reportPropertiesChanges$;
const objectsProperties = this. objectsProperties$;
const textPropertiesChanges = this.textPropertiesChanges$;
const toggleLabels = this. toggleLabels$;
combineLatest([
reportProps,
objectsProperties,
textPropertiesChanges,
toggleLabels
]).pipe(filter(() => this.loaded)).subscribe(([
reportPropsValue,
objPropsValue,
textChangesValue,
toggleLabelsValue
]) => {
this.registryReportSettings.reportProperties = reportPropsValue;
this.registryReportSettings.reportObjectsProperties = objPropsValue;
this.registryReportSettings.textProperties = textChangesValue;
this.registryReportSettings.visibleText = toggleLabelsValue;
// Once you set all the values, you can update the Behavior Subject
this.registryReportSettings$.next(this.registryReportSettings);
})
我有以下四个听众:
this.reportPropertiesChanges$.pipe(filter(() => this.loaded)).subscribe((v) => {
this.registryReportSettings.reportProperties = v;
this.registryReportSettings$.next(this.registryReportSettings);
});
this.objectsProperties$.pipe(filter(() => this.loaded)).subscribe((v) => {
this.registryReportSettings.reportObjectsProperties = v;
this.registryReportSettings$.next(this.registryReportSettings);
});
this.textPropertiesChanges$.pipe(filter(() => this.loaded)).subscribe((v) => {
this.registryReportSettings.textProperties = v;
this.registryReportSettings$.next(this.registryReportSettings);
});
this.toggleLabels$.pipe(filter(() => this.loaded)).subscribe((v) => {
this.registryReportSettings.visibleText = v;
this.registryReportSettings$.next(this.registryReportSettings);
});
如何将它们合并为一个运算符?我无法使用 combineLatest,因为并非所有流都可以启动。
您可以使用 merge,如果任何 Observable 将发出一个值,它将触发
this.reportPropertiesChanges$.pipe(map(reportProperties => ({reportProperties})));
this.objectsProperties$.pipe(map(reportObjectsProperties => ({reportObjectsProperties})));
this.textPropertiesChanges$.pipe(map(textProperties => ({textProperties})))
this.toggleLabels$.pipe(map(visibleText => ({visibleText})));
merge(
this.reportPropertiesChanges$,
this.objectsProperties$,
this.textPropertiesChanges$,
this.toggleLabels$
)
.pipe(
filter(() => this.loaded)
).subscribe((p) => {
this.registryReportSettings = {...this.registryReportSettings, ...p };
this.registryReportSettings$.next(this.registryReportSettings);
});
我会将其重构为:
import { merge } from 'rxjs';
import { map } from 'rxjs/operators';
...
const forwardProp = prop => value => ({prop, value});
const obs = [
this.reportPropertiesChanges$.pipe(map(forwardProp('reportProperties'))),
this.objectsProperties$.pipe(map(forwardProp('reportObjectsProperties'))),
this.textPropertiesChanges$.pipe(map(forwardProp('textProperties'))),
this.toggleLabels$.pipe(map(forwardProp('visibleText')))
];
merge(...obs).pipe(filter(() => this.loaded)).subscribe(({prop, value}) => {
this.registryReportSettings[prop] = value;
this.registryReportSettings$.next(this.registryReportSettings);
});
您也可以使用 rxjs 中的 combineLatest 运算符。它将使您能够访问一个操作员中的所有 4 个可订阅对象。像这样:
const reportProps = this.reportPropertiesChanges$;
const objectsProperties = this. objectsProperties$;
const textPropertiesChanges = this.textPropertiesChanges$;
const toggleLabels = this. toggleLabels$;
combineLatest([
reportProps,
objectsProperties,
textPropertiesChanges,
toggleLabels
]).pipe(filter(() => this.loaded)).subscribe(([
reportPropsValue,
objPropsValue,
textChangesValue,
toggleLabelsValue
]) => {
this.registryReportSettings.reportProperties = reportPropsValue;
this.registryReportSettings.reportObjectsProperties = objPropsValue;
this.registryReportSettings.textProperties = textChangesValue;
this.registryReportSettings.visibleText = toggleLabelsValue;
// Once you set all the values, you can update the Behavior Subject
this.registryReportSettings$.next(this.registryReportSettings);
})