mirror of
https://github.com/status-im/react-native.git
synced 2025-01-14 19:44:13 +00:00
[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:
parent
9105b22925
commit
6e2f07fb81
@ -36,7 +36,7 @@ class NavigationEventPool {
|
|||||||
this._list = [];
|
this._list = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
get(type: String, target: Object, data: any): NavigationEvent {
|
get(type: string, target: Object, data: any): NavigationEvent {
|
||||||
var event;
|
var event;
|
||||||
if (this._list.length > 0) {
|
if (this._list.length > 0) {
|
||||||
event = this._list.pop();
|
event = this._list.pop();
|
||||||
@ -59,13 +59,13 @@ class NavigationEvent {
|
|||||||
_defaultPrevented: boolean;
|
_defaultPrevented: boolean;
|
||||||
_disposed: boolean;
|
_disposed: boolean;
|
||||||
_target: ?Object;
|
_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);
|
return _navigationEventPool.get(type, target, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(type: String, target: Object, data: any) {
|
constructor(type: string, target: Object, data: any) {
|
||||||
this._type = type;
|
this._type = type;
|
||||||
this._target = target;
|
this._target = target;
|
||||||
this._data = data;
|
this._data = data;
|
||||||
|
@ -31,8 +31,9 @@ var EventEmitter = require('EventEmitter');
|
|||||||
var NavigationEvent = require('NavigationEvent');
|
var NavigationEvent = require('NavigationEvent');
|
||||||
|
|
||||||
type EventParams = {
|
type EventParams = {
|
||||||
eventType: String;
|
|
||||||
data: any;
|
data: any;
|
||||||
|
didEmitCallback: ?Function;
|
||||||
|
eventType: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
class NavigationEventEmitter extends EventEmitter {
|
class NavigationEventEmitter extends EventEmitter {
|
||||||
@ -47,22 +48,36 @@ class NavigationEventEmitter extends EventEmitter {
|
|||||||
this._target = target;
|
this._target = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(eventType: String, data: any): void {
|
emit(
|
||||||
|
eventType: string,
|
||||||
|
data: any,
|
||||||
|
didEmitCallback: ?Function
|
||||||
|
): void {
|
||||||
if (this._emitting) {
|
if (this._emitting) {
|
||||||
// An event cycle that was previously created hasn't finished yet.
|
// An event cycle that was previously created hasn't finished yet.
|
||||||
// Put this event cycle into the queue and will finish them later.
|
// Put this event cycle into the queue and will finish them later.
|
||||||
this._emitQueue.push({eventType, data});
|
this._emitQueue.push({eventType, data, didEmitCallback});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._emitting = true;
|
this._emitting = true;
|
||||||
|
|
||||||
var event = new NavigationEvent(eventType, this._target, data);
|
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;
|
this._emitting = false;
|
||||||
|
|
||||||
while (this._emitQueue.length) {
|
while (this._emitQueue.length) {
|
||||||
var arg = this._emitQueue.shift();
|
var arg = this._emitQueue.shift();
|
||||||
this.emit(arg.eventType, arg.data);
|
this.emit(arg.eventType, arg.data, arg.didEmitCallback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,27 +34,48 @@ jest
|
|||||||
var NavigationEventEmitter = require('NavigationEventEmitter');
|
var NavigationEventEmitter = require('NavigationEventEmitter');
|
||||||
|
|
||||||
describe('NavigationEventEmitter', () => {
|
describe('NavigationEventEmitter', () => {
|
||||||
it('emit event', () => {
|
it('emits event', () => {
|
||||||
var target = {};
|
var context = {};
|
||||||
var emitter = new NavigationEventEmitter(target);
|
var emitter = new NavigationEventEmitter(context);
|
||||||
var focusCounter = 0;
|
var logs = [];
|
||||||
var focusTarget;
|
|
||||||
|
emitter.addListener('ping', (event) => {
|
||||||
|
var {type, data, target, defaultPrevented} = event;
|
||||||
|
|
||||||
|
logs.push({
|
||||||
|
data,
|
||||||
|
defaultPrevented,
|
||||||
|
target,
|
||||||
|
type,
|
||||||
|
});
|
||||||
|
|
||||||
emitter.addListener('focus', (event) => {
|
|
||||||
focusCounter++;
|
|
||||||
focusTarget = event.target;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
emitter.emit('focus');
|
emitter.emit('ping', 'hello');
|
||||||
emitter.emit('blur');
|
|
||||||
|
|
||||||
expect(focusCounter).toBe(1);
|
expect(logs.length).toBe(1);
|
||||||
expect(focusTarget).toBe(target);
|
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('put nested emit call in queue', () => {
|
it('does not emit event that has no listeners', () => {
|
||||||
var target = {};
|
var context = {};
|
||||||
var emitter = new NavigationEventEmitter(target);
|
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 = [];
|
var logs = [];
|
||||||
|
|
||||||
emitter.addListener('one', () => {
|
emitter.addListener('one', () => {
|
||||||
@ -77,4 +98,63 @@ describe('NavigationEventEmitter', () => {
|
|||||||
|
|
||||||
expect(logs).toEqual([1, 2, 3, 4, 5]);
|
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',
|
||||||
|
]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user