-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreenshotManager.js
66 lines (57 loc) · 1.36 KB
/
screenshotManager.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
var Screenshot = require('./screenshot'),
EventEmitter = require('events').EventEmitter,
util = require('util'),
_ = require('lodash');
var index = 0;
var ScreenshotManager = function(amount) {
EventEmitter.call(this);
this.amount = amount || 3;
this.curruntAmout = 0;
this.cache = {length: this.amount};
this.init();
};
util.inherits(ScreenshotManager, EventEmitter);
_.merge(ScreenshotManager.prototype, {
init: function() {
for(var i = 0; i < this.amount; i++) {
this.createScreenshot();
}
},
createScreenshot: function(index) {
var self = this;
var screenshot = new Screenshot();
screenshot.on('create', function() {
var item = {pid: this.pid, screenshot: this, crash: false};
if(index) {
self.cache[index] = item
} else {
self.cache[self.curruntAmout++] = item;
if(self.curruntAmout === self.amount) {
self.emit('inited');
}
}
});
screenshot.on('crash', function() {
self.createScreenshot(self.getIndex(this.pid));
});
},
getScreenshot: function() {
while(true) {
var item = this.cache[index%this.amount];
index = ++index%this.amount;
if(!item.crash) {
return item.screenshot;
}
}
},
getIndex: function(pid) {
for(var i = 0; i < this.amount; i++) {
var item = this.cache[i];
if(item.pid === pid) {
return i;
}
}
return -1;
}
});
module.exports = ScreenshotManager;