-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlazyman.js
92 lines (91 loc) · 2.34 KB
/
lazyman.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class LazyMan {
constructor(name = 'Tom', tasks = []) {
this.name = name
this.tasks = tasks
const self = this
const fn = ((name) => {
return () => {
console.log('------------------>')
console.log(`Hi! This is ${name} !`);
self.next()
}
})(name)
this.tasks.push(fn)
setTimeout(() => {
self.next()
}, 0)
}
next(time = 0) {
const fn = this.tasks.shift()
setTimeout(()=>{
fn && fn()
},time)
}
eat(food) {
const self = this
const fn = ((food) => {
return () => {
console.log('------------------>')
console.log(`Eatting ${food} ~~`)
self.next()
}
})(food)
this.tasks.push(fn)
return this
}
first(thing) {
const self = this
const fn = ((thing) => {
return () => {
console.log('------------------>')
console.log(`Do this ${thing} first`)
self.next()
}
})(thing)
this.tasks.unshift(fn)
return this
}
sleep(time) {
const self = this
const fn = ((time) => {
setTimeout(() => {
console.log('------------------>')
console.log(`Sleep ${time} hours`)
self.next()
}, time * 1000)
})(time)
this.tasks.push(fn)
return this
}
takeABreak(time = 1){
const self = this
const fn = ((time)=>{
return () => {
setTimeout(()=>{
console.log('------------------>')
console.log(`Take a break for ${time} hours`)
self.next()
},time * 1000)
}
})(time)
this.tasks.push(fn)
return this
}
play(time) {
const self = this
const fn = ((time) => {
return () => {
setTimeout(() => {
console.log('------------------>')
console.log(`Play ${time} hours`)
self.next()
}, time * 1000)
}
})(time)
this.tasks.push(fn)
return this
}
}
function aLazyMan(name, tasks) {
return new LazyMan(name, tasks)
}