Files
configucci/src/models/Env.ts

152 lines
3.3 KiB
TypeScript

import { EnvParam } from './EnvParam';
import { NamedEntity } from './types';
/**
* Environment configuration containing parameters
*/
export class Env implements NamedEntity {
constructor(
public id?: number,
public name?: string,
public params: EnvParam[] = []
) {}
/**
* Checks if this is the DEFAULT environment
*/
public isDefault(): boolean {
return this.name === 'DEFAULT';
}
/**
* Adds a new parameter to the environment (backward compatibility)
*/
public addParams(param: EnvParam): Env {
return this.addParam(param);
}
/**
* Adds a new parameter to the environment
*/
public addParam(param: EnvParam): Env {
const newParam = new EnvParam(
param.id ?? this.generateId(),
param.name,
param.value,
param.isChanged
);
return new Env(this.id, this.name, [...this.params, newParam]);
}
/**
* Removes a parameter by ID
*/
public delParam(paramIdOrParam: number | EnvParam): Env {
const paramId = typeof paramIdOrParam === 'number' ? paramIdOrParam : paramIdOrParam.id;
return this.removeParam(paramId!);
}
/**
* Removes a parameter by ID
*/
public removeParam(paramId: number): Env {
return new Env(
this.id,
this.name,
this.params.filter(p => p.id !== paramId)
);
}
/**
* Updates an existing parameter (backward compatibility)
*/
public updateParams(updatedParam: EnvParam): Env {
return this.updateParam(updatedParam);
}
/**
* Updates an existing parameter
*/
public updateParam(updatedParam: EnvParam): Env {
const index = this.params.findIndex(p => p.id === updatedParam.id);
if (index === -1) {
return this;
}
const newParams = [...this.params];
newParams[index] = updatedParam;
return new Env(this.id, this.name, newParams);
}
/**
* Gets a parameter by name
*/
public getParamByName(name: string): EnvParam | undefined {
return this.params.find(p => p.name === name);
}
/**
* Gets all parameter names
*/
public getParamNames(): string[] {
return this.params.map(p => p.name).filter((n): n is string => !!n);
}
/**
* Creates a copy with updated values
*/
public update(updates: Partial<Env>): Env {
return new Env(
updates.id ?? this.id,
updates.name ?? this.name,
updates.params ?? this.params
);
}
/**
* Generates a unique ID for new parameters
*/
private generateId(): number {
return Date.now() % 100000 + Math.floor(Math.random() * 10000);
}
}
/**
* Base class for environment events (backward compatibility)
*/
export class AppEvent<T> {
constructor(public payload: T) {}
public static add<T>(payload: T): AppEvent<T> {
return new AddEvent(payload);
}
public static del<T>(payload: T): AppEvent<T> {
return new RemoveEvent(payload);
}
public static update<T>(payload: T): AppEvent<T> {
return new UpdateEvent(payload);
}
}
/**
* Event for adding a parameter
*/
export class AddEvent<T> extends AppEvent<T> {}
/**
* Event for updating a parameter
*/
export class UpdateEvent<T> extends AppEvent<T> {}
/**
* Event for removing a parameter (backward compatibility)
*/
export class DelEvent<T> extends AppEvent<T> {}
/**
* Event for removing a parameter
*/
export class RemoveEvent<T> extends AppEvent<T> {}