@mondora/microfrontends
Version:
Library for embedding frontends into one another
34 lines • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const penpal_1 = require("penpal");
const constants_1 = require("./constants");
class ParentApp {
constructor(options) {
// State
this.launched = false;
this.parentMethods = null;
this.onLaunch = options.onLaunch;
this.parentOrigin = options.parentOrigin;
this.launch();
}
async call(method, ...params) {
if (!this.launched) {
throw new Error("Child app not yet launched: can't call parent methods before the launch");
}
return this.parentMethods[method](...params);
}
async launch() {
const connection = penpal_1.connectToParent({
parentOrigin: this.parentOrigin,
methods: {
[constants_1.SET_LAUNCH_DATA]: async (launchData) => {
await this.onLaunch.call(this, launchData);
this.launched = true;
}
}
});
this.parentMethods = await connection.promise;
}
}
exports.default = ParentApp;
//# sourceMappingURL=ParentApp.js.map