瀏覽代碼

added events().onNavigationEvent(..)

Daniel Zlotin 6 年之前
父節點
當前提交
ebbed69e6e

+ 8
- 0
docs/api/EventsRegistry.md 查看文件

@@ -40,4 +40,12 @@
40 40
 
41 41
 ---
42 42
 
43
+## onNavigationEvent
44
+
45
+`onNavigationEvent(callback: function): EventSubscription`
46
+
47
+[source](https://github.com/wix/react-native-navigation/blob/v2/lib/src/events/EventsRegistry.ts#L28)
48
+
49
+---
50
+
43 51
 

+ 8
- 0
docs/api/NativeEventsReceiver.md 查看文件

@@ -32,4 +32,12 @@
32 32
 
33 33
 ---
34 34
 
35
+## registerOnNavigationEvent
36
+
37
+`registerOnNavigationEvent(callback: function): EventSubscription`
38
+
39
+[source](https://github.com/wix/react-native-navigation/blob/v2/lib/src/adapters/NativeEventsReceiver.ts#L26)
40
+
41
+---
42
+
35 43
 

+ 4
- 0
lib/src/adapters/NativeEventsReceiver.ts 查看文件

@@ -22,4 +22,8 @@ export class NativeEventsReceiver {
22 22
   registerOnNavigationButtonPressed(callback: (params) => void): EventSubscription {
23 23
     return this.emitter.addListener('RNN.onNavigationButtonPressed', callback);
24 24
   }
25
+
26
+  public registerOnNavigationEvent(callback: (params) => void): EventSubscription {
27
+    return this.emitter.addListener('RNN.onNavigationEvent', callback);
28
+  }
25 29
 }

+ 22
- 0
lib/src/events/EventsRegistry.test.ts 查看文件

@@ -82,4 +82,26 @@ describe('EventsRegistry', () => {
82 82
     commandsObserver.notify('theCommandName', { x: 1 });
83 83
     expect(cb).not.toHaveBeenCalled();
84 84
   });
85
+
86
+  it('onNavigationEvent', () => {
87
+    const subscription = {};
88
+    const cb = jest.fn();
89
+    mockNativeEventsReceiver.registerOnNavigationEvent.mockReturnValueOnce(subscription);
90
+
91
+    const result = uut.onNavigationEvent(cb);
92
+
93
+    expect(result).toBe(subscription);
94
+    expect(mockNativeEventsReceiver.registerOnNavigationEvent).toHaveBeenCalledTimes(1);
95
+
96
+    mockNativeEventsReceiver.registerOnNavigationEvent.mock.calls[0][0]({ name: 'the event name', params: { a: 1 } });
97
+    expect(cb).toHaveBeenCalledWith('the event name', { a: 1 });
98
+  });
99
+
100
+  it('onNavigationEvent unregister', () => {
101
+    const subscription = {};
102
+    const cb = jest.fn();
103
+    mockNativeEventsReceiver.registerOnNavigationEvent.mockReturnValueOnce(subscription);
104
+    const result = uut.onNavigationEvent(cb);
105
+    expect(result).toBe(subscription);
106
+  });
85 107
 });

+ 4
- 0
lib/src/events/EventsRegistry.ts 查看文件

@@ -24,4 +24,8 @@ export class EventsRegistry {
24 24
   public onNavigationCommand(callback: (name: string, params: any) => void): EventSubscription {
25 25
     return this.commandsObserver.register(callback);
26 26
   }
27
+
28
+  public onNavigationEvent(callback: (name: string, params: any) => void): EventSubscription {
29
+    return this.nativeEventsReceiver.registerOnNavigationEvent(({ name, params }) => callback(name, params));
30
+  }
27 31
 }