all files / src/ store.ts

83.61% Statements 51/61
50% Branches 7/14
66.67% Functions 6/9
86.44% Lines 51/59
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                                      41×     41× 41×                       16×                             14× 14×           18×             18×   18× 18× 18×     18×     18×              
/* 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 Redux = require("redux");
var redux_thunk_1 = require("redux-thunk");
var createLogger = require("redux-logger");
var Widgets = require("./widgets/widgets");
var WidgetConfig = require("./widgets/widgetConfig.js");
var Layouts = require("./layouts/layouts.js");
var Datasource = require("./datasource/datasource");
var DatasourceData = require("./datasource/datasourceData");
var Global = require("./dashboard/global.js");
var Import = require("./dashboard/import");
var Modal = require("./modal/modalDialog.js");
var Persist = require("./persistence");
var Plugins = require('./pluginApi/pluginLoader');
var redux_form_1 = require("redux-form");
var Action = require("./actionNames");
var WidgetPlugins = require("./widgets/widgetPlugins");
var DatasourcePlugins = require("./datasource/datasourcePlugins");
var Config = require("./config");
// TODO: name all reducers ***Reducer
var appReducer = Redux.combineReducers({
    config: Config.config,
    currentLayout: Layouts.currentLayout,
    datasourceData: DatasourceData.datasourceData,
    datasourcePlugins: DatasourcePlugins.datasourcePlugins,
    datasources: Datasource.datasources,
    form: redux_form_1.reducer,
    global: Global.global,
    layouts: Layouts.layouts,
    modalDialog: Modal.modalDialog,
    pluginLoader: Plugins.pluginLoaderReducer,
    widgetConfig: WidgetConfig.widgetConfigDialog,
    widgetPlugins: WidgetPlugins.widgetPlugins,
    widgets: Widgets.widgets
});
var reducer = function (state, action) {
    Iif (action.type === Action.CLEAR_STATE) {
        state = undefined;
    }
    state = Import.importReducer(state, action);
    return appReducer(state, action);
};
var logger = createLogger({
    duration: false,
    timestamp: true,
    logErrors: true,
    predicate: function (getState, action) {
        if (action.type.startsWith("redux-form")) {
            return false;
        }
        return !action.doNotLog;
    }
});
function emptyState() {
    return {
        config: null,
        widgets: {},
        datasources: {},
        datasourceData: {},
        datasourcePlugins: {},
        widgetPlugins: {},
        pluginLoader: {
            loadingUrls: []
        }
    };
}
exports.emptyState = emptyState;
/**
 * Create a store as empty as possible
 */
function createEmpty(options) {
    Iif (options === void 0) { options = { log: true }; }
    return create(emptyState(), options);
}
exports.createEmpty = createEmpty;
/**
 * Create a store with default values
 */
function createDefault(options) {
    Iif (options === void 0) { options = { log: true }; }
    return create(undefined, options);
}
exports.createDefault = createDefault;
function testStoreOptions() {
    return { log: false, persist: false };
}
exports.testStoreOptions = testStoreOptions;
function defaultStoreOptions() {
    return { log: true, persist: true };
}
exports.defaultStoreOptions = defaultStoreOptions;
/**
 * Create a store and execute all side-effects to have a consistent app
 */
function create(initialState, options) {
    if (!initialState) {
        initialState = Persist.loadFromLocalStorage();
    }
    var middleware = [];
    middleware.push(redux_thunk_1.default);
    Iif (options.persist) {
        middleware.push(Persist.persistenceMiddleware);
    }
    Iif (options.log) {
        middleware.push(logger); // must be last
    }
    return Redux.createStore(reducer, initialState, Redux.applyMiddleware.apply(Redux, middleware));
}
exports.create = create;
function clearState() {
    return {
        type: Action.CLEAR_STATE
    };
}
exports.clearState = clearState;