# Code coverage report for src/core/Cache.js

# all files / src/core/ Cache.js

72.34% Statements 34/47
63.64% Branches 14/22
62.5% Functions 10/16
72.34% Lines 34/47

    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
    1101×

    1×

    307×

    307×

    307×

    1×

    377×
    377×
    49×

    49×

    377×

    377×

    354×

    640×

    40×
    40×
    40×

    337×
    337×
    337×
    337×
    168×
    21×

    173×
    173×
    173×

    355×
    355×
    355×
    355×

    7×
    7×

    283×

    1×

     var DIRTY_PREFIX = '__dt__';

    var Cache = function () {

        this._contextId = 0;

        this._caches = [];

        this._context = {};
    };

    Cache.prototype = {

        use: function (contextId, documentSchema) {
            var caches = this._caches;
            if (!caches[contextId]) {
                caches[contextId] = {};

                Iif (documentSchema) {
                    caches[contextId] = documentSchema();
                }
            }
            this._contextId = contextId;

            this._context = caches[contextId];
        },

        put: function (key, value) {
            this._context[key] = value;
        },

        get: function (key) {
            return this._context[key];
        },

        dirty: function (field) {
            field = field || '';
            var key = DIRTY_PREFIX + field;
            this.put(key, true);
        },

        dirtyAll: function (field) {
            field = field || '';
            var key = DIRTY_PREFIX + field;
            var caches = this._caches;
            for (var i = 0; i < caches.length; i++) {
                if (caches[i]) {
                    caches[i][key] = true;
                }
            }
        },

        fresh: function (field) {
            field = field || '';
            var key = DIRTY_PREFIX + field;
            this.put(key, false);
        },

        freshAll: function (field) {
            field = field || '';
            var key = DIRTY_PREFIX + field;
            var caches = this._caches;
            for (var i = 0; i < caches.length; i++) {
                if (caches[i]) {
                    caches[i][key] = false;
                }
            }
        },

        isDirty: function (field) {
            field = field || '';
            var key = DIRTY_PREFIX + field;
            var context = this._context;
            return  !context.hasOwnProperty(key)
                || context[key] === true;
        },

        deleteContext: function (contextId) {
            delete this._caches[contextId];
            this._context = {};
        },

        delete: function (key) {
            delete this._context[key];
        },

        clearAll: function () {
            this._caches = {};
        },

        getContext: function () {
            return this._context;
        },

        eachContext : function (cb, context) {
            var keys = Object.keys(this._caches);
            keys.forEach(function (key) {
                cb && cb.call(context, key);
            });
        },

        miss: function (key) {
            return ! this._context.hasOwnProperty(key);
        }
    };

    Cache.prototype.constructor = Cache;

    export default Cache;