This commit unifies executable ID structure across categories and scripts, paving the way for more complex ID solutions for #262. It also refactors related code to adapt to the changes. Key changes: - Change numeric IDs to string IDs for categories - Use named types for string IDs to improve code clarity - Add unit tests to verify ID uniqueness Other supporting changes: - Separate concerns in entities for data access and executables by using separate abstractions (`Identifiable` and `RepositoryEntity`) - Simplify usage and construction of entities. - Remove `BaseEntity` for simplicity. - Move creation of categories/scripts to domain layer - Refactor CategoryCollection for better validation logic isolation - Rename some categories to keep the names (used as pseudo-IDs) unique on Windows.
56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
import type { IApplication } from '@/domain/IApplication';
|
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
|
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
|
|
import { EventSource } from '@/infrastructure/Events/EventSource';
|
|
import { assertInRange } from '@/application/Common/Enum';
|
|
import { CategoryCollectionState } from './State/CategoryCollectionState';
|
|
import type { IApplicationContext, IApplicationContextChangedEvent } from './IApplicationContext';
|
|
import type { ICategoryCollectionState } from './State/ICategoryCollectionState';
|
|
|
|
type StateMachine = Map<OperatingSystem, ICategoryCollectionState>;
|
|
|
|
export class ApplicationContext implements IApplicationContext {
|
|
public readonly contextChanged = new EventSource<IApplicationContextChangedEvent>();
|
|
|
|
public collection: ICategoryCollection;
|
|
|
|
public currentOs: OperatingSystem;
|
|
|
|
public get state(): ICategoryCollectionState {
|
|
return this.states[this.collection.os];
|
|
}
|
|
|
|
private readonly states: StateMachine;
|
|
|
|
public constructor(
|
|
public readonly app: IApplication,
|
|
initialContext: OperatingSystem,
|
|
) {
|
|
this.states = initializeStates(app);
|
|
this.changeContext(initialContext);
|
|
}
|
|
|
|
public changeContext(os: OperatingSystem): void {
|
|
assertInRange(os, OperatingSystem);
|
|
if (this.currentOs === os) {
|
|
return;
|
|
}
|
|
const collection = this.app.getCollection(os);
|
|
this.collection = collection;
|
|
const event: IApplicationContextChangedEvent = {
|
|
newState: this.states[os],
|
|
oldState: this.states[this.currentOs],
|
|
};
|
|
this.contextChanged.notify(event);
|
|
this.currentOs = os;
|
|
}
|
|
}
|
|
|
|
function initializeStates(app: IApplication): StateMachine {
|
|
const machine = new Map<OperatingSystem, ICategoryCollectionState>();
|
|
for (const collection of app.collections) {
|
|
machine[collection.os] = new CategoryCollectionState(collection);
|
|
}
|
|
return machine;
|
|
}
|