@atlaskit/util-service-support
Version:
A library of support classes for integrating React components with REST HTTP services
58 lines • 1.62 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
export var AbstractResource = /*#__PURE__*/function () {
function AbstractResource() {
_classCallCheck(this, AbstractResource);
_defineProperty(this, "listeners", new Set());
}
return _createClass(AbstractResource, [{
key: "subscribe",
value: function subscribe(onChange) {
this.listeners.add(onChange);
if (this.lastResult) {
// Notify subscribe of last result (i.e. initial state)
onChange.result(this.lastResult);
}
}
}, {
key: "unsubscribe",
value: function unsubscribe(onChange) {
this.listeners.delete(onChange);
}
}, {
key: "notifyResult",
value: function notifyResult(result) {
this.listeners.forEach(function (onChange) {
onChange.result(result);
});
}
}, {
key: "notifyError",
value: function notifyError(error) {
this.listeners.forEach(function (onChange) {
if (onChange.error) {
onChange.error(error);
}
});
}
}, {
key: "notifyInfo",
value: function notifyInfo(info) {
this.listeners.forEach(function (onChange) {
if (onChange.info) {
onChange.info(info);
}
});
}
}, {
key: "notifyNotReady",
value: function notifyNotReady() {
this.listeners.forEach(function (onChange) {
if (onChange.notReady) {
onChange.notReady();
}
});
}
}]);
}();