UNPKG

@pubby/sdk

Version:
134 lines (129 loc) 5.51 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib = require('tslib'); var module$1 = require('./module.js'); var meta = require('./meta.js'); require('./modules/index.js'); var index = require('./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(index.ConfigModule, config); } Pubby.prototype.use = function (ModuleConstructor, options) { if (!this[meta.$modulesMap].has(ModuleConstructor)) { // Adicionar a chave primeiro para indicar que o módulo está sendo inicializado (null) this[meta.$modulesMap].set(ModuleConstructor, null); // Instancia o módulo e define no mapa this[meta.$modulesMap].set(ModuleConstructor, new ModuleConstructor(this, options)); } return this; }; Pubby.prototype.has = function (name) { return this[name] instanceof module$1.PubbyModule; }; Pubby.prototype.require = function (ModuleConstructor) { if (!this[meta.$modulesMap].has(ModuleConstructor)) { throw new Error("Module " + ModuleConstructor.name + " is required!"); } return true; }; Pubby.prototype.init = function () { return tslib.__awaiter(this, void 0, void 0, function () { var _b, _c, module_1, e_1_1; var e_1, _d; return tslib.__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 = tslib.__values(this[meta.$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 tslib.__awaiter(this, void 0, void 0, function () { var _b, _c, module_2, e_2_1; var e_2, _d; return tslib.__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 = tslib.__values(this[meta.$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 = meta.$modulesMap; exports.Pubby = Pubby;