1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-10 13:40:06 +00:00

Allow for on-config updated pushes to services

This is an inversion of control for low-level services that cannot depend on ConfigService. Instead, they can register with a subscription to be notified when the server config updates and set their own feature flag states from that.
This commit is contained in:
Matt Gibson
2025-03-05 16:38:58 -08:00
parent 5ade8f69bc
commit ebb5584d43
2 changed files with 22 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { Observable } from "rxjs";
import { Observable, Subscription } from "rxjs";
import { SemVer } from "semver";
import { FeatureFlag, FeatureFlagValueType } from "../../../enums/feature-flag.enum";
@@ -10,6 +10,8 @@ import { Region } from "../environment.service";
import { ServerConfig } from "./server-config";
export type ConfigCallback = (serverConfig: ServerConfig) => void;
export abstract class ConfigService {
/** The server config of the currently active user */
serverConfig$: Observable<ServerConfig | null>;
@@ -54,4 +56,10 @@ export abstract class ConfigService {
* Triggers a check that the config for the currently active user is up-to-date. If it is not, it will be fetched from the server and stored.
*/
abstract ensureConfigFetched(): Promise<void>;
abstract broadcastConfigChangesTo(...listeners: OnServerConfigChange[]): Subscription;
}
export interface OnServerConfigChange {
onServerConfigChange(newConfig: ServerConfig): void;
}

View File

@@ -10,6 +10,7 @@ import {
of,
shareReplay,
Subject,
Subscription,
switchMap,
tap,
} from "rxjs";
@@ -20,7 +21,11 @@ import { AuthenticationStatus } from "../../../auth/enums/authentication-status"
import { FeatureFlag, getFeatureFlagValue } from "../../../enums/feature-flag.enum";
import { UserId } from "../../../types/guid";
import { ConfigApiServiceAbstraction } from "../../abstractions/config/config-api.service.abstraction";
import { ConfigService } from "../../abstractions/config/config.service";
import {
ConfigCallback,
ConfigService,
OnServerConfigChange,
} from "../../abstractions/config/config.service";
import { ServerConfig } from "../../abstractions/config/server-config";
import { Environment, EnvironmentService, Region } from "../../abstractions/environment.service";
import { LogService } from "../../abstractions/log.service";
@@ -53,6 +58,7 @@ export const GLOBAL_SERVER_CONFIGURATIONS = KeyDefinition.record<ServerConfig, A
// FIXME: currently we are limited to api requests for active users. Update to accept a UserId and APIUrl once ApiService supports it.
export class DefaultConfigService implements ConfigService {
private failedFetchFallbackSubject = new Subject<ServerConfig>();
private callbacks: ConfigCallback[] = [];
serverConfig$: Observable<ServerConfig>;
@@ -149,6 +155,12 @@ export class DefaultConfigService implements ConfigService {
await firstValueFrom(this.serverConfig$);
}
broadcastConfigChangesTo(...listeners: OnServerConfigChange[]): Subscription {
return this.serverConfig$.subscribe((config) =>
listeners.forEach((listener) => listener.onServerConfigChange(config)),
);
}
private olderThanRetrievalInterval(date: Date) {
return new Date().getTime() - date.getTime() > RETRIEVAL_INTERVAL;
}