@@ -11,6 +11,9 @@ import StressViewProvider from './views/stress/provider/StressViewProvider';
11
11
12
12
let judgeViewProvider : JudgeViewProvider ;
13
13
let stressViewProvider : StressViewProvider ;
14
+ let competitiveCompanionServer : http . Server | undefined = undefined ;
15
+
16
+ let competitiveCompanionStatusItem : vscode . StatusBarItem ;
14
17
15
18
function registerViewProviders ( context : vscode . ExtensionContext ) : void {
16
19
judgeViewProvider = new JudgeViewProvider ( context ) ;
@@ -176,12 +179,29 @@ function registerCommands(context: vscode.ExtensionContext): void {
176
179
} ,
177
180
) ,
178
181
) ;
182
+
183
+ context . subscriptions . push (
184
+ vscode . commands . registerCommand (
185
+ 'fastolympiccoding.listenForCompetitiveCompanion' ,
186
+ ( ) => listenForCompetitiveCompanion ( ) ,
187
+ ) ,
188
+ ) ;
189
+ context . subscriptions . push (
190
+ vscode . commands . registerCommand (
191
+ 'fastolympiccoding.stopCompetitiveCompanion' ,
192
+ ( ) => stopCompetitiveCompanion ( ) ,
193
+ ) ,
194
+ ) ;
179
195
}
180
196
181
197
function listenForCompetitiveCompanion ( ) {
198
+ if ( competitiveCompanionServer !== undefined ) {
199
+ return ;
200
+ }
201
+
182
202
let problemDatas : IProblem [ ] = [ ] ;
183
203
let cnt = 0 ;
184
- const server = http . createServer ( ( req , res ) => {
204
+ competitiveCompanionServer = http . createServer ( ( req , res ) => {
185
205
if ( req . method !== 'POST' ) {
186
206
res . end ( ) ;
187
207
return ;
@@ -194,7 +214,7 @@ function listenForCompetitiveCompanion() {
194
214
} ) ;
195
215
req . on ( 'end' , ( ) => {
196
216
void ( async ( ) => {
197
- res . end ( ) ;
217
+ res . end ( ( ) => req . socket . unref ( ) ) ;
198
218
199
219
problemDatas . push ( JSON . parse ( ccData ) as IProblem ) ;
200
220
if ( problemDatas . length === 0 ) {
@@ -296,25 +316,56 @@ function listenForCompetitiveCompanion() {
296
316
} ) ( ) ;
297
317
} ) ;
298
318
} ) ;
299
- server . listen ( 1327 ) ;
319
+
320
+ competitiveCompanionServer . once ( 'connection' , ( socket ) => {
321
+ socket . unref ( ) ;
322
+ } ) ;
323
+ competitiveCompanionServer . once ( 'listening' , ( ) => {
324
+ competitiveCompanionStatusItem . show ( ) ;
325
+ } ) ;
326
+ competitiveCompanionServer . once ( 'error' , ( error ) =>
327
+ vscode . window . showErrorMessage (
328
+ `Competitive Companion listener error: ${ error } ` ,
329
+ ) ,
330
+ ) ;
331
+ competitiveCompanionServer . once ( 'close' , ( ) => {
332
+ competitiveCompanionServer = undefined ;
333
+ competitiveCompanionStatusItem . hide ( ) ;
334
+ } ) ;
335
+
336
+ const config = vscode . workspace . getConfiguration ( 'fastolympiccoding' ) ;
337
+ // biome-ignore lint/style/noNonNullAssertion: Default value provided by VSCode
338
+ const port = config . get < number > ( 'port' ) ! ;
339
+ competitiveCompanionServer . listen ( port ) ;
300
340
}
301
341
302
- function addActiveStatus ( context : vscode . ExtensionContext ) : void {
303
- const statusItem = vscode . window . createStatusBarItem (
304
- 'fastolympiccoding.active' ,
342
+ function stopCompetitiveCompanion ( ) {
343
+ if ( competitiveCompanionServer === undefined ) {
344
+ return ;
345
+ }
346
+
347
+ competitiveCompanionServer . close ( ) ;
348
+ }
349
+
350
+ function createCompetitiveCompanionStatus ( context : vscode . ExtensionContext ) {
351
+ const competitiveCompanionStatus = vscode . window . createStatusBarItem (
352
+ 'fastolympiccoding.listeningForCompetitiveCompanion' ,
305
353
vscode . StatusBarAlignment . Left ,
306
354
) ;
307
- statusItem . name = 'Fast Olympic Coding Indicator' ;
308
- statusItem . text = '$(zap)' ;
309
- statusItem . tooltip = 'Fast Olympic Coding is Active' ;
310
- statusItem . show ( ) ;
311
- context . subscriptions . push ( statusItem ) ;
355
+ competitiveCompanionStatus . name = 'Competitive Companion Indicator' ;
356
+ competitiveCompanionStatus . text = '$(zap)' ;
357
+ competitiveCompanionStatus . tooltip = 'Listening For Competitive Companion' ;
358
+ competitiveCompanionStatus . hide ( ) ;
359
+ context . subscriptions . push ( competitiveCompanionStatus ) ;
360
+
361
+ return competitiveCompanionStatus ;
312
362
}
313
363
314
364
export function activate ( context : vscode . ExtensionContext ) : void {
315
365
registerViewProviders ( context ) ;
316
366
registerCommands ( context ) ;
317
367
registerDocumentContentProviders ( context ) ;
318
368
listenForCompetitiveCompanion ( ) ;
319
- addActiveStatus ( context ) ;
369
+
370
+ competitiveCompanionStatusItem = createCompetitiveCompanionStatus ( context ) ;
320
371
}
0 commit comments