Commit 112208d2 authored by Tristan Cavelier's avatar Tristan Cavelier

Split parametersToObject into two methods (parametersToObject and makeCallbacks)

The jio methods post, put, get, remove and allDocs has been modified to use
these new private methods.
parent 89aa1436
...@@ -122,33 +122,48 @@ ...@@ -122,33 +122,48 @@
} }
}); });
priv.parametersToObject = function (list,nodoc) { priv.makeCallbacks = function (param, callback1, callback2) {
var param = {}, i = 0; param.callback = function (err, val) {
if (!nodoc) { if (err) {
param.doc = list[i]; param.error(err);
i ++;
}
if (typeof list[i] === 'object') {
param.options = list[i];
i ++;
} else { } else {
param.options = {}; param.success(val);
} }
param.callback = function (err,val){}; };
param.success = function (val) { param.success = function (val) {
param.callback(undefined,val); param.callback(undefined,val);
}; };
param.error = function (err) { param.error = function (err) {
param.callback(err,undefined); param.callback(err,undefined);
}; };
if (typeof list[i] === 'function') { if (typeof callback1 === 'function') {
if (typeof list[i+1] === 'function') { if (typeof callback2 === 'function') {
param.success = list[i]; param.success = callback1;
param.error = list[i+1]; param.error = callback2;
} else { } else {
param.callback = list[i]; param.callback = callback1;
}
}
};
priv.parametersToObject = function (list, default_options) {
var k, i = 0, callbacks = [], param = {options:{}};
for (i = 0; i < list.length; i += 1) {
if (typeof list[i] === 'object') {
// this is the option
param.options = list[i];
for (k in default_options) {
if ((typeof default_options[k]) !== (typeof list[i][k])) {
param.options[k] = default_options[k];
}
}
}
if (typeof list[i] === 'function') {
// this is a callback
callbacks.push(list[i]);
} }
} }
priv.makeCallbacks(param,callbacks[0],callbacks[1]);
return param; return param;
}; };
...@@ -174,11 +189,13 @@ ...@@ -174,11 +189,13 @@
*/ */
Object.defineProperty(that,"post",{ Object.defineProperty(that,"post",{
configurable:false,enumerable:false,writable:false,value: configurable:false,enumerable:false,writable:false,value:
function() { function(doc, options, success, error) {
var param = priv.parametersToObject(arguments); var param = priv.parametersToObject(
param.options.max_retry = param.options.max_retry || 0; [options, success, error],
{max_retry:0}
);
priv.addJob(postCommand,{ priv.addJob(postCommand,{
doc:param.doc, doc:doc,
options:param.options, options:param.options,
callbacks:{success:param.success,error:param.error} callbacks:{success:param.success,error:param.error}
}); });
...@@ -201,11 +218,13 @@ ...@@ -201,11 +218,13 @@
*/ */
Object.defineProperty(that,"put",{ Object.defineProperty(that,"put",{
configurable:false,enumerable:false,writable:false,value: configurable:false,enumerable:false,writable:false,value:
function() { function(doc, options, success, error) {
var param = priv.parametersToObject(arguments); var param = priv.parametersToObject(
param.options.max_retry = param.options.max_retry || 0; [options, success, error],
{max_retry:0}
);
priv.addJob(putCommand,{ priv.addJob(putCommand,{
doc:param.doc, doc:doc,
options:param.options, options:param.options,
callbacks:{success:param.success,error:param.error} callbacks:{success:param.success,error:param.error}
}); });
...@@ -229,11 +248,13 @@ ...@@ -229,11 +248,13 @@
*/ */
Object.defineProperty(that,"get",{ Object.defineProperty(that,"get",{
configurable:false,enumerable:false,writable:false,value: configurable:false,enumerable:false,writable:false,value:
function() { function(id, options, success, error) {
var param = priv.parametersToObject(arguments); var param = priv.parametersToObject(
param.options.max_retry = param.options.max_retry || 3; [options,success,error],
{max_retry:3}
);
priv.addJob(getCommand,{ priv.addJob(getCommand,{
docid:param.doc, docid:id,
options:param.options, options:param.options,
callbacks:{success:param.success,error:param.error} callbacks:{success:param.success,error:param.error}
}); });
...@@ -256,11 +277,13 @@ ...@@ -256,11 +277,13 @@
*/ */
Object.defineProperty(that,"remove",{ Object.defineProperty(that,"remove",{
configurable:false,enumerable:false,writable:false,value: configurable:false,enumerable:false,writable:false,value:
function() { function(doc, options, success, callback) {
var param = priv.parametersToObject(arguments); var param = priv.parametersToObject(
param.options.max_retry = param.options.max_retry || 0; [options,success,callback],
{max_retry:0}
);
priv.addJob(removeCommand,{ priv.addJob(removeCommand,{
doc:param.doc, doc:doc,
options:param.options, options:param.options,
callbacks:{success:param.success,error:param.error} callbacks:{success:param.success,error:param.error}
}); });
...@@ -283,9 +306,11 @@ ...@@ -283,9 +306,11 @@
*/ */
Object.defineProperty(that,"allDocs",{ Object.defineProperty(that,"allDocs",{
configurable:false,enumerable:false,writable:false,value: configurable:false,enumerable:false,writable:false,value:
function() { function(options, success, error) {
var param = priv.parametersToObject(arguments,'no doc'); var param = priv.parametersToObject(
param.options.max_retry = param.options.max_retry || 3; [options,success.error],
{max_retry: 3}
);
priv.addJob(allDocsCommand,{ priv.addJob(allDocsCommand,{
options:param.options, options:param.options,
callbacks:{success:param.success,error:param.error} callbacks:{success:param.success,error:param.error}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment