Add node context/flow/global

This commit is contained in:
Nick O'Leary 2015-12-29 22:16:51 +00:00
parent b95dc2ecce
commit 5ead3342cc
6 changed files with 203 additions and 2 deletions

View File

@ -20,7 +20,7 @@ var when = require("when");
var redUtil = require("../util");
var Log = require("../log");
var context = require("./context");
var flows = require("./flows");
function Node(n) {
@ -63,6 +63,12 @@ Node.prototype.updateWires = function(wires) {
}
}
Node.prototype.context = function() {
if (!this._context) {
this._context = context.get(this._alias||this.id,this.z);
}
return this._context;
}
Node.prototype._on = Node.prototype.on;

View File

@ -0,0 +1,80 @@
/**
* Copyright 2015 IBM Corp.
*
* 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 = {};
var obj = seed || {};
obj.get = function get(key) {
return util.getMessageProperty(data,key);
};
obj.set = function set(key, value) {
util.setMessageProperty(data,key,value);
}
return obj;
}
var contexts = {};
var globalContext = null;
function getContext(localId,flowId) {
var contextId = localId;
if (flowId) {
contextId = localId+":"+flowId;
}
if (contexts[contextId]) {
return contexts[contextId];
}
var newContext = createContext(contextId);
if (flowId) {
newContext.flow = getContext(flowId);
if (globalContext) {
newContext.global = globalContext;
}
}
contexts[contextId] = newContext;
return newContext;
}
function deleteContext(id,flowId) {
var contextId = id;
if (flowId) {
contextId = id+":"+flowId;
}
delete contexts[contextId];
}
function clean(flowConfig) {
var activeIds = {};
var contextId;
var node;
for (var id in contexts) {
if (contexts.hasOwnProperty(id)) {
var idParts = id.split(":");
if (!flowConfig.allNodes[idParts[0]]) {
delete contexts[id];
}
}
}
}
module.exports = {
init: function(settings) {
globalContext = createContext("global",settings.functionGlobalContext || {});
},
get: getContext,
clean:clean
};

View File

@ -20,6 +20,7 @@ var when = require("when");
var Flow = require('./Flow');
var typeRegistry = require("../registry");
var context = require("../context")
var credentials = require("../credentials");
var flowUtil = require("./util");
@ -116,6 +117,7 @@ function setConfig(_config,type,muteLog) {
return credentials.clean(activeConfig).then(function() {
if (started) {
return stop(type,diff,muteLog).then(function() {
context.clean(activeFlowConfig);
start(type,diff,muteLog);
}).otherwise(function(err) {
})

View File

@ -158,7 +158,7 @@ module.exports = {
changed[removed[id].z] = newConfig.allNodes[removed[id].z];
if (changed[removed[id].z].type === "subflow") {
changedSubflows[removed[id].z] = changed[removed[id].z];
delete removed[id];
//delete removed[id];
}
}
} else {

View File

@ -21,6 +21,7 @@ var fs = require("fs");
var registry = require("./registry");
var credentials = require("./credentials");
var flows = require("./flows");
var context = require("./context");
var Node = require("./Node");
var log = require("../log");
@ -69,6 +70,7 @@ function init(runtime) {
credentials.init(runtime.storage);
flows.init(runtime.settings,runtime.storage);
registry.init(runtime);
context.init(runtime.settings);
}
function disableNode(id) {

View File

@ -0,0 +1,111 @@
/**
* Copyright 2015 IBM Corp.
*
* 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 should = require("should");
var sinon = require('sinon');
var Context = require("../../../../red/runtime/nodes/context");
describe('context', function() {
beforeEach(function() {
Context.init({});
});
afterEach(function() {
Context.clean({allNodes:{}});
});
it('stores local property',function() {
var context1 = Context.get("1","flowA");
should.not.exist(context1.get("foo"));
context1.set("foo","test");
context1.get("foo").should.eql("test");
});
it('stores local property - creates parent properties',function() {
var context1 = Context.get("1","flowA");
context1.set("foo.bar","test");
context1.get("foo").should.eql({bar:"test"});
});
it('deletes local property',function() {
var context1 = Context.get("1","flowA");
context1.set("foo.abc.bar1","test1");
context1.set("foo.abc.bar2","test2");
context1.get("foo.abc").should.eql({bar1:"test1",bar2:"test2"});
context1.set("foo.abc.bar1",undefined);
context1.get("foo.abc").should.eql({bar2:"test2"});
context1.set("foo.abc",undefined);
should.not.exist(context1.get("foo.abc"));
context1.set("foo",undefined);
should.not.exist(context1.get("foo"));
});
it('stores flow property',function() {
var context1 = Context.get("1","flowA");
should.not.exist(context1.flow.get("foo"));
context1.flow.set("foo","test");
context1.flow.get("foo").should.eql("test");
});
it('stores global property',function() {
var context1 = Context.get("1","flowA");
should.not.exist(context1.global.get("foo"));
context1.global.set("foo","test");
context1.global.get("foo").should.eql("test");
});
it('keeps local context local', function() {
var context1 = Context.get("1","flowA");
var context2 = Context.get("2","flowA");
should.not.exist(context1.get("foo"));
should.not.exist(context2.get("foo"));
context1.set("foo","test");
context1.get("foo").should.eql("test");
should.not.exist(context2.get("foo"));
});
it('flow context accessible to all flow nodes', function() {
var context1 = Context.get("1","flowA");
var context2 = Context.get("2","flowA");
should.not.exist(context1.flow.get("foo"));
should.not.exist(context2.flow.get("foo"));
context1.flow.set("foo","test");
context1.flow.get("foo").should.eql("test");
context2.flow.get("foo").should.eql("test");
});
it('flow context not shared to nodes on other flows', function() {
var context1 = Context.get("1","flowA");
var context2 = Context.get("2","flowB");
should.not.exist(context1.flow.get("foo"));
should.not.exist(context2.flow.get("foo"));
context1.flow.set("foo","test");
context1.flow.get("foo").should.eql("test");
should.not.exist(context2.flow.get("foo"));
});
it('global context shared to all nodes', function() {
var context1 = Context.get("1","flowA");
var context2 = Context.get("2","flowB");
should.not.exist(context1.global.get("foo"));
should.not.exist(context2.global.get("foo"));
context1.global.set("foo","test");
context1.global.get("foo").should.eql("test");
context2.global.get("foo").should.eql("test");
});
});