UNPKG

node-red-contrib-dynamic-dimmer

Version:
115 lines (114 loc) 4.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DimProcessorFactory = exports.DimProcessor = void 0; var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var d3_ease_1 = require("d3-ease"); var math = require("mathjs"); var ROUNDING_DECIMALS = 4; var T_ROUNDING_DECIMALS = 6; var DimProcessor = (function () { function DimProcessor(config, scheduler) { this.config = config; this.scheduler = scheduler; this.dimStopSignal$ = new rxjs_1.Subject(); this.config = Object.assign({ eventInterval: 200, minValue: 0, maxValue: 100, step: 0.1 }, config); this.defaultConfig = Object.assign(config); this.currentValue = this.config.minValue; this.t = 0; this.timeSequence$ = (0, rxjs_1.interval)(this.config.eventInterval, this.scheduler); this.easeFn = d3_ease_1.easeLinear; } DimProcessor.prototype.dim = function (target, start, config) { var _this = this; this.mergeDimConfig(config); this.stopDimProcessing(); this.dimStopSignal$ = new rxjs_1.Subject(); this.t = start !== null && start !== void 0 ? start : this.t; var context = Object.assign(this.config, { currentValue: this.currentValue, t: this.t, target: target, sign: (target < this.t) ? -1.0 : 1.0, easeFn: this.easeFn }); var targetNotReached = function (target, t, targetSign) { return (targetSign === 1.0) ? t <= target : t >= target; }; return (0, rxjs_1.combineLatest)(this.timeSequence$, (0, rxjs_1.of)(context), function (t, c) { return c; }) .pipe((0, operators_1.map)(function (c) { c.t = math.chain(c.t) .add(c.sign * c.step) .round(T_ROUNDING_DECIMALS) .done(); c.currentValue = math.chain(c.easeFn(c.t)) .multiply(c.maxValue) .round(ROUNDING_DECIMALS) .done(); return c; }), (0, operators_1.takeWhile)(function (c) { return targetNotReached(c.target, c.t, c.sign); }), (0, operators_1.takeUntil)(this.dimStopSignal$), (0, operators_1.map)(function (c) { c.currentValue = Math.max(Math.min(c.maxValue, c.currentValue), c.minValue); c.t = Math.max(Math.min(1.0, c.t), 0.0); _this.currentValue = c.currentValue; _this.t = c.t; return c.currentValue; })); }; DimProcessor.prototype.stopDimProcessing = function () { this.dimStopSignal$.next(); this.dimStopSignal$.complete(); }; DimProcessor.prototype.pause = function () { this.stopDimProcessing(); return (0, rxjs_1.of)(this.currentValue); }; DimProcessor.prototype.set = function (target, config) { var _this = this; this.mergeDimConfig(Object.assign(this.defaultConfig, config)); this.stopDimProcessing(); var context = Object.assign(this.config, { currentValue: this.currentValue, t: target, target: target, sign: 1.0, easeFn: this.easeFn }); return (0, rxjs_1.of)(context) .pipe((0, operators_1.map)(function (c) { c.currentValue = math.chain(c.easeFn(c.t)) .multiply(c.maxValue) .round(ROUNDING_DECIMALS) .done(); c.currentValue = Math.max(Math.min(c.maxValue, c.currentValue), c.minValue); _this.currentValue = c.currentValue; _this.t = c.t; return _this.currentValue; })); }; DimProcessor.prototype.reset = function (config) { return this.set(0.0, config); }; DimProcessor.prototype.mergeDimConfig = function (newConfig) { if (newConfig !== undefined) { this.config = Object.assign(this.config, newConfig); this.currentValue = Math.min(Math.max(this.currentValue, this.config.minValue), this.config.maxValue); this.timeSequence$ = (0, rxjs_1.interval)(this.config.eventInterval, this.scheduler); } }; return DimProcessor; }()); exports.DimProcessor = DimProcessor; var DimProcessorFactory = (function () { function DimProcessorFactory() { } DimProcessorFactory.getProcessor = function (config) { return new DimProcessor(config); }; return DimProcessorFactory; }()); exports.DimProcessorFactory = DimProcessorFactory;