UNPKG

raas-client

Version:
65 lines 3.47 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const raas_core_1 = require("raas-core"); const common_1 = require("./common"); class ServerModel { constructor(connection, emitter) { this.connection = connection; this.emitter = emitter; this.listenToServerChanges(); } listenToServerChanges() { this.connection.onNotification(raas_core_1.Messages.Client.ServerAddedNotification.type, handle => { this.emitter.emit('serverAdded', handle); }); this.connection.onNotification(raas_core_1.Messages.Client.ServerDeletedNotification.type, handle => { this.emitter.emit('serverRemoved', handle); }); } createServerFromConfigAsync(config, timeout = 2000) { return __awaiter(this, void 0, void 0, function* () { return common_1.Common.sendSimpleRequest(this.connection, raas_core_1.Messages.Server.CancelServerInstallationRequest.type, config, timeout, common_1.ErrorMessages.CREATESERVER_TIMEOUT); }); } createServerFromConfig(config, timeout = 2000) { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { const timer = setTimeout(() => { return reject(new Error(common_1.ErrorMessages.CREATESERVER_TIMEOUT)); }, timeout); let result; const listener = (handle) => { 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(raas_core_1.Messages.Server.AddServerRequest.type, config); })); } deleteServerSync(serverHandle, timeout = 2000) { const listener = (param) => { return param.id === serverHandle.id; }; return common_1.Common.sendRequestSync(this.connection, raas_core_1.Messages.Server.DeleteServerRequest.type, serverHandle, this.emitter, 'serverRemoved', listener, timeout, common_1.ErrorMessages.DELETESERVER_TIMEOUT); } deleteServerAsync(serverHandle, timeout = 2000) { return common_1.Common.sendSimpleRequest(this.connection, raas_core_1.Messages.Server.DeleteServerRequest.type, serverHandle, timeout, common_1.ErrorMessages.DELETESERVER_TIMEOUT); } getServerHandles(timeout = 2000) { return common_1.Common.sendSimpleRequest(this.connection, raas_core_1.Messages.Server.GetServersRequest.type, null, timeout, common_1.ErrorMessages.GETSERVERS_TIMEOUT); } } exports.ServerModel = ServerModel; //# sourceMappingURL=serverModel.js.map