All files / src/util common.ts

51.35% Statements 19/37
50% Branches 2/4
9.09% Functions 1/11
51.35% Lines 19/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68      1x                                                                                     1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x  
import { MessageConnection } from 'vscode-jsonrpc';
import { EventEmitter } from 'events';
 
export class Common {
 
    static sendSimpleRequest(connection: MessageConnection, messageType: any, payload: any, timeout: number, timeoutMessage: string): Promise<any> {
        return new Promise((resolve, reject) => {
            const timer = setTimeout(() => {
                reject(new Error(timeoutMessage));
            }, timeout);
 
            return connection.sendRequest(messageType, payload).then(result => {
                clearTimeout(timer);
                resolve(result);
            });
        });
    }
 
    static sendRequestSync(connection: MessageConnection, messageType: any, payload: any, emitter: EventEmitter,
         eventId: string, listener: (params: any) => boolean, timeout: number, timeoutMessage: string): Promise<any> {
        return new Promise<any>((resolve, reject) => {
            const timer = setTimeout(() => {
                return reject(new Error(timeoutMessage));
            }, timeout);
 
            let response: Thenable<any>;
            const handler = (params: any) => {
                if (listener(params)) {
                    response.then(() => {
                        clearTimeout(timer);
                        emitter.removeListener(eventId, listener);
                        resolve(params);
                    });
                }
            };
 
            emitter.prependListener(eventId, handler);
            response = connection.sendRequest(messageType, payload);
        });
    }
 
    static sendSimpleNotification(connection: MessageConnection, messageType: any, payload: any): void {
        connection.sendNotification(messageType, payload);
    }
}
 
export namespace ErrorMessages {
 
    export const ADDINSTALLABLE_TIMEOUT = 'Failed to add installable server in time';
    export const UPDATEINSTALLABLE_TIMEOUT = 'Failed to update installable server in time';
    export const DELETEINSTALLABLE_TIMEOUT = 'Failed to delete installable server in time';
    export const GETALLINSTALLABLESERVERS_TIMEOUT = 'Failed to retrieve all installable servers in time';
 
    export const ADDINSTALLEDSERVER_TIMEOUT = 'Failed to add installed server in time';
    export const INSTALLSERVER_TIMEOUT = 'Failed to install server in time';
    export const CANCELINSTALLION_TIMEOUT = 'Failed to cancel server installation in time';
    export const DELETEINSTALLION_TIMEOUT = 'Failed to delete server installation in time';
    export const UPDATEINSTALLATION_TIMEOUT = 'Failed to update server installation in time';
    export const GETALLINSTALLEDSERVERS_TIMEOUT = 'Failed to retrieve all installed servers in time';
    export const GETALLINSTALLINGSERVERS_TIMEOUT = 'Failed to retrieve all installing servers in time';
 
    export const STARTSERVER_TIMEOUT = 'Failed to start server in time';
    export const STOPSERVER_TIMEOUT = 'Failed to stop server in time';
    export const STARTANALYSIS_TIMEOUT = 'Failed to start analysis in time';
    export const CANCELANALYSIS_TIMEOUT = 'Failed to cancel analysis in time';
    export const GETALLANALYSES_TIMEOUT = 'Failed to retrieve all analyses in time';
    export const GETALLSERVERHANDLES_TIMEOUT = 'Failed to retrieve all analyses in time';
}