Skip to content
Permalink
Browse files

Add Promise class

  • Loading branch information
ry committed Jun 28, 2009
1 parent ed926da commit 7cd09874c666f0ce64b1d7776de74f55ff3e53ab
Showing with 70 additions and 3 deletions.
  1. +31 −0 src/events.cc
  2. +26 −2 src/events.h
  3. +12 −1 src/events.js
  4. +1 −0 src/node.cc
@@ -55,3 +55,34 @@ EventEmitter::Emit (const char *type, int argc, Handle<Value> argv[])

return true;
}

Persistent<FunctionTemplate> Promise::constructor_template;

void
Promise::Initialize (v8::Handle<v8::Object> target)
{
HandleScope scope;

Local<FunctionTemplate> t = FunctionTemplate::New();
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->Inherit(EventEmitter::constructor_template);

// All prototype methods are defined in events.js

target->Set(String::NewSymbol("Promise"),
constructor_template->GetFunction());
}

Promise*
Promise::Create (void)
{
HandleScope scope;

Local<Object> handle =
Promise::constructor_template->GetFunction()->NewInstance();
Promise *promise = new Promise(handle);
ObjectWrap::InformV8ofAllocation(promise);

return promise;
}

@@ -7,14 +7,38 @@
namespace node {

class EventEmitter : public ObjectWrap {
public:
public:
static void Initialize (v8::Handle<v8::Object> target);
static v8::Persistent<v8::FunctionTemplate> constructor_template;
virtual size_t size (void) { return sizeof(EventEmitter); };

bool Emit (const char *type, int argc, v8::Handle<v8::Value> argv[]);

EventEmitter (v8::Handle<v8::Object> handle)
: ObjectWrap(handle) { };
: ObjectWrap(handle) { }
};

class Promise : public EventEmitter {
public:
static void Initialize (v8::Handle<v8::Object> target);
static v8::Persistent<v8::FunctionTemplate> constructor_template;
virtual size_t size (void) { return sizeof(Promise); };

bool EmitSuccess (int argc, v8::Handle<v8::Value> argv[])
{
return Emit("Success", argc, argv);
}

bool EmitError (int argc, v8::Handle<v8::Value> argv[])
{
return Emit("Error", argc, argv);
}

static Promise* Create (void);

protected:

Promise (v8::Handle<v8::Object> handle) : EventEmitter(handle) { }
};

} // namespace node
@@ -30,4 +30,15 @@ emitter.emit = function (type, args) {
}
};

})(); // end annonymous namespace
// node.Promise is defined in src/events.cc
var promise = node.Promise.prototype;

promise.addCallback = function (listener) {
this.addListener("Success", listener);
};

promise.addErrback = function (listener) {
this.addListener("Error", listener);
};

})(); // end anonymous namespace
@@ -313,6 +313,7 @@ Load (int argc, char *argv[])
NODE_SET_METHOD(node_obj, "reallyExit", node_exit);

EventEmitter::Initialize(node_obj);
Promise::Initialize(node_obj);
Timer::Initialize(node_obj);
Process::Initialize(node_obj);

0 comments on commit 7cd0987

Please sign in to comment.
You can’t perform that action at this time.