pandora
Version:
103 lines • 3.85 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* The reconciler of applet
* 1. Inject the applets, and manage them
* 2. Resolve the applet's config
* 3. Instantiate the applet and drive it's lifecycle
*/
class AppletReconciler {
/**
* @param {ProcessRepresentation} processRepresentation - ProcessRepresentation object
* @param {WorkerContext} context - WorkerContext object
*/
constructor(processRepresentation, context) {
this.appletSet = [];
this.instanceMap = new Map;
this.processRepresentation = processRepresentation;
this.context = context;
}
/**
* Receive an applet by AppletRepresentation
* @param {AppletRepresentation} applet - AppletRepresentation object
* @returns {void}
*/
receiveAppletRepresentation(applet) {
this.appletSet.push(applet);
// Midway 5 有需求,在 Service 启动阶段中访问未启动 Applet,预先实例化一下
this.getAppletInstance(applet);
}
/**
* Get an AppletRepresentation object by certain applet's name
* @param name - Name of applet
* @returns {AppletRepresentation}
*/
getRepresentationByAppletName(name) {
for (const applet of this.appletSet) {
if (applet.appletName === name) {
return applet;
}
}
return null;
}
/**
* Get an instance of applet
* @param {AppletRepresentation | string} represent - AppletRepresentation or applet's name
* @return {Applet}
*/
getAppletInstance(represent) {
if (typeof represent === 'string') {
represent = this.getRepresentationByAppletName(represent);
}
if (!represent) {
return null;
}
if (!this.instanceMap.has(represent)) {
represent.config = represent.configResolver ?
represent.configResolver(this.context.workerContextAccessor, represent.config) : represent.config;
const Entry = represent.appletEntry;
const applet = new Entry({
appletName: represent.appletName,
category: represent.category,
config: represent.config,
context: this.context.workerContextAccessor
});
this.instanceMap.set(represent, applet);
}
return this.instanceMap.get(represent);
}
/**
* Start all the applets
* @return {Promise<void>}
*/
start() {
return __awaiter(this, void 0, void 0, function* () {
for (const represent of this.appletSet) {
const inst = this.getAppletInstance(represent);
inst.start && (yield inst.start());
}
});
}
/**
* Stop all the applets
* @return {Promise<void>}
*/
stop() {
return __awaiter(this, void 0, void 0, function* () {
for (const represent of this.appletSet) {
const inst = this.getAppletInstance(represent);
inst.stop && (yield inst.stop());
}
});
}
}
exports.AppletReconciler = AppletReconciler;
//# sourceMappingURL=AppletReconciler.js.map