all files / src/datasource/ datasourcePluginFactory.ts

65.38% Statements 34/52
37.5% Branches 6/16
63.64% Functions 7/11
65.38% Lines 34/52
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                                                                                                                           
/* 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;