mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 16:23:44 +00:00
[PM-17563] [PM-19754] Migrate Security Task Module to libs/common (#14036)
* [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
This commit is contained in:
37
libs/common/src/vault/utils/observable-utilities.ts
Normal file
37
libs/common/src/vault/utils/observable-utilities.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user