all files / src/ persistence.ts

29.31% Statements 17/58
4.55% Branches 1/22
11.11% Functions 1/9
29.82% Lines 17/57
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                                                                                                                                                     
/* 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 $ = require('jquery');
var lastAction = { type: "NONE" };
var allowSave = true;
var saveTimeout;
function clearData() {
    if (window.confirm("Wipe app data and reload page?")) {
        if (saveTimeout) {
            clearTimeout(saveTimeout);
        }
        window.localStorage.setItem("appState", undefined);
        location.reload();
    }
}
exports.clearData = clearData;
// TODO: type middleware
function persistenceMiddleware(_a) {
    var getState = _a.getState;
    return function (next) { return function (action) {
        var nextState = next(action);
        if (!allowSave) {
            lastAction = action;
            return nextState;
        }
        if (!action.doNotPersist) {
            // we wait some before we save
            // this leads to less saving (max every 100ms) without loosing actions
            // if we would just block saving for some time after saving an action we would loose the last actions
            allowSave = false;
            saveTimeout = setTimeout(function () {
                save(getState());
                console.log('Saved state @' + lastAction.type);
                allowSave = true;
            }, 100);
        }
        lastAction = action;
        return nextState;
    }; };
}
exports.persistenceMiddleware = persistenceMiddleware;
function save(state) {
    var target = state.config.persistenceTarget;
    var savableState = _.assign({}, state);
    delete savableState.form;
    delete savableState.modalDialog;
    if (target === "local-storage") {
        saveToLocalStorage(savableState);
    }
    else if (target) {
        saveToServer(target, savableState);
    }
}
function saveToServer(target, state) {
    $.post({
        url: target,
        data: JSON.stringify(state),
        dataType: 'json',
        contentType: "application/json; charset=utf-8"
    });
}
function saveToLocalStorage(state) {
    if (typeof window === 'undefined') {
        console.warn("Can not save to local storage in current environment.");
        return;
    }
    window.localStorage.setItem("appState", JSON.stringify(state));
}
function loadFromLocalStorage() {
    Eif (typeof window === 'undefined') {
        console.warn("Can not load from local storage in current environment.");
        return undefined;
    }
    var stateString = window.localStorage.getItem("appState");
    var state = undefined;
    try {
        if (stateString !== undefined && stateString !== "undefined") {
            state = JSON.parse(stateString);
        }
    }
    catch (e) {
        console.error("Failed to load state from local storage. Data:", stateString, e.message);
    }
    console.log("Loaded state:", state);
    return state !== null ? state : undefined;
}
exports.loadFromLocalStorage = loadFromLocalStorage;