/* 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;
|