all files / src/datasource/ datasourcePluginInstance.ts

28.77% Statements 21/73
2.78% Branches 1/36
20% Functions 2/10
28.77% Lines 21/73
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                                                                                                                                                                                                                   
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var Datasource = require("./datasource");
var DatasourceData = require("./datasourceData");
var pluginTypes_1 = require("../pluginApi/pluginTypes");
/**
 * Represents a plugin instance, state should be saved in store!
 */
var DatasourcePluginInstance = (function () {
    function DatasourcePluginInstance(id, store) {
        var _this = this;
        this.id = id;
        this.store = store;
        this.oldDsState = null;
        this.frameInitialized = false;
        this.disposed = false;
        Iif (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 dsState = state.datasources[id];
            if (dsState === 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 (dsState !== _this.oldDsState) {
                console.log("old state: ", _this.oldDsState);
                _this.oldDsState = dsState;
                console.log("old state: ", _this.oldDsState);
                _this.sendDatasourceState();
            }
        });
    }
    Object.defineProperty(DatasourcePluginInstance.prototype, "state", {
        get: function () {
            var state = this.store.getState();
            var dsState = state.datasources[this.id];
            if (!dsState) {
                throw new Error("Can not get state of non existing datasource with id " + this.id);
            }
            return dsState;
        },
        enumerable: true,
        configurable: true
    });
    DatasourcePluginInstance.prototype.handleMessage = function (msg) {
        switch (msg.type) {
            case pluginTypes_1.MESSAGE_INIT: {
                this.frameInitialized = true;
                this.sendInitialDatasourceState();
                this.store.dispatch(Datasource.finishedLoading(this.id));
                console.log("Datasource initialized");
                break;
            }
            case pluginTypes_1.MESSAGE_DATA: {
                this.store.dispatch(DatasourceData.fetchedDatasourceData(this.state.id, msg.payload));
                break;
            }
            default:
                break;
        }
    };
    DatasourcePluginInstance.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, '*');
    };
    DatasourcePluginInstance.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;
    };
    DatasourcePluginInstance.prototype.sendDatasourceState = function () {
        console.log("Send state to datasource");
        var state = this.store.getState();
        var dsState = state.datasources[this.id];
        this.sendMessage({
            type: pluginTypes_1.MESSAGE_STATE,
            payload: dsState
        });
    };
    DatasourcePluginInstance.prototype.sendInitialDatasourceState = function () {
        var state = this.store.getState();
        var dsState = state.datasources[this.id];
        this.sendMessage({
            type: pluginTypes_1.MESSAGE_INITIAL_STATE,
            payload: dsState
        });
    };
    return DatasourcePluginInstance;
}());
exports.DatasourcePluginInstance = DatasourcePluginInstance;