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()
@