Skip to content

Commit 23ea819

Browse files
committed
lazy-load the new autocompleter code on first use
1 parent 22294e5 commit 23ea819

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { Script, createContext, runInContext } from 'vm';
4949
import { installPasteSupport } from './repl-paste-support';
5050
import util from 'util';
5151

52-
import { MongoDBAutocompleter } from '@mongodb-js/mongodb-ts-autocomplete';
52+
import type { MongoDBAutocompleter } from '@mongodb-js/mongodb-ts-autocomplete';
5353

5454
declare const __non_webpack_require__: any;
5555

@@ -439,21 +439,21 @@ class MongoshNodeRepl implements EvaluationListener {
439439
this.outputFinishString += installPasteSupport(repl);
440440

441441
const origReplCompleter = promisify(repl.completer.bind(repl)); // repl.completer is callback-style
442-
let newMongoshCompleter: MongoDBAutocompleter;
442+
443+
let newMongoshCompleter: MongoDBAutocompleter | undefined;
443444
let oldMongoshCompleter: (
444445
line: string
445446
) => Promise<[string[], string, 'exclusive'] | [string[], string]>;
447+
446448
if (process.env.USE_NEW_AUTOCOMPLETE) {
447-
const autocompletionContext = instanceState.getAutocompletionContext();
448-
newMongoshCompleter = new MongoDBAutocompleter({
449-
context: autocompletionContext,
450-
});
449+
// we will lazily instantiate the new autocompleter on first use
451450
} else {
452451
oldMongoshCompleter = completer.bind(
453452
null,
454453
instanceState.getAutocompleteParameters()
455454
);
456455
}
456+
457457
const innerCompleter = async (
458458
text: string
459459
): Promise<[string[], string]> => {
@@ -468,6 +468,20 @@ class MongoshNodeRepl implements EvaluationListener {
468468
})(),
469469
(async () => {
470470
if (process.env.USE_NEW_AUTOCOMPLETE) {
471+
if (!newMongoshCompleter) {
472+
// only import the autocompleter code the first time we need it to
473+
// hide the time it takes from the initial startup time
474+
const { MongoDBAutocompleter } = await import(
475+
'@mongodb-js/mongodb-ts-autocomplete'
476+
);
477+
478+
const autocompletionContext =
479+
instanceState.getAutocompletionContext();
480+
newMongoshCompleter = new MongoDBAutocompleter({
481+
context: autocompletionContext,
482+
});
483+
}
484+
471485
const results = await newMongoshCompleter.autocomplete(text);
472486
const transformed = transformAutocompleteResults(text, results);
473487
return transformed;

0 commit comments

Comments
 (0)