UNPKG

@iotize/tap

Version:

IoTize Device client for Javascript

1,110 lines (1,100 loc) 109 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('rxjs'), require('rxjs/operators'), require('@iotize/common/error'), require('@iotize/tap'), require('@iotize/tap/client/impl'), require('@iotize/tap/service/impl/variable'), require('@iotize/tap/client/api'), require('@iotize/tap/service/impl/target'), require('@iotize/tap/service/impl/interface')) : typeof define === 'function' && define.amd ? define('@iotize/tap/data', ['exports', 'rxjs', 'rxjs/operators', '@iotize/common/error', '@iotize/tap', '@iotize/tap/client/impl', '@iotize/tap/service/impl/variable', '@iotize/tap/client/api', '@iotize/tap/service/impl/target', '@iotize/tap/service/impl/interface'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.iotize = global.iotize || {}, global.iotize.tap = global.iotize.tap || {}, global.iotize.tap.data = {}), global.rxjs, global.rxjs.operators, global.error, global.iotize.tap, global.iotize.tap.client.impl, global.iotize.tap.service.impl.variable, global.iotize.tap.client.api)); })(this, (function (exports, rxjs, operators, error, tap, impl, variable, api) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise */ 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); }; function __extends(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 __()); } var __assign = function () { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function __param(paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); }; } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } function __awaiter$b(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function () { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } var __createBinding = Object.create ? (function (o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } }); }) : (function (o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; }); function __exportStar(m, o) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p); } function __values(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } /** @deprecated */ function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } /** @deprecated */ function __spreadArrays() { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; } function __spreadArray(to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); } function __await(v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } function __asyncGenerator(thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } } function __asyncDelegator(o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } } function __asyncValues(o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function (v) { resolve({ value: v, done: d }); }, reject); } } function __makeTemplateObject(cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; } ; var __setModuleDefault = Object.create ? (function (o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function (o, v) { o["default"] = v; }; function __importStar(mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; } function __importDefault(mod) { return (mod && mod.__esModule) ? mod : { default: mod }; } function __classPrivateFieldGet(receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); } function __classPrivateFieldSet(receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; } exports.MonitorEngine = void 0; (function (MonitorEngine) { var State; (function (State) { State[State["START"] = 0] = "START"; State[State["PAUSE"] = 1] = "PAUSE"; State[State["STOP"] = 2] = "STOP"; })(State = MonitorEngine.State || (MonitorEngine.State = {})); })(exports.MonitorEngine || (exports.MonitorEngine = {})); var __awaiter$a = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var TAG$1 = 'TapMonitorEngine'; var MonitoringController = /** @class */ (function () { function MonitoringController(initalState) { if (initalState === void 0) { initalState = INITAL_MONITORING_CONFIG; } this.initalState = initalState; this.subject = new rxjs.BehaviorSubject(this.initalState); } Object.defineProperty(MonitoringController.prototype, "state", { get: function () { return this.subject.value.state; }, enumerable: false, configurable: true }); MonitoringController.prototype.start = function (options) { this.subject.next(Object.assign({ state: exports.MonitorEngine.State.START, }, options)); return this; }; MonitoringController.prototype.stop = function () { this.subject.next({ state: exports.MonitorEngine.State.STOP, }); return this; }; MonitoringController.prototype.pause = function () { this.subject.next({ state: exports.MonitorEngine.State.PAUSE, }); return this; }; MonitoringController.prototype.setPeriod = function (period) { this.subject.next({ period: period, }); }; MonitoringController.prototype.asSubject = function () { return this.subject; }; return MonitoringController; }()); function createDataStreamMonitor(stream, ticker, eventSubject) { var _this = this; return ticker.pipe(operators.flatMap(function (tickerState) { return __awaiter$a(_this, void 0, void 0, function () { return __generator(this, function (_a) { // debug(TAG, `Running call at time ${time}`); return [2 /*return*/, stream .readRaw() .then(function (value) { // TODO event ? return value; }) .catch(function (error) { eventSubject.next({ type: 'error', error: error, stream: stream, timerState: tickerState, }); })]; }); }); }), // filter<DataType | undefined, DataType>((value): value is DataType => { // return value !== undefined; // }), operators.share()); } var INITAL_MONITORING_CONFIG = { dueTime: 0, period: 1000, refresh: false, state: exports.MonitorEngine.State.STOP, }; function createMonitoringTicker( // call: (time: number) => Promise<DataType>, controller) { var controllerSubject = controller instanceof MonitoringController ? controller.asSubject() : controller; var lastState = Object.assign(Object.assign({}, INITAL_MONITORING_CONFIG), controllerSubject.value); return controllerSubject.pipe(operators.switchMap(function (partialState) { var newState = Object.assign(Object.assign({}, lastState), partialState); lastState = newState; // console.log(TAG, 'Apply new monitoring state: ', newState); switch (newState.state) { case exports.MonitorEngine.State.STOP: return rxjs.empty(); case exports.MonitorEngine.State.START: return rxjs.timer(newState.dueTime, newState.period).pipe(operators.map(function (time) { return ({ time: time, state: newState, }); })); case exports.MonitorEngine.State.PAUSE: return rxjs.never(); default: throw new Error("InternalErorr: Not implemented monitoring state: " + newState.state); } })); } exports.TapDataError = /** @class */ (function (_super) { __extends(TapDataError, _super); function TapDataError(msg, code, cause) { var _this = _super.call(this, msg, code) || this; _this.cause = cause; return _this; } TapDataError.unexpectedEncodedValueError = function (variable, rawData, err) { return new TapDataError("An error occured while encoding value for variable " + variable.id + ". " + err.message, TapDataError.Code.UnexpectedEncodedValueError); }; TapDataError.unexpectedVariableDataRead = function (variable, rawData, err) { return new TapDataError("An error occured while decoding value for variable " + variable.id + ". " + err.message, TapDataError.Code.UnexpectedVariableData); }; TapDataError.bundleNotFound = function (key) { return new TapDataError("Bundle with identifier \"" + key.toString() + "\" is not registered", TapDataError.Code.BundleNotFound); }; TapDataError.variableNotFound = function (key) { return new TapDataError("Variable with identifier \"" + key.toString() + "\" is not registered", TapDataError.Code.VariableNotFound); }; TapDataError.bundleNotConfigured = function (config, err) { return new TapDataError("Bundle with id \"" + config.id + "\" does not exist on this Tap. You should reconfigure your Tap to add this bundle.", TapDataError.Code.BundleNotConfigured, err); }; TapDataError.variableNotConfigured = function (config, err) { return new TapDataError("Variable with id \"" + config.id + "\" does not exist on this Tap. You should reconfigure your Tap to add this variable.", TapDataError.Code.VariableNotConfigured, err); }; return TapDataError; }(error.CodeError)); (function (TapDataError) { var Code; (function (Code) { Code["UnexpectedVariableData"] = "TapDataErrorUnexpectedVariableData"; Code["UnexpectedEncodedValueError"] = "TapDataErrorUnexpectedEncodedValueError"; Code["BundleNotFound"] = "TapDataErrorBundleNotFound"; Code["VariableNotFound"] = "TapDataErrorVariableNotFound"; Code["BundleNotConfigured"] = "TapDataErrorBundleNotConfigured"; Code["VariableNotConfigured"] = "TapDataErrorVariableNotConfigured"; })(Code = TapDataError.Code || (TapDataError.Code = {})); })(exports.TapDataError || (exports.TapDataError = {})); var ɵ0 = function (v) { return v; }, ɵ1 = function (v) { return v; }; var BLANK_CONVERTER = { encode: ɵ0, decode: ɵ1, }; var _a; var ASCII_WITH_END_OF_STRING_CHAR_CONVERTER = new impl.StringConverter('ascii', { addEndOfStringCharacter: true, replaceEmptyStringWithEndOfLineChar: true, stripEndOfStringCharacter: true, }); function computeByteLengthFromFormat(info) { return Math.ceil(computeBitLengthFromFormat(info) / 8); } function computeBitLengthFromFormat(info) { return info.length * variableFormatToBitSize(info.format); } function variableFormatToByteSize(format) { return Math.ceil(variableFormatToBitSize(format) / 8); } function variableFormatToBitSize(format) { switch (format) { case tap.VariableFormat._1_BIT: return 1; case tap.VariableFormat._8_BITS: return 8; case tap.VariableFormat._16_BITS: return 16; case tap.VariableFormat._32_BITS: return 32; default: // TODO better error throw new Error("InternalError: missing bit length for format value value \"" + format + "\" (name: " + tap.VariableFormat[format] + ")"); } } function variableDataTypeToByteSize(dataType) { return typeConfig[dataType].size; } /** * Map Variable Type to a byte size when encoded as a Tap frame. */ var typeConfig = (_a = {}, _a[variable.VariableType.Data.BOOLEAN] = { size: 1, }, _a[variable.VariableType.Data.UINT8] = { size: 1, }, _a[variable.VariableType.Data.UINT16] = { size: 2, }, _a[variable.VariableType.Data.UINT32] = { size: 4, }, _a[variable.VariableType.Data.INT8] = { size: 1, }, _a[variable.VariableType.Data.INT16] = { size: 2, }, _a[variable.VariableType.Data.INT32] = { size: 4, }, _a[variable.VariableType.Data.FLOAT32] = { size: 4, }, _a[variable.VariableType.Data.ASCII] = { size: 1, }, _a); function getConverterFromVariableConfig(variableConfig, stringConverter) { if (stringConverter === void 0) { stringConverter = ASCII_WITH_END_OF_STRING_CHAR_CONVERTER; } var typeKey = variableConfig.dataType; if (!(typeKey in variable.VariableType.Data)) { throw new Error("Unknown variable type key \"" + typeKey + "\""); // TODO better error } return getConverterFromVariableType(variable.VariableType.Data[typeKey], variableConfig.length || 1, stringConverter); } /** * Get converter from variable type and length (number of element) * @param dataType * @param length > 1 if it's an array. * * @return undefined if converter does not exist for the given type */ function getConverterFromVariableType(dataType, length, stringConverter) { if (length === void 0) { length = 1; } if (stringConverter === void 0) { stringConverter = ASCII_WITH_END_OF_STRING_CHAR_CONVERTER; } if (dataType === variable.VariableType.Data.ASCII) { return stringConverter; } var converter = getRawConverterFromVariableDataType(dataType); if (converter && length > 1) { return new impl.ArrayConverter(converter, { sizeOfItem: variableDataTypeToByteSize(dataType), }); } else { return converter; } } // type TypeMapping = { // [VariableType.Data.ASCII]: string; // [VariableType.Data.FLOAT32]: number; // [VariableType.Data.INT32]: number; // [VariableType.Data.INT16]: number; // [VariableType.Data.INT8]: number; // [VariableType.Data.UINT8]: number; // [VariableType.Data.UINT16]: number; // [VariableType.Data.UINT32]: number; // } /** * Get raw converter instance from variable type * TODO improve output typing with mapping * @param format * @param length */ function getRawConverterFromVariableDataType(type) { switch (type) { case variable.VariableType.Data.BOOLEAN: return impl.BooleanConverter.instanceBit0(); case variable.VariableType.Data.UINT8: return impl.NumberConverter.uint8(); case variable.VariableType.Data.UINT16: return impl.NumberConverter.uint16(); case variable.VariableType.Data.UINT32: return impl.NumberConverter.uint32(); case variable.VariableType.Data.INT8: return impl.NumberConverter.int8(); case variable.VariableType.Data.INT16: return impl.NumberConverter.int16(); case variable.VariableType.Data.INT32: return impl.NumberConverter.int32(); case variable.VariableType.Data.FLOAT32: return impl.FloatConverter.instance32(); case variable.VariableType.Data.ASCII: return impl.StringConverter.ascii(); default: return undefined; } } var __awaiter$9 = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var AbstractBundleDataStream = /** @class */ (function () { function AbstractBundleDataStream(_variables) { this._variables = _variables; } Object.defineProperty(AbstractBundleDataStream.prototype, "variables", { get: function () { return Object.values(this._variables); }, enumerable: false, configurable: true }); AbstractBundleDataStream.prototype.readRaw = function () { return __awaiter$9(this, void 0, void 0, function () { var raw; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.readRawInner()]; case 1: raw = _b.sent(); this.notifyRawValue(raw); return [2 /*return*/, raw]; } }); }); }; // abstract read(): Promise<DataType>; AbstractBundleDataStream.prototype.read = function () { return __awaiter$9(this, void 0, void 0, function () { return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.readRaw()]; case 1: _b.sent(); return [2 /*return*/, this.valueSnapshot]; } }); }); }; AbstractBundleDataStream.prototype.write = function (value) { return __awaiter$9(this, void 0, void 0, function () { var _b, _c, _i, key; return __generator(this, function (_d) { switch (_d.label) { case 0: _b = []; for (_c in value) _b.push(_c); _i = 0; _d.label = 1; case 1: if (!(_i < _b.length)) return [3 /*break*/, 4]; key = _b[_i]; return [4 /*yield*/, this.variable(key).writeRaw(value[key])]; case 2: _d.sent(); _d.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); }); }; AbstractBundleDataStream.prototype.variable = function (id) { if (!(id in this._variables)) { throw new Error("Variable \"" + id + "\" not found in bundle \"" + this.id.toString() + "\""); } return this._variables[id]; }; Object.defineProperty(AbstractBundleDataStream.prototype, "rawValues", { get: function () { var variables = Object.values(this._variables); return rxjs.combineLatest(variables.map(function (v) { return v.rawValues.pipe(operators.startWith(undefined)); })).pipe(operators.map(function (values) { return values.reduce(function (accumulator, v, index) { accumulator[variables[index].id] = v; // TODO return accumulator; }, {}); })); }, enumerable: false, configurable: true }); Object.defineProperty(AbstractBundleDataStream.prototype, "rawValueSnaphot", { get: function () { var variables = Object.values(this._variables); return variables.reduce(function (accumulator, v, index) { accumulator[variables[index].id] = v.rawValueSnaphot; return accumulator; }, {}); }, enumerable: false, configurable: true }); Object.defineProperty(AbstractBundleDataStream.prototype, "valueSnapshot", { get: function () { var variables = Object.values(this._variables); return variables.reduce(function (accumulator, v, index) { accumulator[variables[index].id] = v.valueSnapshot; return accumulator; }, {}); }, enumerable: false, configurable: true }); Object.defineProperty(AbstractBundleDataStream.prototype, "values", { get: function () { var variables = Object.values(this._variables); return rxjs.combineLatest(variables.map(function (v) { return v.values.pipe(operators.startWith(undefined)); })).pipe(operators.map(function (values) { return values.reduce(function (accumulator, v, index) { accumulator[variables[index].id] = v; return accumulator; }, {}); })); }, enumerable: false, configurable: true }); AbstractBundleDataStream.prototype.notifyRawValue = function (v) { var _a; return __awaiter$9(this, void 0, void 0, function () { var id, value; return __generator(this, function (_b) { for (id in v) { value = v[id]; if (value !== undefined) { (_a = this._variables[id]) === null || _a === void 0 ? void 0 : _a.notifyRawValue(value); } } return [2 /*return*/]; }); }); }; AbstractBundleDataStream.prototype.findVariable = function (id) { return id in this._variables ? this._variables[id] : undefined; }; AbstractBundleDataStream.prototype.hasVariable = function (id) { return !!this.findVariable(id); }; AbstractBundleDataStream.prototype.writeRaw = function (values) { return __awaiter$9(this, void 0, void 0, function () { var _b, _c, _i, key, value; return __generator(this, function (_d) { switch (_d.label) { case 0: _b = []; for (_c in values) _b.push(_c); _i = 0; _d.label = 1; case 1: if (!(_i < _b.length)) return [3 /*break*/, 4]; key = _b[_i]; value = values[key]; if (!(value !== undefined)) return [3 /*break*/, 3]; return [4 /*yield*/, this.variable(key).writeRaw(value)]; case 2: _d.sent(); _d.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); }); }; return AbstractBundleDataStream; }()); var AbstractEditableDataStream = /** @class */ (function () { function AbstractEditableDataStream() { var _this = this; // static fromCustom(arg0: CustomVariableConfig) { // return new Variable<T>(config, ); // } // public static fromConfig<T>(config: VariableConfig) { // return new Variable<T>(config, ); // } // public static fromMemory(config: MemoryInfoConfig) { // return new Variable<T>(config, ); // } this.rawDataStream = new rxjs.Subject(); // = new Subject<RawType>(); // private selfReadStream = new Subject<RawType>(); this.values = this.rawDataStream.pipe(operators.map(function (v) { return _this.decodeValue(v); })); // if (dataStream) { // this.dataStream = merge( // dataStream, // this.selfReadStream // ); // } // else { // this.dataStream = this.selfReadStream; // } // this.dataStream = this.dataStream // .pipe( // tap(v => this.lastRawValue = v) // ); // // Do we need to subscribe ? // this.dataStream.subscribe(); } Object.defineProperty(AbstractEditableDataStream.prototype, "lastRawValue", { get: function () { return this._lastRawValue; }, enumerable: false, configurable: true }); Object.defineProperty(AbstractEditableDataStream.prototype, "rawValueSnaphot", { get: function () { return this.lastRawValue; }, enumerable: false, configurable: true }); Object.defineProperty(AbstractEditableDataStream.prototype, "valueSnapshot", { get: function () { if (this.lastRawValue === undefined) { return undefined; } return this.decodeValue(this.lastRawValue); }, enumerable: false, configurable: true }); Object.defineProperty(AbstractEditableDataStream.prototype, "rawValues", { get: function () { return this.rawDataStream; }, enumerable: false, configurable: true }); // { // const value = await this.rawReader(this.context); // this.notifyRawValue(value); // return value; // } AbstractEditableDataStream.prototype.notifyRawValue = function (newValue) { return __awaiter$9(this, void 0, void 0, function () { return __generator(this, function (_b) { this._lastRawValue = newValue; this.rawDataStream.next(newValue); return [2 /*return*/]; }); }); }; AbstractEditableDataStream.prototype.readRaw = function () { return __awaiter$9(this, void 0, void 0, function () { var value; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.readRawInner()]; case 1: value = _b.sent(); this.notifyRawValue(value); return [2 /*return*/, value]; } }); }); }; AbstractEditableDataStream.prototype.read = function () { return __awaiter$9(this, void 0, void 0, function () { var p; return __generator(this, function (_b) { switch (_b.label) { case 0: p = this.values.pipe(operators.first()).toPromise(); return [4 /*yield*/, this.readRaw()]; case 1: _b.sent(); return [2 /*return*/, p]; } }); }); }; AbstractEditableDataStream.prototype.write = function (value) { return __awaiter$9(this, void 0, void 0, function () { var encoded; return __generator(this, function (_b) { encoded = this.encodeValue(value); return [2 /*return*/, this.writeRaw(encoded)]; }); }); }; AbstractEditableDataStream.prototype.encodeValue = function (value) { try { return this.converter.encode(value); } catch (err) { throw exports.TapDataError.unexpectedEncodedValueError(this, value, err); } }; AbstractEditableDataStream.prototype.decodeValue = function (data) { try { return this.converter.decode(data); } catch (err) { throw exports.TapDataError.unexpectedVariableDataRead(this, data, err); } }; return AbstractEditableDataStream; }()); var __awaiter$8 = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /** * You should use ConverterVariableView instead */ var VariableView = /** @class */ (function () { function VariableView(id, context) { this.id = id; this.context = context; } Object.defineProperty(VariableView.prototype, "converter", { get: function () { return this.context.converter; }, enumerable: false, configurable: true }); Object.defineProperty(VariableView.prototype, "indexes", { get: function () { return this.context.indexes; }, enumerable: false, configurable: true }); Object.defineProperty(VariableView.prototype, "rawValues", { get: function () { var _this = this; return this.context.variable.rawValues.pipe(operators.map(function (rawData) { return _this.subDataView(rawData); })); }, enumerable: false, configurable: true }); Object.defineProperty(VariableView.prototype, "valueSnapshot", { get: function () { var lastRawValue = this.rawValueSnaphot; return lastRawValue !== undefined ? this.decodeValue(this.subDataView(lastRawValue)) : undefined; }, enumerable: false, configurable: true }); Object.defineProperty(VariableView.prototype, "values", { get: function () { var _this = this; return this.rawValues.pipe(operators.map(function (v) { return _this.decodeValue(v); })); }, enumerable: false, configurable: true }); Object.defineProperty(VariableView.prototype, "rawValueSnaphot", { get: function () { var parentValueSnapshot = this.context.variable.rawValueSnaphot; return parentValueSnapshot !== undefined ? this.subDataView(parentValueSnapshot) : parentValueSnapshot; }, enumerable: false, configurable: true }); VariableView.prototype.notifyRawValue = function (newValue) { }; VariableView.prototype.read = function () { return __awaiter$8(this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = this.decodeValue; return [4 /*yield*/, this.readRaw()]; case 1: return [2 /*return*/, _a.apply(this, [_b.sent()])]; } }); }); }; VariableView.prototype.write = function (value) { return this.writeRaw(this.encodeValue(value)); }; VariableView.prototype.readRaw = function () { return __awaiter$8(this, void 0, void 0, function () { var rawData; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.context.variable.readRaw()]; case 1: rawData = _a.sent(); return [2 /*return*/, this.subDataView(rawData)]; } }); }); }; VariableView.prototype.writeRaw = function (value) { return __awaiter$8(this, void 0, void 0, function () { var existingData, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!this.context.useLastKnownValueBeforeWrite) return [3 /*break*/, 3]; _a = this.context.variable.rawValueSnaphot; if (_a) return [3 /*break*/, 2]; return [4 /*yield*/, this.context.variable.readRaw()]; case 1: _a = (_b.sent()); _b.label = 2; case 2: existingData = _a; return [3 /*break*/, 5]; case 3: return [4 /*yield*/, this.context.variable.readRaw()]; case 4: existingData = _b.sent(); _b.label = 5; case 5: this.indexes.forEach(function (subIndex, index) { existingData[subIndex] = value[index]; }); return [4 /*yield*/, this.context.variable.writeRaw(existingData)]; case 6: _b.sent(); return [2 /*return*/]; } }); }); }; VariableView.prototype.subDataView = function (rawData) { return this.indexes.reduce(function (acc, subIndex, index) { acc[index] = rawData[subIndex]; return acc; }, new Uint8Array(this.indexes.length)); }; VariableView.prototype.encodeValue = function (value) { try { return this.converter.encode(value); } catch (err) { throw e