Improve code.
This commit is contained in:
parent
648e170fa8
commit
78a53743de
6 changed files with 41 additions and 41 deletions
28
lib/Curl.js
28
lib/Curl.js
|
@ -1,6 +1,6 @@
|
|||
// Generated by ToffeeScript 1.4.0
|
||||
(function() {
|
||||
var Curl, curls, id, m, p;
|
||||
var Curl, id, m, p;
|
||||
|
||||
try {
|
||||
Curl = require(__dirname + '/../build/Release/node-curl').Curl;
|
||||
|
@ -8,10 +8,6 @@
|
|||
Curl = require(__dirname + '/../build/default/node-curl').Curl;
|
||||
}
|
||||
|
||||
curls = {};
|
||||
|
||||
id = 0;
|
||||
|
||||
Curl.prototype.setopt_user_ = function(option_id, value) {
|
||||
return this.options[option_id] = value;
|
||||
};
|
||||
|
@ -58,26 +54,15 @@
|
|||
};
|
||||
|
||||
Curl.prototype.on = function(event, callback) {
|
||||
var _this = this;
|
||||
switch (event) {
|
||||
case 'data':
|
||||
this.on_write = callback;
|
||||
break;
|
||||
case 'error':
|
||||
this.on_error = function() {
|
||||
var rt;
|
||||
rt = callback();
|
||||
curls[_this.id] = null;
|
||||
return rt;
|
||||
};
|
||||
this.on_error = callback;
|
||||
break;
|
||||
case 'end':
|
||||
this.on_end = function() {
|
||||
var rt;
|
||||
rt = callback();
|
||||
curls[_this.id] = null;
|
||||
return rt;
|
||||
};
|
||||
this.on_end = callback;
|
||||
break;
|
||||
default:
|
||||
throw new Error("invalid event type " + event);
|
||||
|
@ -85,9 +70,14 @@
|
|||
return this;
|
||||
};
|
||||
|
||||
Curl.prototype.close = function() {
|
||||
return this.close_();
|
||||
};
|
||||
|
||||
id = 0;
|
||||
|
||||
Curl.prototype.perform = function() {
|
||||
this.id = ++id;
|
||||
curls[this.id] = this;
|
||||
this.perform_();
|
||||
Curl.process();
|
||||
return this;
|
||||
|
|
|
@ -3,8 +3,6 @@ try
|
|||
catch e
|
||||
{Curl} = require __dirname + '/../build/default/node-curl'
|
||||
|
||||
curls = {}
|
||||
id = 0
|
||||
Curl::setopt_user_ = (option_id, value) ->
|
||||
@options[option_id] = value
|
||||
|
||||
|
@ -52,24 +50,20 @@ Curl::on = (event, callback) ->
|
|||
@on_write = callback
|
||||
when 'error'
|
||||
# (Error error) ->
|
||||
@on_error = =>
|
||||
rt = callback()
|
||||
curls[@id] = null
|
||||
rt
|
||||
|
||||
@on_error = callback
|
||||
when 'end'
|
||||
# () ->
|
||||
@on_end = =>
|
||||
rt = callback()
|
||||
curls[@id] = null
|
||||
rt
|
||||
@on_end = callback
|
||||
else
|
||||
throw new Error("invalid event type #{event}")
|
||||
@
|
||||
|
||||
Curl::close = () ->
|
||||
@close_()
|
||||
|
||||
id = 0
|
||||
Curl::perform = ->
|
||||
@id = ++id
|
||||
curls[@id] = @
|
||||
@perform_()
|
||||
Curl.process()
|
||||
@
|
||||
|
@ -96,5 +90,6 @@ Curl.process = ->
|
|||
Curl.in_process = false
|
||||
|
||||
|
||||
|
||||
module.exports = Curl
|
||||
# vim: sw=2 ts=2 sts=2 expandtab :
|
||||
|
|
|
@ -50,8 +50,6 @@
|
|||
if ((_ref = this.options) == null) {
|
||||
this.options = {};
|
||||
}
|
||||
c = this.curl_;
|
||||
c.chunks = [];
|
||||
length = 0;
|
||||
this.debug = (_ref1 = (_ref2 = this.defaultOptions.DEBUG) != null ? _ref2 : this.options.DEBUG) != null ? _ref1 : this.debug;
|
||||
this.effectiveOptions = {};
|
||||
|
@ -69,6 +67,8 @@
|
|||
this.setOptions({
|
||||
URL: this.url
|
||||
});
|
||||
c = this.curl_;
|
||||
c.chunks = [];
|
||||
c.on('data', function(chunk) {
|
||||
curl.log("receive " + chunk.length + " bytes");
|
||||
c.chunks.push(chunk);
|
||||
|
|
|
@ -30,13 +30,9 @@ class CurlBuilder
|
|||
[@url, @options] = args
|
||||
@options ?= {}
|
||||
|
||||
c = @curl_
|
||||
c.chunks = []
|
||||
length = 0
|
||||
|
||||
@debug = @defaultOptions.DEBUG ? @options.DEBUG ? @debug
|
||||
|
||||
|
||||
@effectiveOptions = {}
|
||||
for k, v of @defaultOptions
|
||||
@effectiveOptions[k] = v
|
||||
|
@ -47,6 +43,8 @@ class CurlBuilder
|
|||
@setOptions @effectiveOptions
|
||||
@setOptions {URL: @url}
|
||||
|
||||
c = @curl_
|
||||
c.chunks = []
|
||||
c.on 'data', (chunk) ->
|
||||
curl.log "receive #{chunk.length} bytes"
|
||||
c.chunks.push chunk
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue