Skip to content

Commit

Permalink
feat(effects): add lazy concatLatestFrom operator for TS 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
david committed Nov 25, 2020
1 parent aa9bf1a commit cc60fe4
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 0 deletions.
71 changes: 71 additions & 0 deletions modules/effects/spec/concat_latest_from_operator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { of } from 'rxjs';
import { skipWhile } from 'rxjs/operators';
import { hot } from 'jasmine-marbles';
import { concatLatestFrom } from '../src/concat_latest_from_operator';

describe('concatLatestFrom', () => {
describe('no triggering value appears in source', () => {
it('should not evaluate the array', () => {
let evaluated = false;
const toBeLazilyEvaluated = () => {
evaluated = true;
return of(4);
};

const numbers$ = hot('-a-b-', { a: 1, b: 2 }).pipe(
skipWhile((num) => num < 3),
concatLatestFrom(() => [toBeLazilyEvaluated()])
);

expect(numbers$).toBeObservable(hot('----'));
expect(evaluated).toBe(false);
});
it('should not evaluate the observable', () => {
let evaluated = false;
const toBeLazilyEvaluated = () => {
evaluated = true;
return of(4);
};

const numbers$ = hot('-a-b-', { a: 1, b: 2 }).pipe(
skipWhile((num) => num < 3),
concatLatestFrom(() => toBeLazilyEvaluated())
);

expect(numbers$).toBeObservable(hot('----'));
expect(evaluated).toBe(false);
});
});
describe('a triggering value appears in source', () => {
it('should evaluate the array of observables', () => {
let evaluated = false;
const toBeLazilyEvaluated = () => {
evaluated = true;
return of(4);
};

const numbers$ = hot('-a-b-c-', { a: 1, b: 2, c: 3 }).pipe(
skipWhile((num) => num < 3),
concatLatestFrom(() => [toBeLazilyEvaluated()])
);

expect(numbers$).toBeObservable(hot('-----d', { d: [3, 4] }));
expect(evaluated).toBe(true);
});
it('should evaluate the observable', () => {
let evaluated = false;
const toBeLazilyEvaluated = () => {
evaluated = true;
return of(4);
};

const numbers$ = hot('-a-b-c-', { a: 1, b: 2, c: 3 }).pipe(
skipWhile((num) => num < 3),
concatLatestFrom(() => toBeLazilyEvaluated())
);

expect(numbers$).toBeObservable(hot('-----d', { d: [3, 4] }));
expect(evaluated).toBe(true);
});
});
});
186 changes: 186 additions & 0 deletions modules/effects/src/concat_latest_from_operator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import { Observable, of, OperatorFunction } from 'rxjs';
import { concatMap, withLatestFrom } from 'rxjs/operators';

type TypeOfObservable<T> = T extends Observable<infer U> ? U : never;

export function concatLatestFrom<T extends [Observable<unknown>], V>(
observableFactory: (value: V) => T
): OperatorFunction<V, [V, TypeOfObservable<T[0]>]>;
export function concatLatestFrom<
T extends [Observable<unknown>, Observable<unknown>],
V
>(
observableFactory: (value: V) => T
): OperatorFunction<V, [V, TypeOfObservable<T[0]>, TypeOfObservable<T[1]>]>;
export function concatLatestFrom<
T extends [Observable<unknown>, Observable<unknown>, Observable<unknown>],
V
>(
observableFactory: (value: V) => T
): OperatorFunction<
V,
[V, TypeOfObservable<T[0]>, TypeOfObservable<T[1]>, TypeOfObservable<T[2]>]
>;
export function concatLatestFrom<
T extends [
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>
],
V
>(
observableFactory: (value: V) => T
): OperatorFunction<
V,
[
V,
TypeOfObservable<T[0]>,
TypeOfObservable<T[1]>,
TypeOfObservable<T[2]>,
TypeOfObservable<T[3]>
]
>;
export function concatLatestFrom<
T extends [
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>
],
V
>(
observableFactory: (value: V) => T
): OperatorFunction<
V,
[
V,
TypeOfObservable<T[0]>,
TypeOfObservable<T[1]>,
TypeOfObservable<T[2]>,
TypeOfObservable<T[3]>,
TypeOfObservable<T[4]>
]
>;
export function concatLatestFrom<
T extends [
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>
],
V
>(
observableFactory: (value: V) => T
): OperatorFunction<
V,
[
V,
TypeOfObservable<T[0]>,
TypeOfObservable<T[1]>,
TypeOfObservable<T[2]>,
TypeOfObservable<T[3]>,
TypeOfObservable<T[4]>,
TypeOfObservable<T[5]>
]
>;
export function concatLatestFrom<
T extends [
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>
],
V
>(
observableFactory: (value: V) => T
): OperatorFunction<
V,
[
V,
TypeOfObservable<T[0]>,
TypeOfObservable<T[1]>,
TypeOfObservable<T[2]>,
TypeOfObservable<T[3]>,
TypeOfObservable<T[4]>,
TypeOfObservable<T[5]>,
TypeOfObservable<T[6]>
]
>;
export function concatLatestFrom<
T extends [
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>,
Observable<unknown>
],
V
>(
observableFactory: (value: V) => T
): OperatorFunction<
V,
[
V,
TypeOfObservable<T[0]>,
TypeOfObservable<T[1]>,
TypeOfObservable<T[2]>,
TypeOfObservable<T[3]>,
TypeOfObservable<T[4]>,
TypeOfObservable<T[5]>,
TypeOfObservable<T[6]>,
TypeOfObservable<T[7]>
]
>;
export function concatLatestFrom<T extends Observable<unknown>[], V>(
observableFactory: (value: V) => T
): OperatorFunction<V, [V, ...any[]]>;
export function concatLatestFrom<T extends Observable<unknown>, V>(
observableFactory: (value: V) => T
): OperatorFunction<V, [V, TypeOfObservable<T>]>;

/**
* @description
* RxJS operator that lazily evaluates an Observable or list of Observables.
*
* Use to mitigate performance impact of `store.select`
*
* ## Example
* ```ts
* effectName$ = createEffect(() =>
* this.actions$.pipe(
* ofType(FeatureActions.actionOne),
* // The call to this.store.select will not be performed until actionOne is received
* concatLatestFrom(() => this.store.select(getDataState)),
* filter(([action, data]) => data.enabled),
* map(() => FeatureActions.actionTwo())
* )
* );
* ```
* @param observablesFactory A factory function which returns an `Observable` or `Observable[]`.
* @returns Returns an `OperatorFunction` that returns an Observable which combines the source and latest value from input Observables
*/
export function concatLatestFrom<V>(
observablesFactory:
| ((value: V) => Observable<any>)
| ((value: V) => Observable<any>[])
): OperatorFunction<V, [V, ...any[]]> {
return concatMap((value: V) => {
const observables = observablesFactory(value);
const observablesAsArray = Array.isArray(observables)
? observables
: [observables];
return of(value).pipe(withLatestFrom(...observablesAsArray)) as Observable<
[V, ...unknown[]]
>;
}) as OperatorFunction<V, [V, ...any[]]>;
}

0 comments on commit cc60fe4

Please sign in to comment.