Skip to content

Commit e4c8a25

Browse files
committed
Rewirte package
1 parent 13255a1 commit e4c8a25

File tree

3 files changed

+88
-110
lines changed

3 files changed

+88
-110
lines changed

.gitignore

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
lib-cov
2-
*.seed
3-
*.log
4-
*.csv
5-
*.dat
6-
*.out
7-
*.pid
8-
*.gz
9-
10-
pids
11-
logs
12-
results
13-
1+
.nyc_output
2+
coverage
143
node_modules
15-
npm-debug.log
4+
5+
npm-debug.log

index.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
;(function(global) {
2+
'use strict'
3+
4+
var nextTick = function (fn) { setTimeout(fn, 0) }
5+
if (typeof process !== 'undefined' && process && typeof process.nextTick === 'function') {
6+
// node.js and the like
7+
nextTick = process.nextTick
8+
}
9+
10+
function checkNumber (n) {
11+
if (typeof n !== 'number' || isNaN(n) || !isFinite(n)) {
12+
throw new TypeError('expected number, got ' + n)
13+
}
14+
if (n % 1 !== 0 || n < 1) {
15+
throw new RangeError('expected positive integer number, got ' + n)
16+
}
17+
}
18+
19+
function checkFunction (f) {
20+
if (typeof f !== 'function') throw new TypeError('expected function, got ' + f)
21+
}
22+
23+
function Semaphore (capacity) {
24+
if (!(this instanceof Semaphore)) return new Semaphore(capacity)
25+
26+
this._capacity = capacity || 1
27+
checkNumber(this._capacity)
28+
29+
this._current = 0
30+
this._queue = []
31+
this._leave = this.leave.bind(this)
32+
}
33+
34+
Semaphore.prototype.take = function () {
35+
var item = {}
36+
if (typeof arguments[0] === 'function') {
37+
item.task = arguments[0]
38+
item.n = arguments[1] || 1
39+
} else {
40+
item.task = arguments[1]
41+
item.n = arguments[0] || 1
42+
}
43+
44+
checkFunction(item.task)
45+
checkNumber(item.n)
46+
if (item.n > this._capacity) {
47+
throw new RangeError('expected number in [1, ' + this._capacity + '], got ' + item.n)
48+
}
49+
50+
if (this._current + item.n > this._capacity) {
51+
this._queue.push(item)
52+
} else {
53+
this._current += item.n
54+
item.task(this._leave)
55+
}
56+
}
57+
58+
Semaphore.prototype.leave = function (n) {
59+
n = n || 1
60+
checkNumber(n)
61+
62+
this._current = Math.max(this._current - n, 0)
63+
64+
if (this._queue.length === 0) return
65+
if (this._current + this._queue[0].n > this._capacity) return
66+
67+
var item = this._queue.shift()
68+
this._current += item.n
69+
70+
var leave = this._leave
71+
nextTick(function () { item.task(leave) })
72+
}
73+
74+
if (typeof exports === 'object') {
75+
// node export
76+
module.exports = Semaphore
77+
} else if (typeof define === 'function' && define.amd) {
78+
// amd export
79+
define(function () { return Semaphore })
80+
} else {
81+
// browser global
82+
global.semaphore = global.Semaphore = Semaphore
83+
}
84+
}(this))

lib/semaphore.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)