UNPKG

diffusion

Version:

Diffusion JavaScript client

161 lines (160 loc) 7.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkerStreamRegistryAdapter = exports.WorkerValueStreamResponder = void 0; var conversation_id_1 = require("./../../conversation/conversation-id"); var datatypes_1 = require("./../../data/datatypes"); var buffer_output_stream_1 = require("./../../io/buffer-output-stream"); var TopicSpecificationSerialiser = require("./../../topics/details/topic-specification-serialiser"); var selector_set_1 = require("./../../topics/selector-set"); var topic_selector_parser_1 = require("./../../topics/topic-selector-parser"); var uint8array_1 = require("./../../util/uint8array"); var worker_value_stream_proxy_1 = require("./../../webworker/topics/worker-value-stream-proxy"); var worker_command_1 = require("./../../webworker/worker-command"); var error_reason_1 = require("../../../errors/error-reason"); /** * An adapter that listens to value stream events in the shared worker and sends the events to the * registry proxy in the tab */ var WorkerValueStreamResponder = /** @class */ (function () { function WorkerValueStreamResponder(registry, selector, datatype, fallback, respond, adapterId) { this.respond = respond; this.adapterId = adapterId; this.valueStream = new worker_value_stream_proxy_1.WorkerValueStreamProxy(registry, selector, datatype, fallback); this.valueStream.subscription.on({ open: this.onOpen.bind(this), value: this.onValue.bind(this), subscribe: this.onSubscription.bind(this), unsubscribe: this.onUnsubscription.bind(this), error: this.onSubscriptionError.bind(this) }); } /** * Handle the open event */ WorkerValueStreamResponder.prototype.onOpen = function () { this.respond(this.adapterId, worker_command_1.WorkerStreamCommand.OPEN); }; /** * Handle a value event * * @param path the topic path * @param specification the topic specification * @param oldContent the buffer containing the old content * @param content the content that was received * @param oldValue the old value * @param newValue the new value */ WorkerValueStreamResponder.prototype.onValue = function (path, specification, newValue, oldValue) { var bos = new buffer_output_stream_1.BufferOutputStream(); TopicSpecificationSerialiser.write(bos, specification); var response = { path: path, specification: bos.getBase64(), newValue: uint8array_1.uint8ToBase64(newValue), oldValue: oldValue ? uint8array_1.uint8ToBase64(oldValue) : null }; this.respond(this.adapterId, worker_command_1.WorkerStreamCommand.VALUE, undefined, JSON.stringify(response)); }; /** * Handle a subscription event * * @param path the topic path * @param specification the topic specification */ WorkerValueStreamResponder.prototype.onSubscription = function (path, specification) { var bos = new buffer_output_stream_1.BufferOutputStream(); TopicSpecificationSerialiser.write(bos, specification); var response = { path: path, specification: bos.getBase64() }; this.respond(this.adapterId, worker_command_1.WorkerStreamCommand.SUBSCRIPTION, undefined, JSON.stringify(response)); }; /** * Handle a subscription event * * @param path the topic path * @param specification the topic specification * @param reason the reason for unsubscribing */ WorkerValueStreamResponder.prototype.onUnsubscription = function (path, specification, reason) { var bos = new buffer_output_stream_1.BufferOutputStream(); TopicSpecificationSerialiser.write(bos, specification); var response = { path: path, specification: bos.getBase64(), // eslint-disable-next-line deprecation/deprecation reason: reason.id }; this.respond(this.adapterId, worker_command_1.WorkerStreamCommand.UNSUBSCRIPTION, undefined, JSON.stringify(response)); }; /** * Handle a subscription error * * @param error the error */ WorkerValueStreamResponder.prototype.onSubscriptionError = function (error) { if (error && error.id && error_reason_1.ErrorReason[error.id]) { this.respond(this.adapterId, worker_command_1.WorkerStreamCommand.SUBSCRIPTION_ERROR, error.id.toString(10)); } }; return WorkerValueStreamResponder; }()); exports.WorkerValueStreamResponder = WorkerValueStreamResponder; var FALLBACK_SELECTOR = new selector_set_1.SelectorSet([]); var WorkerStreamRegistryAdapter = /** @class */ (function () { function WorkerStreamRegistryAdapter(internal) { this.internal = internal; this.streams = {}; } /** * Add a stream to the registry * * @param selector the topic selector * @param stream the stream adapter to add */ WorkerStreamRegistryAdapter.prototype.add = function (action, respond) { var topicSelector = topic_selector_parser_1.parseSelector(action.topic); var datatype = action.datatype === 'any' ? datatypes_1.DataTypes.any() : datatypes_1.DataTypes.get(action.datatype); if (datatype === null) { return; } var stream = new WorkerValueStreamResponder(this.internal.getStreamRegistry(), topicSelector, datatype, false, respond, conversation_id_1.ConversationId.fromString(action.adapterId)); this.streams[action.adapterId] = stream; }; /** * Add a fallback stream to the registry * * @param stream the stream adapter to add */ WorkerStreamRegistryAdapter.prototype.addFallback = function (action, respond) { var datatype = action.datatype === 'any' ? datatypes_1.DataTypes.any() : datatypes_1.DataTypes.get(action.datatype); if (datatype === null) { return; } var stream = new WorkerValueStreamResponder(this.internal.getStreamRegistry(), FALLBACK_SELECTOR, datatype, true, respond, conversation_id_1.ConversationId.fromString(action.adapterId)); this.streams[action.adapterId] = stream; }; /** * Remove a stream from the registry * * @param stream the stream adapter to remove */ WorkerStreamRegistryAdapter.prototype.remove = function (action) { var stream = this.streams[action.adapterId]; this.internal.getStreamRegistry().remove(stream.valueStream); delete this.streams[action.adapterId]; }; /** * Close the stream registry * * All streams will receive an {@link ErrorReason.SESSION_CLOSED SESSION_CLOSED} * subscription error. No further values will be emitted. */ WorkerStreamRegistryAdapter.prototype.close = function () { this.internal.getStreamRegistry().close(); this.streams = {}; }; return WorkerStreamRegistryAdapter; }()); exports.WorkerStreamRegistryAdapter = WorkerStreamRegistryAdapter;