Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Yaxel Perez
jio
Commits
14aa39ea
Commit
14aa39ea
authored
Jan 10, 2013
by
Sven Franck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
jslint pass command.js (2indent)
parent
fbdf009d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
283 additions
and
289 deletions
+283
-289
src/jio/commands/command.js
src/jio/commands/command.js
+283
-289
No files found.
src/jio/commands/command.js
View file @
14aa39ea
var
command
=
function
(
spec
,
my
)
{
// -*- jslint: --nomen --todo --maxlen 80 -*-
var
that
=
{};
var
command
=
function
(
spec
,
my
)
{
spec
=
spec
||
{};
var
that
=
{},
my
=
my
||
{};
priv
=
{};
// Attributes //
var
priv
=
{};
priv
.
commandlist
=
{
'
post
'
:
postCommand
,
'
put
'
:
putCommand
,
'
get
'
:
getCommand
,
'
remove
'
:
removeCommand
,
'
allDocs
'
:
allDocsCommand
,
'
putAttachment
'
:
putAttachmentCommand
};
// creates the good command thanks to his label
if
(
spec
.
label
&&
priv
.
commandlist
[
spec
.
label
])
{
priv
.
label
=
spec
.
label
;
delete
spec
.
label
;
return
priv
.
commandlist
[
priv
.
label
](
spec
,
my
);
}
priv
.
tried
=
0
;
priv
.
doc
=
spec
.
doc
||
{};
if
(
typeof
priv
.
doc
!==
"
object
"
)
{
priv
.
doc
=
{
"
_id
"
:
priv
.
doc
.
toString
()};
}
priv
.
docid
=
spec
.
docid
||
priv
.
doc
.
_id
;
priv
.
option
=
spec
.
options
||
{};
priv
.
callbacks
=
spec
.
callbacks
||
{};
priv
.
success
=
priv
.
callbacks
.
success
||
function
(){};
priv
.
error
=
priv
.
callbacks
.
error
||
function
(){};
priv
.
retry
=
function
()
{
that
.
error
({
status
:
13
,
statusText
:
'
Fail Retry
'
,
error
:
'
fail_retry
'
,
message
:
'
Impossible to retry.
'
,
reason
:
'
Impossible to retry.
'
});
};
priv
.
end
=
function
()
{};
priv
.
on_going
=
false
;
// Methods //
/**
* Returns a serialized version of this command.
* @method serialized
* @return {object} The serialized command.
*/
that
.
serialized
=
function
()
{
var
o
=
{};
o
[
"
label
"
]
=
that
.
getLabel
();
o
[
"
tried
"
]
=
priv
.
tried
;
o
[
"
doc
"
]
=
that
.
cloneDoc
();
o
[
"
option
"
]
=
that
.
cloneOption
();
return
o
;
};
/**
* Returns the label of the command.
* @method getLabel
* @return {string} The label.
*/
that
.
getLabel
=
function
()
{
return
'
command
'
;
};
/**
* Gets the document id
* @method getDocId
* @return {string} The document id
*/
that
.
getDocId
=
function
()
{
if
(
typeof
priv
.
docid
!==
"
string
"
)
{
return
undefined
;
}
return
priv
.
docid
.
split
(
'
/
'
)[
0
];
};
/**
* Gets the attachment id
* @method getAttachmentId
* @return {string} The attachment id
*/
that
.
getAttachmentId
=
function
()
{
if
(
typeof
priv
.
docid
!==
"
string
"
)
{
return
undefined
;
}
return
priv
.
docid
.
split
(
'
/
'
)[
1
];
};
/**
* Returns the label of the command.
* @method getDoc
* @return {object} The document.
*/
that
.
getDoc
=
function
()
{
return
priv
.
doc
;
};
/**
* Returns the data of the attachment
* @method getAttachmentData
* @return {string} The data
*/
that
.
getAttachmentData
=
function
()
{
return
priv
.
doc
.
_data
||
""
;
};
/**
* Returns the data length of the attachment
* @method getAttachmentLength
* @return {number} The length
*/
that
.
getAttachmentLength
=
function
()
{
return
(
priv
.
doc
.
_data
||
""
).
length
;
};
/**
* Returns the mimetype of the attachment
* @method getAttachmentMimeType
* @return {string} The mimetype
*/
that
.
getAttachmentMimeType
=
function
()
{
return
priv
.
doc
.
_mimetype
;
};
/**
* Generate the md5sum of the attachment data
* @method md5SumAttachmentData
* @return {string} The md5sum
*/
that
.
md5SumAttachmentData
=
function
()
{
return
hex_md5
(
priv
.
doc
.
_data
||
""
);
};
/**
* Returns an information about the document.
* @method getDocInfo
* @param {string} infoname The info name.
* @return The info value.
*/
that
.
getDocInfo
=
function
(
infoname
)
{
return
priv
.
doc
[
infoname
];
};
/**
spec
=
spec
||
{};
* Returns the value of an option.
my
=
my
||
{};
* @method getOption
* @param {string} optionname The option name.
* @return The option value.
*/
that
.
getOption
=
function
(
optionname
)
{
return
priv
.
option
[
optionname
];
};
/**
priv
.
commandlist
=
{
* Validates the storage.
'
post
'
:
postCommand
,
* @param {object} storage The storage.
'
put
'
:
putCommand
,
*/
'
get
'
:
getCommand
,
that
.
validate
=
function
(
storage
)
{
'
remove
'
:
removeCommand
,
if
(
typeof
priv
.
docid
===
"
string
"
&&
'
allDocs
'
:
allDocsCommand
,
!
priv
.
docid
.
match
(
/^
[^\/]
+
([\/][^\/]
+
)?
$/
))
{
'
putAttachment
'
:
putAttachmentCommand
that
.
error
({
};
status
:
21
,
statusText
:
'
Invalid Document Id
'
,
// creates the good command thanks to his label
error
:
'
invalid_document_id
'
,
if
(
spec
.
label
&&
priv
.
commandlist
[
spec
.
label
])
{
message
:
'
The document id must be like "abc" or "abc/def".
'
,
priv
.
label
=
spec
.
label
;
reason
:
'
The document id is no like "abc" or "abc/def"
'
delete
spec
.
label
;
});
return
priv
.
commandlist
[
priv
.
label
](
spec
,
my
);
return
false
;
}
}
if
(
!
that
.
validateState
())
{
return
false
;
}
return
storage
.
validate
();
};
/*
priv
.
tried
=
0
;
* Extend this function
priv
.
doc
=
spec
.
doc
||
{};
*/
if
(
typeof
priv
.
doc
!==
"
object
"
)
{
that
.
validateState
=
function
()
{
priv
.
doc
=
{
return
true
;
"
_id
"
:
priv
.
doc
.
toString
()
};
};
}
priv
.
docid
=
spec
.
docid
||
priv
.
doc
.
_id
;
priv
.
option
=
spec
.
options
||
{};
priv
.
callbacks
=
spec
.
callbacks
||
{};
priv
.
success
=
priv
.
callbacks
.
success
||
function
()
{};
priv
.
error
=
priv
.
callbacks
.
error
||
function
()
{};
priv
.
retry
=
function
()
{
that
.
error
({
status
:
13
,
statusText
:
'
Fail Retry
'
,
error
:
'
fail_retry
'
,
message
:
'
Impossible to retry.
'
,
reason
:
'
Impossible to retry.
'
});
};
priv
.
end
=
function
()
{};
priv
.
on_going
=
false
;
/**
// Methods //
* Check if the command can be retried.
/**
* @method canBeRetried
* Returns a serialized version of this command.
* @return {boolean} The result
* @method serialized
*/
* @return {object} The serialized command.
that
.
canBeRetried
=
function
()
{
*/
return
(
typeof
priv
.
option
.
max_retry
===
'
undefined
'
||
that
.
serialized
=
function
()
{
priv
.
option
.
max_retry
===
0
||
var
o
=
{};
priv
.
tried
<
priv
.
option
.
max_retry
);
o
.
label
=
that
.
getLabel
();
};
o
.
tried
=
priv
.
tried
;
o
.
doc
=
that
.
cloneDoc
();
o
.
option
=
that
.
cloneOption
();
return
o
;
};
/**
/**
* Gets the number time the command has been trie
d.
* Returns the label of the comman
d.
* @method getTried
* @method getLabel
* @return {number} The number of time the command has been tried
* @return {string} The label.
*/
*/
that
.
getTried
=
function
()
{
that
.
getLabel
=
function
()
{
return
priv
.
tried
;
return
'
command
'
;
};
};
/**
/**
* Delegate actual excecution the storage.
* Gets the document id
* @param {object} storage The storage.
* @method getDocId
*/
* @return {string} The document id
that
.
execute
=
function
(
storage
)
{
*/
if
(
!
priv
.
on_going
)
{
that
.
getDocId
=
function
()
{
if
(
that
.
validate
(
storage
))
{
if
(
typeof
priv
.
docid
!==
"
string
"
)
{
priv
.
tried
++
;
return
undefined
;
priv
.
on_going
=
true
;
}
storage
.
execute
(
that
);
return
priv
.
docid
.
split
(
'
/
'
)[
0
];
}
};
}
};
/**
/**
* Execute the good method from the storage.
* Gets the attachment id
* Override this function.
* @method getAttachmentId
* @method executeOn
* @return {string} The attachment id
* @param {object} storage The storage.
*/
*/
that
.
getAttachmentId
=
function
()
{
that
.
executeOn
=
function
(
storage
)
{};
if
(
typeof
priv
.
docid
!==
"
string
"
)
{
return
undefined
;
}
return
priv
.
docid
.
split
(
'
/
'
)[
1
];
};
that
.
success
=
function
(
return_value
)
{
/**
priv
.
on_going
=
false
;
* Returns the label of the command.
priv
.
success
(
return_value
);
* @method getDoc
priv
.
end
(
doneStatus
());
* @return {object} The document.
};
*/
that
.
getDoc
=
function
()
{
return
priv
.
doc
;
};
that
.
retry
=
function
(
return_error
)
{
/**
priv
.
on_going
=
false
;
* Returns the data of the attachment
if
(
that
.
canBeRetried
())
{
* @method getAttachmentData
priv
.
retry
();
* @return {string} The data
}
else
{
*/
that
.
error
(
return_error
);
that
.
getAttachmentData
=
function
()
{
}
return
priv
.
doc
.
_data
||
""
;
};
};
that
.
error
=
function
(
return_error
)
{
/**
priv
.
on_going
=
false
;
* Returns the data length of the attachment
priv
.
error
(
return_error
);
* @method getAttachmentLength
priv
.
end
(
failStatus
());
* @return {number} The length
};
*/
that
.
getAttachmentLength
=
function
()
{
return
(
priv
.
doc
.
_data
||
""
).
length
;
};
that
.
end
=
function
()
{
/**
priv
.
end
(
doneStatus
());
* Returns the mimetype of the attachment
};
* @method getAttachmentMimeType
* @return {string} The mimetype
*/
that
.
getAttachmentMimeType
=
function
()
{
return
priv
.
doc
.
_mimetype
;
};
that
.
onSuccessDo
=
function
(
fun
)
{
/**
if
(
fun
)
{
* Generate the md5sum of the attachment data
priv
.
success
=
fun
;
* @method md5SumAttachmentData
}
else
{
* @return {string} The md5sum
return
priv
.
success
;
*/
}
that
.
md5SumAttachmentData
=
function
()
{
};
return
hex_md5
(
priv
.
doc
.
_data
||
""
);
};
that
.
onErrorDo
=
function
(
fun
)
{
/**
if
(
fun
)
{
* Returns an information about the document.
priv
.
error
=
fun
;
* @method getDocInfo
}
else
{
* @param {string} infoname The info name.
return
priv
.
error
;
* @return The info value.
}
*/
};
that
.
getDocInfo
=
function
(
infoname
)
{
return
priv
.
doc
[
infoname
];
};
that
.
onEndDo
=
function
(
fun
)
{
/**
priv
.
end
=
fun
;
* Returns the value of an option.
};
* @method getOption
* @param {string} optionname The option name.
* @return The option value.
*/
that
.
getOption
=
function
(
optionname
)
{
return
priv
.
option
[
optionname
];
};
that
.
onRetryDo
=
function
(
fun
)
{
/**
priv
.
retry
=
fun
;
* Validates the storage.
};
* @param {object} storage The storage.
*/
that
.
validate
=
function
(
storage
)
{
if
(
typeof
priv
.
docid
===
"
string
"
&&
!
priv
.
docid
.
match
(
"
^[^
\
/]+([
\
/][^
\
/]+)?$
"
))
{
that
.
error
({
status
:
21
,
statusText
:
'
Invalid Document Id
'
,
error
:
'
invalid_document_id
'
,
message
:
'
The document id must be like "abc" or "abc/def".
'
,
reason
:
'
The document id is no like "abc" or "abc/def"
'
});
return
false
;
}
if
(
!
that
.
validateState
())
{
return
false
;
}
return
storage
.
validate
();
};
/**
/*
* Is the command can be restored by another JIO : yes.
* Extend this function
* @method canBeRestored
*/
* @return {boolean} true
that
.
validateState
=
function
()
{
*/
return
true
;
that
.
canBeRestored
=
function
()
{
};
return
true
;
};
/**
/**
* Clones the command and returns it.
* Check if the command can be retried.
* @method clone
* @method canBeRetried
* @return {object} The cloned command.
* @return {boolean} The result
*/
*/
that
.
clone
=
function
()
{
that
.
canBeRetried
=
function
()
{
return
command
(
that
.
serialized
(),
my
);
return
(
priv
.
option
.
max_retry
===
undefined
||
};
priv
.
option
.
max_retry
===
0
||
priv
.
tried
<
priv
.
option
.
max_retry
);
};
/**
/**
* Clones the command options and returns the clone version.
* Gets the number time the command has been tried.
* @method cloneOption
* @method getTried
* @return {object} The clone of the command options.
* @return {number} The number of time the command has been tried
*/
*/
that
.
cloneOption
=
function
()
{
that
.
getTried
=
function
()
{
return
JSON
.
parse
(
JSON
.
stringify
(
priv
.
option
));
return
priv
.
tried
;
};
};
/**
* Clones the document and returns the clone version.
* @method cloneDoc
* @return {object} The clone of the document.
*/
that
.
cloneDoc
=
function
()
{
return
JSON
.
parse
(
JSON
.
stringify
(
priv
.
doc
));
};
return
that
;
/**
};
* Delegate actual excecution the storage.
* @param {object} storage The storage.
*/
that
.
execute
=
function
(
storage
)
{
if
(
!
priv
.
on_going
)
{
if
(
that
.
validate
(
storage
))
{
priv
.
tried
+=
1
;
priv
.
on_going
=
true
;
storage
.
execute
(
that
);
}
}
};
/**
* Execute the good method from the storage.
* Override this function.
* @method executeOn
* @param {object} storage The storage.
*/
that
.
executeOn
=
function
(
storage
)
{};
that
.
success
=
function
(
return_value
)
{
priv
.
on_going
=
false
;
priv
.
success
(
return_value
);
priv
.
end
(
doneStatus
());
};
that
.
retry
=
function
(
return_error
)
{
priv
.
on_going
=
false
;
if
(
that
.
canBeRetried
())
{
priv
.
retry
();
}
else
{
that
.
error
(
return_error
);
}
};
that
.
error
=
function
(
return_error
)
{
priv
.
on_going
=
false
;
priv
.
error
(
return_error
);
priv
.
end
(
failStatus
());
};
that
.
end
=
function
()
{
priv
.
end
(
doneStatus
());
};
that
.
onSuccessDo
=
function
(
fun
)
{
if
(
fun
)
{
priv
.
success
=
fun
;
}
else
{
return
priv
.
success
;
}
};
that
.
onErrorDo
=
function
(
fun
)
{
if
(
fun
)
{
priv
.
error
=
fun
;
}
else
{
return
priv
.
error
;
}
};
that
.
onEndDo
=
function
(
fun
)
{
priv
.
end
=
fun
;
};
that
.
onRetryDo
=
function
(
fun
)
{
priv
.
retry
=
fun
;
};
/**
* Is the command can be restored by another JIO : yes.
* @method canBeRestored
* @return {boolean} true
*/
that
.
canBeRestored
=
function
()
{
return
true
;
};
/**
* Clones the command and returns it.
* @method clone
* @return {object} The cloned command.
*/
that
.
clone
=
function
()
{
return
command
(
that
.
serialized
(),
my
);
};
/**
* Clones the command options and returns the clone version.
* @method cloneOption
* @return {object} The clone of the command options.
*/
that
.
cloneOption
=
function
()
{
return
JSON
.
parse
(
JSON
.
stringify
(
priv
.
option
));
};
/**
* Clones the document and returns the clone version.
* @method cloneDoc
* @return {object} The clone of the document.
*/
that
.
cloneDoc
=
function
()
{
return
JSON
.
parse
(
JSON
.
stringify
(
priv
.
doc
));
};
return
that
;
};
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment