All files / src/util serverModel.ts

89.29% Statements 25/28
100% Branches 7/7
80% Functions 12/15
88.89% Lines 24/27

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 69 70  1x   1x   1x           43x 43x 43x       12x     12x           1x         8x 8x 4x       8x 6x 4x 4x 4x 4x       8x   8x         1x     1x         1x         1x      
import { MessageConnection } from 'vscode-jsonrpc';
import { Protocol, Messages } from 'raas-core';
import { EventEmitter } from 'events';
import { Common, ErrorMessages } from './common';
 
export class ServerModel {
 
    private connection: MessageConnection;
    private emitter: EventEmitter;
 
    constructor(connection: MessageConnection, emitter: EventEmitter) {
        this.connection = connection;
        this.emitter = emitter;
        this.listenToServerChanges();
    }
 
    private listenToServerChanges() {
        this.connection.onNotification(Messages.Client.ServerAddedNotification.type, handle => {
            this.emitter.emit('serverAdded', handle);
        });
        this.connection.onNotification(Messages.Client.ServerRemovedNotification.type, handle => {
            this.emitter.emit('serverRemoved', handle);
        });
    }
 
    async createServerFromConfigAsync(config: Protocol.ServerConfig, timeout: number = 2000): Promise<Protocol.Status> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.CreateServerRequest.type, config,
            timeout, ErrorMessages.CREATESERVER_TIMEOUT);
    }
 
    createServerFromConfig(config: Protocol.ServerConfig, timeout: number = 2000): Promise<Protocol.ServerConfig> {
        return new Promise<Protocol.ServerConfig>(async (resolve, reject) => {
            const timer = setTimeout(() => {
                return reject(new Error(ErrorMessages.CREATESERVER_TIMEOUT));
            }, timeout);
 
            let result: Thenable<Protocol.Status>;
            const listener = (handle: Protocol.ServerConfig) => {
                if (handle.id === config.id) {
                    result.then(status => {
                        clearTimeout(timer);
                        this.emitter.removeListener('serverAdded', listener);
                        resolve(handle);
                    });
                }
            };
            this.emitter.prependListener('serverAdded', listener);
 
            result = this.connection.sendRequest(Messages.Server.CreateServerRequest.type, config);
        });
    }
 
    deleteServerSync(serverHandle: Protocol.ServerConfig, timeout: number = 2000): Promise<Protocol.ServerConfig> {
        const listener = (param: Protocol.ServerConfig) => {
            return param.id === serverHandle.id;
        };
        return Common.sendRequestSync(this.connection, Messages.Server.DeleteServerRequest.type, serverHandle, this.emitter,
            'serverRemoved', listener, timeout, ErrorMessages.DELETESERVER_TIMEOUT);
    }
 
    deleteServerAsync(serverHandle: Protocol.ServerConfig, timeout: number = 2000): Promise<Protocol.Status> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.DeleteServerRequest.type, serverHandle, timeout,
             ErrorMessages.DELETESERVER_TIMEOUT);
    }
 
    getServerHandles(timeout: number = 2000): Promise<Protocol.ServerConfig[]> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.GetServerHandlesRequest.type, null,
             timeout, ErrorMessages.GETSERVERS_TIMEOUT);
    }
}