UNPKG

value-breakpoints

Version:

Run specific stuff when value reaches a specific range.

241 lines (204 loc) 8.77 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var deffy = require("deffy"), EventEmitter = require("events").EventEmitter, fnResult = require("fn-result"); var BreakpointObj = function () { /** * BreakpointObj * Creates a breakpoint object instance. * * @name BreakpointObj * @function * @param {Object} input An object containing the following fields: * * - `min` (Number): The min value to be caught (default: `-Infinity`). * - `max` (Number): The max value to be caught (default: `+Infinity`). * - `start` (Function): The start handler (optional). * - `inside` (Function): The inside handler (optional). * - `end` (Function): The end handler (optional). * - `event` (String): The event to be emitted when the value is in the specific range. * * @returns {BreakpointObj} The `BreakpointObj` instance containing the following fields: * * - `min` (Number): The min value to be caught (default: `-Infinity`). * - `max` (Number): The max value to be caught (default: `+Infinity`). * - `handlers` (Object): * * - `start` (Function): The start handler (optional). * - `inside` (Function): The inside handler (optional). * - `end` (Function): The end handler (optional). * * - `started` (Boolean): `true`, if the value reached the min number, `false` otherwise. * - `ended` (Boolean): `true`, if the value reached the max number, `false` otherwise. * - `event` (String): The event to be emitted when the value is in the specific range. * - `_` (Object): The original input object. * */ function BreakpointObj(input) { _classCallCheck(this, BreakpointObj); this.min = deffy(input.min, -Infinity); this.max = deffy(input.max, Infinity); this._ = input; this.handlers = { start: input.start, inside: input.inside, end: input.end }; this.started = false; this.ended = false; this.event = input.event; } /** * check * * @name check * @function * @param {Number} val The value to be checked. * @returns {Object} An object containing the following fields: * * - `handlers` (Array): The array of functions to call. * - `isInside` (Boolean): `true`, if the value is in the specified range. * - `wasInside` (Boolean): `true`, if the value was in the specified range one step before. * */ _createClass(BreakpointObj, [{ key: "check", value: function check(val) { var handlers = []; var isInside = this.min <= val && val < this.max; var wasInside = this.wasInside; if (this.handlers.start && isInside && !this.started) { handlers.push(this.handlers.start); this.started = true; } if (this.handlers.inside && isInside) { handlers.push(this.handlers.inside); } if (this.handlers.end && wasInside && val > this.max && !this.ended) { handlers.push(this.handlers.end); this.ended = true; } this.wasInside = isInside; return { handlers: handlers, isInside: isInside, wasInside: wasInside }; } }]); return BreakpointObj; }(); var ValueBreakpoints = function (_EventEmitter) { _inherits(ValueBreakpoints, _EventEmitter); /** * ValueBreakpoints * Run specific stuff when value reaches a specific range. * * @name ValueBreakpoints * @function * @param {Array} brks An array of breakpoint objects. * @returns {ValueBreakpoints} The `ValueBreakpoints` extended from `EventEmitter`. */ function ValueBreakpoints(brks) { _classCallCheck(this, ValueBreakpoints); var _this = _possibleConstructorReturn(this, (ValueBreakpoints.__proto__ || Object.getPrototypeOf(ValueBreakpoints)).call(this)); brks = brks || []; _this.brks = []; _this.add(brks); _this._intervals = []; return _this; } /** * stop * Stops all the intervals. * * @name stop * @function * @returns {ValueBreakpoints} The current `ValueBreakpoints` instance. */ _createClass(ValueBreakpoints, [{ key: "stop", value: function stop() { this._intervals.forEach(function (c) { return c.stop(); }); } /** * add * Adds one or more breakpoints. * * @name add * @function * @param {Object} brk The breakpoint object or an array of breakpoints. * @returns {ValueBreakpoints} The current `ValueBreakpoints` instance. */ }, { key: "add", value: function add(brk) { var _this2 = this; if (Array.isArray(brk)) { brk.forEach(function (c) { return _this2.add(c); }); } else { this.brks.push(new BreakpointObj(brk)); } return this; } /** * check * * @name check * @function * @param {Function|Number} fn The function to be called when checking the value automatically. Manual checking is possible by passing a number. * @param {Number} interval The interval duration. * @returns {ValueBreakpoints} The current `ValueBreakpoints` instance (if a number was passed), or an object: * * - `stop` (Function): If called, it will stop the checking. * - `fn` (Function): The function checking the value. */ }, { key: "check", value: function check(_fn, interval) { var _this3 = this; if (typeof _fn !== "function") { this.brks.forEach(function (c) { var res = c.check(_fn); res.isInside && c.event && _this3.emit(c.event, c, _fn, res); res.handlers.forEach(function (cFn) { return cFn(c, _fn, res); }); }); return this; } else { interval = deffy(interval, 1000); var res = { stop: function stop() { this.stopped = true; }, fn: function fn() { if (res.stopped) { return; } fnResult(_fn, function (err, val) { if (err) { return _this3.emit("error", err); } _this3.check(val); setTimeout(res.fn, interval); }); } }; res.fn(); this._intervals.push(res); return res; } } }]); return ValueBreakpoints; }(EventEmitter); module.exports = ValueBreakpoints;