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