UNPKG

diffusion

Version:

Diffusion JavaScript client

81 lines (80 loc) 3.12 kB
"use strict"; /** * @module Routing */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TopicRouting = void 0; var logger = require("./../util/logger"); var headers_tunnel_1 = require("./../v4-stack/headers-tunnel"); var message_1 = require("./../v4-stack/message"); var log = logger.create('V4 Client Routing'); /** * Routing the incoming messages to the correct handler */ var TopicRouting = /** @class */ (function () { /** * Create a new TopicRouting instance * * @param errorHandler the error handler * @param serviceAdapter the service adapter that will receive service * requests, responses and errors * @param conversationSet the conversation set that handles fetch replies * @param cache the topic cache that handles topic values and deltas * @param registry the stream registry that is passed on to the topic * {@link cache} */ function TopicRouting(errorHandler, serviceAdapter, conversationSet, cache, registry) { this.errorHandler = errorHandler; this.serviceAdapter = serviceAdapter; this.conversationSet = conversationSet; this.cache = cache; this.registry = registry; } /** * Route a message to the correct handler * * @param message the incoming message that needs to be handled * * @throws a {@link RuntimeError} if a message is received that could not be handled */ TopicRouting.prototype.route = function (message) { switch (message.type) { case message_1.types.FETCH_REPLY: var cid = headers_tunnel_1.cidFromHeaders(message.headers); this.conversationSet.respondIfPresent(cid, message); break; case message_1.types.SERVICE_REQUEST: case message_1.types.SERVICE_RESPONSE: case message_1.types.SERVICE_ERROR: this.serviceAdapter.onMessage(message.type, message.getInputStream()); break; case message_1.types.TOPIC_VALUE: this.cache.handleValue(message.id, message.data, this.registry, this.errorHandler); break; case message_1.types.TOPIC_DELTA: this.cache.handleDelta(message.id, message.data, this.registry, this.errorHandler); break; default: log.debug('Unhandled V4 message: ' + message.type, message); } }; /** * Subscribe to a new topic * * @param info the topic info */ TopicRouting.prototype.subscribe = function (info) { this.cache.handleSubscription(info, this.registry); }; /** * Unsubscribe from a topic * * @param id the topic id to unsubscribe from * @param reason the reason for unsubscribing */ TopicRouting.prototype.unsubscribe = function (id, reason) { this.cache.handleUnsubscription(id, reason, this.registry); }; return TopicRouting; }()); exports.TopicRouting = TopicRouting;