From f0ae318f57e0c9cb5f4ed3d4d70fbbedc38bb7e7 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Tue, 13 Feb 2024 15:26:56 -0500 Subject: [PATCH] Link derived state fake to parent observable. (#7922) --- libs/common/spec/fake-state-provider.ts | 2 +- libs/common/spec/fake-state.ts | 32 +++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/libs/common/spec/fake-state-provider.ts b/libs/common/spec/fake-state-provider.ts index 406cc1719b..dabfbb34e0 100644 --- a/libs/common/spec/fake-state-provider.ts +++ b/libs/common/spec/fake-state-provider.ts @@ -198,7 +198,7 @@ export class FakeDerivedStateProvider implements DerivedStateProvider { let result = this.states.get(deriveDefinition.buildCacheKey()) as DerivedState; if (result == null) { - result = new FakeDerivedState(); + result = new FakeDerivedState(parentState$, deriveDefinition, dependencies); this.states.set(deriveDefinition.buildCacheKey(), result); } return result; diff --git a/libs/common/spec/fake-state.ts b/libs/common/spec/fake-state.ts index bd033424e0..96f3d41dd1 100644 --- a/libs/common/spec/fake-state.ts +++ b/libs/common/spec/fake-state.ts @@ -1,4 +1,4 @@ -import { Observable, ReplaySubject, firstValueFrom, map, timeout } from "rxjs"; +import { Observable, ReplaySubject, concatMap, firstValueFrom, map, timeout } from "rxjs"; import { DerivedState, @@ -6,12 +6,14 @@ import { SingleUserState, ActiveUserState, KeyDefinition, + DeriveDefinition, } from "../src/platform/state"; // eslint-disable-next-line import/no-restricted-paths -- using unexposed options for clean typing in test class import { StateUpdateOptions } from "../src/platform/state/state-update-options"; // eslint-disable-next-line import/no-restricted-paths -- using unexposed options for clean typing in test class import { CombinedState, activeMarker } from "../src/platform/state/user-state"; import { UserId } from "../src/types/guid"; +import { DerivedStateDependencies } from "../src/types/state"; import { FakeAccountService } from "./fake-account-service"; @@ -204,11 +206,33 @@ export class FakeActiveUserState implements ActiveUserState { } } -export class FakeDerivedState implements DerivedState { +export class FakeDerivedState + implements DerivedState +{ // eslint-disable-next-line rxjs/no-exposed-subjects -- exposed for testing setup - stateSubject = new ReplaySubject(1); + stateSubject = new ReplaySubject(1); - forceValue(value: T): Promise { + constructor( + parentState$: Observable, + deriveDefinition: DeriveDefinition, + dependencies: TDeps, + ) { + parentState$ + .pipe( + concatMap(async (v) => { + const newState = deriveDefinition.derive(v, dependencies); + if (newState instanceof Promise) { + return newState; + } + return Promise.resolve(newState); + }), + ) + .subscribe((newState) => { + this.stateSubject.next(newState); + }); + } + + forceValue(value: TTo): Promise { this.stateSubject.next(value); return Promise.resolve(value); }