Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

New function names for clarity #14

Merged
merged 4 commits into from
Mar 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ async function f () {

const s = new Sema(13, { capacity: arr.length })
await Promise.all(arr.map(async (elem) => {
await s.v()
await s.acquire()
console.log(elem, s.nrWaiting())
await new Promise((resolve) => setTimeout(resolve, getRnd(500, 3000)))
s.p()
s.release()
}))
console.log('hello')
}
Expand Down
4 changes: 2 additions & 2 deletions examples/pausing.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ function resume() {

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

console.log(line)

s.p()
s.release()
}

rl.on('line', (line) => {
Expand Down
4 changes: 2 additions & 2 deletions examples/pooling.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const redis = require('promise-redis')
async function f () {
const red = new Sema(3, { initFn: () => redis().createClient(process.env.REDIS_URL) })

const db = await red.v()
const db = await red.acquire()
console.log(await db.get('id'))
red.p(db)
red.release(db)

const dbs = await red.drain()
dbs.map((db) => db.quit())
Expand Down
8 changes: 6 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ declare module 'async-sema' {
drain(): Promise<string[]>;

nrWaiting(): number;

v(): Promise<string>;

acquire(): Promise<string>;

p(token?: string): void;
v(): Promise<string>;

release(token?: string): void;

}

Expand Down
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Native
const EventEmitter = require('events')
const util = require('util')

// Packages
const Deque = require('double-ended-queue')
Expand Down Expand Up @@ -43,7 +44,7 @@ class Sema {
}
}

async v () {
async acquire () {
let token = this.free.pop()

if (token) {
Expand All @@ -59,15 +60,21 @@ class Sema {
this.waiting.push({ resolve, reject })
})
}
async v () {
return this.acquire();
}

p (token) {
release (token) {
this.releaseEmitter.emit('release', this.noTokens ? '1' : token)
}
p (token) {
return this.release(token)
}

drain () {
const a = new Array(this.nrTokens)
for (let i = 0; i < this.nrTokens; i++) {
a[i] = this.v()
a[i] = this.acquire()
}
return Promise.all(a)
}
Expand All @@ -77,4 +84,6 @@ class Sema {
}
}

Sema.prototype.v = util.deprecate(Sema.prototype.v, '`v()` is deperecated; use `acquire()` instead')
Sema.prototype.p = util.deprecate(Sema.prototype.p, '`p()` is deprecated; use `release()` instead')
module.exports = Sema
4 changes: 2 additions & 2 deletions rate-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function rateLimit(rps) {
const sema = new Sema(rps);

return async function rl() {
await sema.v();
setTimeout(() => sema.p(), 1000);
await sema.acquire();
setTimeout(() => sema.release(), 1000);
}
}