import { GlobalState, GlobalStateProvider, KeyDefinition, ActiveUserState, SingleUserState, SingleUserStateProvider, StateProvider, ActiveUserStateProvider, } from "../src/platform/state"; import { UserId } from "../src/types/guid"; import { FakeActiveUserState, FakeGlobalState, FakeSingleUserState } from "./fake-state"; export class FakeGlobalStateProvider implements GlobalStateProvider { states: Map> = new Map(); get(keyDefinition: KeyDefinition): GlobalState { let result = this.states.get(keyDefinition.buildCacheKey("global")) as GlobalState; if (result == null) { result = new FakeGlobalState(); this.states.set(keyDefinition.buildCacheKey("global"), result); } return result; } getFake(keyDefinition: KeyDefinition): FakeGlobalState { return this.get(keyDefinition) as FakeGlobalState; } } export class FakeSingleUserStateProvider implements SingleUserStateProvider { states: Map> = new Map(); get(userId: UserId, keyDefinition: KeyDefinition): SingleUserState { let result = this.states.get(keyDefinition.buildCacheKey("user", userId)) as SingleUserState; if (result == null) { result = new FakeSingleUserState(userId); this.states.set(keyDefinition.buildCacheKey("user", userId), result); } return result; } getFake(userId: UserId, keyDefinition: KeyDefinition): FakeSingleUserState { return this.get(userId, keyDefinition) as FakeSingleUserState; } } export class FakeActiveUserStateProvider implements ActiveUserStateProvider { states: Map> = new Map(); get(keyDefinition: KeyDefinition): ActiveUserState { let result = this.states.get( keyDefinition.buildCacheKey("user", "active"), ) as ActiveUserState; if (result == null) { result = new FakeActiveUserState(); this.states.set(keyDefinition.buildCacheKey("user", "active"), result); } return result; } getFake(keyDefinition: KeyDefinition): FakeActiveUserState { return this.get(keyDefinition) as FakeActiveUserState; } } export class FakeStateProvider implements StateProvider { getActive(keyDefinition: KeyDefinition): ActiveUserState { return this.activeUser.get(keyDefinition); } getGlobal(keyDefinition: KeyDefinition): GlobalState { return this.global.get(keyDefinition); } getUser(userId: UserId, keyDefinition: KeyDefinition): SingleUserState { return this.singleUser.get(userId, keyDefinition); } global: FakeGlobalStateProvider = new FakeGlobalStateProvider(); singleUser: FakeSingleUserStateProvider = new FakeSingleUserStateProvider(); activeUser: FakeActiveUserStateProvider = new FakeActiveUserStateProvider(); }