BREAKING CHANGE(constraints): make TaskConstraintGroup constraint matcher input-aware and add shouldExecute pre-execution hook
This commit is contained in:
@@ -80,14 +80,18 @@ export class TaskManager {
|
||||
// Gather applicable constraints
|
||||
const applicableGroups: Array<{ group: TaskConstraintGroup<any>; key: string }> = [];
|
||||
for (const group of this.constraintGroups) {
|
||||
const key = group.getConstraintKey(task);
|
||||
const key = group.getConstraintKey(task, input);
|
||||
if (key !== null) {
|
||||
applicableGroups.push({ group, key });
|
||||
}
|
||||
}
|
||||
|
||||
// No constraints apply → trigger directly
|
||||
// No constraints apply → check shouldExecute then trigger directly
|
||||
if (applicableGroups.length === 0) {
|
||||
const shouldRun = await this.checkAllShouldExecute(task, input);
|
||||
if (!shouldRun) {
|
||||
return undefined;
|
||||
}
|
||||
return task.trigger(input);
|
||||
}
|
||||
|
||||
@@ -97,22 +101,47 @@ export class TaskManager {
|
||||
return this.executeWithConstraintTracking(task, input, applicableGroups);
|
||||
}
|
||||
|
||||
// Blocked → enqueue with deferred promise
|
||||
// Blocked → enqueue with deferred promise and cached constraint keys
|
||||
const deferred = plugins.smartpromise.defer<any>();
|
||||
this.constraintQueue.push({ task, input, deferred });
|
||||
const constraintKeys = new Map<string, string>();
|
||||
for (const { group, key } of applicableGroups) {
|
||||
constraintKeys.set(group.name, key);
|
||||
}
|
||||
this.constraintQueue.push({ task, input, deferred, constraintKeys });
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
private async checkAllShouldExecute(task: Task<any, any, any>, input?: any): Promise<boolean> {
|
||||
for (const group of this.constraintGroups) {
|
||||
const shouldRun = await group.checkShouldExecute(task, input);
|
||||
if (!shouldRun) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private async executeWithConstraintTracking(
|
||||
task: Task<any, any, any>,
|
||||
input: any,
|
||||
groups: Array<{ group: TaskConstraintGroup<any>; key: string }>,
|
||||
): Promise<any> {
|
||||
// Acquire slots
|
||||
// Acquire slots synchronously to prevent race conditions
|
||||
for (const { group, key } of groups) {
|
||||
group.acquireSlot(key);
|
||||
}
|
||||
|
||||
// Check shouldExecute after acquiring slots
|
||||
const shouldRun = await this.checkAllShouldExecute(task, input);
|
||||
if (!shouldRun) {
|
||||
// Release slots and drain queue
|
||||
for (const { group, key } of groups) {
|
||||
group.releaseSlot(key);
|
||||
}
|
||||
this.drainConstraintQueue();
|
||||
return undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
return await task.trigger(input);
|
||||
} finally {
|
||||
@@ -131,23 +160,30 @@ export class TaskManager {
|
||||
for (const entry of this.constraintQueue) {
|
||||
const applicableGroups: Array<{ group: TaskConstraintGroup<any>; key: string }> = [];
|
||||
for (const group of this.constraintGroups) {
|
||||
const key = group.getConstraintKey(entry.task);
|
||||
const key = group.getConstraintKey(entry.task, entry.input);
|
||||
if (key !== null) {
|
||||
applicableGroups.push({ group, key });
|
||||
}
|
||||
}
|
||||
|
||||
// No constraints apply anymore (group removed?) → run directly
|
||||
// No constraints apply anymore (group removed?) → check shouldExecute then run
|
||||
if (applicableGroups.length === 0) {
|
||||
entry.task.trigger(entry.input).then(
|
||||
(result) => entry.deferred.resolve(result),
|
||||
(err) => entry.deferred.reject(err),
|
||||
);
|
||||
this.checkAllShouldExecute(entry.task, entry.input).then((shouldRun) => {
|
||||
if (!shouldRun) {
|
||||
entry.deferred.resolve(undefined);
|
||||
return;
|
||||
}
|
||||
entry.task.trigger(entry.input).then(
|
||||
(result) => entry.deferred.resolve(result),
|
||||
(err) => entry.deferred.reject(err),
|
||||
);
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const allCanRun = applicableGroups.every(({ group, key }) => group.canRun(key));
|
||||
if (allCanRun) {
|
||||
// executeWithConstraintTracking handles shouldExecute check internally
|
||||
this.executeWithConstraintTracking(entry.task, entry.input, applicableGroups).then(
|
||||
(result) => entry.deferred.resolve(result),
|
||||
(err) => entry.deferred.reject(err),
|
||||
|
||||
Reference in New Issue
Block a user