@@ -5,6 +5,7 @@ import { primordials } from "ext:core/mod.js";
55const {
66 FunctionPrototypeBind,
77 MapPrototypeDelete,
8+ MapPrototypeGet,
89 MapPrototypeSet,
910 NumberIsFinite,
1011 SafeArrayIterator,
@@ -33,6 +34,7 @@ export { kTimerId } from "ext:deno_web/02_timers.js";
3334// Timeout values > TIMEOUT_MAX are set to 1.
3435export const TIMEOUT_MAX = 2 ** 31 - 1 ;
3536
37+ export const kDestroy = Symbol ( "destroy" ) ;
3638export const kTimeout = Symbol ( "timeout" ) ;
3739const kRefed = Symbol ( "refed" ) ;
3840const createTimer = Symbol ( "createTimer" ) ;
@@ -43,7 +45,15 @@ const createTimer = Symbol("createTimer");
4345 *
4446 * @type {Map<number, Timeout> }
4547 */
46- export const activeTimers = new SafeMap ( ) ;
48+ const activeTimers = new SafeMap ( ) ;
49+
50+ /**
51+ * @param {number } id
52+ * @returns {Timeout | undefined }
53+ */
54+ export function getActiveTimer ( id ) {
55+ return MapPrototypeGet ( activeTimers , id ) ;
56+ }
4757
4858// Timer constructor function.
4959export function Timeout ( callback , after , args , isRepeat , isRefed ) {
@@ -87,6 +97,11 @@ Timeout.prototype[createTimer] = function () {
8797 return id ;
8898} ;
8999
100+ Timeout . prototype [ kDestroy ] = function ( ) {
101+ this . _destroyed = true ;
102+ MapPrototypeDelete ( activeTimers , this [ kTimerId ] ) ;
103+ } ;
104+
90105// Make sure the linked list only shows the minimal necessary information.
91106Timeout . prototype [ inspect . custom ] = function ( _ , options ) {
92107 return inspect ( this , {
@@ -101,6 +116,7 @@ Timeout.prototype[inspect.custom] = function (_, options) {
101116Timeout . prototype . refresh = function ( ) {
102117 if ( ! this . _destroyed ) {
103118 clearTimeout_ ( this [ kTimerId ] ) ;
119+ MapPrototypeDelete ( activeTimers , this [ kTimerId ] ) ;
104120 this [ kTimerId ] = this [ createTimer ] ( ) ;
105121 }
106122 return this ;
0 commit comments