import { BehaviorSubject, Observable } from "rxjs"; import { GlobalState, StateUpdateOptions, GlobalStateProvider, KeyDefinition, } from "@bitwarden/state"; export class StorybookGlobalState implements GlobalState { private _state$ = new BehaviorSubject(null); constructor(initialValue?: T | null) { this._state$.next(initialValue ?? null); } async update( configureState: (state: T | null, dependency: TCombine) => T | null, options?: Partial>, ): Promise { const currentState = this._state$.value; const newState = configureState(currentState, null as TCombine); this._state$.next(newState); return newState; } get state$(): Observable { return this._state$.asObservable(); } setValue(value: T | null): void { this._state$.next(value); } } export class StorybookGlobalStateProvider implements GlobalStateProvider { private states = new Map>(); get(keyDefinition: KeyDefinition): GlobalState { const key = `${keyDefinition.fullName}_${keyDefinition.stateDefinition.defaultStorageLocation}`; if (!this.states.has(key)) { this.states.set(key, new StorybookGlobalState()); } return this.states.get(key)!; } }