File tree Expand file tree Collapse file tree 7 files changed +34
-3
lines changed Expand file tree Collapse file tree 7 files changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -106,6 +106,10 @@ class BaseClient extends EventEmitter {
106
106
toJSON ( ...props ) {
107
107
return flatten ( this , ...props ) ;
108
108
}
109
+
110
+ async [ Symbol . asyncDispose ] ( ) {
111
+ await this . destroy ( ) ;
112
+ }
109
113
}
110
114
111
115
module . exports = BaseClient ;
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
+ const { polyfillDispose } = require ( '@discordjs/util' ) ;
3
4
const { __exportStar } = require ( 'tslib' ) ;
4
5
6
+ polyfillDispose ( ) ;
7
+
5
8
// "Root" classes (starting points)
6
9
exports . BaseClient = require ( './client/BaseClient' ) ;
7
10
exports . Client = require ( './client/Client' ) ;
Original file line number Diff line number Diff line change @@ -517,7 +517,7 @@ export abstract class Base {
517
517
public valueOf ( ) : string ;
518
518
}
519
519
520
- export class BaseClient extends EventEmitter {
520
+ export class BaseClient extends EventEmitter implements AsyncDisposable {
521
521
public constructor ( options ?: ClientOptions | WebhookClientOptions ) ;
522
522
private decrementMaxListeners ( ) : void ;
523
523
private incrementMaxListeners ( ) : void ;
@@ -526,6 +526,7 @@ export class BaseClient extends EventEmitter {
526
526
public rest : REST ;
527
527
public destroy ( ) : void ;
528
528
public toJSON ( ...props : Record < string , boolean | string > [ ] ) : unknown ;
529
+ public [ Symbol . asyncDispose ] ( ) : Promise < void > ;
529
530
}
530
531
531
532
export type GuildCacheMessage < Cached extends CacheType > = CacheTypeReducer <
Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ export * from './range.js';
3
3
export * from './calculateShardId.js' ;
4
4
export * from './runtime.js' ;
5
5
export * from './userAgentAppendix.js' ;
6
+ export * from './polyfillDispose.js' ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Polyfill for `Symbol.dispose` and `Symbol.asyncDispose` which is used as a part of
3
+ * {@link https://github.com/tc39/proposal-explicit-resource-management}. Node versions below 18.x
4
+ * don't have these symbols by default, so we need to polyfill them.
5
+ */
6
+ export function polyfillDispose ( ) {
7
+ // Polyfill for `Symbol.dispose` and `Symbol.asyncDispose` if not available.
8
+ // Taken from https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management
9
+
10
+ // @ts -expect-error This is a polyfill, so it's fine to write
11
+ Symbol . dispose ??= Symbol ( 'Symbol.dispose' ) ;
12
+ // @ts -expect-error Same as above
13
+ Symbol . asyncDispose ??= Symbol ( 'Symbol.asyncDispose' ) ;
14
+ }
Original file line number Diff line number Diff line change 1
1
import type { REST } from '@discordjs/rest' ;
2
2
import { range , type Awaitable } from '@discordjs/util' ;
3
+ import { polyfillDispose } from '@discordjs/util' ;
3
4
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter' ;
4
5
import {
5
6
Routes ,
@@ -17,6 +18,9 @@ import type { IIdentifyThrottler } from '../throttling/IIdentifyThrottler.js';
17
18
import { DefaultWebSocketManagerOptions , type CompressionMethod , type Encoding } from '../utils/constants.js' ;
18
19
import type { WebSocketShardDestroyOptions , WebSocketShardEvents } from './WebSocketShard.js' ;
19
20
21
+ // We put this here because in index.ts WebSocketManager seems to be outputted before polyfillDispose() is called from tsup.
22
+ polyfillDispose ( ) ;
23
+
20
24
/**
21
25
* Represents a range of shard ids
22
26
*/
@@ -199,7 +203,7 @@ export interface ManagerShardEventsMap {
199
203
] ;
200
204
}
201
205
202
- export class WebSocketManager extends AsyncEventEmitter < ManagerShardEventsMap > {
206
+ export class WebSocketManager extends AsyncEventEmitter < ManagerShardEventsMap > implements AsyncDisposable {
203
207
/**
204
208
* The options being used by this manager
205
209
*/
@@ -334,4 +338,8 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> {
334
338
public fetchStatus ( ) {
335
339
return this . strategy . fetchStatus ( ) ;
336
340
}
341
+
342
+ public async [ Symbol . asyncDispose ] ( ) {
343
+ await this . destroy ( ) ;
344
+ }
337
345
}
Original file line number Diff line number Diff line change 42
42
43
43
// Language and Environment
44
44
"experimentalDecorators" : true ,
45
- "lib" : [" ESNext" ],
45
+ "lib" : [" ESNext" , " esnext.disposable " ],
46
46
"target" : " ESNext" ,
47
47
"useDefineForClassFields" : true
48
48
},
You can’t perform that action at this time.
0 commit comments