'use strict'; 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; } /* * Licensed Materials - Property of IBM * IBM Business Analytics (C) Copyright IBM Corp. 2019 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ /** * @class StateImpl * @hideconstructor * @classdesc Implementation class for StateApi that is used to control a state */ define(['underscore', '../../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../../lib/@waca/dashboard-common/dist/api/Error', '../StateAPI', '../PrivateStateAPI', './StateAPISpec'], function (_, APIFactory, APIError, StateAPI, PrivateStateAPI, StateAPISpec) { var StateImpl = function (_StateAPISpec) { _inherits(StateImpl, _StateAPISpec); function StateImpl(options) { _classCallCheck(this, StateImpl); var _this = _possibleConstructorReturn(this, _StateAPISpec.call(this, options)); _this._pendingStatusMap = {}; _this._status = StateAPI.STATUS.CREATED; _this._error = undefined; _this._api = APIFactory.createAPI(_this, [StateAPI]); _this._privateApi = APIFactory.createAPI(_this, [StateAPI, PrivateStateAPI]); _this._statusCountTracker = {}; return _this; } StateImpl.prototype.getAPI = function getAPI(type) { if (type === 'internal') { return this._privateApi; } else { return this._api; } }; StateImpl.prototype.destroy = function destroy() { this._api = null; }; StateImpl.prototype._isValidStatus = function _isValidStatus(status) { return _.values(StateAPI.STATUS).indexOf(status) > -1; }; /** * @implements StateAPI#getStatus */ StateImpl.prototype.getStatus = function getStatus() { return this._status; }; /** * @implements StateAPI#setStatus */ StateImpl.prototype.setStatus = function setStatus(status) { if (!this._isValidStatus(status)) { throw new Error('Invalid status: ' + status); } this._status = status; if (!this._statusCountTracker[status]) { this._statusCountTracker[status] = 1; } else { this._statusCountTracker[status]++; } // resolve all promises waiting for the status change if (_.isArray(this._pendingStatusMap[status])) { while (this._pendingStatusMap[status].length > 0) { this._pendingStatusMap[status][0].resolve(this._getResolveState(status)); this._pendingStatusMap[status].splice(0, 1); } } }; StateImpl.prototype._getResolveState = function _getResolveState(status) { return { status: status, count: this._statusCountTracker[status] }; }; /** * @implements StateAPI#whenStatusChanges */ StateImpl.prototype.whenStatusChanges = function whenStatusChanges(status) { var _this2 = this; if (!this._isValidStatus(status)) { throw new Error('Invalid status: ' + status); } // simply resolve if current status is the expected status if (status === this._status) { return Promise.resolve(this._getResolveState(status)); } // keep track of all promises per each status if (!this._pendingStatusMap[status]) { this._pendingStatusMap[status] = []; } return new Promise(function (resolve, reject) { _this2._pendingStatusMap[status].push({ 'resolve': resolve, 'reject': reject }); }); }; /** * @implements StateAPI#getError */ StateImpl.prototype.getError = function getError() { return this._error; }; /** * @implements StateAPI#setError */ StateImpl.prototype.setError = function setError(error) { // use != to check for both null and undefined if (error != null && !(error instanceof APIError)) { error = new APIError(error); } this._error = error; this.invokeCallbacks(StateImpl.ON_CHANGE_ERROR, error); }; /** * @implements StateAPI#clearError */ StateImpl.prototype.clearError = function clearError() { this.setError(); }; /** * @implements StateAPI#onChangeError */ StateImpl.prototype.onChangeError = function onChangeError(callback) { this.registerCallback(StateImpl.ON_CHANGE_ERROR, callback); }; return StateImpl; }(StateAPISpec); StateImpl.ON_CHANGE_ERROR = 'onChangeError'; return StateImpl; }); //# sourceMappingURL=StateImpl.js.map