import { Connection } from '@salesforce/core' ; import { WorkspaceContextUtil } from '@salesforce/salesforcedx-utils-vscode/out/src' ; import { ChannelService } from '@salesforce/salesforcedx-utils-vscode/out/src/commands' ; import * as debounce from 'debounce' ; import { DescribeSObjectResult } from 'jsforce' ; import * as vscode from 'vscode' ; import { nls } from './messages' ; import { telemetryService } from './telemetry' ; export const channelService = ChannelService . getInstance ( nls . localize ( 'soql_channel_name' ) ) ; export const workspaceContext = WorkspaceContextUtil . getInstance ( ) ; function showChannelAndErrorMessage ( e ) { channelService . appendLine ( e ) ; const message = nls . localize ( 'error_connection' ) ; vscode . window . showErrorMessage ( message ) ; telemetryService . sendException ( 'soql_sf_connection_error' , e . message ) ; } export const debouncedShowChannelAndErrorMessage = debounce ( showChannelAndErrorMessage , 1000 ) ; export async function withSFConnection ( f ) { try { const conn = await workspaceContext . getConnection ( ) ; return f ( ( conn as unknown ) as Connection ) ; } catch ( e ) { debouncedShowChannelAndErrorMessage ( e ) ; } } export async function retrieveSObjects ( ) { return new Promise < string [ ] > ( ( resolve , reject ) => { return withSFConnection ( async ( conn ) => { conn . describeGlobal$ ( ( err , describeGlobalResult ) => { if ( err ) { reject ( err ) ; } else if ( describeGlobalResult ) { const sobjectNames = describeGlobalResult . sobjects . filter ( o => o . queryable ) . map ( o => o . name ) ; resolve ( sobjectNames ) ; } else { resolve ( [ ] ) ; } } ) ; } ) ; } ) ; } export async function retrieveSObject ( sobjectName ) { return new Promise < DescribeSObjectResult > ( ( resolve , reject ) => { return withSFConnection ( async ( conn ) => { conn . describe$ ( sobjectName , ( err , sobject ) => { if ( err ) { reject ( err ) ; } else { resolve ( sobject ) ; } } ) ; } ) ; } ) ; } workspaceContext . onOrgChange ( async ( orgInfo ) => { await withSFConnection ( conn => { conn . describeGlobal$ . clear ( ) ; conn . describe$ . clear ( ) ; } ) ; } ) ; export function onOrgChange ( f ) { workspaceContext . onOrgChange ( f ) ; } export function isDefaultOrgSet ( ) { return ! ! workspaceContext . username ; }