import Bluebird from"../deps/bluebird_fork.js";let current,callback,cancellationResolve,cancellationToken,delay,defer;function invoke(){if((delay=void 0)===defer){const e=callback;callback=void 0,e()}}let immediate;class ExclusiveTimer{get cancellationToken(){return current!==this?(void 0===immediate&&(immediate=Bluebird.resolve()),immediate):(void 0===cancellationToken&&(cancellationToken=new Bluebird(e=>cancellationResolve=e)),cancellationToken)}start(e,i){current!==this&&"function"==typeof i&&(void 0!==current&&current.reset(),current=this,callback=i,delay=setTimeout(invoke,e))}reset(){current===this&&(callback=void 0,(current=void 0)!==delay&&(clearTimeout(delay),delay=void 0),void 0!==defer&&(clearTimeout(defer),defer=void 0),void 0!==cancellationResolve&&(cancellationResolve(),cancellationResolve=void 0,cancellationToken=void 0))}resume(e){current===this&&(void 0!==callback&&("function"==typeof e&&(callback=e),void 0===delay&&(delay=setTimeout(invoke))),void 0!==defer&&(clearTimeout(defer),defer=void 0))}suspend(){current===this&&void 0===defer&&(defer=setTimeout(()=>this.reset()))}}export{ExclusiveTimer};