-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现一个 runAllTask #25
Comments
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);
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
题目
The text was updated successfully, but these errors were encountered: