2024-01-04 20:47:49 +01:00
|
|
|
import { Observable } from "rxjs";
|
|
|
|
|
2023-11-29 15:59:50 +01:00
|
|
|
import {
|
|
|
|
GlobalState,
|
|
|
|
GlobalStateProvider,
|
|
|
|
KeyDefinition,
|
2023-12-05 16:20:16 +01:00
|
|
|
ActiveUserState,
|
|
|
|
SingleUserState,
|
2023-12-18 13:50:04 +01:00
|
|
|
SingleUserStateProvider,
|
|
|
|
StateProvider,
|
|
|
|
ActiveUserStateProvider,
|
2024-01-04 20:47:49 +01:00
|
|
|
DerivedState,
|
|
|
|
DeriveDefinition,
|
|
|
|
DerivedStateProvider,
|
2023-11-29 15:59:50 +01:00
|
|
|
} from "../src/platform/state";
|
2023-12-05 16:20:16 +01:00
|
|
|
import { UserId } from "../src/types/guid";
|
2024-01-04 22:50:55 +01:00
|
|
|
import { DerivedStateDependencies } from "../src/types/state";
|
2023-11-29 15:59:50 +01:00
|
|
|
|
2024-01-04 20:47:49 +01:00
|
|
|
import {
|
|
|
|
FakeActiveUserState,
|
|
|
|
FakeDerivedState,
|
|
|
|
FakeGlobalState,
|
|
|
|
FakeSingleUserState,
|
|
|
|
} from "./fake-state";
|
2023-11-29 15:59:50 +01:00
|
|
|
|
|
|
|
export class FakeGlobalStateProvider implements GlobalStateProvider {
|
2023-12-05 16:20:16 +01:00
|
|
|
states: Map<string, GlobalState<unknown>> = new Map();
|
2023-11-29 15:59:50 +01:00
|
|
|
get<T>(keyDefinition: KeyDefinition<T>): GlobalState<T> {
|
2023-12-05 16:20:16 +01:00
|
|
|
let result = this.states.get(keyDefinition.buildCacheKey("global")) as GlobalState<T>;
|
2023-11-29 15:59:50 +01:00
|
|
|
|
|
|
|
if (result == null) {
|
|
|
|
result = new FakeGlobalState<T>();
|
2023-12-05 16:20:16 +01:00
|
|
|
this.states.set(keyDefinition.buildCacheKey("global"), result);
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFake<T>(keyDefinition: KeyDefinition<T>): FakeGlobalState<T> {
|
2023-12-05 16:20:16 +01:00
|
|
|
return this.get(keyDefinition) as FakeGlobalState<T>;
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 13:50:04 +01:00
|
|
|
export class FakeSingleUserStateProvider implements SingleUserStateProvider {
|
2023-12-05 16:20:16 +01:00
|
|
|
states: Map<string, SingleUserState<unknown>> = new Map();
|
|
|
|
get<T>(userId: UserId, keyDefinition: KeyDefinition<T>): SingleUserState<T> {
|
|
|
|
let result = this.states.get(keyDefinition.buildCacheKey("user", userId)) as SingleUserState<T>;
|
2023-11-29 15:59:50 +01:00
|
|
|
|
|
|
|
if (result == null) {
|
2023-12-05 16:20:16 +01:00
|
|
|
result = new FakeSingleUserState<T>(userId);
|
|
|
|
this.states.set(keyDefinition.buildCacheKey("user", userId), result);
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-05 16:20:16 +01:00
|
|
|
getFake<T>(userId: UserId, keyDefinition: KeyDefinition<T>): FakeSingleUserState<T> {
|
|
|
|
return this.get(userId, keyDefinition) as FakeSingleUserState<T>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 13:50:04 +01:00
|
|
|
export class FakeActiveUserStateProvider implements ActiveUserStateProvider {
|
2023-12-05 16:20:16 +01:00
|
|
|
states: Map<string, ActiveUserState<unknown>> = new Map();
|
|
|
|
get<T>(keyDefinition: KeyDefinition<T>): ActiveUserState<T> {
|
|
|
|
let result = this.states.get(
|
|
|
|
keyDefinition.buildCacheKey("user", "active"),
|
|
|
|
) as ActiveUserState<T>;
|
|
|
|
|
|
|
|
if (result == null) {
|
|
|
|
result = new FakeActiveUserState<T>();
|
|
|
|
this.states.set(keyDefinition.buildCacheKey("user", "active"), result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFake<T>(keyDefinition: KeyDefinition<T>): FakeActiveUserState<T> {
|
|
|
|
return this.get(keyDefinition) as FakeActiveUserState<T>;
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-18 13:50:04 +01:00
|
|
|
|
|
|
|
export class FakeStateProvider implements StateProvider {
|
|
|
|
getActive<T>(keyDefinition: KeyDefinition<T>): ActiveUserState<T> {
|
|
|
|
return this.activeUser.get(keyDefinition);
|
|
|
|
}
|
|
|
|
|
|
|
|
getGlobal<T>(keyDefinition: KeyDefinition<T>): GlobalState<T> {
|
|
|
|
return this.global.get(keyDefinition);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUser<T>(userId: UserId, keyDefinition: KeyDefinition<T>): SingleUserState<T> {
|
|
|
|
return this.singleUser.get(userId, keyDefinition);
|
|
|
|
}
|
|
|
|
|
2024-01-04 20:47:49 +01:00
|
|
|
getDerived<TFrom, TTo, TDeps extends DerivedStateDependencies>(
|
|
|
|
parentState$: Observable<TFrom>,
|
|
|
|
deriveDefinition: DeriveDefinition<unknown, TTo, TDeps>,
|
2024-01-04 22:50:55 +01:00
|
|
|
dependencies: TDeps,
|
2024-01-04 20:47:49 +01:00
|
|
|
): DerivedState<TTo> {
|
|
|
|
return this.derived.get(parentState$, deriveDefinition, dependencies);
|
|
|
|
}
|
|
|
|
|
2023-12-18 13:50:04 +01:00
|
|
|
global: FakeGlobalStateProvider = new FakeGlobalStateProvider();
|
|
|
|
singleUser: FakeSingleUserStateProvider = new FakeSingleUserStateProvider();
|
|
|
|
activeUser: FakeActiveUserStateProvider = new FakeActiveUserStateProvider();
|
2024-01-04 20:47:49 +01:00
|
|
|
derived: FakeDerivedStateProvider = new FakeDerivedStateProvider();
|
|
|
|
}
|
|
|
|
|
|
|
|
export class FakeDerivedStateProvider implements DerivedStateProvider {
|
|
|
|
states: Map<string, DerivedState<unknown>> = new Map();
|
|
|
|
get<TFrom, TTo, TDeps extends DerivedStateDependencies>(
|
|
|
|
parentState$: Observable<TFrom>,
|
|
|
|
deriveDefinition: DeriveDefinition<TFrom, TTo, TDeps>,
|
2024-01-04 22:50:55 +01:00
|
|
|
dependencies: TDeps,
|
2024-01-04 20:47:49 +01:00
|
|
|
): DerivedState<TTo> {
|
|
|
|
let result = this.states.get(deriveDefinition.buildCacheKey()) as DerivedState<TTo>;
|
|
|
|
|
|
|
|
if (result == null) {
|
|
|
|
result = new FakeDerivedState<TTo>();
|
|
|
|
this.states.set(deriveDefinition.buildCacheKey(), result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2023-12-18 13:50:04 +01:00
|
|
|
}
|