1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Use native Promise instead of when.js

This commit is contained in:
HirokiUchikawa 2018-06-20 19:50:55 +09:00
parent 23b887c30e
commit dd81d947fc
5 changed files with 85 additions and 90 deletions

View File

@ -16,7 +16,6 @@
var clone = require("clone"); var clone = require("clone");
var log = require("../../log"); var log = require("../../log");
var when = require("when");
var settings; var settings;
var contexts = {}; var contexts = {};
@ -59,14 +58,14 @@ function load() {
try{ try{
plugin = require("./"+plugins[pluginName].module); plugin = require("./"+plugins[pluginName].module);
}catch(err){ }catch(err){
return when.reject(new Error(log._("context.error-module-not-loaded", {module:plugins[pluginName].module}))); return Promise.reject(new Error(log._("context.error-module-not-loaded", {module:plugins[pluginName].module})));
} }
} else { } else {
plugin = plugins[pluginName].module; plugin = plugins[pluginName].module;
} }
externalContexts[pluginName] = plugin(config); externalContexts[pluginName] = plugin(config);
}else{ }else{
return when.reject(new Error(log._("context.error-module-not-defined", {storage:pluginName}))); return Promise.reject(new Error(log._("context.error-module-not-defined", {storage:pluginName})));
} }
} }
for(var plugin in externalContexts){ for(var plugin in externalContexts){
@ -78,10 +77,10 @@ function load() {
if(externalContexts.hasOwnProperty(plugins["default"])){ if(externalContexts.hasOwnProperty(plugins["default"])){
externalContexts["default"] = externalContexts[plugins["default"]]; externalContexts["default"] = externalContexts[plugins["default"]];
}else{ }else{
return when.reject(new Error(log._("context.error-invalid-default-module", {storage:plugins["default"]}))); return Promise.reject(new Error(log._("context.error-invalid-default-module", {storage:plugins["default"]})));
} }
} }
return when.all(promises); return Promise.all(promises);
} else { } else {
noContextStorage = true; noContextStorage = true;
return externalContexts["_"].open(); return externalContexts["_"].open();
@ -224,7 +223,7 @@ function deleteContext(id,flowId) {
delete contexts[contextId]; delete contexts[contextId];
return externalContexts["_"].delete(contextId); return externalContexts["_"].delete(contextId);
}else{ }else{
return when.resolve(); return Promise.resolve();
} }
} }
@ -243,7 +242,7 @@ function clean(flowConfig) {
} }
} }
} }
return when.all(promises); return Promise.all(promises);
} }
function close() { function close() {
@ -253,7 +252,7 @@ function close() {
promises.push(externalContexts[plugin].close()); promises.push(externalContexts[plugin].close());
} }
} }
return when.all(promises); return Promise.all(promises);
} }
module.exports = { module.exports = {

View File

@ -16,7 +16,6 @@
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require("path"); var path = require("path");
var when = require("when");
var util = require("../../util"); var util = require("../../util");
function getStoragePath(storageBaseDir, scope) { function getStoragePath(storageBaseDir, scope) {
@ -67,10 +66,10 @@ function loadFile(storagePath){
if(exists === true){ if(exists === true){
return fs.readFile(storagePath, "utf8"); return fs.readFile(storagePath, "utf8");
}else{ }else{
return when.resolve(undefined); return Promise.resolve(undefined);
} }
}).catch(function(err){ }).catch(function(err){
throw when.reject(err); throw Promise.reject(err);
}); });
} }
@ -80,11 +79,11 @@ function LocalFileSystem(config){
} }
LocalFileSystem.prototype.open = function(){ LocalFileSystem.prototype.open = function(){
return when.resolve(); return Promise.resolve();
} }
LocalFileSystem.prototype.close = function(){ LocalFileSystem.prototype.close = function(){
return when.resolve(); return Promise.resolve();
} }
LocalFileSystem.prototype.getAsync = function(scope, key) { LocalFileSystem.prototype.getAsync = function(scope, key) {
@ -96,7 +95,7 @@ LocalFileSystem.prototype.getAsync = function(scope, key) {
return undefined return undefined
} }
}).catch(function(err){ }).catch(function(err){
return when.reject(err); return Promise.reject(err);
}); });
}; };
@ -110,7 +109,7 @@ LocalFileSystem.prototype.setAsync =function(scope, key, value) {
var str = JSON.stringify(obj, undefined, 4); var str = JSON.stringify(obj, undefined, 4);
return fs.outputFile(storagePath + ".json", str, {encoding:"utf8",flag:"w+"}); return fs.outputFile(storagePath + ".json", str, {encoding:"utf8",flag:"w+"});
}).catch(function(err){ }).catch(function(err){
return when.reject(err); return Promise.reject(err);
}); });
}; };
@ -123,7 +122,7 @@ LocalFileSystem.prototype.keysAsync = function(scope){
return [] return []
} }
}).catch(function(err){ }).catch(function(err){
return when.reject(err); return Promise.reject(err);
}); });
}; };
@ -135,7 +134,7 @@ LocalFileSystem.prototype.delete = function(scope){
LocalFileSystem.prototype.clean = function(activeNodes){ LocalFileSystem.prototype.clean = function(activeNodes){
var self = this; var self = this;
return fs.readdir(self.storageBaseDir).then(function(dirs){ return fs.readdir(self.storageBaseDir).then(function(dirs){
return when.all(dirs.reduce(function(result, item){ return Promise.all(dirs.reduce(function(result, item){
if(item !== "global" && activeNodes.indexOf(item) === -1){ if(item !== "global" && activeNodes.indexOf(item) === -1){
result.push(fs.remove(path.join(self.storageBaseDir,item))); result.push(fs.remove(path.join(self.storageBaseDir,item)));
} }
@ -143,9 +142,9 @@ LocalFileSystem.prototype.clean = function(activeNodes){
},[])); },[]));
}).catch(function(err){ }).catch(function(err){
if(err.code == 'ENOENT') { if(err.code == 'ENOENT') {
return when.resolve(); return Promise.resolve();
}else{ }else{
return when.reject(err); return Promise.reject(err);
} }
}); });
} }

View File

@ -15,18 +15,17 @@
**/ **/
var util = require("../../util"); var util = require("../../util");
var when = require("when");
function Memory(config){ function Memory(config){
this.data = {}; this.data = {};
} }
Memory.prototype.open = function(){ Memory.prototype.open = function(){
return when.resolve(); return Promise.resolve();
}; };
Memory.prototype.close = function(){ Memory.prototype.close = function(){
return when.resolve(); return Promise.resolve();
}; };
Memory.prototype.get = function(scope, key) { Memory.prototype.get = function(scope, key) {
@ -46,7 +45,7 @@ Memory.prototype.set =function(scope, key, value) {
Memory.prototype.keys = function(scope){ Memory.prototype.keys = function(scope){
if(!this.data[scope]){ if(!this.data[scope]){
return []; return [];
} }
if (scope !== "global") { if (scope !== "global") {
return Object.keys(this.data[scope]); return Object.keys(this.data[scope]);
} else { } else {
@ -57,15 +56,15 @@ Memory.prototype.keys = function(scope){
}; };
Memory.prototype.getAsync = function(scope, key) { Memory.prototype.getAsync = function(scope, key) {
return when.resolve(this.get(scope, key)); return Promise.resolve(this.get(scope, key));
}; };
Memory.prototype.setAsync =function(scope, key, value) { Memory.prototype.setAsync =function(scope, key, value) {
return when.resolve(this.set(scope, key, value)); return Promise.resolve(this.set(scope, key, value));
}; };
Memory.prototype.keysAsync = function(scope){ Memory.prototype.keysAsync = function(scope){
return when.resolve(this.keys(scope)); return Promise.resolve(this.keys(scope));
}; };
Memory.prototype.delete = function(scope){ Memory.prototype.delete = function(scope){

View File

@ -16,7 +16,6 @@
var should = require("should"); var should = require("should");
var sinon = require('sinon'); var sinon = require('sinon');
var when = require("when")
var rewire = require("rewire"); var rewire = require("rewire");
var Context = require("../../../../../red/runtime/nodes/context/index"); var Context = require("../../../../../red/runtime/nodes/context/index");
@ -169,20 +168,20 @@ describe('context', function() {
describe('external context storage',function() { describe('external context storage',function() {
var sandbox = sinon.sandbox.create(); var sandbox = sinon.sandbox.create();
var stubGetAsync = sandbox.stub().returns(when.resolve()); var stubGetAsync = sandbox.stub().returns(Promise.resolve());
var stubSetAsync = sandbox.stub().returns(when.resolve()); var stubSetAsync = sandbox.stub().returns(Promise.resolve());
var stubKeysAsync = sandbox.stub().returns(when.resolve()); var stubKeysAsync = sandbox.stub().returns(Promise.resolve());
var stubDelete = sandbox.stub().returns(when.resolve()); var stubDelete = sandbox.stub().returns(Promise.resolve());
var stubClean = sandbox.stub().returns(when.resolve()); var stubClean = sandbox.stub().returns(Promise.resolve());
var stubOpen = sandbox.stub().returns(when.resolve()); var stubOpen = sandbox.stub().returns(Promise.resolve());
var stubClose = sandbox.stub().returns(when.resolve()); var stubClose = sandbox.stub().returns(Promise.resolve());
var stubGetAsync2 = sandbox.stub().returns(when.resolve()); var stubGetAsync2 = sandbox.stub().returns(Promise.resolve());
var stubSetAsync2 = sandbox.stub().returns(when.resolve()); var stubSetAsync2 = sandbox.stub().returns(Promise.resolve());
var stubKeysAsync2 = sandbox.stub().returns(when.resolve()); var stubKeysAsync2 = sandbox.stub().returns(Promise.resolve());
var stubDelete2 = sandbox.stub().returns(when.resolve()); var stubDelete2 = sandbox.stub().returns(Promise.resolve());
var stubClean2 = sandbox.stub().returns(when.resolve()); var stubClean2 = sandbox.stub().returns(Promise.resolve());
var stubOpen2 = sandbox.stub().returns(when.resolve()); var stubOpen2 = sandbox.stub().returns(Promise.resolve());
var stubClose2 = sandbox.stub().returns(when.resolve()); var stubClose2 = sandbox.stub().returns(Promise.resolve());
var testPlugin = function(config){ var testPlugin = function(config){
function Test(){} function Test(){}
Test.prototype.getAsync = stubGetAsync; Test.prototype.getAsync = stubGetAsync;
@ -262,7 +261,7 @@ describe('context', function() {
}); });
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
return when.all([ return Promise.all([
context.setAsync("##%&.sign","sign1").then(function(){ context.setAsync("##%&.sign","sign1").then(function(){
return context.getAsync("##%&.sign").should.finally.equal("sign1"); return context.getAsync("##%&.sign").should.finally.equal("sign1");
}), }),
@ -279,7 +278,7 @@ describe('context', function() {
Context.init({contextStorage:{_:{module:testPlugin}}}); Context.init({contextStorage:{_:{module:testPlugin}}});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
return when.all([ return Promise.all([
context.setAsync("#_.foo","bar"), context.setAsync("#_.foo","bar"),
context.getAsync("#_.foo"), context.getAsync("#_.foo"),
context.keysAsync("#_") context.keysAsync("#_")
@ -333,7 +332,7 @@ describe('context', function() {
Context.init({contextStorage:contextStorage}); Context.init({contextStorage:contextStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
return when.all([ return Promise.all([
context.setAsync("#test.foo","test"), context.setAsync("#test.foo","test"),
context.getAsync("#test.foo"), context.getAsync("#test.foo"),
context.keysAsync("#test") context.keysAsync("#test")
@ -348,7 +347,7 @@ describe('context', function() {
Context.init({contextStorage:contextStorage}); Context.init({contextStorage:contextStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
return when.all([ return Promise.all([
context.flow.setAsync("#test.foo","test"), context.flow.setAsync("#test.foo","test"),
context.flow.getAsync("#test.foo"), context.flow.getAsync("#test.foo"),
context.flow.keysAsync("#test") context.flow.keysAsync("#test")
@ -363,7 +362,7 @@ describe('context', function() {
Context.init({contextStorage:contextStorage}); Context.init({contextStorage:contextStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
return when.all([ return Promise.all([
context.global.setAsync("#test.foo","test"), context.global.setAsync("#test.foo","test"),
context.global.getAsync("#test.foo"), context.global.getAsync("#test.foo"),
context.global.keysAsync("#test") context.global.keysAsync("#test")
@ -378,7 +377,7 @@ describe('context', function() {
Context.init({contextStorage:contextDefaultStorage}); Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
return when.all([ return Promise.all([
context.setAsync("#nonexist.foo","test"), context.setAsync("#nonexist.foo","test"),
context.getAsync("#nonexist.foo"), context.getAsync("#nonexist.foo"),
context.keysAsync("#nonexist") context.keysAsync("#nonexist")
@ -396,7 +395,7 @@ describe('context', function() {
Context.init({contextStorage:contextDefaultStorage}); Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
return when.all([ return Promise.all([
context.setAsync("#default.foo","default"), context.setAsync("#default.foo","default"),
context.getAsync("#default.foo"), context.getAsync("#default.foo"),
context.keysAsync("#default") context.keysAsync("#default")
@ -414,7 +413,7 @@ describe('context', function() {
Context.init({contextStorage:contextDefaultStorage}); Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
return when.all([ return Promise.all([
context.setAsync("#.foo","alias"), context.setAsync("#.foo","alias"),
context.getAsync("#.foo"), context.getAsync("#.foo"),
context.keysAsync("#") context.keysAsync("#")
@ -432,7 +431,7 @@ describe('context', function() {
Context.init({contextStorage:contextAlias}); Context.init({contextStorage:contextAlias});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
return when.all([ return Promise.all([
context.setAsync("#.foo","alias"), context.setAsync("#.foo","alias"),
context.getAsync("#.foo"), context.getAsync("#.foo"),
context.keysAsync("#") context.keysAsync("#")
@ -510,7 +509,7 @@ describe('context', function() {
return Context.close(); return Context.close();
}); });
it('should work correctly with the valid key name',function() { it('should work correctly with the valid key name',function() {
return when.all([ return Promise.all([
context.setAsync("#memory.azAZ09#_","valid"), context.setAsync("#memory.azAZ09#_","valid"),
context.setAsync("#memory.a.b","ab") context.setAsync("#memory.a.b","ab")
]).then(function(){ ]).then(function(){

View File

@ -17,7 +17,6 @@
var should = require('should'); var should = require('should');
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require("path"); var path = require("path");
var when = require("when");
var LocalFileSystem = require('../../../../../red/runtime/nodes/context/localfilesystem'); var LocalFileSystem = require('../../../../../red/runtime/nodes/context/localfilesystem');
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context")); var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"));
@ -76,14 +75,14 @@ describe('localfilesystem',function() {
}); });
it('should not shared context with other scope', function() { it('should not shared context with other scope', function() {
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined() context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){ ]).then(function(){
return when.all([context.setAsync("nodeX","foo","testX"), return Promise.all([context.setAsync("nodeX","foo","testX"),
context.setAsync("nodeY","foo","testY")]) context.setAsync("nodeY","foo","testY")])
}).then(function(){ }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("testX"), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.equal("testX"),
context.getAsync("nodeY","foo").should.be.finally.equal("testY")]); context.getAsync("nodeY","foo").should.be.finally.equal("testY")]);
}); });
}); });
@ -234,19 +233,19 @@ describe('localfilesystem',function() {
}); });
it('should enumerate context keys in each scopes', function() { it('should enumerate context keys in each scopes', function() {
return when.all([context.keysAsync("nodeX"), return Promise.all([context.keysAsync("nodeX"),
context.keysAsync("nodeY") context.keysAsync("nodeY")
]).then(function(results){ ]).then(function(results){
results[0].should.be.an.Array(); results[0].should.be.an.Array();
results[0].should.be.empty(); results[0].should.be.empty();
results[1].should.be.an.Array(); results[1].should.be.an.Array();
results[1].should.be.empty(); results[1].should.be.empty();
}).then(function(){ }).then(function(){
return when.all([context.setAsync("nodeX","foo","bar"), return Promise.all([context.setAsync("nodeX","foo","bar"),
context.setAsync("nodeY","hoge","piyo")]); context.setAsync("nodeY","hoge","piyo")]);
}).then(function(){ }).then(function(){
return when.all([context.keysAsync("nodeX"), return Promise.all([context.keysAsync("nodeX"),
context.keysAsync("nodeY")]); context.keysAsync("nodeY")]);
}).then(function(results){ }).then(function(results){
results[0].should.have.length(1); results[0].should.have.length(1);
results[0][0].should.equal("foo"); results[0][0].should.equal("foo");
@ -258,55 +257,55 @@ describe('localfilesystem',function() {
describe('#delete',function() { describe('#delete',function() {
it('should delete context',function() { it('should delete context',function() {
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined() context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){ ]).then(function(){
return when.all([context.setAsync("nodeX","foo","abc"), return Promise.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]); context.setAsync("nodeY","foo","abc")]);
}).then(function(){ }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")]) context.getAsync("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){ }).then(function(){
return context.delete("nodeX"); return context.delete("nodeX");
}).then(function(){ }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")]); context.getAsync("nodeY","foo").should.be.finally.equal("abc")]);
}); });
}); });
}); });
describe('#clean',function() { describe('#clean',function() {
it('should clean unnecessary context',function() { it('should clean unnecessary context',function() {
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined() context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(values){ ]).then(function(){
return when.all([context.setAsync("nodeX","foo","abc"), return Promise.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]); context.setAsync("nodeY","foo","abc")]);
}).then(function(){ }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")]) context.getAsync("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){ }).then(function(){
return context.clean([]); return context.clean([]);
}).then(function(){ }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()]); context.getAsync("nodeY","foo").should.be.finally.undefined()]);
}); });
}); });
it('should not clean active context',function() { it('should not clean active context',function() {
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined() context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){ ]).then(function(){
return when.all([context.setAsync("nodeX","foo","abc"), return Promise.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]); context.setAsync("nodeY","foo","abc")]);
}).then(function(){ }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")]) context.getAsync("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){ }).then(function(){
return context.clean(["nodeX"]); return context.clean(["nodeX"]);
}).then(function(){ }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"), return Promise.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.undefined()]); context.getAsync("nodeY","foo").should.be.finally.undefined()]);
}); });
}); });
}); });