Open Rain120 opened 2 years ago
function runAllTask(list, cb) {
const map = new Map();
try {
while (list.length) {
const cur = list.shift();
if (!cur.deps.length) {
map.set(cur.id, cur.runTask());
} else {
const params = [];
const finished = cur.deps.every(dep => {
if (map.has(dep)) {
params.push(map.get(dep));
return true;
} else {
list.push(cur);
return false;
}
});
if (finished) {
const res = cur.runTask(...params);
map.set(cur.id, res);
}
}
}
cb(null, Object.fromEntries(map));
} catch (error) {
cb(error, null);
}
}
function runAllTask(list, cb) {
const map= {};
const res = {};
function runOneTask(id, deps = []) {
if (res[id]) {
return res[id];
}
const depRes = [];
map[id].deps.forEach(dep => {
deps.push(dep);
depRes.push(runOneTask(dep, deps))
});
res[id] = map[id].runTask(...depRes);
deps = deps.filter(item => item === id);
return res[id];
}
try {
list.forEach(item => {
map[item.id] = item;
});
list.forEach(item => {
runOneTask(item.id);
});
cb(null, res);
} catch (error) {
cb(error, null);
}
}
题目