Skip to content

Commit

Permalink
[PM-14416] Improve null value filtering for task service
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-melton committed Dec 31, 2024
1 parent 1545766 commit 4c4cceb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions libs/vault/src/tasks/services/default-task.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from "@angular/core";
import { filter, map, switchMap } from "rxjs";
import { map, switchMap } from "rxjs";

import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
Expand All @@ -8,7 +8,7 @@ import { StateProvider } from "@bitwarden/common/platform/state";
import { SecurityTaskId, UserId } from "@bitwarden/common/types/guid";
import { SecurityTask, SecurityTaskStatus, TaskService } from "@bitwarden/vault";

import { perUserCache$ } from "../../utils/observable-utilities";
import { filterOutNullish, perUserCache$ } from "../../utils/observable-utilities";
import { SecurityTaskData } from "../models/security-task.data";
import { SecurityTaskResponse } from "../models/security-task.response";
import { SECURITY_TASKS } from "../state/security-task.state";
Expand All @@ -35,7 +35,7 @@ export class DefaultTaskService implements TaskService {
}
return tasks;
}),
filter((tasks) => tasks != null),
filterOutNullish(),
map((tasks) => tasks.map((t) => new SecurityTask(t))),
);
});
Expand Down
17 changes: 16 additions & 1 deletion libs/vault/src/utils/observable-utilities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable, shareReplay } from "rxjs";
import { filter, Observable, OperatorFunction, shareReplay } from "rxjs";

import { UserId } from "@bitwarden/common/types/guid";

Expand All @@ -20,3 +20,18 @@ export function perUserCache$<TValue>(
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);
}

0 comments on commit 4c4cceb

Please sign in to comment.