From e1ce89e38d3520ad11960074f74c381429c0251a Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Sat, 6 Nov 2021 02:02:49 +0200 Subject: [PATCH] add shareLast operator --- src/lib/utils/operators.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/utils/operators.ts b/src/lib/utils/operators.ts index a8de46e..16292d1 100644 --- a/src/lib/utils/operators.ts +++ b/src/lib/utils/operators.ts @@ -1,5 +1,5 @@ -import { distinctUntilChanged, map } from 'rxjs/operators'; -import { OperatorFunction, pipe } from 'rxjs'; +import { distinctUntilChanged, map, shareReplay } from 'rxjs/operators'; +import { MonoTypeOperatorFunction, OperatorFunction, pipe } from 'rxjs'; export function get(predicate: (value: T, index: number) => boolean): OperatorFunction { return map(entities => entities.find(predicate)); @@ -17,5 +17,9 @@ export function filterEach(predicate: (value: T, index: number) => boolean): return map(entities => entities.filter(predicate)); } +export function shareLast(values = 1): MonoTypeOperatorFunction { + return shareReplay({ bufferSize: values, refCount: true }); +} + export const toLengthValue = (entities: unknown[]): number => entities?.length ?? 0; export const getLength = pipe(map(toLengthValue), distinctUntilChanged());