[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;

View File

@ -31,8 +31,9 @@ var EventEmitter = require('EventEmitter');
var NavigationEvent = require('NavigationEvent');
type EventParams = {
eventType: String;
data: any;
didEmitCallback: ?Function;
eventType: string;
};
class NavigationEventEmitter extends EventEmitter {
@ -47,22 +48,36 @@ class NavigationEventEmitter extends EventEmitter {
this._target = target;
}
emit(eventType: String, data: any): void {
emit(
eventType: string,
data: any,
didEmitCallback: ?Function
): void {
if (this._emitting) {
// An event cycle that was previously created hasn't finished yet.
// Put this event cycle into the queue and will finish them later.
this._emitQueue.push({eventType, data});
this._emitQueue.push({eventType, data, didEmitCallback});
return;
}
this._emitting = true;
var event = new NavigationEvent(eventType, this._target, data);
super.emit(eventType, event);
// EventEmitter#emit only takes `eventType` as `String`. Casting `eventType`
// to `String` to make @flow happy.
super.emit(String(eventType), event);
if (typeof didEmitCallback === 'function') {
didEmitCallback.call(this._target, event);
}
event.dispose();
this._emitting = false;
while (this._emitQueue.length) {
var arg = this._emitQueue.shift();
this.emit(arg.eventType, arg.data);
this.emit(arg.eventType, arg.data, arg.didEmitCallback);
}
}
}

View File

@ -34,27 +34,48 @@ jest
var NavigationEventEmitter = require('NavigationEventEmitter');
describe('NavigationEventEmitter', () => {
it('emit event', () => {
var target = {};
var emitter = new NavigationEventEmitter(target);
var focusCounter = 0;
var focusTarget;
it('emits event', () => {
var context = {};
var emitter = new NavigationEventEmitter(context);
var logs = [];
emitter.addListener('focus', (event) => {
focusCounter++;
focusTarget = event.target;
emitter.addListener('ping', (event) => {
var {type, data, target, defaultPrevented} = event;
logs.push({
data,
defaultPrevented,
target,
type,
});
emitter.emit('focus');
emitter.emit('blur');
expect(focusCounter).toBe(1);
expect(focusTarget).toBe(target);
});
it('put nested emit call in queue', () => {
var target = {};
var emitter = new NavigationEventEmitter(target);
emitter.emit('ping', 'hello');
expect(logs.length).toBe(1);
expect(logs[0].target).toBe(context);
expect(logs[0].type).toBe('ping');
expect(logs[0].data).toBe('hello');
expect(logs[0].defaultPrevented).toBe(false);
});
it('does not emit event that has no listeners', () => {
var context = {};
var emitter = new NavigationEventEmitter(context);
var pinged = false;
emitter.addListener('ping', () => {
pinged = true;
});
emitter.emit('yo', 'bo');
expect(pinged).toBe(false);
});
it('puts nested emit call in a queue', () => {
var context = {};
var emitter = new NavigationEventEmitter(context);
var logs = [];
emitter.addListener('one', () => {
@ -77,4 +98,63 @@ describe('NavigationEventEmitter', () => {
expect(logs).toEqual([1, 2, 3, 4, 5]);
});
it('calls callback after emitting', () => {
var context = {};
var emitter = new NavigationEventEmitter(context);
var logs = [];
emitter.addListener('ping', (event) => {
var {type, data, target, defaultPrevented} = event;
logs.push({
data,
defaultPrevented,
target,
type,
});
event.preventDefault();
});
emitter.emit('ping', 'hello', (event) => {
var {type, data, target, defaultPrevented} = event;
logs.push({
data,
defaultPrevented,
target,
type,
});
});
expect(logs.length).toBe(2);
expect(logs[1].target).toBe(context);
expect(logs[1].type).toBe('ping');
expect(logs[1].data).toBe('hello');
expect(logs[1].defaultPrevented).toBe(true);
});
it('calls callback after emitting the current event and before ' +
'emitting the next event', () => {
var context = {};
var emitter = new NavigationEventEmitter(context);
var logs = [];
emitter.addListener('ping', (event) => {
logs.push('ping');
emitter.emit('pong');
});
emitter.addListener('pong', (event) => {
logs.push('pong');
});
emitter.emit('ping', null, () => {
logs.push('did-ping');
});
expect(logs).toEqual([
'ping',
'did-ping',
'pong',
]);
});
});