Catch exception.

This commit is contained in:
Miao Jiang 2013-02-22 02:07:26 +08:00
parent 279e9f04cc
commit 8f707b34b1
3 changed files with 38 additions and 24 deletions

View file

@ -1,6 +1,6 @@
// Generated by ToffeeScript 1.4.0
(function() {
var Curl, id, m, p;
var Curl, curls, id, m, p;
try {
Curl = require(__dirname + '/../build/Release/node-curl').Curl;
@ -53,16 +53,29 @@
DEBUG: 'DEBUG'
};
id = 0;
curls = {};
Curl.prototype.on = function(event, callback) {
var _this = this;
switch (event) {
case 'data':
this.on_write = callback;
this.on_write = function(chunk) {
return callback.call(_this, chunk);
};
break;
case 'error':
this.on_error = callback;
this.on_error = function(e) {
delete curls[_this.id];
return callback.call(_this, e);
};
break;
case 'end':
this.on_end = callback;
this.on_end = function() {
delete curls[_this.id];
return callback.call(_this);
};
break;
default:
throw new Error("invalid event type " + event);
@ -71,13 +84,13 @@
};
Curl.prototype.close = function() {
delete curls[this.id];
return this.close_();
};
id = 0;
Curl.prototype.perform = function() {
this.id = ++id;
curls[this.id] = this;
this.perform_();
Curl.process();
return this;

View file

@ -42,28 +42,37 @@ Curl.user_options =
RAW: 'RAW'
DEBUG: 'DEBUG'
id = 0
curls = {}
# on 'data' must be returns the chunk length
Curl::on = (event, callback) ->
switch event
when 'data'
# (Buffer chunk) ->
@on_write = callback
@on_write = (chunk) =>
callback.call @, chunk
when 'error'
# (Error error) ->
@on_error = callback
@on_error = (e) =>
delete curls[@id]
callback.call @, e
when 'end'
# () ->
@on_end = callback
@on_end = =>
delete curls[@id]
callback.call @
else
throw new Error("invalid event type #{event}")
@
Curl::close = () ->
delete curls[@id]
@close_()
id = 0
Curl::perform = ->
@id = ++id
curls[@id] = @
@perform_()
Curl.process()
@

View file

@ -29,14 +29,12 @@ class NodeCurl
CURL * curl;
v8::Persistent<v8::Object> handle;
v8::Persistent<v8::Object> ref;
int ref_no;
bool in_curlm;
std::vector<curl_slist*> slists;
std::map<int, std::string> strings;
NodeCurl(v8::Handle<v8::Object> object)
: in_curlm(false), ref_no(0)
: in_curlm(false)
{
++count;
v8::V8::AdjustAmountOfExternalAllocatedMemory(2*4096);
@ -121,7 +119,11 @@ class NodeCurl
{
node::Buffer * buffer = node::Buffer::New(data, n);
v8::Handle<v8::Value> argv[] = { buffer->handle_ };
n = cb->ToObject()->CallAsFunction(handle, 1, argv)->Int32Value();
v8::Handle<v8::Value> rt = cb->ToObject()->CallAsFunction(handle, 1, argv);
if (rt.IsEmpty())
return 0;
else
return rt->Int32Value();
}
return n;
}
@ -330,12 +332,6 @@ class NodeCurl
curl->on_end(&msg_copy);
else
curl->on_error(&msg_copy);
--curl->ref_no;
if (curl->ref_no == 0)
curl->ref.Dispose();
}
}
}
@ -360,10 +356,6 @@ class NodeCurl
curl->in_curlm = true;
++running_handles;
if (curl->ref_no == 0)
curl->ref=v8::Persistent<v8::Object>::New(curl->handle);
++curl->ref_no;
return args.This();
}