UNPKG

diffusion

Version:

Diffusion JavaScript client

108 lines (107 loc) 4.31 kB
"use strict"; /** * @module Routing */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.DatatypeCacheEntry = void 0; var buffer_slice_1 = require("./../data/buffer-slice"); var topic_cache_entry_1 = require("./../routing/topic-cache-entry"); /** * Implementation of {@link TopicCacheEntry} for topics with a given datatype */ var DatatypeCacheEntry = /** @class */ (function (_super) { __extends(DatatypeCacheEntry, _super); /** * Create a new DatatypeCacheEntry instance * * @param streams the stream adapters * @param path the topic path * @param specification the topic specification * @param datatype the data type of the entry */ function DatatypeCacheEntry(streams, path, specification, datatype) { var _this = _super.call(this, streams, path, specification) || this; _this.datatype = datatype; if (specification.properties) { if (specification.properties.DONT_RETAIN_VALUE) { _this.dontRetainValue = JSON.parse(specification.properties.DONT_RETAIN_VALUE); } } return _this; } /** * Handle a value * * Reads a value of the entry's data type and caches it. The calls {@link * notifyValue} * * @param received the content as a Uint8Array * @param registry the stream registry * @param errorHandler an error handler that will be called with an {@link InvalidDataError} if an error occurs */ DatatypeCacheEntry.prototype.handleValue = function (received, registry, errorHandler) { var oldValue = this.value; var oldContent = this.buffer || null; try { var newValue = this.datatype.readValue(received); this.notifyValue(oldContent, received, oldValue, newValue, registry); if (!this.dontRetainValue) { this.value = newValue; this.buffer = received; } } catch (e) { errorHandler(e); } }; /** * Handle a delta * * Reads the delta of the entry's data type and caches it. The calls {@link * notifyValue} * * @param received the content as a Uint8Array * @param registry the stream registry * @param errorHandler an error handler that will be called with an {@link InvalidDataError} if an error occurs */ DatatypeCacheEntry.prototype.handleDelta = function (received, registry, errorHandler) { var oldValue = this.value; var oldContent = this.buffer || null; try { var deltaType = this.datatype.deltaType('binary'); var delta = deltaType.readDelta(received); var _a = deltaType.applyImpl(oldValue, delta, this.buffer ? new buffer_slice_1.BufferSlice(this.buffer) : undefined), value = _a.value, cbor = _a.cbor; this.value = value; this.buffer = cbor.$buffer; this.notifyDelta(oldContent, cbor.$buffer, delta, oldValue, this.value, registry); } catch (e) { errorHandler(e); } }; /** * @inheritdoc */ DatatypeCacheEntry.prototype.notifyValueToNewStream = function (path, specification, stream) { if (this.buffer) { stream.onValue(path, specification, null, this.buffer, null, this.value); } }; return DatatypeCacheEntry; }(topic_cache_entry_1.TopicCacheEntry)); exports.DatatypeCacheEntry = DatatypeCacheEntry;