Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions workspace-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class WorkspaceLoader {
}

load(): Promise<void> {
let workspaceKey = this.getWorkspaceKey();
const workspaceKey = this.getWorkspaceKey();

if (!workspaceKey || workspaceKey === "") {
console.error("Workspace is not defined");
Expand All @@ -165,15 +165,15 @@ export class WorkspaceLoader {
* Returns workspace key from current address or empty string when it is undefined.
*/
getWorkspaceKey(): string {
let result: string = window.location.pathname.substr(1);
const result: string = window.location.pathname.substr(1);
return result.substr(result.indexOf('/') + 1, result.length);
}

/**
* Returns base websocket URL.
*/
websocketBaseURL(): string {
let wsProtocol = 'http:' === document.location.protocol ? 'ws' : 'wss';
const wsProtocol = 'http:' === document.location.protocol ? 'ws' : 'wss';
return wsProtocol + '://' + document.location.host;
}

Expand Down Expand Up @@ -298,8 +298,8 @@ export class WorkspaceLoader {

connectMasterApi(): Promise<CheJsonRpcMasterApi> {
return new Promise((resolve, reject) => {
const entryPoint = this.websocketBaseURL() + WEBSOCKET_CONTEXT + this.getAuthenticationToken();
const master = new CheJsonRpcMasterApi(new WebsocketClient(), entryPoint);
const entryPoint = this.websocketBaseURL() + WEBSOCKET_CONTEXT;
const master = new CheJsonRpcMasterApi(new WebsocketClient(), entryPoint, this);
master.connect(entryPoint)
.then(() => resolve(master))
.catch((error: any) => reject(error));
Expand Down Expand Up @@ -347,11 +347,11 @@ export class WorkspaceLoader {
*/
openIDE() : void {
this.getWorkspace(this.workspace.id).then((workspace) => {
let machines = workspace.runtime.machines;
for (let machineName in machines) {
let servers = machines[machineName].servers;
for (let serverId in servers) {
let attributes = servers[serverId].attributes;
const machines = workspace.runtime.machines;
for (const machineName in machines) {
const servers = machines[machineName].servers;
for (const serverId in servers) {
const attributes = servers[serverId].attributes;
if (attributes['type'] === 'ide') {
this.openURL(servers[serverId].url + this.getQueryString());
return;
Expand Down
24 changes: 14 additions & 10 deletions workspace-loader/src/json-rpc/che-json-rpc-master-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'use strict';
import {CheJsonRpcApiClient} from './che-json-rpc-api-service';
import { ICommunicationClient, CODE_REQUEST_TIMEOUT, CommunicationClientEvent } from './json-rpc-client';
import { WorkspaceLoader } from '../index';

enum MasterChannels {
ENVIRONMENT_OUTPUT = <any>'runtime/log',
Expand All @@ -36,19 +37,22 @@ export class CheJsonRpcMasterApi {
private fetchingClientIdTimeout = 5000;

private client: ICommunicationClient;
private loader: WorkspaceLoader;

constructor(client: ICommunicationClient,
entryPoint: string) {
entryPoint: string,
loader: WorkspaceLoader) {
this.cheJsonRpcApi = new CheJsonRpcApiClient(client);
this.client = client;
this.loader = loader;

client.addListener('open', () => this.onConnectionOpen());
client.addListener('close', (event: any) => {
switch (event.code) {
case 1000: // normal close
break;
default:
this.connect(entryPoint);
this.connect(entryPoint).catch(console.error);
}
});
}
Expand Down Expand Up @@ -107,6 +111,7 @@ export class CheJsonRpcMasterApi {
* @returns {IPromise<IHttpPromiseCallbackArg<any>>}
*/
connect(entryPoint: string): Promise<any> {
entryPoint += this.loader.getAuthenticationToken();
if (this.clientId) {
let clientId = `clientId=${this.clientId}`;
// in case of reconnection
Expand All @@ -119,9 +124,8 @@ export class CheJsonRpcMasterApi {
}
entryPoint += clientId;
}
return this.cheJsonRpcApi.connect(entryPoint).then(() => {
return this.fetchClientId();
});
return this.cheJsonRpcApi.connect(entryPoint).then(() =>
this.fetchClientId());
}

/**
Expand Down Expand Up @@ -193,7 +197,7 @@ export class CheJsonRpcMasterApi {
* @param callback callback to process event
*/
subscribeWorkspaceStatus(workspaceId: string, callback: Function): void {
let statusHandler = (message: any) => {
const statusHandler = (message: any) => {
if (workspaceId === message.workspaceId) {
callback(message);
}
Expand Down Expand Up @@ -239,8 +243,8 @@ export class CheJsonRpcMasterApi {
* @param callback callback
*/
private subscribe(channel: MasterChannels, workspaceId: string, callback: Function): void {
let method: string = channel.toString();
let params = {method: method, scope: {workspaceId: workspaceId}};
const method: string = channel.toString();
const params = {method: method, scope: {workspaceId: workspaceId}};
this.cheJsonRpcApi.subscribe(SUBSCRIBE, method, callback, params);
}

Expand All @@ -252,8 +256,8 @@ export class CheJsonRpcMasterApi {
* @param callback callback
*/
private unsubscribe(channel: MasterChannels, workspaceId: string, callback: Function): void {
let method: string = channel.toString();
let params = {method: method, scope: {workspaceId: workspaceId}};
const method: string = channel.toString();
const params = {method: method, scope: {workspaceId: workspaceId}};
this.cheJsonRpcApi.unsubscribe(UNSUBSCRIBE, method, callback, params);
}
}
16 changes: 8 additions & 8 deletions workspace-loader/src/json-rpc/json-rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ export class JsonRpcClient {
* @returns {IPromise<any>}
*/
request(method: string, params?: any): Promise<any> {
let deferred = new Deffered();
let id: string = (this.counter++).toString();
const deferred = new Deffered();
const id: string = (this.counter++).toString();
this.pendingRequests.set(id, deferred);

let request: IRequest = {
const request: IRequest = {
jsonrpc: JSON_RPC_VERSION,
id: id,
method: method,
Expand All @@ -125,7 +125,7 @@ export class JsonRpcClient {
* @param params params (optional)
*/
notify(method: string, params?: any): void {
let request: INotification = {
const request: INotification = {
jsonrpc: JSON_RPC_VERSION,
method: method,
params: params
Expand Down Expand Up @@ -158,7 +158,7 @@ export class JsonRpcClient {
* @param handler handler
*/
public removeNotificationHandler(method: string, handler: Function): void {
let handlers = this.notificationHandlers.get(method);
const handlers = this.notificationHandlers.get(method);

if (handlers) {
handlers.splice(handlers.indexOf(handler), 1);
Expand All @@ -184,8 +184,8 @@ export class JsonRpcClient {
* @param message message
*/
private processNotification(message: any): void {
let method = message.method;
let handlers = this.notificationHandlers.get(method);
const method = message.method;
const handlers = this.notificationHandlers.get(method);
if (handlers && handlers.length > 0) {
handlers.forEach((handler: Function) => {
handler(message.params);
Expand All @@ -199,7 +199,7 @@ export class JsonRpcClient {
* @param message
*/
private processResponseMessage(message: any): void {
let promise = this.pendingRequests.get(message.id);
const promise = this.pendingRequests.get(message.id);
if (message.result) {
promise.resolve(message.result);
return;
Expand Down
12 changes: 10 additions & 2 deletions workspace-loader/src/json-rpc/websocket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ export class WebsocketClient implements ICommunicationClient {
}
}

private sleep(ms: number): Promise<any> {
return new Promise<any>(resolve => setTimeout(resolve, ms));
}

/**
* Sends pointed data.
*
* @param data to be sent
*/
send(data: any): void {
this.websocketStream.send(JSON.stringify(data));
async send(data: any): Promise<void> {
while (this.websocketStream.readyState !== this.websocketStream.OPEN) {
/* Wait for the reconnection establshed. */
await this.sleep(1000);
}
return this.websocketStream.send(JSON.stringify(data));
}

private callHandlers(event: CommunicationClientEvent, data?: any): void {
Expand Down