Skip to content

Commit 601b0db

Browse files
OlliVleo
authored andcommitted
New function names for clarity (#14)
* New function names for clarity The function names `v()` and `p()` in this library were accidentally reversed from the tradition. Since the function names might be a bit confusing even if named correctly, we should rename them as `acquire()` and `release()` but also provide the old names for backwards compatibility. Fixes #13 * Missed this * Add deprecation warnings * Fix ts definitions
1 parent 8cf24ac commit 601b0db

File tree

6 files changed

+26
-13
lines changed

6 files changed

+26
-13
lines changed

examples/basic.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ async function f () {
1616

1717
const s = new Sema(13, { capacity: arr.length })
1818
await Promise.all(arr.map(async (elem) => {
19-
await s.v()
19+
await s.acquire()
2020
console.log(elem, s.nrWaiting())
2121
await new Promise((resolve) => setTimeout(resolve, getRnd(500, 3000)))
22-
s.p()
22+
s.release()
2323
}))
2424
console.log('hello')
2525
}

examples/pausing.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ function resume() {
2323

2424
const s = new Sema(5, { pauseFn: pause, resumeFn: resume })
2525
async function parse(line) {
26-
await s.v()
26+
await s.acquire()
2727

2828
console.log(line)
2929

30-
s.p()
30+
s.release()
3131
}
3232

3333
rl.on('line', (line) => {

examples/pooling.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const redis = require('promise-redis')
66
async function f () {
77
const red = new Sema(3, { initFn: () => redis().createClient(process.env.REDIS_URL) })
88

9-
const db = await red.v()
9+
const db = await red.acquire()
1010
console.log(await db.get('id'))
11-
red.p(db)
11+
red.release(db)
1212

1313
const dbs = await red.drain()
1414
dbs.map((db) => db.quit())

index.d.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ declare module 'async-sema' {
88
drain(): Promise<string[]>;
99

1010
nrWaiting(): number;
11+
12+
v(): Promise<string>;
13+
14+
acquire(): Promise<string>;
1115

1216
p(token?: string): void;
13-
14-
v(): Promise<string>;
17+
18+
release(token?: string): void;
1519

1620
}
1721

index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Native
22
const EventEmitter = require('events')
3+
const util = require('util')
34

45
// Packages
56
const Deque = require('double-ended-queue')
@@ -43,7 +44,7 @@ class Sema {
4344
}
4445
}
4546

46-
async v () {
47+
async acquire () {
4748
let token = this.free.pop()
4849

4950
if (token) {
@@ -59,15 +60,21 @@ class Sema {
5960
this.waiting.push({ resolve, reject })
6061
})
6162
}
63+
async v () {
64+
return this.acquire();
65+
}
6266

63-
p (token) {
67+
release (token) {
6468
this.releaseEmitter.emit('release', this.noTokens ? '1' : token)
6569
}
70+
p (token) {
71+
return this.release(token)
72+
}
6673

6774
drain () {
6875
const a = new Array(this.nrTokens)
6976
for (let i = 0; i < this.nrTokens; i++) {
70-
a[i] = this.v()
77+
a[i] = this.acquire()
7178
}
7279
return Promise.all(a)
7380
}
@@ -77,4 +84,6 @@ class Sema {
7784
}
7885
}
7986

87+
Sema.prototype.v = util.deprecate(Sema.prototype.v, '`v()` is deperecated; use `acquire()` instead')
88+
Sema.prototype.p = util.deprecate(Sema.prototype.p, '`p()` is deprecated; use `release()` instead')
8089
module.exports = Sema

rate-limit.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = function rateLimit(rps) {
44
const sema = new Sema(rps);
55

66
return async function rl() {
7-
await sema.v();
8-
setTimeout(() => sema.p(), 1000);
7+
await sema.acquire();
8+
setTimeout(() => sema.release(), 1000);
99
}
1010
}

0 commit comments

Comments
 (0)