[Navigator] Add a callback that is called after emitting an event.

Summary:
While adeveloper requests the emitter to emit an event, the emitter
may not emit the event immediately instead of putting the request
into a queue and process it later.

This diff allows the developer to provide a callback which will be called
when the event has been emitted.

For instance:

```
class NavigationContext {
  push(nextRoute) {
    var nextStack = this._stack.push(nextRoute);
    this.emit(
      'change',
      {
        reason: 'push',
        nextStack: nextStack,
        nextRoute: nextRoute,
      },
      this._onPush
    );
  }

  _onPush(event){
    if (event.defaultPrevented) {
      return;
    }
    this._stack = event.nextStack;
    this.emit('change');
  }
}
```
This commit is contained in:
Hedger Wang
2015-07-20 22:49:53 -07:00
parent 9105b22925
commit 6e2f07fb81
3 changed files with 119 additions and 24 deletions

View File

@@ -36,7 +36,7 @@ class NavigationEventPool {
this._list = [];
}
get(type: String, target: Object, data: any): NavigationEvent {
get(type: string, target: Object, data: any): NavigationEvent {
var event;
if (this._list.length > 0) {
event = this._list.pop();
@@ -59,13 +59,13 @@ class NavigationEvent {
_defaultPrevented: boolean;
_disposed: boolean;
_target: ?Object;
_type: ?String;
_type: ?string;
static pool(type: String, target: Object, data: any): NavigationEvent {
static pool(type: string, target: Object, data: any): NavigationEvent {
return _navigationEventPool.get(type, target, data);
}
constructor(type: String, target: Object, data: any) {
constructor(type: string, target: Object, data: any) {
this._type = type;
this._target = target;
this._data = data;