Open cjfff opened 5 years ago
const LazyMan = (function() {
class LazyMan {
constructor(name) {
this.name = name;
this.tasks = [() => {
console.log(` Hi I am ${name}`);
}]
}
static of(name) {
return new LazyMan(name)
}
static generaterSleepPromise(time) {
return () => new Promise((resolve) => {
setTimeout(() => {
console.log(`等待了${time}秒`);
resolve()
}, time * 1000)
})
}
sleep(time) {
this.tasks.push(LazyMan.generaterSleepPromise(time))
return this
}
sleepFirst(time) {
this.tasks.splice(this.tasks.length - 2, 0, LazyMan.generaterSleepPromise(time))
return this
}
eat(food) {
this.tasks.push(() => {
console.log(`I am eating ${food}`)
})
return this
}
async run() {
for (const fn of this.tasks) {
await fn()
}
}
}
return (name) => new LazyMan(name)
})()
LazyMan('Tony').run();
LazyMan('Tony').sleep(10).eat('lunch').run();
LazyMan('Tony').eat('lunch').sleep(10).eat('dinner').run();
LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(10).eat('junk food').run();
class LazyMan {
constructor(name) {
this.middleFn = [];
console.log(`Hi I am ${name}`);
}
wait(time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, time);
});
}
sleepFirst(time) {
this.middleFn.unshift(() => {
return this.wait(time);
});
return this;
}
sleep(time) {
this.middleFn.push(() => {
return this.wait(time);
});
return this;
}
eat(food) {
this.middleFn.push(() => {
console.log(`I am eating ${food}`);
});
return this;
}
async run() {
for (const fn of this.middleFn) {
await fn();
}
this.middleFn.length = 0;
}
}
new LazyMan("Tony")
.eat("lunch")
.eat("dinner")
.sleepFirst(5000)
.sleep(10000)
.eat("junk food")
.run();
const LazyMan = (function() {
class LazyMan {
constructor(name) {
this.name = name;
this.sleepFirstTime = 0;
this.sleepTime = 0;
this.consoleList = [];
this.cutDownTime = 0;
console.log(`Hi I am ${name}`);
setTimeout(() => {
setTimeout(() => {
this.consoleList.forEach(info => {
if (info.sleepTime) {
// TODO 需要优化,否则死机的
setTimeout(
() => {
console.log(`I am eating ${info.foot}`);
},
info.sleepTime * 1000,
info,
);
} else {
console.log(`I am eating ${info.foot}`);
}
});
}, this.sleepFirstTime * 1000);
}, 0);
}
sleep(time) {
this.sleepTime = time;
return this;
}
sleepFirst(time) {
this.sleepFirstTime = time;
return this;
}
eat(foot) {
// 将对应的事件push到数组中去
this.consoleList.push({
sleepTime: this.sleepTime,
foot,
});
return this;
}
}
return name => new LazyMan(name);
})();
LazyMan('Tony')
.eat('lunch')
.eat('dinner')
.sleepFirst(5)
.sleep(10)
.eat('junk food');
class LazyMan {
constructor(name) {
this.task = []
this.timer = setTimeout(() => {
this.run()
}, 0)
console.log(`Hi I am ${name}`)
}
sleep(second) {
const fn = () => {
setTimeout(() => {
this.run()
}, second * 1000)
}
this.task.push(fn)
return this
}
eat(food) {
const fn = () => {
console.log(`I am eating ${food}`)
this.run()
}
this.task.push(fn)
return this
}
sleepFirst(second) {
const fn = () => {
setTimeout(() => {
this.run()
}, second * 1000)
}
this.task.unshift(fn)
return this
}
run() {
const first = this.task.shift()
first && first()
}
}
new LazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(10).eat('junk food')
class LazyMan { constructor(name) { this.middleFn = []; console.log(`Hi I am ${name}`); } wait(time) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(); }, time); }); } sleepFirst(time) { this.middleFn.unshift(() => { return this.wait(time); }); return this; } sleep(time) { this.middleFn.push(() => { return this.wait(time); }); return this; } eat(food) { this.middleFn.push(() => { console.log(`I am eating ${food}`); }); return this; } async run() { for (const fn of this.middleFn) { await fn(); } this.middleFn.length = 0; } } new LazyMan("Tony") .eat("lunch") .eat("dinner") .sleepFirst(5000) .sleep(10000) .eat("junk food") .run();
执行javascript new LazyMan('Tony').eat('lunch').sleep(10).eat('dinner');
的时候只打印hi i amtony