Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7406ec6

Browse files
committed
refactor(state): wip selections add spreads to smosh
1 parent 910331e commit 7406ec6

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

libs/state/selections/docs/Readme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,25 @@ export class ProblemComponent {
177177
// ✔ GOOD: Derive view model from model 👇
178178
viewModel$ = smosh({ title: this.state.select('title')}, this.sortedSlice$);
179179

180+
// target API
181+
viewModel$ = smosh({
182+
prop1: 'prop1', // string
183+
prop2: prop1$ // Observable<string>
184+
},
185+
slice1$, // Observable<{prop3: 3}>
186+
slice2$ // Observable<{prop4: 'four'}>,
187+
// durationSelector$ (optional)
188+
);
189+
190+
191+
// ✔ GOOD: Derive view model from model 👇
192+
viewModel$ = smosh({
193+
title: this.state.select('title'),
194+
...this.sortedSlice$
195+
});
196+
197+
198+
180199
constructor(private globalState: GlobalState, private state: RxState<Model>) {
181200
// ...
182201

libs/state/selections/spec/smosh.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ describe('createSmoshObservable', () => {
351351
const prop3$ = cold('--f--', { f: { prop3: f } });
352352
const vm$: Observable<ViewModelTest> = smosh(
353353
undefined,
354-
[prop1$, prop2$, prop3$],
354+
[prop1$, /*prop2$, prop3$*/],
355355
{ durationSelector: cold('s') }
356356
);
357357
const psubs = '^----';

libs/state/selections/src/lib/smosh.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { distinctUntilChanged, filter, map, shareReplay } from 'rxjs/operators';
44
import { Promise } from '@rx-angular/cdk/zone-less/browser';
55
import { coalesceWith } from '@rx-angular/cdk/coalescing';
66
import { ExtractObservableValue } from '../../../../cdk/internals/core/src/lib/model';
7-
import { NonUndefined, NotEmpty, ObservableMap } from './interfaces';
7+
import { NotEmpty, ObservableMap } from './interfaces';
88
import { coerceObservable } from '@rx-angular/cdk/coercing';
99

1010
const resolvedPromise = Promise.resolve();
@@ -72,9 +72,7 @@ export function smosh<T extends ObservableMap | (Partial<T> & NotEmpty<T>), U ex
7272
obj[keys[i]] = values[i];
7373
}
7474
return obj;
75-
}),
76-
// by using shareReplay we share the last composition work done to create the accumulated object
77-
shareReplay(1)
75+
})
7876
)
7977
spreads = spreads.map(o => o.pipe(
8078
// we avoid using the nullish operator later ;)
@@ -83,8 +81,17 @@ export function smosh<T extends ObservableMap | (Partial<T> & NotEmpty<T>), U ex
8381
distinctUntilChanged()
8482
)
8583
);
86-
return merge(...spreads, obj$).pipe(scan((acc, slice) => ({...acc, ...slice}), {})).pipe(
84+
85+
return merge(...spreads, obj$).pipe(scan((acc, slice) => {
86+
const ks = Object.keys(slice) as (keyof T)[];
87+
for (let i = 0; i < ks.length; i++) {
88+
acc[ks[i] as any] = slice[ks[i]];
89+
}
90+
return acc as any;
91+
}, {})).pipe(
8792
// As combineLatest will emit multiple times for a change in multiple properties we coalesce those emissions together
88-
coalesceWith(durationSelector)
93+
coalesceWith(durationSelector),
94+
// by using shareReplay we share the last composition work done to create the accumulated object
95+
shareReplay(1)
8996
);
9097
}

0 commit comments

Comments
 (0)