Use the callback instead of Promise in context API

and remove unnecessary functions
This commit is contained in:
HirokiUchikawa
2018-06-22 17:11:54 +09:00
parent fd67d08402
commit f2fa26fb07
6 changed files with 449 additions and 497 deletions

View File

@@ -161,9 +161,6 @@
"error-module-not-loaded": "'__module__' could not be loaded",
"error-module-not-defined": "'module' is not defined in '__storage__' of settings.contextStorage",
"error-invalid-default-module": "Invalid storage '__storage__' is specified as a default storage",
"error-key-zero-length": "Invalid property expression: zero-length",
"error-unexpected-space-character": "Invalid property expression: unexpected ' ' at position __index__",
"error-empty-key": "Invalid property expression: key is empty",
"error-use-undefined-storage": "Undefined storage '__storage__' is specified"
}

View File

@@ -16,6 +16,7 @@
var clone = require("clone");
var log = require("../../log");
var memory = require("./memory");
var settings;
var contexts = {};
@@ -28,7 +29,6 @@ function init(_settings) {
externalContexts = {};
// init memory plugin
var memory = require("./memory");
var seed = settings.functionGlobalContext || {};
externalContexts["_"] = memory();
externalContexts["_"].setGlobalContext(seed);
@@ -95,43 +95,6 @@ function copySettings(config, settings){
});
}
function parseStorage(key) {
if (!key || key.charAt(0) !== '#') {
return "";
} else {
var endOfStorageName = key.indexOf(".");
if (endOfStorageName == -1) {
endOfStorageName = key.length;
}
return key.substring(1,endOfStorageName)||"default";
}
}
function parseKey(key) {
if (!key) {
throw new Error(log._("context.error-key-zero-length"));
}
var indexSpace = key.indexOf(" ");
if (indexSpace != -1) {
throw new Error(log._("context.error-unexpected-space-character", {index:indexSpace}));
}
var keyPath = { storage: "", key: "" };
var indexDot = key.indexOf(".");
// The key of "#file" should be treated as a key without persistable context.
if (indexDot != -1) {
keyPath.storage = parseStorage(key);
}
if (keyPath.storage) {
keyPath.key = key.substring(indexDot + 1);
} else {
keyPath.key = key;
}
if(!keyPath.key) {
throw new Error(log._("context.error-empty-key"));
}
return keyPath;
}
function getContextStorage(storage) {
if (noContextStorage || !storage) {
return externalContexts["_"];
@@ -150,47 +113,30 @@ function createContext(id,seed) {
var scope = id;
var obj = seed || {};
obj.get = function(key) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
if(!keyPath.storage){
return context.get(scope, keyPath.key);
}else{
throw new Error(keyPath.storage + " does not support get(). Use getAsync()");
obj.get = function(key, storage, callback) {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
} else if(typeof storage === "string" && typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
return getContextStorage(storage).get(scope, key, callback);
};
obj.set = function(key, value) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
if(!keyPath.storage){
return context.set(scope, keyPath.key, value);
}else{
throw new Error(keyPath.storage + " does not support set(). Use setAsync()");
obj.set = function(key, value, storage, callback) {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
}
getContextStorage(storage).set(scope, key, value, callback);
};
obj.keys = function(storage) {
var storageName = parseStorage(storage);
var context = getContextStorage(storageName);
if(!storageName){
return context.keys(scope);
}else{
throw new Error(storageName + " does not support keys(). Use keysAsync()");
obj.keys = function(storage, callback) {
if (typeof storage === 'function') {
callback = storage;
storage = "default";
} else if(typeof storage === "string" && typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
};
obj.getAsync = function(key) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.get(scope, keyPath.key);
};
obj.setAsync = function(key, value) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.set(scope, keyPath.key, value);
};
obj.keysAsync = function(storage) {
var storageName = parseStorage(storage);
var context = getContextStorage(storageName);
return context.keys(scope);
return getContextStorage(storage).keys(scope, callback);
};
return obj;
}

View File

@@ -86,43 +86,52 @@ LocalFileSystem.prototype.close = function(){
return Promise.resolve();
}
LocalFileSystem.prototype.get = function(scope, key) {
LocalFileSystem.prototype.get = function(scope, key, callback) {
if(typeof callback !== "function"){
throw new Error("Callback must be a function");
}
var storagePath = getStoragePath(this.storageBaseDir ,scope);
return loadFile(storagePath + ".json").then(function(data){
loadFile(storagePath + ".json").then(function(data){
if(data){
return util.getMessageProperty(JSON.parse(data),key);
callback(null, util.getMessageProperty(JSON.parse(data),key));
}else{
return undefined
callback(null, undefined);
}
}).catch(function(err){
return Promise.reject(err);
callback(err);
});
};
LocalFileSystem.prototype.set =function(scope, key, value) {
LocalFileSystem.prototype.set =function(scope, key, value, callback) {
var storagePath = getStoragePath(this.storageBaseDir ,scope);
return loadFile(storagePath + ".json").then(function(data){
loadFile(storagePath + ".json").then(function(data){
var obj = data ? JSON.parse(data) : {}
util.setMessageProperty(obj,key,value);
return obj;
}).then(function(obj){
var str = JSON.stringify(obj, undefined, 4);
return fs.outputFile(storagePath + ".json", str, {encoding:"utf8",flag:"w+"});
return fs.outputFile(storagePath + ".json", JSON.stringify(obj, undefined, 4), "utf8");
}).then(function(){
if(typeof callback === "function"){
callback(null);
}
}).catch(function(err){
return Promise.reject(err);
if(typeof callback === "function"){
callback(err);
}
});
};
LocalFileSystem.prototype.keys = function(scope){
LocalFileSystem.prototype.keys = function(scope, callback){
if(typeof callback !== "function"){
throw new Error("Callback must be a function");
}
var storagePath = getStoragePath(this.storageBaseDir ,scope);
return loadFile(storagePath + ".json").then(function(data){
loadFile(storagePath + ".json").then(function(data){
if(data){
return Object.keys(JSON.parse(data));
callback(null, Object.keys(JSON.parse(data)));
}else{
return []
callback(null, []);
}
}).catch(function(err){
return Promise.reject(err);
callback(err);
});
};

View File

@@ -50,7 +50,7 @@ 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" && key !== "setAsync" && key !== "getAsync" && key !== "keysAsync";
return key !== "set" && key !== "get" && key !== "keys";
});
}
};