@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
194 lines • 7.47 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2018 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate, __param } from "tslib";
import { inject, injectable, optional, postConstruct, named } from 'inversify';
import { ILogger } from '../common/logger.js';
import { Emitter } from '../common/event.js';
import { DefaultFrontendApplicationContribution } from './frontend-application-contribution.js';
import { StatusBar, StatusBarAlignment } from './status-bar/status-bar.js';
import { Disposable, DisposableCollection, nls } from '../common/index.js';
import { WebSocketConnectionSource } from './messaging/ws-connection-source.js';
/**
* Service for listening on backend connection changes.
*/
export const ConnectionStatusService = Symbol('ConnectionStatusService');
/**
* The connection status.
*/
export var ConnectionStatus;
(function (ConnectionStatus) {
/**
* Connected to the backend.
*/
ConnectionStatus[ConnectionStatus["ONLINE"] = 0] = "ONLINE";
/**
* The connection is lost between frontend and backend.
*/
ConnectionStatus[ConnectionStatus["OFFLINE"] = 1] = "OFFLINE";
})(ConnectionStatus || (ConnectionStatus = {}));
let ConnectionStatusOptions = class ConnectionStatusOptions {
static { this.DEFAULT = {
offlineTimeout: 5000,
}; }
};
ConnectionStatusOptions = __decorate([
injectable()
], ConnectionStatusOptions);
export { ConnectionStatusOptions };
export const PingService = Symbol('PingService');
let AbstractConnectionStatusService = class AbstractConnectionStatusService {
constructor(options = ConnectionStatusOptions.DEFAULT) {
this.options = options;
this.statusChangeEmitter = new Emitter();
this.connectionStatus = ConnectionStatus.ONLINE;
}
get onStatusChange() {
return this.statusChangeEmitter.event;
}
get currentStatus() {
return this.connectionStatus;
}
dispose() {
this.statusChangeEmitter.dispose();
}
updateStatus(success) {
const previousStatus = this.connectionStatus;
const newStatus = success ? ConnectionStatus.ONLINE : ConnectionStatus.OFFLINE;
if (previousStatus !== newStatus) {
this.connectionStatus = newStatus;
this.fireStatusChange(newStatus);
}
}
fireStatusChange(status) {
this.statusChangeEmitter.fire(status);
}
};
__decorate([
inject(ILogger),
named('core:AbstractConnectionStatusService')
], AbstractConnectionStatusService.prototype, "logger", void 0);
AbstractConnectionStatusService = __decorate([
injectable(),
__param(0, inject(ConnectionStatusOptions)),
__param(0, optional())
], AbstractConnectionStatusService);
export { AbstractConnectionStatusService };
let FrontendConnectionStatusService = class FrontendConnectionStatusService extends AbstractConnectionStatusService {
init() {
this.wsConnectionProvider.onSocketDidOpen(() => {
this.updateStatus(true);
this.schedulePing();
});
this.wsConnectionProvider.onSocketDidClose(() => {
this.clearTimeout(this.scheduledPing);
this.updateStatus(false);
});
this.wsConnectionProvider.onIncomingMessageActivity(() => {
// natural activity
this.updateStatus(true);
this.schedulePing();
});
}
schedulePing() {
this.clearTimeout(this.scheduledPing);
this.scheduledPing = this.setTimeout(async () => {
await this.performPingRequest();
this.schedulePing();
}, this.options.offlineTimeout);
}
async performPingRequest() {
try {
await this.pingService.ping();
this.updateStatus(true);
}
catch (e) {
this.updateStatus(false);
await this.logger.error(e);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setTimeout(handler, timeout) {
return window.setTimeout(handler, timeout);
}
clearTimeout(handle) {
if (handle !== undefined) {
window.clearTimeout(handle);
}
}
};
__decorate([
inject(WebSocketConnectionSource)
], FrontendConnectionStatusService.prototype, "wsConnectionProvider", void 0);
__decorate([
inject(PingService)
], FrontendConnectionStatusService.prototype, "pingService", void 0);
__decorate([
inject(ILogger),
named('core:FrontendConnectionStatusService')
], FrontendConnectionStatusService.prototype, "logger", void 0);
__decorate([
postConstruct()
], FrontendConnectionStatusService.prototype, "init", null);
FrontendConnectionStatusService = __decorate([
injectable()
], FrontendConnectionStatusService);
export { FrontendConnectionStatusService };
let ApplicationConnectionStatusContribution = class ApplicationConnectionStatusContribution extends DefaultFrontendApplicationContribution {
constructor(connectionStatusService, statusBar, logger) {
super();
this.connectionStatusService = connectionStatusService;
this.statusBar = statusBar;
this.logger = logger;
this.toDisposeOnOnline = new DisposableCollection();
this.statusbarId = 'connection-status';
this.connectionStatusService.onStatusChange(state => this.onStateChange(state));
}
onStateChange(state) {
switch (state) {
case ConnectionStatus.OFFLINE: {
this.handleOffline();
break;
}
case ConnectionStatus.ONLINE: {
this.handleOnline();
break;
}
}
}
handleOnline() {
this.toDisposeOnOnline.dispose();
}
handleOffline() {
this.statusBar.setElement(this.statusbarId, {
alignment: StatusBarAlignment.LEFT,
text: nls.localizeByDefault('Offline'),
tooltip: nls.localize('theia/localize/offlineTooltip', 'Cannot connect to backend.'),
priority: 5000
});
this.toDisposeOnOnline.push(Disposable.create(() => this.statusBar.removeElement(this.statusbarId)));
document.body.classList.add('theia-mod-offline');
this.toDisposeOnOnline.push(Disposable.create(() => document.body.classList.remove('theia-mod-offline')));
}
};
ApplicationConnectionStatusContribution = __decorate([
injectable(),
__param(0, inject(ConnectionStatusService)),
__param(1, inject(StatusBar)),
__param(2, inject(ILogger)),
__param(2, named('core:ApplicationConnectionStatusContribution'))
], ApplicationConnectionStatusContribution);
export { ApplicationConnectionStatusContribution };
//# sourceMappingURL=connection-status-service.js.map