Merge pull request #2030 from node-red-hitachi/scope-parent

Allow access of scope parent
This commit is contained in:
Nick O'Leary
2019-02-06 14:10:57 +00:00
committed by GitHub
2 changed files with 126 additions and 4 deletions

View File

@@ -18,6 +18,7 @@ var clone = require("clone");
var log = require("@node-red/util").log;
var util = require("@node-red/util").util;
var memory = require("./memory");
var flows;
var settings;
@@ -47,6 +48,7 @@ function logUnknownStore(name) {
}
function init(_settings) {
flows = require("../flows");
settings = _settings;
contexts = {};
stores = {};
@@ -198,8 +200,24 @@ function getContextStorage(storage) {
}
}
function followParentContext(parent, key) {
if (key === "$parent") {
return [parent, undefined];
}
else if (key.startsWith("$parent.")) {
var len = "$parent.".length;
var new_key = key.substring(len);
var ctx = parent;
while (ctx && new_key.startsWith("$parent.")) {
ctx = ctx.$parent;
new_key = new_key.substring(len);
}
return [ctx, new_key];
}
return null;
}
function createContext(id,seed) {
function createContext(id,seed,parent) {
// Seed is only set for global context - sourced from functionGlobalContext
var scope = id;
var obj = seed || {};
@@ -254,6 +272,21 @@ function createContext(id,seed) {
if (!storage) {
storage = keyParts.store || "_";
}
var result = followParentContext(parent, key);
if (result) {
var [ctx, new_key] = result;
if (ctx && new_key) {
return ctx.get(new_key, storage, callback);
}
else {
if (callback) {
return callback(undefined);
}
else {
return undefined;
}
}
}
} else {
if (!storage) {
storage = "_";
@@ -321,6 +354,19 @@ function createContext(id,seed) {
if (!storage) {
storage = keyParts.store || "_";
}
var result = followParentContext(parent, key);
if (result) {
var [ctx, new_key] = result;
if (ctx && new_key) {
return ctx.set(new_key, value, storage, callback);
}
else {
if (callback) {
return callback();
}
return undefined;
}
}
} else {
if (!storage) {
storage = "_";
@@ -361,10 +407,36 @@ function createContext(id,seed) {
}
}
});
if (parent) {
Object.defineProperty(obj, "$parent", {
value: parent
});
}
return obj;
}
function getContext(localId,flowId) {
function createRootContext() {
var obj = {};
Object.defineProperties(obj, {
get: {
value: function(key, storage, callback) {
return undefined;
}
},
set: {
value: function(key, value, storage, callback) {
}
},
keys: {
value: function(storage, callback) {
return undefined;
}
}
});
return obj;
}
function getContext(localId,flowId,parent) {
var contextId = localId;
if (flowId) {
contextId = localId+":"+flowId;
@@ -372,10 +444,19 @@ function getContext(localId,flowId) {
if (contexts.hasOwnProperty(contextId)) {
return contexts[contextId];
}
var newContext = createContext(contextId);
var newContext = createContext(contextId,undefined,parent);
if (flowId) {
var node = flows.get(flowId);
var parent = undefined;
if (node && node.type.startsWith("subflow:")) {
parent = node.context().flow;
}
else {
parent = createRootContext();
}
var flowContext = getContext(flowId,undefined,parent);
Object.defineProperty(newContext, 'flow', {
value: getContext(flowId)
value: flowContext
});
}
Object.defineProperty(newContext, 'global', {