/* 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 _ = require("lodash");
var Datasource = require("./datasource");
var datasourcePluginInstance_1 = require("./datasourcePluginInstance");
/**
* Connects a datasource to the application state
*/
var DataSourcePluginFactory = (function () {
function DataSourcePluginFactory(_type, _datasource, _store) {
this._type = _type;
this._datasource = _datasource;
this._store = _store;
this._pluginInstances = {};
this._disposed = false;
//this._unsubscribe = _store.subscribe(() => this.handleStateChange());
}
Object.defineProperty(DataSourcePluginFactory.prototype, "type", {
get: function () {
return this._type;
},
enumerable: true,
configurable: true
});
Object.defineProperty(DataSourcePluginFactory.prototype, "disposed", {
get: function () {
return this._disposed;
},
enumerable: true,
configurable: true
});
DataSourcePluginFactory.prototype.getInstance = function (id) {
Iif (this._disposed === true) {
throw new Error("Try to get datasource of destroyed type. " + JSON.stringify({ id: id, type: this.type }));
}
Eif (!this._pluginInstances[id]) {
return this.createInstance(id);
}
return this._pluginInstances[id];
};
DataSourcePluginFactory.prototype.dispose = function () {
this._disposed = true;
_.valuesIn(this._pluginInstances).forEach(function (plugin) {
if (_.isFunction(plugin.dispose)) {
try {
plugin.dispose();
}
catch (e) {
console.error("Failed to destroy Datasource instance", plugin);
}
}
});
this._pluginInstances = {};
};
DataSourcePluginFactory.prototype.handleStateChange = function () {
var _this = this;
var state = this._store.getState();
if (this.oldDatasourcesState === state.datasources) {
return;
}
this.oldDatasourcesState = state.datasources;
// Create Datasource instances for missing data sources in store
_.valuesIn(state.datasources)
.filter(function (dsState) { return dsState.type === _this.type; })
.forEach(function (dsState) {
if (_this._pluginInstances[dsState.id] === undefined) {
_this._pluginInstances[dsState.id] = _this.createInstance(dsState.id);
_this._store.dispatch(Datasource.finishedLoading(dsState.id));
}
});
};
DataSourcePluginFactory.prototype.createInstance = function (id) {
Iif (this._disposed === true) {
throw new Error("Try to create datasource of destroyed type: " + JSON.stringify({ id: id, type: this.type }));
}
Iif (this._pluginInstances[id] !== undefined) {
throw new Error("Can not create datasource instance. It already exists: " + JSON.stringify({
id: id,
type: this.type
}));
}
var state = this._store.getState();
var dsState = state.datasources[id];
if (!dsState) {
throw new Error("Can not create instance of non existing datasource with id " + id);
}
return new datasourcePluginInstance_1.DatasourcePluginInstance(id, this._store);
};
return DataSourcePluginFactory;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = DataSourcePluginFactory;
|