Skip to content
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

Open
Rain120 opened this issue Mar 17, 2022 · 2 comments
Open

实现一个 runAllTask #25

Rain120 opened this issue Mar 17, 2022 · 2 comments

Comments

@Rain120
Copy link
Owner

Rain120 commented Mar 17, 2022

题目

const input = [{
    id: 'task1',
    deps: [],
    runTask: () => 3,
}, {
    id: 'task2',
    deps: ['task1', 'task3'],
    // res1 task1的 runTask(), res3 task3的runTask()
    runTask: (res1, res3) => 1 + res1 + res3,
}, {
    id: 'task3',
    deps: ['task1'],
    runTask: (res1) => 5 + res1,
}, {
    id: 'task4',
    deps: ['task1', 'task2'],
    runTask: (res1, res2) => 3 + res1 + res2,
}, ];

runAllTask(input, (err, res) => {
    console.log(res);
    /** 
        res应该为:
        { task1: 3, task2: 12, task3: 8, task4: 18 }
    */
});
@Rain120
Copy link
Owner Author

Rain120 commented Mar 17, 2022

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);
    }
}

@Rain120
Copy link
Owner Author

Rain120 commented Mar 17, 2022

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant