• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* eslint-disable node-core/require-common-first, node-core/required-modules */
2
3'use strict';
4
5const assert = require('assert');
6const kLimit = Symbol('limit');
7const kCallback = Symbol('callback');
8const common = require('./');
9
10class Countdown {
11  constructor(limit, cb) {
12    assert.strictEqual(typeof limit, 'number');
13    assert.strictEqual(typeof cb, 'function');
14    this[kLimit] = limit;
15    this[kCallback] = common.mustCall(cb);
16  }
17
18  dec() {
19    assert(this[kLimit] > 0, 'Countdown expired');
20    if (--this[kLimit] === 0)
21      this[kCallback]();
22    return this[kLimit];
23  }
24
25  get remaining() {
26    return this[kLimit];
27  }
28}
29
30module.exports = Countdown;
31