来个简单的任务调度

160 阅读1分钟
class Schedule {
    constructor() {
        this.tasks = [];
    }
    run(task) {
        const wrapTask = () => {
            task();
            this.exec();
        }
        this.tasks.push(wrapTask);
        this.exec();
        return this;
    }
    sleep(time) {
        const sleepTask = () => {
            console.log(`sleep: ${time}`)
            setTimeout(() => {
                this.exec();
            }, time)
        }
        this.tasks.push(sleepTask);
        return this;
    }
    exec() {
        const task = this.tasks.shift();
        if (task) {
            task();
        }
    }
}

const runner = new Schedule()

runner.run(() => {
    console.log(1);
}).sleep(2000).sleep(3000).run(() => {
    console.log(2);
})

class Schedule1 {
    constructor() {
        this.tasks = [];
    }
    run(task) {
        const wrapTask = async () => {
            await task();
            await this.exec();
        }
        this.tasks.push(wrapTask);
        this.exec();
        return this;
    }
    sleep(time) {
        const sleepTask = async () => {
            console.log(`sleep: ${time}`);
            let timer;
            await new Promise((resolve) => {
                timer = setTimeout(resolve, time)
            });
            clearTimeout(timer);
            await this.exec();
        }
        this.tasks.push(sleepTask);
        this.exec();
        return this;
    }
    async exec() {
        if (this.running) {
            return;
        }
        this.running = true;
        this.doExec();
    }
    async doExec() {
        const task = this.tasks.shift();
        if (task) {
            await task();
        }
        if (this.tasks.length) {
            await this.doExec();
        } else {
            this.running = false;
        }
    }
}

const runner1 = new Schedule1()

runner1.run(async () => {
    console.log('task1 start');
    await new Promise((resolve) => {
        setTimeout(() => {
            console.log(1);
            resolve();
        }, 5000);
    });
    console.log('task1 end');
}).sleep(2000).sleep(3000).run(async () => {
    console.log('task2 start');
    await new Promise((resolve) => {
        setTimeout(() => {
            console.log(2);
            resolve();
        }, 5000)
    });
})

class Schedule2 {
    constructor() {
        this.tasks = [];
    }
    run(task) {
        const wrapTask = async () => {
            await task();
            await this.exec();
        }
        this.tasks.push(wrapTask);
        this.exec();
        return this;
    }
    sleep(time) {
        const sleepTask = async () => {
            console.log(`sleep: ${time}`);
            let timer;
            await new Promise((resolve) => {
                timer = setTimeout(resolve, time)
            });
            clearTimeout(timer);
            await this.exec();
        }
        this.tasks.push(sleepTask);
        this.exec();
        return this;
    }
    async exec() {
        if (this.running) {
            return;
        }
        this.running = true;
        this.doExec();
    }
    async doExec() {
        const task = this.tasks.shift();
        if (task) {
            await task();
        }
        if (this.tasks.length) {
            await this.doExec();
        } else {
            this.running = false;
        }
    }
}

const runner2 = new Schedule2()

runner1.run(async () => {
    console.log('task1 start');
    await new Promise((resolve) => {
        setTimeout(() => {
            console.log(1);
            resolve();
        }, 5000);
    });
    console.log('task1 end');
}).sleep(2000).sleep(3000).run(async () => {
    console.log('task2 start');
    await new Promise((resolve) => {
        setTimeout(() => {
            console.log(2);
            resolve();
        }, 5000)
    });
})