@iotize/device-client.js
Version:
IoTize Device client for Javascript
92 lines (91 loc) • 3.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var variable_monitor_interface_1 = require("./variable-monitor.interface");
var util_1 = require("../../../core/util");
var logger_1 = require("../../../logger");
var logger = logger_1.default('Bundle');
/**
* Monitor implementation for a variable thanks to bundle resource
*/
var BundleVariableMonitor = /** @class */ (function () {
function BundleVariableMonitor() {
this.state = variable_monitor_interface_1.VariableMonitorState.START;
this._options = {};
}
BundleVariableMonitor.prototype.start = function (options) {
this.state = variable_monitor_interface_1.VariableMonitorState.START;
return this;
};
BundleVariableMonitor.prototype.isStarted = function () {
return this.state === variable_monitor_interface_1.VariableMonitorState.START;
};
BundleVariableMonitor.prototype.pause = function () {
this.state = variable_monitor_interface_1.VariableMonitorState.PAUSE;
this._emit('PAUSE');
return this;
};
BundleVariableMonitor.prototype.stop = function () {
this.state = variable_monitor_interface_1.VariableMonitorState.STOP;
this.pause();
this._emit('STOP');
if (this._values) {
this._values.complete();
this._values = undefined;
}
if (this._events) {
this._events.complete();
this._events = undefined;
}
this._previousValue = undefined;
return this;
};
BundleVariableMonitor.prototype.values = function () {
if (!this._values) {
this._values = new rxjs_1.Subject();
}
return this._values;
};
BundleVariableMonitor.prototype.events = function () {
if (!this._events) {
this._events = new rxjs_1.Subject();
}
return this._events;
};
BundleVariableMonitor.prototype.notifyNewValue = function (data) {
if (!this.isStarted()) {
return;
}
if (this._options.forceChange || this.valueHasChanged(data)) {
this.onValueChanged(data);
}
};
BundleVariableMonitor.prototype.onValueChanged = function (data) {
logger.debug('valueHasChanged', data, this._previousValue);
this._previousValue = data;
if (!this._values) {
this._values = new rxjs_1.Subject();
}
this._values.next(data);
if (this._events) {
this._events.next({
type: 'READ_SUCCESS',
payload: data
});
}
};
BundleVariableMonitor.prototype.valueHasChanged = function (newValue) {
return this._previousValue === undefined || !util_1.Util.deepEqual(this._previousValue, newValue);
};
BundleVariableMonitor.prototype._emit = function (type) {
if (!this._events) {
this._events = new rxjs_1.Subject();
}
this._events.next({
type: type,
payload: undefined
});
};
return BundleVariableMonitor;
}());
exports.BundleVariableMonitor = BundleVariableMonitor;