@wroud/preconditions
Version:
A flexible and extensible library for managing preconditions in JavaScript and TypeScript applications. Simplify validation and preparation of entities like nodes, connections, and features with dynamic precondition handling.
45 lines • 1.57 kB
JavaScript
export class PreconditionManager {
preconditions = [];
/**
* Registers a new precondition for this entity type.
* @param precondition - The precondition to register.
*/
register(...precondition) {
this.preconditions.push(...precondition);
}
/**
* Checks if all applicable preconditions are fulfilled for the given entity instance.
* @param entity - The entity instance.
* @return {Promise<boolean>} - Resolves to true if all applicable preconditions are fulfilled.
*/
async checkPreconditions(entity) {
for (const precondition of this.getApplicablePreconditions(entity)) {
const isFulfilled = await precondition.check(entity);
if (!isFulfilled) {
return false;
}
}
return true;
}
/**
* Attempts to fulfill all applicable preconditions for the given entity instance.
* @param entity - The entity instance.
* @return {Promise<void>}
*/
async fulfillPreconditions(entity) {
for (const precondition of this.getApplicablePreconditions(entity)) {
const isFulfilled = await precondition.check(entity);
if (!isFulfilled) {
await precondition.fulfill(entity);
}
}
}
*getApplicablePreconditions(entity) {
for (const precondition of this.preconditions) {
if (precondition.isApplicable(entity)) {
yield precondition;
}
}
}
}
//# sourceMappingURL=PreconditionManager.js.map