# all files / red/runtime/nodes/ context.js

100% Statements 44/44
80% Branches 16/20
100% Functions 8/8
100% Lines 44/44

    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

    1×
    1×
    1×

    1×
    51×
    51×
    51×
    30×

    51×
    16×

    51×
    3×

    51×

    1×
    1×

    1×
    32×
    32×
    16×

    32×
    3×

    29×
    29×
    16×

    29×
    29×

    29×
    29×

    1×
    1×
    1×
    1×

    1×

    1×
    14×
    14×
    14×
    14×
    28×
    28×
    28×
    28×

    1×

    22×

     /**
     * Copyright JS Foundation and other contributors, http://js.foundation
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     **/

    var clone = require("clone");
    var when = require("when");
    var util = require("../util");

    function createContext(id,seed) {
        var data = seed || {};
        var obj = seed || {};
        obj.get = function get(key) {
            return util.getMessageProperty(data,key);
        };
        obj.set = function set(key, value) {
            util.setMessageProperty(data,key,value);
        }
        obj.keys = function() {
            return Object.keys(data);
        }
        return obj;
    }

    var contexts = {};
    var globalContext = null;

    function getContext(localId,flowId) {
        var contextId = localId;
        if (flowId) {
            contextId = localId+":"+flowId;
        }
        if (contexts.hasOwnProperty(contextId)) {
            return contexts[contextId];
        }
        var newContext = createContext(contextId);
        if (flowId) {
            newContext.flow = getContext(flowId);
        }
        Eif (globalContext) {
            newContext.global = globalContext;
        }
        contexts[contextId] = newContext;
        return newContext;
    }
    function deleteContext(id,flowId) {
        var contextId = id;
        Eif (flowId) {
            contextId = id+":"+flowId;
        }
        delete contexts[contextId];
    }
    function clean(flowConfig) {
        var activeIds = {};
        var contextId;
        var node;
        for (var id in contexts) {
            Eif (contexts.hasOwnProperty(id)) {
                var idParts = id.split(":");
                Eif (!flowConfig.allNodes.hasOwnProperty(idParts[0])) {
                    delete contexts[id];
                }
            }
        }
    }
    module.exports = {
        init: function(settings) {
            globalContext = createContext("global",settings.functionGlobalContext || {});
        },
        get: getContext,
        delete: deleteContext,
        clean:clean
    };