1- const debug = require ( 'debug' ) ( 'callback-queue' ) ;
2- const assert = require ( 'assert' ) ;
1+ import _debug = require( 'debug' )
2+ import assert = require( 'assert' )
3+
4+ const debug = _debug ( 'callback-queue' )
35
46/**
57 * Callback queue
6- * @type {Map }
78 */
8- const callbackQueue = new Map ( ) ;
9+ const callbackQueue = new Map < string , Set < Thunk > > ( )
910
1011// cache reference
11- const nextTick = ( typeof setImmediate === 'function' && setImmediate ) || process . nextTick ;
12+ const nextTick = ( typeof setImmediate === 'function' && setImmediate ) || process . nextTick
13+ export type Thunk = ( ...args : unknown [ ] ) => unknown
1214
1315/**
1416 * Iterates over callbacks and calls them with passed args
15- * @param { Array } bucket
16- * @param { Array } args
17+ * @param bucket
18+ * @param args
1719 */
18- function iterateOverCallbacks ( bucket , args ) {
20+ function iterateOverCallbacks ( bucket : Set < Thunk > , args : unknown [ ] ) {
1921 // set iterator
2022 for ( const thunk of bucket ) {
21- nextTick ( thunk , ...args ) ;
23+ nextTick ( thunk , ...args )
2224 }
2325}
2426
@@ -33,36 +35,40 @@ function iterateOverCallbacks(bucket, args) {
3335 * @param {String } key - unique key, based on which requests are bucketed
3436 * @param {Function } callback - callback that should be added into requests queue
3537 */
36- exports . add = function add ( key , callback ) {
37- assert . equal ( typeof key , 'string' , 'key must be a truthy string' ) ;
38- assert . ok ( key , 'key must be a truthy string' ) ;
39- assert . equal ( typeof callback , 'function' , 'callback must be a function' ) ;
38+ export function add ( key : string , callback : Thunk ) : Thunk | false {
39+ assert . strictEqual ( typeof key , 'string' , 'key must be a truthy string' )
40+ assert . ok ( key , 'key must be a truthy string' )
41+ assert . strictEqual ( typeof callback , 'function' , 'callback must be a function' )
4042
41- const bucket = callbackQueue . get ( key ) ;
43+ const bucket = callbackQueue . get ( key )
4244 if ( bucket ) {
43- bucket . add ( callback ) ;
44- return false ;
45+ bucket . add ( callback )
46+ return false
4547 }
4648
4749 // push new set into queue
48- callbackQueue . set ( key , new Set ( [ callback ] ) ) ;
50+ callbackQueue . set ( key , new Set ( [ callback ] ) )
4951
50- return function queuedCallback ( ...args ) {
52+ /**
53+ * Returns forwarder function that needs to be invoked
54+ * when data has been processed
55+ */
56+ return function forwarder ( ...args : unknown [ ] ) {
5157 // its essential that we do not use any reference, because of GC
5258 // when object reaches certain number of nullified values - its recreated using compactObject
5359 // function. Therefore we need to grab a reference when callback needs to be invoked and not at
5460 // other time
55- const callbacks = callbackQueue . get ( key ) ;
61+ const callbacks = callbackQueue . get ( key )
5662 if ( ! callbacks ) {
57- debug ( 'Callbacks couldn\'t be invoked' ) ;
58- return null ;
63+ debug ( 'Callbacks couldn\'t be invoked' )
64+ return null
5965 }
6066
61- debug ( 'calling callback for key %s' , key ) ;
62- callbackQueue . delete ( key ) ;
63- return iterateOverCallbacks ( callbacks , args ) ;
64- } ;
65- } ;
67+ debug ( 'calling callback for key %s' , key )
68+ callbackQueue . delete ( key )
69+ return iterateOverCallbacks ( callbacks , args )
70+ }
71+ }
6672
6773/**
6874 * Call this if you are absolutely sure you need to abort the request
@@ -74,13 +80,13 @@ exports.add = function add(key, callback) {
7480 * @param {String } key
7581 * @param {Error } error
7682 */
77- exports . remove = function remove ( key , error ) {
78- const bucket = callbackQueue . get ( key ) ;
83+ export function remove ( key : string , error : Error ) : void | false {
84+ const bucket = callbackQueue . get ( key )
7985 if ( ! bucket ) {
80- return false ;
86+ return false
8187 }
8288
83- assert . ok ( error instanceof Error , 'you must pass an instance of Error object when canceling requests' ) ;
84- callbackQueue . delete ( key ) ;
85- return iterateOverCallbacks ( bucket , [ error ] ) ;
86- } ;
89+ assert . ok ( error instanceof Error , 'you must pass an instance of Error object when canceling requests' )
90+ callbackQueue . delete ( key )
91+ return iterateOverCallbacks ( bucket , [ error ] )
92+ }
0 commit comments