UNPKG

@eclipse-ditto/ditto-javascript-client-dom

Version:

DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.

102 lines 3.71 kB
/*! * Copyright (c) 2019 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0 * * SPDX-License-Identifier: EPL-2.0 */ import { AbstractResilienceHandler, connectionLostError, connectionUnavailableError, ResilienceHandlerFactory } from './websocket-resilience-interfaces'; /** * An implementation of ResilienceHandler without buffering. */ export class BufferlessResilienceHandler extends AbstractResilienceHandler { constructor(webSocketBuilder, stateHandler, requestHandler) { super(stateHandler, requestHandler); this.requests = new Map(); this.resolveWebSocket(webSocketBuilder.withHandler(this)); } sendRequest(id, request) { if (this.stateHandler.isConnected()) { const jsonified = request.toJson(); this.requests.set(id, jsonified); this.webSocket.executeCommand(jsonified); } else { this.handleFailure(id, this.connectionProblemRejectionReason()); } } send(message) { if (this.stateHandler.isConnected()) { this.webSocket.executeCommand(message); return Promise.resolve(); } return Promise.reject(this.connectionProblemRejectionReason()); } handleResponse(correlationId, response) { this.requests.delete(correlationId); this.requestHandler.handleInput(correlationId, response); } handleFailure(id, reason) { this.requests.delete(id); this.requestHandler.handleError(id, reason); } /** * Returns the reason to reject a Promise based on the current state of the connection. * * @returns The error to reject with */ connectionProblemRejectionReason() { return this.stateHandler.isWorking() ? connectionUnavailableError : connectionLostError; } /** * Handles the promise for a new web socket. Once the Promise is resolved it will be set as the web socket for the resilience handler. * If the Promise gets rejected the reconnection process will be stopped and all further requests rejected. * * @param promise - The promise for the new web socket */ resolveWebSocket(promise) { promise .then(socket => { this.webSocket = socket; this.stateHandler.connected(); }, error => { this.stateHandler.disconnected(); this.rejectAllOngoing(connectionLostError); throw error; }); } /** * Rejects all ongoing requests. * * @param reason - The reason to reject the requests with */ rejectAllOngoing(reason) { const ongoingRequests = new Map(this.requests); ongoingRequests.forEach((_, id) => this.handleFailure(id, reason)); } } /** * A Factory for a BufferlessResilienceHandler. */ export class BufferlessResilienceHandlerFactory extends ResilienceHandlerFactory { constructor() { super(); } /** * Provides an instance of BufferlessResilienceHandlerFactory. * * @returns The instance of BufferlessResilienceHandlerFactory */ static getInstance() { return new BufferlessResilienceHandlerFactory(); } withRequestHandler(requestHandler) { return new BufferlessResilienceHandler(this.webSocketBuilder, this.stateHandler, requestHandler); } } //# sourceMappingURL=websocket-resilience-handler-bufferless.js.map