-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
114 lines (96 loc) · 2.27 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var Promise = require('bluebird');
var EventEmitter = require('events').EventEmitter;
var util = require('util')
/**
* pending ponyfill
* http://bluebirdjs.com/docs/api/deferred-migration.html
*
* @return {{promise: Promise, resolve: Function, reject: Function}}
*/
function pendingPromise() {
ret = {};
ret.promise = new Promise(function(res, rej) {
ret.resolve = res;
ret.reject = rej;
});
return ret;
};
/**
* @constructor
* @param {{rooms: number}} opt_opts
*/
function PSemaphore(opt_opts) {
opt_opts = opt_opts || {};
this.queue = [];
this.rooms = opt_opts.rooms || 1;
this.active = [{Promise: {promise: Promise.resolve()}}]
}
util.inherits(PSemaphore, EventEmitter);
/**
* @private
* @return {number}
*/
PSemaphore.prototype._nextRoom = function() {
var nextRoom = -1;
if (this.rooms > this.active.length) {
nextRoom = this.active.length;
}
this.active.forEach(function(v, i) {
if (!v.Promise.promise.isPending() && nextRoom == -1) {
nextRoom = i;
}
});
this.emit('roomFound', nextRoom);
return nextRoom;
}
/**
* @private
*/
PSemaphore.prototype._processNext = function() {
// if the queue is empty no need to assign it
if (this.queue.length == 0) {
this.emit('workDone');
return;
}
var openRoom = this._nextRoom();
if (openRoom != -1) {
this._assignRoom(openRoom);
}
}
/**
* @private
* @param {number} room
*/
PSemaphore.prototype._assignRoom = function(room) {
this.emit('roomAssigned', room);
var worker = this.queue.shift();
this.active[room] = worker;
// not calling worker() directly here has the advantage that worker()
// does not necessarily have to return a bluebird-style Promise,
// or even a Promise instance at all
Promise.resolve()
.then(worker)
.then(function(v) {
worker.Promise.resolve(v);
}.bind(this))
.catch(function(e) {
worker.Promise.reject(e);
}.bind(this))
.finally(function() {
setImmediate(function(){
this._processNext();
}.bind(this))
}.bind(this));
};
/**
* @param {Function} work
* @return {Promise<any>}
*/
PSemaphore.prototype.add = function(work) {
this.emit('workAdded');
work.Promise = pendingPromise();
this.queue.push(work);
this._processNext();
return work.Promise.promise;
};
module.exports = PSemaphore