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

Allow .get/set/keys to return asynchronous results

This commit is contained in:
HirokiUchikawa 2018-06-01 11:44:45 +09:00
parent a835f9f0cb
commit 6e34f0697c
3 changed files with 56 additions and 5 deletions

View File

@ -118,7 +118,7 @@ function parseKey(key) {
} }
var keyPath = { storage: "", key: "" }; var keyPath = { storage: "", key: "" };
var indexDot = key.indexOf("."); var indexDot = key.indexOf(".");
// The key of "$file" should be treated as a key without persistable context. // The key of "#file" should be treated as a key without persistable context.
if (indexDot != -1) { if (indexDot != -1) {
keyPath.storage = parseStorage(key); keyPath.storage = parseStorage(key);
} }
@ -154,17 +154,44 @@ function createContext(id,seed) {
obj.get = function(key) { obj.get = function(key) {
var keyPath = parseKey(key); var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage); var context = getContextStorage(keyPath.storage);
return context.get(scope, keyPath.key); if(key === keyPath.key){
return context.get(scope, keyPath.key);
}else{
return context.getAsync(scope, keyPath.key);
}
}; };
obj.set = function(key, value) { obj.set = function(key, value) {
var keyPath = parseKey(key); var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage); var context = getContextStorage(keyPath.storage);
return context.set(scope, keyPath.key, value); if(key === keyPath.key){
return context.set(scope, keyPath.key, value);
}else{
return context.setAsync(scope, keyPath.key, value);
}
}; };
obj.keys = function(storage) { obj.keys = function(storage) {
var storageName = parseStorage(storage); var storageName = parseStorage(storage);
var context = getContextStorage(storageName); var context = getContextStorage(storageName);
return context.keys(scope); if(!storage){
return context.keys(scope);
}else{
return context.keysAsync(scope);
}
};
obj.getAsync = function(key) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.getAsync(scope, keyPath.key);
};
obj.setAsync = function(key, value) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.setAsync(scope, keyPath.key, value);
};
obj.keysAsync = function(storage) {
var storageName = parseStorage(storage);
var context = getContextStorage(storageName);
return context.keysAsync(scope);
}; };
return obj; return obj;
} }

View File

@ -110,6 +110,18 @@ LocalFileSystem.prototype.keys = function(scope) {
return Object.keys(this.storages[scope].getData("/")); return Object.keys(this.storages[scope].getData("/"));
} }
LocalFileSystem.prototype.getAsync = function(scope, key) {
return when.resolve(this.get(scope, key));
};
LocalFileSystem.prototype.setAsync =function(scope, key, value) {
return when.resolve(this.set(scope, key, value));
};
LocalFileSystem.prototype.keysAsync = function(scope){
return when.resolve(this.keys(scope));
};
LocalFileSystem.prototype.delete = function(scope){ LocalFileSystem.prototype.delete = function(scope){
var self = this; var self = this;
if(this.storages[scope]){ if(this.storages[scope]){

View File

@ -51,11 +51,23 @@ Memory.prototype.keys = function(scope){
return Object.keys(this.data[scope]); return Object.keys(this.data[scope]);
} else { } else {
return Object.keys(this.data[scope]).filter(function (key) { return Object.keys(this.data[scope]).filter(function (key) {
return key !== "set" && key !== "get" && key !== "keys"; return key !== "set" && key !== "get" && key !== "keys" && key !== "setAsync" && key !== "getAsync" && key !== "keysAsync";
}); });
} }
}; };
Memory.prototype.getAsync = function(scope, key) {
return when.resolve(this.get(scope, key));
};
Memory.prototype.setAsync =function(scope, key, value) {
return when.resolve(this.set(scope, key, value));
};
Memory.prototype.keysAsync = function(scope){
return when.resolve(this.keys(scope));
};
Memory.prototype.delete = function(scope){ Memory.prototype.delete = function(scope){
delete this.data[scope]; delete this.data[scope];
return Promise.resolve(); return Promise.resolve();