1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.LibraryPlugin = exports.isViewLibrary = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const abstract_1 = require("./abstract");
|
6 | function isViewLibrary(profile) {
|
7 | return !!profile.location;
|
8 | }
|
9 | exports.isViewLibrary = isViewLibrary;
|
10 | class 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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 | }
|
80 | exports.LibraryPlugin = LibraryPlugin;
|
81 |
|
\ | No newline at end of file |