UNPKG

3.57 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.LibraryPlugin = exports.isViewLibrary = void 0;
4const tslib_1 = require("tslib");
5const abstract_1 = require("./abstract");
6function isViewLibrary(profile) {
7 return !!profile.location;
8}
9exports.isViewLibrary = isViewLibrary;
10class LibraryPlugin extends abstract_1.Plugin {
11 constructor(library, profile) {
12 super(profile);
13 this.library = library;
14 this.profile = profile;
15 profile.methods.forEach(method => {
16 if (!library[method]) {
17 throw new Error(`Method ${method} is exposed by LibraryPlugin ${profile.name}. But library doesn't expose this method`);
18 }
19 });
20 this.isView = isViewLibrary(profile);
21 if (this.isView && !this['render']) {
22 throw new Error(`Profile ${profile.name} define the location ${profile.location}, but method "render" is not implemented`);
23 }
24 }
25 activate() {
26 const _super = Object.create(null, {
27 activate: { get: () => super.activate }
28 });
29 return tslib_1.__awaiter(this, void 0, void 0, function* () {
30 if (this.isView) {
31 yield this.call(this.profile.location, 'addView', this.profile, this['render']());
32 }
33 // Forward event to the library
34 if (this.profile.notifications) {
35 if (!this.library.events || !this.library.events.emit) {
36 throw new Error(`"events" object from Library of plugin ${this.name} should implement "emit"`);
37 }
38 Object.keys(this.profile.notifications).forEach(name => {
39 this.profile.notifications[name].forEach(key => {
40 this.on(name, key, (payload) => this.library.events.emit(`[${name}] ${key}`, ...payload));
41 });
42 });
43 }
44 // Start listening on events from the library
45 if (this.profile.events) {
46 if (!this.library.events || !this.library.events.emit) {
47 throw new Error(`"events" object from Library of plugin ${this.name} should implement "emit"`);
48 }
49 this.profile.events.forEach(event => {
50 this.library.events.on(event, (...payload) => this.emit(event, ...payload));
51 });
52 }
53 // Start listening before running onActivation
54 _super.activate.call(this);
55 });
56 }
57 deactivate() {
58 var _a;
59 if (this.isView) {
60 this.call(this.profile.location, 'removeView', this.profile);
61 }
62 // Stop listening on events
63 if (this.profile.notifications) {
64 Object.keys(this.profile.notifications).forEach(name => {
65 this.profile.notifications[name].forEach(key => this.off(name, key));
66 });
67 }
68 // Stop listening on events from the library
69 (_a = this.profile.events) === null || _a === void 0 ? void 0 : _a.forEach(e => { var _a; return (_a = this.library.events) === null || _a === void 0 ? void 0 : _a.removeAllListeners(e); });
70 super.deactivate();
71 }
72 /** Call a method from this plugin */
73 callPluginMethod(key, payload) {
74 if (!this.library[key]) {
75 throw new Error(`LibraryPlugin ${this.name} doesn't expose method ${key}`);
76 }
77 return this.library[key](...payload);
78 }
79}
80exports.LibraryPlugin = LibraryPlugin;
81//# sourceMappingURL=library.js.map
\No newline at end of file