summaryrefslogtreecommitdiff
path: root/src/node_events.h
blob: 23433a543f5e12da4cbd74d8d6e9a032f01a9211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#ifndef SRC_EVENTS_H_
#define SRC_EVENTS_H_

#include <node_object_wrap.h>
#include <v8.h>

namespace node {

class EventEmitter : public ObjectWrap {
 public:
  static void Initialize(v8::Local<v8::FunctionTemplate> ctemplate);
  static v8::Persistent<v8::FunctionTemplate> constructor_template;

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

 protected:
  static v8::Handle<v8::Value> Emit(const v8::Arguments& args);

  EventEmitter() : ObjectWrap () { }
};

class Promise : public EventEmitter {
 public:
  static void Initialize(v8::Handle<v8::Object> target);

  static v8::Persistent<v8::FunctionTemplate> constructor_template;
  static Promise* Create(void);

  bool EmitSuccess(int argc, v8::Handle<v8::Value> argv[]);
  bool EmitError(int argc, v8::Handle<v8::Value> argv[]);
  void Block();

  v8::Handle<v8::Object> Handle() {
    return handle_;
  }

 protected:
  static v8::Handle<v8::Value> New(const v8::Arguments& args);
  static v8::Handle<v8::Value> Block(const v8::Arguments& args);
  static v8::Handle<v8::Value> EmitSuccess(const v8::Arguments& args);
  static v8::Handle<v8::Value> EmitError(const v8::Arguments& args);

  virtual void Detach(void);

  bool has_fired_;
  bool blocking_;
  Promise *prev_; /* for the prev in the Poor Man's coroutine stack */

  void Destack();

  Promise() : EventEmitter() {
    has_fired_ = false;
    blocking_ = false;
    prev_ = NULL;
  }
};

}  // namespace node
#endif  // SRC_EVENTS_H_