all files / src/datasource/ datasourcePlugins.ts

64.1% Statements 25/39
52.94% Branches 9/17
33.33% Functions 2/6
63.16% Lines 24/38
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                                                                                                                        43× 43× 43×                                 39×                                                      
/* 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 Action = require("../actionNames");
var reducer_js_1 = require("../util/reducer.js");
var dashboard_1 = require("../dashboard");
// TODO: does it work to have the URL as ID?
var initialState = {
    "random": {
        id: "random",
        url: "./plugins/datasources/randomDatasource.js",
        typeInfo: {
            type: "will-be-loaded"
        },
        isLoading: true
    },
    "time": {
        id: "time",
        url: "./plugins/datasources/timeDatasource.js",
        typeInfo: {
            type: "will-be-loaded"
        },
        isLoading: true
    },
    "static-data": {
        id: "static-data",
        url: "./plugins/datasources/staticData.js",
        typeInfo: {
            type: "will-be-loaded"
        },
        isLoading: true
    },
    "digimondo-firefly-datasource": {
        id: "digimondo-firefly-datasource",
        url: "./plugins/datasources/digimondoFirefly.js",
        typeInfo: {
            type: "will-be-loaded"
        },
        isLoading: true
    }
};
function unloadPlugin(type) {
    return function (dispatch) {
        // When the plugin is still loading, or never loaded successfully we can not find it
        if (dashboard_1.default.getInstance().widgetPluginRegistry.hasPlugin(type)) {
            var dsFactory = dashboard_1.default.getInstance().datasourcePluginRegistry.getPlugin(type);
            dsFactory.dispose();
        }
        // TODO: Should we remove the url from plugin loader and cancel loading when the plugin is still loading?
        dispatch(deletePlugin(type));
    };
}
exports.unloadPlugin = unloadPlugin;
function usePublishedDatasourcePlugin(type, url, typeInfo) {
    return {
        type: Action.USE_PUBLISHED_DATASOURCE_PLUGIN,
        id: type,
        url: url,
        typeInfo: typeInfo
    };
}
exports.usePublishedDatasourcePlugin = usePublishedDatasourcePlugin;
function deletePlugin(type) {
    return {
        type: Action.DELETE_DATASOURCE_PLUGIN,
        id: type
    };
}
var pluginsCrudReducer = reducer_js_1.genCrudReducer([Action.DATASOURCE_PLUGIN_FINISHED_LOADING, Action.DELETE_DATASOURCE_PLUGIN], datasourcePlugin);
function datasourcePlugins(state, action) {
    if (state === void 0) { state = initialState; }
    state = pluginsCrudReducer(state, action);
    switch (action.type) {
        case Action.USE_PUBLISHED_DATASOURCE_PLUGIN: {
            if (state[action.id]) {
                return _.assign({}, state, (_a = {},
                    _a[action.id] = datasourcePlugin(state[action.id], action),
                    _a
                ));
            }
            return state;
        }
        case Action.STARTED_LOADING_PLUGIN_FROM_URL: {
            if (state[action.id]) {
                return _.assign({}, state, (_b = {},
                    _b[action.id] = datasourcePlugin(state[action.id], action),
                    _b
                ));
            }
            return state;
        }
        default:
            return state;
    }
    var _a, _b;
}
exports.datasourcePlugins = datasourcePlugins;
function datasourcePlugin(state, action) {
    switch (action.type) {
        case Action.USE_PUBLISHED_DATASOURCE_PLUGIN: {
            return _.assign({}, state, {
                url: action.url,
                typeInfo: action.typeInfo
            });
        }
        case Action.DATASOURCE_PLUGIN_FINISHED_LOADING:
            Iif (!action.typeInfo.type) {
                // TODO: Catch this earlier
                throw new Error("A Plugin needs a type name. Please define TYPE_INFO.type");
            }
            return {
                id: action.typeInfo.type,
                url: action.url,
                typeInfo: action.typeInfo,
                isLoading: false
            };
        case Action.STARTED_LOADING_PLUGIN_FROM_URL:
            return _.assign({}, state, {
                isLoading: true
            });
        default:
            return state;
    }
}