UNPKG

@ayanaware/bento

Version:

Modular runtime framework designed to solve complex tasks

156 lines 4.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntityLoader = void 0; const errors_1 = require("@ayanaware/errors"); const Entity_1 = require("../../entities/interfaces/Entity"); class EntityLoader { constructor() { this.name = 'EntityLoader'; /** * Entities we manage */ this.entities = new Set(); this.pending = new Set(); } async onLoad() { return this.handlePending(); } async onUnload() { // unload entities we manage for (const name of this.entities) await this.api.bento.removeEntity(name); } async handlePending() { if (this.pending.size < 1) return; for (const entity of this.pending) { const name = await this.api.bento.addEntity(entity); this.entities.add(name); this.pending.delete(entity); } } /** * Detects if a value is Entitylike * Entitylike values are not null objects or functions * * @param v Value * @returns boolean */ isEntitylike(v) { return this.isClasslike(v) || this.isFunctionlike(v); } /** * Detects if a value is Classlike. * Classlike values are functions that have a prototype object * * @param v Value * @returns boolean */ isClasslike(v) { return typeof v === 'function' && typeof v.prototype === 'object'; } /** * Detects if a value is Functionlike * Functionlike values are functions that have no prototype object * * @param v Value * @returns boolean */ isFunctionlike(v) { return typeof v === 'function' && typeof v.prototype === 'undefined'; } /** * Tries to find Entity and then instantiate it. An Entity can be a class or an object. * * @param entity Uninstantiated Entity * * @throws ProcessingError If no Entity found or fails to instantiate * @returns EntityInstance */ instantiate(entity) { let instance; // instance Classlike or Functionlike try { if (this.isClasslike(entity)) instance = new entity(); else if (this.isFunctionlike(entity)) instance = entity(); else instance = entity; } catch (e) { throw new errors_1.ProcessingError('instantiate(): Failed to instantiate').setCause(e); } // check for `name` if (typeof instance.name !== 'string') throw new errors_1.ProcessingError('instantiate(): Instance does not have the name property'); return instance; } /** * Bulk Instantiate Entities and add them to Bento * * @param entities Class or Object Array * @param type EntityType */ async addEntities(entities, type) { const promises = entities.map(async (entity) => this.addEntity(entity, type)); return Promise.all(promises); } /** * Instantiate Entity and add to Bento * * @param entity Class or Object * @param type EntityType */ async addEntity(entity, type) { if (!this.isEntitylike(entity)) throw new errors_1.IllegalArgumentError('addEntity(): Value not Entitylike'); const instance = this.instantiate(entity); instance.type = type; // API not available. Add to pending if (!this.api) { this.pending.add(instance); return; } const name = await this.api.bento.addEntity(instance); this.entities.add(name); } /** * Bulk Instantiate Plugins and add them to Bento * * @param plugins Class or Object Array */ async addPlugins(plugins) { const promises = plugins.map(async (plugin) => this.addPlugin(plugin)); return Promise.all(promises); } /** * Instantiate Plugin and add to Bento * @param plugin Class or Object * * @returns Promise */ async addPlugin(plugin) { return this.addEntity(plugin, Entity_1.EntityType.PLUGIN); } /** * Bulk Instantiate Components and add them to Bento * * @param components Class or Object Array */ async addComponents(components) { const promises = components.map(async (component) => this.addComponent(component)); return Promise.all(promises); } /** * Instantiate Component and add to Bento * @param component Class or Object * * @returns Promise */ async addComponent(component) { return this.addEntity(component, Entity_1.EntityType.COMPONENT); } } exports.EntityLoader = EntityLoader; //# sourceMappingURL=EntityLoader.js.map