Browse Source

Add Promise class

v0.7.4-release
Ryan 16 years ago
parent
commit
7cd09874c6
  1. 31
      src/events.cc
  2. 28
      src/events.h
  3. 13
      src/events.js
  4. 1
      src/node.cc

31
src/events.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;
}

28
src/events.h

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

13
src/events.js

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

1
src/node.cc

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

Loading…
Cancel
Save