2023-11-29 15:59:50 +01:00
|
|
|
import {
|
|
|
|
GlobalState,
|
|
|
|
GlobalStateProvider,
|
|
|
|
KeyDefinition,
|
2023-12-05 16:20:16 +01:00
|
|
|
ActiveUserState,
|
|
|
|
SingleUserState,
|
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";
|
2023-11-29 15:59:50 +01:00
|
|
|
|
2023-12-05 16:20:16 +01:00
|
|
|
import { FakeActiveUserState, 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-05 16:20:16 +01:00
|
|
|
export class FakeSingleUserStateProvider {
|
|
|
|
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>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class FakeActiveUserStateProvider {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|