mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2024-12-21 19:08:24 +01:00
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
|
|
export interface Operation {
|
|
values: {id:any};
|
|
type: string;
|
|
operations?: Operation[];
|
|
}
|
|
|
|
export function organizeWaitOperations(operations: Operation[]) {
|
|
|
|
// Initialize an object to store the counts and associated "done" operations
|
|
const waitCounts = {};
|
|
const doneOperations = {};
|
|
|
|
// Function to count "type: wait" operations and associate "done" operations per id
|
|
function countWaitOperationsAndDone(operations: Operation[]) {
|
|
for (const operation of operations) {
|
|
if (operation.type === "wait") {
|
|
const id = operation.values.id;
|
|
if (id in waitCounts) {
|
|
waitCounts[id]++;
|
|
} else {
|
|
waitCounts[id] = 1;
|
|
}
|
|
}
|
|
if (operation.type === "done") {
|
|
const id = operation.values.id;
|
|
doneOperations[id] = operation;
|
|
}
|
|
if (operation.operations) {
|
|
countWaitOperationsAndDone(operation.operations);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Start counting and associating from the root operations
|
|
countWaitOperationsAndDone(operations);
|
|
|
|
// Combine counts and associated "done" operations
|
|
const result = {};
|
|
for (const id in waitCounts) {
|
|
result[id] = {
|
|
waitCount: waitCounts[id],
|
|
doneOperation: doneOperations[id],
|
|
input: []
|
|
};
|
|
}
|
|
return result;
|
|
}
|
|
|