From c7f51d967edade30193b11062c85d736016587cb Mon Sep 17 00:00:00 2001 From: Amir Omidi Date: Mon, 28 Oct 2019 14:57:03 -0700 Subject: [PATCH 1/2] Cleanup the copy output --- src/sql/platform/query/common/gridDataProvider.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/sql/platform/query/common/gridDataProvider.ts b/src/sql/platform/query/common/gridDataProvider.ts index d1969233dfc7..b907e9f1f4be 100644 --- a/src/sql/platform/query/common/gridDataProvider.ts +++ b/src/sql/platform/query/common/gridDataProvider.ts @@ -43,8 +43,8 @@ export interface IGridDataProvider { } export async function getResultsString(provider: IGridDataProvider, selection: Slick.Range[], includeHeaders?: boolean): Promise { - let headers: Map = new Map(); // Maps a column index -> header - let rows: Map> = new Map(); // Maps row index -> column index -> actual row value + let headers: Map = new Map(); // Maps a column index -> header + let rows: Map> = new Map(); // Maps row index -> column index -> actual row value const eol = provider.getEolString(); // create a mapping of the ranges to get promises @@ -95,6 +95,13 @@ export async function getResultsString(provider: IGridDataProvider, selection: S } await p; } + headers = new Map([...headers].sort(([k1], [k2]): number => { + return k1 - k2; + })); + + rows = new Map([...rows].sort(([k1], [k2]): number => { + return k1 - k2; + })); let copyString = ''; if (includeHeaders) { @@ -103,7 +110,6 @@ export async function getResultsString(provider: IGridDataProvider, selection: S const rowKeys = [...headers.keys()].sort(); - rows = new Map([...rows.entries()].sort()); for (let rowEntry of rows) { let rowMap = rowEntry[1]; for (let rowIdx of rowKeys) { From 1be29390586f9aa86d8f2308fcf988e857d1399b Mon Sep 17 00:00:00 2001 From: Amir Omidi Date: Mon, 28 Oct 2019 15:28:02 -0700 Subject: [PATCH 2/2] Use helper functions and cleanup promise handling --- .../platform/query/common/gridDataProvider.ts | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/sql/platform/query/common/gridDataProvider.ts b/src/sql/platform/query/common/gridDataProvider.ts index b907e9f1f4be..c4a71694d47a 100644 --- a/src/sql/platform/query/common/gridDataProvider.ts +++ b/src/sql/platform/query/common/gridDataProvider.ts @@ -48,8 +48,8 @@ export async function getResultsString(provider: IGridDataProvider, selection: S const eol = provider.getEolString(); // create a mapping of the ranges to get promises - let tasks = selection.map((range, i) => { - return async () => { + let tasks: (() => Promise)[] = selection.map((range) => { + return async (): Promise => { let startCol = range.fromCell; let startRow = range.fromRow; @@ -88,28 +88,24 @@ export async function getResultsString(provider: IGridDataProvider, selection: S }; }); - if (tasks.length > 0) { - let p = tasks[0](); - for (let i = 1; i < tasks.length; i++) { - p = p.then(tasks[i]); - } - await p; - } - headers = new Map([...headers].sort(([k1], [k2]): number => { - return k1 - k2; - })); + // Set the tasks gathered above to execute + let actionedTasks: Promise[] = tasks.map(t => { return t(); }); - rows = new Map([...rows].sort(([k1], [k2]): number => { - return k1 - k2; - })); + // Make sure all these tasks have executed + await Promise.all(actionedTasks); + + const sortResults = (e1: [number, any], e2: [number, any]) => { + return e1[0] - e2[0]; + }; + headers = new Map([...headers].sort(sortResults)); + rows = new Map([...rows].sort(sortResults)); let copyString = ''; if (includeHeaders) { copyString = [...headers.values()].join('\t').concat(eol); } - const rowKeys = [...headers.keys()].sort(); - + const rowKeys = [...headers.keys()]; for (let rowEntry of rows) { let rowMap = rowEntry[1]; for (let rowIdx of rowKeys) {