Skip to content

Commit baaf657

Browse files
authored
Use await with terminate to work with wsl node version (#132)
1 parent a92380b commit baaf657

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

src/language/mongoDBService.ts

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default class MongoDBService {
2828
_cachedFields: object;
2929
_cachedDatabases: [];
3030
_cachedCollections: object;
31-
_cachedShellSymbols: object;
31+
_cachedShellSymbols: any;
3232
_extensionPath?: string;
3333
_visitor: Visitor;
3434

@@ -185,21 +185,21 @@ export default class MongoDBService {
185185
worker.postMessage(ServerCommands.EXECUTE_ALL_FROM_PLAYGROUND);
186186

187187
// Listen for results from the worker thread.
188-
worker.on('message', ([error, result]) => {
188+
worker.on('message', async ([error, result]) => {
189189
if (error) {
190190
this._connection.console.log(
191191
`MONGOSH execute all error: ${util.inspect(error)}`
192192
);
193193
this._connection.sendNotification('showErrorMessage', error.message);
194194
}
195195

196-
worker.terminate().then(() => {
197-
return resolve(result);
198-
});
196+
await worker.terminate();
197+
198+
return resolve(result);
199199
});
200200

201201
// Listen for cancellation request from the language server client.
202-
token.onCancellationRequested(() => {
202+
token.onCancellationRequested(async () => {
203203
this._connection.console.log('PLAYGROUND cancellation requested');
204204
this._connection.sendNotification(
205205
'showInformationMessage',
@@ -221,13 +221,9 @@ export default class MongoDBService {
221221

222222
// Stop the worker and all JavaScript execution
223223
// in the worker thread as soon as possible.
224-
worker.terminate().then((status) => {
225-
this._connection.console.log(
226-
`PLAYGROUND canceled with status: ${status}`
227-
);
224+
await worker.terminate();
228225

229-
return resolve([]);
230-
});
226+
return resolve([]);
231227
});
232228
});
233229
}
@@ -247,19 +243,19 @@ export default class MongoDBService {
247243
this._connection.console.log('MONGOSH get list databases...');
248244
worker.postMessage(ServerCommands.GET_LIST_DATABASES);
249245

250-
worker.on('message', ([error, result]) => {
246+
worker.on('message', async ([error, result]) => {
251247
if (error) {
252248
this._connection.console.log(
253249
`MONGOSH get list databases error: ${util.inspect(error)}`
254250
);
255251
}
256252

257-
worker.terminate().then(() => {
258-
this._connection.console.log(
259-
`MONGOSH found ${result.length} databases`
260-
);
261-
this.updateCurrentSessionDatabases(result);
262-
});
253+
await worker.terminate();
254+
255+
this._connection.console.log(
256+
`MONGOSH found ${result.length} databases`
257+
);
258+
this.updateCurrentSessionDatabases(result);
263259
});
264260
}
265261

@@ -279,21 +275,21 @@ export default class MongoDBService {
279275
this._connection.console.log('MONGOSH get list collections...');
280276
worker.postMessage(ServerCommands.GET_LIST_COLLECTIONS);
281277

282-
worker.on('message', ([error, result]) => {
278+
worker.on('message', async ([error, result]) => {
283279
if (error) {
284280
this._connection.console.log(
285281
`MONGOSH get list collections error: ${util.inspect(error)}`
286282
);
287283
}
288284

289-
worker.terminate().then(() => {
290-
this._connection.console.log(
291-
`MONGOSH found ${result.length} collections`
292-
);
293-
this.updateCurrentSessionCollections(databaseName, result);
285+
await worker.terminate();
294286

295-
return resolve(true);
296-
});
287+
this._connection.console.log(
288+
`MONGOSH found ${result.length} collections`
289+
);
290+
this.updateCurrentSessionCollections(databaseName, result);
291+
292+
return resolve(true);
297293
});
298294
});
299295
}
@@ -319,17 +315,17 @@ export default class MongoDBService {
319315
this._connection.console.log(`SCHEMA for namespace: "${namespace}"`);
320316
worker.postMessage(ServerCommands.GET_FIELDS_FROM_SCHEMA);
321317

322-
worker.on('message', ([error, fields]) => {
318+
worker.on('message', async ([error, fields]) => {
323319
if (error) {
324320
this._connection.console.log(`SCHEMA error: ${util.inspect(error)}`);
325321
}
326322

327-
worker.terminate().then(() => {
328-
this._connection.console.log(`SCHEMA found ${fields.length} fields`);
329-
this.updateCurrentSessionFields(namespace, fields);
323+
await worker.terminate();
324+
325+
this._connection.console.log(`SCHEMA found ${fields.length} fields`);
326+
this.updateCurrentSessionFields(namespace, fields);
330327

331-
return resolve(true);
332-
});
328+
return resolve(true);
333329
});
334330
});
335331
}
@@ -353,7 +349,7 @@ export default class MongoDBService {
353349
// ------ COMPLETION ------ //
354350

355351
// Check if string is a valid property name
356-
private isValidPropertyName(str) {
352+
private isValidPropertyName(str): boolean {
357353
return /^(?![0-9])[a-zA-Z0-9$_]+$/.test(str);
358354
}
359355

@@ -409,6 +405,7 @@ export default class MongoDBService {
409405
textFromEditor: string,
410406
position: { line: number; character: number }
411407
): Promise<[]> {
408+
// eslint-disable-next-line complexity
412409
return new Promise(async (resolve) => {
413410
this._connection.console.log(
414411
`LS text from editor: ${util.inspect(textFromEditor)}`
@@ -449,27 +446,27 @@ export default class MongoDBService {
449446
'VISITOR found shell collection methods completion'
450447
);
451448

452-
return resolve(this._cachedShellSymbols['Collection']);
449+
return resolve(this._cachedShellSymbols.Collection);
453450
}
454451

455452
if (state.isAggregationCursor) {
456453
this._connection.console.log(
457454
'VISITOR found shell aggregation cursor methods completion'
458455
);
459456

460-
return resolve(this._cachedShellSymbols['AggregationCursor']);
457+
return resolve(this._cachedShellSymbols.AggregationCursor);
461458
}
462459

463460
if (state.isFindCursor) {
464461
this._connection.console.log(
465462
'VISITOR found shell cursor methods completion'
466463
);
467464

468-
return resolve(this._cachedShellSymbols['Cursor']);
465+
return resolve(this._cachedShellSymbols.Cursor);
469466
}
470467

471468
if (state.isDbCallExpression) {
472-
let dbCompletions: any = [...this._cachedShellSymbols['Database']];
469+
let dbCompletions: any = [...this._cachedShellSymbols.Database];
473470

474471
if (state.databaseName) {
475472
this._connection.console.log(
@@ -561,7 +558,7 @@ export default class MongoDBService {
561558
return [];
562559
}
563560

564-
public clearCurrentSessionConnection() {
561+
public clearCurrentSessionConnection(): void {
565562
this._serviceProvider = undefined;
566563
this._runtime = undefined;
567564
this._connectionString = undefined;

0 commit comments

Comments
 (0)