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 log = require("../../log");
var when = require("when");
var settings;
var contexts = {};
@ -59,14 +58,14 @@ function load() {
try{
plugin = require("./"+plugins[pluginName].module);
}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 {
plugin = plugins[pluginName].module;
}
externalContexts[pluginName] = plugin(config);
}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){
@ -78,10 +77,10 @@ function load() {
if(externalContexts.hasOwnProperty(plugins["default"])){
externalContexts["default"] = externalContexts[plugins["default"]];
}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 {
noContextStorage = true;
return externalContexts["_"].open();
@ -224,7 +223,7 @@ function deleteContext(id,flowId) {
delete contexts[contextId];
return externalContexts["_"].delete(contextId);
}else{
return when.resolve();
return Promise.resolve();
}
}
@ -243,7 +242,7 @@ function clean(flowConfig) {
}
}
}
return when.all(promises);
return Promise.all(promises);
}
function close() {
@ -253,7 +252,7 @@ function close() {
promises.push(externalContexts[plugin].close());
}
}
return when.all(promises);
return Promise.all(promises);
}
module.exports = {

View File

@ -16,7 +16,6 @@
var fs = require('fs-extra');
var path = require("path");
var when = require("when");
var util = require("../../util");
function getStoragePath(storageBaseDir, scope) {
@ -67,10 +66,10 @@ function loadFile(storagePath){
if(exists === true){
return fs.readFile(storagePath, "utf8");
}else{
return when.resolve(undefined);
return Promise.resolve(undefined);
}
}).catch(function(err){
throw when.reject(err);
throw Promise.reject(err);
});
}
@ -80,11 +79,11 @@ function LocalFileSystem(config){
}
LocalFileSystem.prototype.open = function(){
return when.resolve();
return Promise.resolve();
}
LocalFileSystem.prototype.close = function(){
return when.resolve();
return Promise.resolve();
}
LocalFileSystem.prototype.getAsync = function(scope, key) {
@ -96,7 +95,7 @@ LocalFileSystem.prototype.getAsync = function(scope, key) {
return undefined
}
}).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);
return fs.outputFile(storagePath + ".json", str, {encoding:"utf8",flag:"w+"});
}).catch(function(err){
return when.reject(err);
return Promise.reject(err);
});
};
@ -123,7 +122,7 @@ LocalFileSystem.prototype.keysAsync = function(scope){
return []
}
}).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){
var self = this;
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){
result.push(fs.remove(path.join(self.storageBaseDir,item)));
}
@ -143,9 +142,9 @@ LocalFileSystem.prototype.clean = function(activeNodes){
},[]));
}).catch(function(err){
if(err.code == 'ENOENT') {
return when.resolve();
return Promise.resolve();
}else{
return when.reject(err);
return Promise.reject(err);
}
});
}

View File

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

View File

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

View File

@ -17,7 +17,6 @@
var should = require('should');
var fs = require('fs-extra');
var path = require("path");
var when = require("when");
var LocalFileSystem = require('../../../../../red/runtime/nodes/context/localfilesystem');
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() {
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return when.all([context.setAsync("nodeX","foo","testX"),
context.setAsync("nodeY","foo","testY")])
return Promise.all([context.setAsync("nodeX","foo","testX"),
context.setAsync("nodeY","foo","testY")])
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("testX"),
context.getAsync("nodeY","foo").should.be.finally.equal("testY")]);
return Promise.all([context.getAsync("nodeX","foo").should.be.finally.equal("testX"),
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() {
return when.all([context.keysAsync("nodeX"),
context.keysAsync("nodeY")
return Promise.all([context.keysAsync("nodeX"),
context.keysAsync("nodeY")
]).then(function(results){
results[0].should.be.an.Array();
results[0].should.be.empty();
results[1].should.be.an.Array();
results[1].should.be.empty();
}).then(function(){
return when.all([context.setAsync("nodeX","foo","bar"),
context.setAsync("nodeY","hoge","piyo")]);
return Promise.all([context.setAsync("nodeX","foo","bar"),
context.setAsync("nodeY","hoge","piyo")]);
}).then(function(){
return when.all([context.keysAsync("nodeX"),
context.keysAsync("nodeY")]);
return Promise.all([context.keysAsync("nodeX"),
context.keysAsync("nodeY")]);
}).then(function(results){
results[0].should.have.length(1);
results[0][0].should.equal("foo");
@ -258,55 +257,55 @@ describe('localfilesystem',function() {
describe('#delete',function() {
it('should delete context',function() {
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return when.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
return Promise.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","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")])
}).then(function(){
return context.delete("nodeX");
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")]);
});
return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")]);
});
});
});
describe('#clean',function() {
it('should clean unnecessary context',function() {
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(values){
return when.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return Promise.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","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")])
}).then(function(){
return context.clean([]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()]);
return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()]);
});
});
it('should not clean active context',function() {
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
return Promise.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return when.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
return Promise.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","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")])
}).then(function(){
return context.clean(["nodeX"]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.undefined()]);
return Promise.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.undefined()]);
});
});
});