UNPKG

matting-editor

Version:

matting-editor

192 lines (145 loc) 6.34 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); var _events = require('events'); var _events2 = _interopRequireDefault(_events); var _cacher = require('./cacher'); var _cacher2 = _interopRequireDefault(_cacher); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } var cacher = new _cacher2.default(); var Snapshot = function (_EventEmitter) { _inherits(Snapshot, _EventEmitter); function Snapshot(initData) { _classCallCheck(this, Snapshot); var _this = _possibleConstructorReturn(this, (Snapshot.__proto__ || Object.getPrototypeOf(Snapshot)).call(this)); _this.limit = 50; _this.flushDelay = 320; _this.initData = initData; _this.currentIndex = -1; _this.snapshots = []; _this.reset(); return _this; } _createClass(Snapshot, [{ key: 'add', value: function add(tag, data) { var flush = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; if (flush) { this.flush(); } var currIdx = this.currentIndex; var snapshots = this.snapshots; var hash = cacher.add(data); var currSnapshot = snapshots[currIdx]; if (currSnapshot && currSnapshot.hash === hash) { cacher.delete(hash); return currSnapshot; } var lastIdx = this.size - 1; if (currIdx < lastIdx) { for (var i = lastIdx; i >= currIdx + 1; --i) { this.removeAt(i); } } if (snapshots.length >= this.limit) { this.removeAt(0); } var snapshot = { timestamp: Date.now(), hash: hash, tag: tag }; snapshots.push(snapshot); this.currentIndex = this.size - 1; this.emit('change'); return snapshot; } }, { key: 'addLazy', value: function addLazy(tag, data) { var _this2 = this; clearTimeout(this.addTimer); this._flush = function () { _this2.add(tag, data); }; this.addTimer = setTimeout(this._flush, this.flushDelay); } }, { key: 'removeAt', value: function removeAt() { var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1; var snapshots = this.snapshots; if (index >= 0 && index < snapshots.length) { snapshots.splice(index, 1); } } }, { key: 'remove', value: function remove(snapshot) { var idx = this.snapshots.indexOf(snapshot); if (idx >= 0) { this.removeAt(idx); } } }, { key: 'flush', value: function flush() { if (typeof this._flush === 'function') { this._flush(); } } }, { key: 'getData', value: function getData() { var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.currentIndex; var snapshot = this.snapshots[index]; return snapshot ? cacher.get(snapshot.hash) : null; } }, { key: 'jumpTo', value: function jumpTo(index) { var snapshots = this.snapshots; if (snapshots[index] && index !== this.currentIndex) { this.currentIndex = index; this.emit('change'); } return this; } }, { key: 'backward', value: function backward() { var offset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : -1; return this.jumpTo(this.currentIndex + offset); } }, { key: 'forward', value: function forward() { var offset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; return this.jumpTo(this.currentIndex + offset); } }, { key: 'reset', value: function reset() { var keepInitData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; clearTimeout(this.addTimer); this.currentIndex = -1; this.snapshots = []; this._flush = null; if (keepInitData && this.initData !== undefined) { this.add('init', this.initData); } } }, { key: 'size', get: function get() { return this.snapshots.length; } }]); return Snapshot; }(_events2.default); exports.default = Snapshot;