From 6e34f0697c8e6787eda0b4e2e45101924d01d6d5 Mon Sep 17 00:00:00 2001 From: HirokiUchikawa Date: Fri, 1 Jun 2018 11:44:45 +0900 Subject: [PATCH] Allow .get/set/keys to return asynchronous results --- red/runtime/nodes/context/index.js | 35 +++++++++++++++++--- red/runtime/nodes/context/localfilesystem.js | 12 +++++++ red/runtime/nodes/context/memory.js | 14 +++++++- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/red/runtime/nodes/context/index.js b/red/runtime/nodes/context/index.js index d1bf32be3..57fc3a747 100644 --- a/red/runtime/nodes/context/index.js +++ b/red/runtime/nodes/context/index.js @@ -118,7 +118,7 @@ function parseKey(key) { } var keyPath = { storage: "", key: "" }; 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) { keyPath.storage = parseStorage(key); } @@ -154,17 +154,44 @@ function createContext(id,seed) { obj.get = function(key) { var keyPath = parseKey(key); 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) { var keyPath = parseKey(key); 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) { var storageName = parseStorage(storage); 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; } diff --git a/red/runtime/nodes/context/localfilesystem.js b/red/runtime/nodes/context/localfilesystem.js index 4909f9c14..730f0020c 100644 --- a/red/runtime/nodes/context/localfilesystem.js +++ b/red/runtime/nodes/context/localfilesystem.js @@ -110,6 +110,18 @@ LocalFileSystem.prototype.keys = function(scope) { 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){ var self = this; if(this.storages[scope]){ diff --git a/red/runtime/nodes/context/memory.js b/red/runtime/nodes/context/memory.js index ba86d086d..1d9711094 100644 --- a/red/runtime/nodes/context/memory.js +++ b/red/runtime/nodes/context/memory.js @@ -51,11 +51,23 @@ Memory.prototype.keys = function(scope){ return Object.keys(this.data[scope]); } else { 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){ delete this.data[scope]; return Promise.resolve();