opendash
Version:
open.DASH data visualization framework
96 lines • 3.53 kB
JavaScript
import { equals } from "..";
var BaseService = /** @class */ (function () {
function BaseService() {
this.enabled = false;
this.loading = true;
this.subscribers = new Set();
}
BaseService.prototype.isEnabled = function () {
return this.enabled;
};
BaseService.prototype.isLoading = function () {
return this.loading;
};
BaseService.prototype.wait = function () {
var _this = this;
if (!this.loading) {
return Promise.resolve();
}
return new Promise(function (resolve) {
var unsubscribe = _this.subscribe(function () {
resolve();
unsubscribe();
});
});
};
BaseService.prototype.subscribe = function (callback) {
var _this = this;
this.subscribers.add(callback);
return function () {
_this.subscribers.delete(callback);
};
};
BaseService.prototype.setLoading = function (value) {
this.loading = value;
this.notifySubscribers();
};
BaseService.prototype.notifySubscribers = function () {
this.subscribers.forEach(function (callback) {
try {
callback();
}
catch (error) {
console.error("Error in service subscription:", error);
}
});
};
BaseService.prototype.clearSubscribers = function () {
this.subscribers.clear();
};
BaseService.prototype.initAdapter = function (adapter, context, services) {
if (!adapter) {
return;
}
this.enabled = true;
adapter.onContext(context);
if (adapter.onUser) {
var current_1 = undefined;
var updateUserOnChange_1 = function () {
if (!services.UserService.isLoading()) {
if (!equals(current_1, services.UserService.currentUser())) {
current_1 = services.UserService.currentUser();
adapter.onUser(current_1);
}
}
};
updateUserOnChange_1();
services.UserService.subscribe(function () {
updateUserOnChange_1();
});
}
if (adapter.onSource) {
var current_2 = undefined;
var descendentsIds_1 = [];
var updateSourceOnChange_1 = function () {
if (!services.SourceService.isLoading()) {
var localCurrent = services.SourceService.getCurrent();
var localDescendents = services.SourceService.getDescendents(localCurrent);
var localDescendentsIds = localDescendents.map(function (source) { return source.id; });
if (!equals(current_2, localCurrent) ||
!equals(descendentsIds_1, localDescendentsIds)) {
current_2 = localCurrent;
descendentsIds_1 = localDescendentsIds;
adapter.onSource(localCurrent, localDescendents);
}
}
};
updateSourceOnChange_1();
services.SourceService.subscribe(function () {
updateSourceOnChange_1();
});
}
};
return BaseService;
}());
export { BaseService };
//# sourceMappingURL=BaseService.js.map