update NavigationContext API.

Summary: 1. Add a new api `top` which returns the root navigator of a nested navigator.
2. Remove the param `context` from the method `addListener` because it's not used and not necessary.

public

Reviewed By: fkgozali

Differential Revision: D2613852

fb-gh-sync-id: 0d5544422ff0be7875824989a4fbefbef9aac986
This commit is contained in:
Hedger Wang 2015-11-03 19:05:28 -08:00 committed by facebook-github-bot-6
parent eed38e9163
commit 4763f89efa
2 changed files with 32 additions and 13 deletions

View File

@ -72,8 +72,8 @@ class NavigationContext {
this._emitCounter = 0;
this._emitQueue = [];
this.addListener('willfocus', this._onFocus, this);
this.addListener('didfocus', this._onFocus, this);
this.addListener('willfocus', this._onFocus);
this.addListener('didfocus', this._onFocus);
}
/* $FlowFixMe - get/set properties not yet supported */
@ -82,6 +82,17 @@ class NavigationContext {
return parent ? parent.getValue() : null;
}
/* $FlowFixMe - get/set properties not yet supported */
get top(): ?NavigationContext {
var result = null;
var parentNode = this.__node.getParent();
while (parentNode) {
result = parentNode.getValue();
parentNode = parentNode.getParent();
}
return result;
}
/* $FlowFixMe - get/set properties not yet supported */
get currentRoute(): any {
return this._currentRoute;
@ -94,7 +105,6 @@ class NavigationContext {
addListener(
eventType: string,
listener: Function,
context: ?Object,
useCapture: ?boolean
): EventSubscription {
if (LegacyEventTypes.has(eventType)) {
@ -106,7 +116,7 @@ class NavigationContext {
this._bubbleEventEmitter;
if (emitter) {
return emitter.addListener(eventType, listener, context);
return emitter.addListener(eventType, listener, this);
} else {
return {remove: emptyFunction};
}

View File

@ -50,6 +50,15 @@ describe('NavigationContext', () => {
expect(child.parent).toBe(parent);
});
it('has `top`', () => {
var top = new NavigationContext();
var parent = new NavigationContext();
var child = new NavigationContext();
top.appendChild(parent);
parent.appendChild(child);
expect(child.top).toBe(top);
});
it('captures event', () => {
var parent = new NavigationContext();
var child = new NavigationContext();
@ -67,8 +76,8 @@ describe('NavigationContext', () => {
});
};
parent.addListener('yo', listener, null, true);
child.addListener('yo', listener, null, true);
parent.addListener('yo', listener, true);
child.addListener('yo', listener, true);
child.emit('yo');
@ -133,8 +142,8 @@ describe('NavigationContext', () => {
var counter = 0;
parent.addListener('yo', event => event.stopPropagation(), null, true);
child.addListener('yo', event => counter++, null, true);
parent.addListener('yo', event => event.stopPropagation(), true);
child.addListener('yo', event => counter++, true);
child.emit('yo');
@ -162,8 +171,8 @@ describe('NavigationContext', () => {
parent.appendChild(child);
var val;
parent.addListener('yo', event => event.preventDefault(), null, true);
child.addListener('yo', event => val = event.defaultPrevented, null, true);
parent.addListener('yo', event => event.preventDefault(), true);
child.addListener('yo', event => val = event.defaultPrevented, true);
child.emit('yo');
@ -205,9 +214,9 @@ describe('NavigationContext', () => {
child.emit('didyo');
});
parent.addListener('yo', listener, null, true);
parent.addListener('didyo', listener, null, true);
child.addListener('yo', listener, null, true);
parent.addListener('yo', listener, true);
parent.addListener('didyo', listener, true);
child.addListener('yo', listener, true);
child.emit('yo');