-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
99 lines (83 loc) · 2.87 KB
/
example.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
93
94
95
96
97
98
99
const { Operation } = require('./src/Operation');
const { BlockOperation } = require('./src/BlockOperation');
const { OperationQueue } = require('./src/OperationQueue');
const axios = require('axios');
class DownloadDataOperation extends Operation {
async run() {
try {
const response = await axios.get('https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22');
const data = response.data;
return this.id;
} catch (e) {
this.cancel();
}
}
}
class TimeOutOperation extends Operation {
constructor(id = null, time = 1000) {
super(id);
this.time = time;
}
run() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, this.time);
})
}
}
const blockOperation6 = new BlockOperation(6, async () => {
const response = await axios.get('https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22');
const data = response.data;
return data;
});
const operation = new DownloadDataOperation(1);
const operation2 = new DownloadDataOperation(2);
const operation3 = new DownloadDataOperation(3);
const operation4 = new TimeOutOperation(4, 1000);
const operation5 = new TimeOutOperation(5, 1000);
const operation7 = new TimeOutOperation(7, 1000);
const operation8 = new TimeOutOperation(8, 1000);
operation.dependencies = [operation2, operation7];
// operation2.dependencies = [operation4, operation8];
// operation3.dependencies = [operation, operation5, operation2];
// operation4.dependencies = [operation5, blockOperation6];
// operation5.dependencies = [blockOperation6];
// operation8.dependencies = [operation7];
blockOperation6.completionCallback = (operation) => {
operation5.data = operation.result;
}
// operation2.dependencies = [operation3];
// operation.on('start', op => {
// console.log('start', op.id);
// });
// operation.on('done', op => {
// console.log('done', op.id);
// });
// operation.on('error', (err, op) => {
// console.log('error', op.id);
// });
// operation.completionCallback = () => {
// // console.log('done')
// }
const operationQueue = new OperationQueue();
operationQueue.maximumConcurrentOperations = 10;
// operationQueue.addOperation(operation);
// operationQueue.addOperation(operation3);
// operationQueue.completionCallback = () => {
// console.log('queue done');
// console.log(operationQueue.totalTime);
// };
(new Operation()).start().catch(e => {});
// operationQueue.addOperations([operation4, operation5, operation7, operation8])
// .then(result => {
// console.log(result)
// })
// .catch(e => {
// console.log(e)
// });
// operation4.cancel();
// operation.start()
// .then(() => {
// console.log('123');
// });