UNPKG

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

Version:

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

102 lines 4.18 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 */ /* tslint:disable:no-empty-interface */ import { BufferlessResilienceHandlerFactory } from './request-factory/resilience/websocket-resilience-handler-bufferless'; import { StandardResilienceHandlerFactory } from './request-factory/resilience/websocket-resilience-handler-standard'; import { NoopWebSocketStateHandler } from './request-factory/resilience/websocket-resilience-interfaces'; import { WebSocketRequestSenderFactory } from './request-factory/websocket-request-sender'; import { WebSocketRequestHandler } from './request-factory/websocket-request-handler'; import { DefaultDittoWebSocketClient } from './ditto-client-websocket'; import { ImmutableURL } from '../auth/auth-provider'; import { Channel } from '../model/ditto-protocol'; import { AbstractBuilder } from './builder-steps'; /** * Implementation of all the methods to build a Context. */ export class WebSocketClientBuilder extends AbstractBuilder { constructor(builder) { super(); this.builder = builder; this.customHandles = {}; this.stateHandler = new NoopWebSocketStateHandler(); } /** * Creates a new WebSocket Builder. * * @param urlBuilder The url builder used by the builder. */ static newBuilder(urlBuilder) { return new WebSocketClientBuilder(urlBuilder); } finalize() { return this; } withStateHandler(handler) { this.stateHandler = handler; return this; } withBuffer(size) { this.resilienceFactory = StandardResilienceHandlerFactory.getInstance(size); return this; } withoutBuffer() { this.resilienceFactory = BufferlessResilienceHandlerFactory.getInstance(); return this; } liveChannel() { this.channel = Channel.live; return this; } twinChannel() { this.channel = Channel.twin; return this; } withCustomThingsHandle(factory) { this.customHandles = Object.assign(this.customHandles, { thingsHandle: factory }); return this; } withCustomFeaturesHandle(factory) { this.customHandles = Object.assign(this.customHandles, { featuresHandle: factory }); return this; } withCustomMessagesHandle(factory) { this.customHandles = Object.assign(this.customHandles, { messagesHandle: factory }); return this; } withCustomEventsHandle(factory) { this.customHandles = Object.assign(this.customHandles, { eventsHandle: factory }); return this; } withCustomCommandsHandle(factory) { this.customHandles = Object.assign(this.customHandles, { commandsHandle: factory }); return this; } build() { const protocol = this.tls ? 'wss' : 'ws'; const path = (this.customPath === undefined) ? '/ws' : this.customPath; const resilienceHandlerFactory = this.resilienceFactory.withContext(this.builder.withConnectionDetails(ImmutableURL.newInstance(protocol, this.domain, `${path}/${this.apiVersion}`), this.authProviders), this.stateHandler); const requester = new WebSocketRequestHandler(resilienceHandlerFactory); return DefaultDittoWebSocketClient.getInstance(new WebSocketRequestSenderFactory(this.apiVersion, this.channel, requester), requester, this.customHandles); } buildClient(tls, domain, apiVersion, stateHandler, channel, authProviders) { this.tls = tls; this.domain = domain; this.apiVersion = apiVersion; this.stateHandler = stateHandler; this.channel = channel; this.resilienceFactory = BufferlessResilienceHandlerFactory.getInstance(); this.authProviders = authProviders; return this.build(); } } //# sourceMappingURL=websocket-client-builder.js.map