From 5ead3342ccc776b1c01ad57571fcfdd02db08557 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 29 Dec 2015 22:16:51 +0000 Subject: [PATCH] Add node context/flow/global --- red/runtime/nodes/Node.js | 8 +- red/runtime/nodes/context.js | 80 ++++++++++++++++++ red/runtime/nodes/flows/index.js | 2 + red/runtime/nodes/flows/util.js | 2 +- red/runtime/nodes/index.js | 2 + test/red/runtime/nodes/context_spec.js | 111 +++++++++++++++++++++++++ 6 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 red/runtime/nodes/context.js create mode 100644 test/red/runtime/nodes/context_spec.js diff --git a/red/runtime/nodes/Node.js b/red/runtime/nodes/Node.js index f167ac96b..8fe82d19c 100644 --- a/red/runtime/nodes/Node.js +++ b/red/runtime/nodes/Node.js @@ -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; diff --git a/red/runtime/nodes/context.js b/red/runtime/nodes/context.js new file mode 100644 index 000000000..6f9e11489 --- /dev/null +++ b/red/runtime/nodes/context.js @@ -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 +}; diff --git a/red/runtime/nodes/flows/index.js b/red/runtime/nodes/flows/index.js index af4009129..a625641b6 100644 --- a/red/runtime/nodes/flows/index.js +++ b/red/runtime/nodes/flows/index.js @@ -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) { }) diff --git a/red/runtime/nodes/flows/util.js b/red/runtime/nodes/flows/util.js index e7feefe96..9017aaa02 100644 --- a/red/runtime/nodes/flows/util.js +++ b/red/runtime/nodes/flows/util.js @@ -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 { diff --git a/red/runtime/nodes/index.js b/red/runtime/nodes/index.js index 795a150da..51adf8ae8 100644 --- a/red/runtime/nodes/index.js +++ b/red/runtime/nodes/index.js @@ -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) { diff --git a/test/red/runtime/nodes/context_spec.js b/test/red/runtime/nodes/context_spec.js new file mode 100644 index 000000000..7dd3cbcf3 --- /dev/null +++ b/test/red/runtime/nodes/context_spec.js @@ -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"); + }); + +});