2024-01-10 16:36:19 +01:00
|
|
|
import { mock } from "jest-mock-extended";
|
2024-03-15 14:05:04 +01:00
|
|
|
import { Observable, map, of, switchMap, take } from "rxjs";
|
2024-01-04 20:47:49 +01:00
|
|
|
|
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,
|
2024-02-26 17:28:40 +01:00
|
|
|
UserKeyDefinition,
|
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-10 16:36:19 +01:00
|
|
|
import { FakeAccountService } from "./fake-account-service";
|
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 {
|
2024-01-10 16:36:19 +01:00
|
|
|
mock = mock<GlobalStateProvider>();
|
|
|
|
establishedMocks: Map<string, FakeGlobalState<unknown>> = new Map();
|
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> {
|
2024-01-10 16:36:19 +01:00
|
|
|
this.mock.get(keyDefinition);
|
2024-08-27 02:44:08 +02:00
|
|
|
const cacheKey = this.cacheKey(keyDefinition);
|
2024-03-05 18:30:40 +01:00
|
|
|
let result = this.states.get(cacheKey);
|
2023-11-29 15:59:50 +01:00
|
|
|
|
|
|
|
if (result == null) {
|
2024-01-10 16:36:19 +01:00
|
|
|
let fake: FakeGlobalState<T>;
|
|
|
|
// Look for established mock
|
|
|
|
if (this.establishedMocks.has(keyDefinition.key)) {
|
|
|
|
fake = this.establishedMocks.get(keyDefinition.key) as FakeGlobalState<T>;
|
|
|
|
} else {
|
|
|
|
fake = new FakeGlobalState<T>();
|
|
|
|
}
|
|
|
|
fake.keyDefinition = keyDefinition;
|
|
|
|
result = fake;
|
2024-03-05 18:30:40 +01:00
|
|
|
this.states.set(cacheKey, result);
|
2024-01-10 16:36:19 +01:00
|
|
|
|
2023-11-29 15:59:50 +01:00
|
|
|
result = new FakeGlobalState<T>();
|
2024-03-05 18:30:40 +01:00
|
|
|
this.states.set(cacheKey, result);
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
return result as GlobalState<T>;
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
private cacheKey(keyDefinition: KeyDefinition<unknown>) {
|
|
|
|
return `${keyDefinition.fullName}_${keyDefinition.stateDefinition.defaultStorageLocation}`;
|
|
|
|
}
|
|
|
|
|
2023-11-29 15:59:50 +01:00
|
|
|
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
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
mockFor<T>(keyDefinition: KeyDefinition<T>, initialValue?: T): FakeGlobalState<T> {
|
|
|
|
const cacheKey = this.cacheKey(keyDefinition);
|
|
|
|
if (!this.states.has(cacheKey)) {
|
|
|
|
this.states.set(cacheKey, new FakeGlobalState<T>(initialValue));
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
2024-08-27 02:44:08 +02:00
|
|
|
return this.states.get(cacheKey) as FakeGlobalState<T>;
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
|
2023-12-18 13:50:04 +01:00
|
|
|
export class FakeSingleUserStateProvider implements SingleUserStateProvider {
|
2024-01-10 16:36:19 +01:00
|
|
|
mock = mock<SingleUserStateProvider>();
|
2023-12-05 16:20:16 +01:00
|
|
|
states: Map<string, SingleUserState<unknown>> = new Map();
|
2024-08-27 02:44:08 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
readonly updateSyncCallback?: (
|
|
|
|
key: UserKeyDefinition<unknown>,
|
|
|
|
userId: UserId,
|
|
|
|
newValue: unknown,
|
|
|
|
) => Promise<void>,
|
|
|
|
) {}
|
|
|
|
|
2024-06-07 09:33:26 +02:00
|
|
|
get<T>(userId: UserId, userKeyDefinition: UserKeyDefinition<T>): SingleUserState<T> {
|
|
|
|
this.mock.get(userId, userKeyDefinition);
|
2024-08-27 02:44:08 +02:00
|
|
|
const cacheKey = this.cacheKey(userId, userKeyDefinition);
|
2024-03-05 18:30:40 +01:00
|
|
|
let result = this.states.get(cacheKey);
|
2023-11-29 15:59:50 +01:00
|
|
|
|
|
|
|
if (result == null) {
|
2024-08-27 02:44:08 +02:00
|
|
|
result = this.buildFakeState(userId, userKeyDefinition);
|
2024-03-05 18:30:40 +01:00
|
|
|
this.states.set(cacheKey, result);
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
return result as SingleUserState<T>;
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
getFake<T>(
|
|
|
|
userId: UserId,
|
|
|
|
userKeyDefinition: UserKeyDefinition<T>,
|
|
|
|
{ allowInit }: { allowInit: boolean } = { allowInit: true },
|
|
|
|
): FakeSingleUserState<T> {
|
|
|
|
if (!allowInit && this.states.get(this.cacheKey(userId, userKeyDefinition)) == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-06-07 09:33:26 +02:00
|
|
|
return this.get(userId, userKeyDefinition) as FakeSingleUserState<T>;
|
2023-12-05 16:20:16 +01:00
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
mockFor<T>(
|
|
|
|
userId: UserId,
|
|
|
|
userKeyDefinition: UserKeyDefinition<T>,
|
|
|
|
initialValue?: T,
|
|
|
|
): FakeSingleUserState<T> {
|
|
|
|
const cacheKey = this.cacheKey(userId, userKeyDefinition);
|
|
|
|
if (!this.states.has(cacheKey)) {
|
|
|
|
this.states.set(cacheKey, this.buildFakeState(userId, userKeyDefinition, initialValue));
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
2024-08-27 02:44:08 +02:00
|
|
|
return this.states.get(cacheKey) as FakeSingleUserState<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildFakeState<T>(
|
|
|
|
userId: UserId,
|
|
|
|
userKeyDefinition: UserKeyDefinition<T>,
|
|
|
|
initialValue?: T,
|
|
|
|
) {
|
|
|
|
const state = new FakeSingleUserState(userId, initialValue, async (...args) => {
|
|
|
|
await this.updateSyncCallback?.(userKeyDefinition, ...args);
|
|
|
|
});
|
|
|
|
state.keyDefinition = userKeyDefinition;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
private cacheKey(userId: UserId, userKeyDefinition: UserKeyDefinition<unknown>) {
|
|
|
|
return `${userKeyDefinitionCacheKey(userKeyDefinition)}_${userId}`;
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
2023-12-05 16:20:16 +01:00
|
|
|
}
|
|
|
|
|
2023-12-18 13:50:04 +01:00
|
|
|
export class FakeActiveUserStateProvider implements ActiveUserStateProvider {
|
2024-02-06 17:35:22 +01:00
|
|
|
activeUserId$: Observable<UserId>;
|
2024-01-10 16:36:19 +01:00
|
|
|
states: Map<string, FakeActiveUserState<unknown>> = new Map();
|
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
constructor(
|
|
|
|
public accountService: FakeAccountService,
|
|
|
|
readonly updateSyncCallback?: (
|
|
|
|
key: UserKeyDefinition<unknown>,
|
|
|
|
userId: UserId,
|
|
|
|
newValue: unknown,
|
|
|
|
) => Promise<void>,
|
|
|
|
) {
|
2024-03-15 01:59:30 +01:00
|
|
|
this.activeUserId$ = accountService.activeAccountSubject.asObservable().pipe(map((a) => a?.id));
|
2024-02-06 17:35:22 +01:00
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
|
2024-06-07 09:33:26 +02:00
|
|
|
get<T>(userKeyDefinition: UserKeyDefinition<T>): ActiveUserState<T> {
|
2024-08-27 02:44:08 +02:00
|
|
|
const cacheKey = userKeyDefinitionCacheKey(userKeyDefinition);
|
2024-03-05 18:30:40 +01:00
|
|
|
let result = this.states.get(cacheKey);
|
2023-12-05 16:20:16 +01:00
|
|
|
|
|
|
|
if (result == null) {
|
2024-08-27 02:44:08 +02:00
|
|
|
result = this.buildFakeState(userKeyDefinition);
|
2024-03-05 18:30:40 +01:00
|
|
|
this.states.set(cacheKey, result);
|
2023-12-05 16:20:16 +01:00
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
return result as ActiveUserState<T>;
|
2023-12-05 16:20:16 +01:00
|
|
|
}
|
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
getFake<T>(
|
|
|
|
userKeyDefinition: UserKeyDefinition<T>,
|
|
|
|
{ allowInit }: { allowInit: boolean } = { allowInit: true },
|
|
|
|
): FakeActiveUserState<T> {
|
|
|
|
if (!allowInit && this.states.get(userKeyDefinitionCacheKey(userKeyDefinition)) == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-06-07 09:33:26 +02:00
|
|
|
return this.get(userKeyDefinition) as FakeActiveUserState<T>;
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
2024-01-10 16:36:19 +01:00
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
mockFor<T>(userKeyDefinition: UserKeyDefinition<T>, initialValue?: T): FakeActiveUserState<T> {
|
|
|
|
const cacheKey = userKeyDefinitionCacheKey(userKeyDefinition);
|
|
|
|
if (!this.states.has(cacheKey)) {
|
|
|
|
this.states.set(cacheKey, this.buildFakeState(userKeyDefinition, initialValue));
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
2024-08-27 02:44:08 +02:00
|
|
|
return this.states.get(cacheKey) as FakeActiveUserState<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildFakeState<T>(userKeyDefinition: UserKeyDefinition<T>, initialValue?: T) {
|
|
|
|
const state = new FakeActiveUserState<T>(this.accountService, initialValue, async (...args) => {
|
|
|
|
await this.updateSyncCallback?.(userKeyDefinition, ...args);
|
|
|
|
});
|
|
|
|
state.keyDefinition = userKeyDefinition;
|
|
|
|
return state;
|
2024-01-10 16:36:19 +01:00
|
|
|
}
|
2023-11-29 15:59:50 +01:00
|
|
|
}
|
2023-12-18 13:50:04 +01:00
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
function userKeyDefinitionCacheKey(userKeyDefinition: UserKeyDefinition<unknown>) {
|
|
|
|
return `${userKeyDefinition.fullName}_${userKeyDefinition.stateDefinition.defaultStorageLocation}`;
|
|
|
|
}
|
|
|
|
|
2023-12-18 13:50:04 +01:00
|
|
|
export class FakeStateProvider implements StateProvider {
|
2024-02-22 21:07:26 +01:00
|
|
|
mock = mock<StateProvider>();
|
2024-06-07 09:33:26 +02:00
|
|
|
getUserState$<T>(userKeyDefinition: UserKeyDefinition<T>, userId?: UserId): Observable<T> {
|
|
|
|
this.mock.getUserState$(userKeyDefinition, userId);
|
2024-02-06 17:35:22 +01:00
|
|
|
if (userId) {
|
2024-06-07 09:33:26 +02:00
|
|
|
return this.getUser<T>(userId, userKeyDefinition).state$;
|
2024-02-06 17:35:22 +01:00
|
|
|
}
|
2024-03-15 14:05:04 +01:00
|
|
|
|
2024-06-07 09:33:26 +02:00
|
|
|
return this.getActive(userKeyDefinition).state$;
|
2024-03-15 14:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getUserStateOrDefault$<T>(
|
2024-06-07 09:33:26 +02:00
|
|
|
userKeyDefinition: UserKeyDefinition<T>,
|
2024-03-15 14:05:04 +01:00
|
|
|
config: { userId: UserId | undefined; defaultValue?: T },
|
|
|
|
): Observable<T> {
|
|
|
|
const { userId, defaultValue = null } = config;
|
2024-06-07 09:33:26 +02:00
|
|
|
this.mock.getUserStateOrDefault$(userKeyDefinition, config);
|
2024-03-15 14:05:04 +01:00
|
|
|
if (userId) {
|
2024-06-07 09:33:26 +02:00
|
|
|
return this.getUser<T>(userId, userKeyDefinition).state$;
|
2024-03-15 14:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.activeUserId$.pipe(
|
|
|
|
take(1),
|
|
|
|
switchMap((userId) =>
|
2024-06-07 09:33:26 +02:00
|
|
|
userId != null ? this.getUser(userId, userKeyDefinition).state$ : of(defaultValue),
|
2024-03-15 14:05:04 +01:00
|
|
|
),
|
|
|
|
);
|
2024-02-06 17:35:22 +01:00
|
|
|
}
|
|
|
|
|
2024-02-08 20:54:15 +01:00
|
|
|
async setUserState<T>(
|
2024-06-07 09:33:26 +02:00
|
|
|
userKeyDefinition: UserKeyDefinition<T>,
|
2024-02-08 20:54:15 +01:00
|
|
|
value: T,
|
|
|
|
userId?: UserId,
|
|
|
|
): Promise<[UserId, T]> {
|
2024-06-07 09:33:26 +02:00
|
|
|
await this.mock.setUserState(userKeyDefinition, value, userId);
|
2024-02-06 17:35:22 +01:00
|
|
|
if (userId) {
|
2024-06-07 09:33:26 +02:00
|
|
|
return [userId, await this.getUser(userId, userKeyDefinition).update(() => value)];
|
2024-02-06 17:35:22 +01:00
|
|
|
} else {
|
2024-06-07 09:33:26 +02:00
|
|
|
return await this.getActive(userKeyDefinition).update(() => value);
|
2024-02-06 17:35:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-07 09:33:26 +02:00
|
|
|
getActive<T>(userKeyDefinition: UserKeyDefinition<T>): ActiveUserState<T> {
|
|
|
|
return this.activeUser.get(userKeyDefinition);
|
2023-12-18 13:50:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getGlobal<T>(keyDefinition: KeyDefinition<T>): GlobalState<T> {
|
|
|
|
return this.global.get(keyDefinition);
|
|
|
|
}
|
|
|
|
|
2024-06-07 09:33:26 +02:00
|
|
|
getUser<T>(userId: UserId, userKeyDefinition: UserKeyDefinition<T>): SingleUserState<T> {
|
|
|
|
return this.singleUser.get(userId, userKeyDefinition);
|
2023-12-18 13:50:04 +01:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-01-10 16:36:19 +01:00
|
|
|
constructor(public accountService: FakeAccountService) {}
|
|
|
|
|
2024-08-27 02:44:08 +02:00
|
|
|
private distributeSingleUserUpdate(
|
|
|
|
key: UserKeyDefinition<unknown>,
|
|
|
|
userId: UserId,
|
|
|
|
newState: unknown,
|
|
|
|
) {
|
|
|
|
if (this.activeUser.accountService.activeUserId === userId) {
|
|
|
|
const state = this.activeUser.getFake(key, { allowInit: false });
|
|
|
|
state?.nextState(newState, { syncValue: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private distributeActiveUserUpdate(
|
|
|
|
key: UserKeyDefinition<unknown>,
|
|
|
|
userId: UserId,
|
|
|
|
newState: unknown,
|
|
|
|
) {
|
|
|
|
this.singleUser
|
|
|
|
.getFake(userId, key, { allowInit: false })
|
|
|
|
?.nextState(newState, { syncValue: false });
|
|
|
|
}
|
|
|
|
|
2023-12-18 13:50:04 +01:00
|
|
|
global: FakeGlobalStateProvider = new FakeGlobalStateProvider();
|
2024-08-27 02:44:08 +02:00
|
|
|
singleUser: FakeSingleUserStateProvider = new FakeSingleUserStateProvider(
|
|
|
|
this.distributeSingleUserUpdate.bind(this),
|
|
|
|
);
|
|
|
|
activeUser: FakeActiveUserStateProvider = new FakeActiveUserStateProvider(
|
|
|
|
this.accountService,
|
|
|
|
this.distributeActiveUserUpdate.bind(this),
|
|
|
|
);
|
2024-01-04 20:47:49 +01:00
|
|
|
derived: FakeDerivedStateProvider = new FakeDerivedStateProvider();
|
2024-02-06 17:35:22 +01:00
|
|
|
activeUserId$: Observable<UserId> = this.activeUser.activeUserId$;
|
2024-01-04 20:47:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
2024-04-26 21:08:39 +02:00
|
|
|
let result = this.states.get(deriveDefinition.buildCacheKey()) as DerivedState<TTo>;
|
2024-01-04 20:47:49 +01:00
|
|
|
|
|
|
|
if (result == null) {
|
2024-02-13 21:26:56 +01:00
|
|
|
result = new FakeDerivedState(parentState$, deriveDefinition, dependencies);
|
2024-04-26 21:08:39 +02:00
|
|
|
this.states.set(deriveDefinition.buildCacheKey(), result);
|
2024-01-04 20:47:49 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2023-12-18 13:50:04 +01:00
|
|
|
}
|