all files / src/widgets/ widgetPluginInstance.ts

17.07% Statements 14/82
0% Branches 0/38
6.67% Functions 1/15
17.07% Lines 14/82
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                                                                                                                                                                                                                                                         
"use strict";
var Widgets = require("./widgets");
var pluginTypes_1 = require("../pluginApi/pluginTypes");
var WidgetPluginInstance = (function () {
    function WidgetPluginInstance(id, store) {
        var _this = this;
        this.id = id;
        this.store = store;
        this.frameInitialized = false;
        this.disposed = false;
        this.oldWidgetState = null;
        this.oldDatasourceData = {};
        if (typeof window !== 'undefined') {
            this.messageListener = function (e) {
                if (_this.disposed) {
                    // TODO: better unit test than runtime checking
                    console.error("Message listener called but WidgetPluginInstance is already disposed");
                    return;
                }
                if (!_this._iFrame && e.origin === "null") {
                    console.log("Discarding message because iFrame not set yet", e.data);
                }
                if (_this._iFrame !== undefined && e.origin === "null" && e.source === _this._iFrame.contentWindow) {
                    _this.handleMessage(e.data);
                }
            };
            window.addEventListener('message', this.messageListener);
        }
        this.unsubscribeStore = store.subscribe(function () {
            if (_this.disposed) {
                // TODO: better unit test than runtime checking
                console.error("Store change observed but WidgetPluginInstance is already disposed");
                return;
            }
            if (!_this.frameInitialized) {
                // We get invalid caches when we send state to the iFrame before it is ready
                return;
            }
            var state = store.getState();
            var widgetState = state.widgets[id];
            if (widgetState === undefined) {
                // This happens for example during import. Where the state is cleared but this class not yet disposed.
                // So we just silently return.
                return;
            }
            if (widgetState !== _this.oldWidgetState) {
                _this.oldWidgetState = widgetState;
                _this.sendPluginState();
            }
            _this.updateDatasourceDataInFrame();
        });
    }
    Object.defineProperty(WidgetPluginInstance.prototype, "iFrame", {
        set: function (element) {
            this._iFrame = element;
            this.sendMessage({ type: pluginTypes_1.MESSAGE_INIT });
        },
        enumerable: true,
        configurable: true
    });
    WidgetPluginInstance.prototype.updateDatasourceDataInFrame = function () {
        var _this = this;
        var state = this.store.getState();
        var widgetState = state.widgets[this.id];
        var widgetPluginState = state.widgetPlugins[widgetState.type];
        widgetPluginState.typeInfo.settings.filter(function (s) {
            return s.type === "datasource";
        }).map(function (s) {
            return widgetState.settings[s.id];
        }).forEach(function (dsId) {
            if (state.datasources[dsId] === undefined) {
                return;
            }
            var data = state.datasourceData[dsId];
            if (data !== _this.oldDatasourceData[dsId]) {
                _this.oldDatasourceData[dsId] = data;
                _this.sendDatasourceData(dsId);
            }
        });
    };
    WidgetPluginInstance.prototype.handleMessage = function (msg) {
        switch (msg.type) {
            case 'init': {
                this.frameInitialized = true;
                this.sendPluginState();
                this.updateDatasourceDataInFrame();
                break;
            }
            case 'updateSetting': {
                this.updateSetting(msg.payload.id, msg.payload.value);
                break;
            }
            default:
                break;
        }
    };
    WidgetPluginInstance.prototype.sendMessage = function (msg) {
        if (!this._iFrame.contentWindow) {
            // This happens during import. We ignore it silently and rely on later disposal to free memory.
            // TODO: Find a way to dispose this instance before this happens.
            return;
        }
        this._iFrame.contentWindow.postMessage(msg, '*');
    };
    WidgetPluginInstance.prototype.sendPluginState = function () {
        var state = this.store.getState();
        var widgetState = state.widgets[this.id];
        this.sendMessage({
            type: pluginTypes_1.MESSAGE_STATE,
            payload: widgetState
        });
    };
    WidgetPluginInstance.prototype.sendDatasourceData = function (dsId) {
        var state = this.store.getState();
        this.sendMessage({
            type: pluginTypes_1.MESSAGE_DATA,
            payload: {
                id: dsId,
                data: state.datasourceData[dsId]
            }
        });
    };
    WidgetPluginInstance.prototype.updateSetting = function (settingId, value) {
        this.store.dispatch(Widgets.updatedSingleSetting(this.id, settingId, value));
    };
    WidgetPluginInstance.prototype.dispose = function () {
        if (!this.disposed && _.isFunction(this.unsubscribeStore)) {
            this.unsubscribeStore();
        }
        if (!this.disposed && _.isFunction(this.messageListener)) {
            window.removeEventListener("message", this.messageListener);
        }
        this.disposed = true;
    };
    return WidgetPluginInstance;
}());
exports.WidgetPluginInstance = WidgetPluginInstance;