StateImpl.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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; }
  4. 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; }
  5. /*
  6. * Licensed Materials - Property of IBM
  7. * IBM Business Analytics (C) Copyright IBM Corp. 2019
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. /**
  11. * @class StateImpl
  12. * @hideconstructor
  13. * @classdesc Implementation class for StateApi that is used to control a state
  14. */
  15. 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) {
  16. var StateImpl = function (_StateAPISpec) {
  17. _inherits(StateImpl, _StateAPISpec);
  18. function StateImpl(options) {
  19. _classCallCheck(this, StateImpl);
  20. var _this = _possibleConstructorReturn(this, _StateAPISpec.call(this, options));
  21. _this._pendingStatusMap = {};
  22. _this._status = StateAPI.STATUS.CREATED;
  23. _this._error = undefined;
  24. _this._api = APIFactory.createAPI(_this, [StateAPI]);
  25. _this._privateApi = APIFactory.createAPI(_this, [StateAPI, PrivateStateAPI]);
  26. _this._statusCountTracker = {};
  27. return _this;
  28. }
  29. StateImpl.prototype.getAPI = function getAPI(type) {
  30. if (type === 'internal') {
  31. return this._privateApi;
  32. } else {
  33. return this._api;
  34. }
  35. };
  36. StateImpl.prototype.destroy = function destroy() {
  37. this._api = null;
  38. };
  39. StateImpl.prototype._isValidStatus = function _isValidStatus(status) {
  40. return _.values(StateAPI.STATUS).indexOf(status) > -1;
  41. };
  42. /**
  43. * @implements StateAPI#getStatus
  44. */
  45. StateImpl.prototype.getStatus = function getStatus() {
  46. return this._status;
  47. };
  48. /**
  49. * @implements StateAPI#setStatus
  50. */
  51. StateImpl.prototype.setStatus = function setStatus(status) {
  52. if (!this._isValidStatus(status)) {
  53. throw new Error('Invalid status: ' + status);
  54. }
  55. this._status = status;
  56. if (!this._statusCountTracker[status]) {
  57. this._statusCountTracker[status] = 1;
  58. } else {
  59. this._statusCountTracker[status]++;
  60. }
  61. // resolve all promises waiting for the status change
  62. if (_.isArray(this._pendingStatusMap[status])) {
  63. while (this._pendingStatusMap[status].length > 0) {
  64. this._pendingStatusMap[status][0].resolve(this._getResolveState(status));
  65. this._pendingStatusMap[status].splice(0, 1);
  66. }
  67. }
  68. };
  69. StateImpl.prototype._getResolveState = function _getResolveState(status) {
  70. return {
  71. status: status,
  72. count: this._statusCountTracker[status]
  73. };
  74. };
  75. /**
  76. * @implements StateAPI#whenStatusChanges
  77. */
  78. StateImpl.prototype.whenStatusChanges = function whenStatusChanges(status) {
  79. var _this2 = this;
  80. if (!this._isValidStatus(status)) {
  81. throw new Error('Invalid status: ' + status);
  82. }
  83. // simply resolve if current status is the expected status
  84. if (status === this._status) {
  85. return Promise.resolve(this._getResolveState(status));
  86. }
  87. // keep track of all promises per each status
  88. if (!this._pendingStatusMap[status]) {
  89. this._pendingStatusMap[status] = [];
  90. }
  91. return new Promise(function (resolve, reject) {
  92. _this2._pendingStatusMap[status].push({
  93. 'resolve': resolve,
  94. 'reject': reject
  95. });
  96. });
  97. };
  98. /**
  99. * @implements StateAPI#getError
  100. */
  101. StateImpl.prototype.getError = function getError() {
  102. return this._error;
  103. };
  104. /**
  105. * @implements StateAPI#setError
  106. */
  107. StateImpl.prototype.setError = function setError(error) {
  108. // use != to check for both null and undefined
  109. if (error != null && !(error instanceof APIError)) {
  110. error = new APIError(error);
  111. }
  112. this._error = error;
  113. this.invokeCallbacks(StateImpl.ON_CHANGE_ERROR, error);
  114. };
  115. /**
  116. * @implements StateAPI#clearError
  117. */
  118. StateImpl.prototype.clearError = function clearError() {
  119. this.setError();
  120. };
  121. /**
  122. * @implements StateAPI#onChangeError
  123. */
  124. StateImpl.prototype.onChangeError = function onChangeError(callback) {
  125. this.registerCallback(StateImpl.ON_CHANGE_ERROR, callback);
  126. };
  127. return StateImpl;
  128. }(StateAPISpec);
  129. StateImpl.ON_CHANGE_ERROR = 'onChangeError';
  130. return StateImpl;
  131. });
  132. //# sourceMappingURL=StateImpl.js.map