2019-05-12 09:11:13 +02:00
|
|
|
/* eslint-disable node-core/require-common-first, node-core/required-modules */
|
2018-02-04 20:38:18 +01:00
|
|
|
|
2017-07-15 00:05:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const kLimit = Symbol('limit');
|
|
|
|
const kCallback = Symbol('callback');
|
2017-11-21 19:33:16 +01:00
|
|
|
const common = require('./');
|
2017-07-15 00:05:24 +02:00
|
|
|
|
|
|
|
class Countdown {
|
|
|
|
constructor(limit, cb) {
|
|
|
|
assert.strictEqual(typeof limit, 'number');
|
|
|
|
assert.strictEqual(typeof cb, 'function');
|
|
|
|
this[kLimit] = limit;
|
2017-11-21 19:33:16 +01:00
|
|
|
this[kCallback] = common.mustCall(cb);
|
2017-07-15 00:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dec() {
|
|
|
|
assert(this[kLimit] > 0, 'Countdown expired');
|
|
|
|
if (--this[kLimit] === 0)
|
|
|
|
this[kCallback]();
|
2017-11-28 07:40:28 +01:00
|
|
|
return this[kLimit];
|
2017-07-15 00:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get remaining() {
|
|
|
|
return this[kLimit];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Countdown;
|