diff --git a/lib/Curl.js b/lib/Curl.js index aa11a24..0101caa 100644 --- a/lib/Curl.js +++ b/lib/Curl.js @@ -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; diff --git a/lib/Curl.toffee b/lib/Curl.toffee index 25dcf84..b06d7bf 100644 --- a/lib/Curl.toffee +++ b/lib/Curl.toffee @@ -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() @ diff --git a/src/node-curl.h b/src/node-curl.h index 715093a..e7460ae 100644 --- a/src/node-curl.h +++ b/src/node-curl.h @@ -29,14 +29,12 @@ class NodeCurl CURL * curl; v8::Persistent handle; - v8::Persistent ref; - int ref_no; bool in_curlm; std::vector slists; std::map strings; NodeCurl(v8::Handle 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 argv[] = { buffer->handle_ }; - n = cb->ToObject()->CallAsFunction(handle, 1, argv)->Int32Value(); + v8::Handle 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::New(curl->handle); - ++curl->ref_no; - return args.This(); }