UNPKG

@iotile/iotile-device

Version:

A typescript library for interfacing with IOTile BLE devices

47 lines 1.78 kB
"use strict"; /** * A sliding window reordering helper class. * * This class takes tuples (value, sequence_number) and a callback that should be * called as `callback(value)` but in order of sequence_number. If a sequence * number is received out of order, it puts it into a holding list to be dispatched * at the appropriate time. */ Object.defineProperty(exports, "__esModule", { value: true }); var SlidingWindowReorderer = /** @class */ (function () { function SlidingWindowReorderer(callback) { this.callback = callback; this.onHold = {}; this.nextExpected = 0; } SlidingWindowReorderer.prototype.call = function (value, sequence) { /** * If there is not a sequence number present, just pass the callback through * without reordering it. */ if (sequence == null) { this.callback(value); return; } if (sequence != this.nextExpected) console.warn("Received out of order: expected " + this.nextExpected + " got " + sequence); this.onHold[sequence] = value; this.tryDispatchInOrder(); }; SlidingWindowReorderer.prototype.tryDispatchInOrder = function () { while (this.nextExpected in this.onHold) { try { var value = this.onHold[this.nextExpected]; delete this.onHold[this.nextExpected]; this.nextExpected += 1; this.callback(value); } catch (err) { console.error("Error in callback in SlidingWindowReorderer", err); } } }; return SlidingWindowReorderer; }()); exports.SlidingWindowReorderer = SlidingWindowReorderer; //# sourceMappingURL=sliding-window.js.map