From 34425ad857528d17e939860ad544c8fe2fcb02aa Mon Sep 17 00:00:00 2001 From: Alena Khineika Date: Wed, 17 Nov 2021 21:55:07 +0100 Subject: [PATCH] refactor: replace temp variables with query functions --- src/language/worker.ts | 72 +++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/src/language/worker.ts b/src/language/worker.ts index aa2734615..f23e6f834 100644 --- a/src/language/worker.ts +++ b/src/language/worker.ts @@ -3,21 +3,39 @@ import { MongoClientOptions } from '@mongosh/service-provider-server'; import { CompletionItemKind } from 'vscode-languageserver/node'; -import { EJSON } from 'bson'; +import { EJSON, Document } from 'bson'; import { ElectronRuntime } from '@mongosh/browser-runtime-electron'; import parseSchema = require('mongodb-schema'); import { parentPort, workerData } from 'worker_threads'; - -import type { PlaygroundResult, PlaygroundDebug, ShellExecuteAllResult } from '../types/playgroundType'; +import { PlaygroundResult, PlaygroundDebug, ShellExecuteAllResult } from '../types/playgroundType'; import { ServerCommands } from './serverCommands'; -type EvaluationResult = { +interface EvaluationResult { printable: any; type: string | null; -}; +} + type WorkerResult = ShellExecuteAllResult; type WorkerError = any | null; +const content = ({ type, printable }: EvaluationResult) => { + if (type === 'Cursor' || type === 'AggregationCursor') { + return JSON.parse(EJSON.stringify(printable.documents)); + } + + return typeof printable === 'string' + ? printable + : JSON.parse(EJSON.stringify(printable)); +}; + +const language = (evaluationResult: EvaluationResult) => { + if (typeof content(evaluationResult) === 'object' && content(evaluationResult) !== null) { + return 'json'; + } + + return 'plaintext'; +}; + const executeAll = async ( codeToEvaluate: string, connectionString: string, @@ -32,10 +50,11 @@ const executeAll = async ( connectionString, connectionOptions ); - const outputLines: PlaygroundDebug = []; + // Create a new instance of the runtime and evaluate code from a playground. const runtime: ElectronRuntime = new ElectronRuntime(serviceProvider); + runtime.setEvaluationListener({ onPrint(values: EvaluationResult[]) { for (const { type, printable } of values) { @@ -49,25 +68,14 @@ const executeAll = async ( } }); const { source, type, printable } = await runtime.evaluate(codeToEvaluate); - const namespace = - source && source.namespace - ? `${source.namespace.db}.${source.namespace.collection}` - : null; - let content = ''; - - if (type === 'Cursor' || type === 'AggregationCursor') { - content = JSON.parse(EJSON.stringify(printable.documents)); - } else { - content = typeof printable === 'string' - ? printable - : JSON.parse(EJSON.stringify(printable)); - } - + const namespace = (source && source.namespace) + ? `${source.namespace.db}.${source.namespace.collection}` + : null; const result: PlaygroundResult = { namespace, type: type ? type : typeof printable, - content, - language: (typeof content === 'object' && content !== null) ? 'json' : 'plaintext' + content: content({ type, printable }), + language: language({ type, printable }) }; return [null, { outputLines, result }]; @@ -133,6 +141,17 @@ const getFieldsFromSchema = async ( } }; +const prepareCompletionItems = (result: Document) => { + if (!result) { + return []; + } + + return result.databases.map((item) => ({ + label: item.name, + kind: CompletionItemKind.Value + })); +}; + const getListDatabases = async ( connectionString: string, connectionOptions: any @@ -145,14 +164,9 @@ const getListDatabases = async ( // TODO: There is a mistake in the service provider interface // Use `admin` as arguments to get list of dbs - // and remove it later when `mongosh` will merge a fix + // and remove it later when `mongosh` will merge a fix. const result = await serviceProvider.listDatabases('admin'); - const databases = result - ? result.databases.map((item) => ({ - label: item.name, - kind: CompletionItemKind.Value - })) - : []; + const databases = prepareCompletionItems(result); return [null, databases]; } catch (error) {