@pubby/sdk
Version:
Pubby Development Kit
130 lines (127 loc) • 5.38 kB
JavaScript
import { __awaiter, __generator, __values } from 'tslib';
import { PubbyModule } from './module.js';
import { $modulesMap } from './meta.js';
import './modules/index.js';
import { ConfigModule } from './modules/config/index.js';
var _a;
var Pubby = /** @class */ (function () {
function Pubby(config) {
if (config === void 0) { config = {
baseURL: "https://api.pubby.club",
}; }
this[_a] = new Map();
this.initialized = false;
this.use(ConfigModule, config);
}
Pubby.prototype.use = function (ModuleConstructor, options) {
if (!this[$modulesMap].has(ModuleConstructor)) {
// Adicionar a chave primeiro para indicar que o módulo está sendo inicializado (null)
this[$modulesMap].set(ModuleConstructor, null);
// Instancia o módulo e define no mapa
this[$modulesMap].set(ModuleConstructor, new ModuleConstructor(this, options));
}
return this;
};
Pubby.prototype.has = function (name) {
return this[name] instanceof PubbyModule;
};
Pubby.prototype.require = function (ModuleConstructor) {
if (!this[$modulesMap].has(ModuleConstructor)) {
throw new Error("Module " + ModuleConstructor.name + " is required!");
}
return true;
};
Pubby.prototype.init = function () {
return __awaiter(this, void 0, void 0, function () {
var _b, _c, module_1, e_1_1;
var e_1, _d;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
if (this.initialized) {
return [2 /*return*/, this];
}
_e.label = 1;
case 1:
_e.trys.push([1, 6, 7, 8]);
_b = __values(this[$modulesMap].values()), _c = _b.next();
_e.label = 2;
case 2:
if (!!_c.done) return [3 /*break*/, 5];
module_1 = _c.value;
if (!(typeof module_1.init === "function")) return [3 /*break*/, 4];
console.log("INIT", module_1.constructor.name);
return [4 /*yield*/, Promise.resolve(module_1.init())];
case 3:
_e.sent();
_e.label = 4;
case 4:
_c = _b.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_1_1 = _e.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 8:
this.initialized = true;
return [2 /*return*/, this];
}
});
});
};
Pubby.prototype.dispose = function () {
return __awaiter(this, void 0, void 0, function () {
var _b, _c, module_2, e_2_1;
var e_2, _d;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
if (!this.initialized) {
return [2 /*return*/];
}
_e.label = 1;
case 1:
_e.trys.push([1, 6, 7, 8]);
_b = __values(this[$modulesMap].values()), _c = _b.next();
_e.label = 2;
case 2:
if (!!_c.done) return [3 /*break*/, 5];
module_2 = _c.value;
if (!(typeof module_2.dispose === "function")) return [3 /*break*/, 4];
console.log("DISPOSE", module_2.constructor.name);
return [4 /*yield*/, Promise.resolve(module_2.dispose())];
case 3:
_e.sent();
_e.label = 4;
case 4:
_c = _b.next();
return [3 /*break*/, 2];
case 5: return [3 /*break*/, 8];
case 6:
e_2_1 = _e.sent();
e_2 = { error: e_2_1 };
return [3 /*break*/, 8];
case 7:
try {
if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
}
finally { if (e_2) throw e_2.error; }
return [7 /*endfinally*/];
case 8:
this.initialized = false;
return [2 /*return*/];
}
});
});
};
return Pubby;
}());
_a = $modulesMap;
export { Pubby };