mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 22:03:36 +00:00
* [PM-17563] Remove references to Angular from TaskService * [PM-17563] Move Task module to libs/common/vault to avoid Angular dependency * [PM-17563] Fix bad imports * [PM-17563] Fix a few more missed imports
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { filter, Observable, OperatorFunction, shareReplay } from "rxjs";
|
|
|
|
import { UserId } from "@bitwarden/common/types/guid";
|
|
|
|
/**
|
|
* Builds an observable once per userId and caches it for future requests.
|
|
* The built observables are shared among subscribers with a replay buffer size of 1.
|
|
* @param create - A function that creates an observable for a given userId.
|
|
*/
|
|
export function perUserCache$<TValue>(
|
|
create: (userId: UserId) => Observable<TValue>,
|
|
): (userId: UserId) => Observable<TValue> {
|
|
const cache = new Map<UserId, Observable<TValue>>();
|
|
return (userId: UserId) => {
|
|
let observable = cache.get(userId);
|
|
if (!observable) {
|
|
observable = create(userId).pipe(shareReplay({ bufferSize: 1, refCount: false }));
|
|
cache.set(userId, observable);
|
|
}
|
|
return observable;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Strongly typed observable operator that filters out null/undefined values and adjusts the return type to
|
|
* be non-nullable.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* const source$ = of(1, null, 2, undefined, 3);
|
|
* source$.pipe(filterOutNullish()).subscribe(console.log);
|
|
* // Output: 1, 2, 3
|
|
* ```
|
|
*/
|
|
export function filterOutNullish<T>(): OperatorFunction<T | undefined | null, T> {
|
|
return filter((v): v is T => v != null);
|
|
}
|