Przeglądaj źródła

Register multiple navigatorEventListeners (#2173)

This commit adds ability to register multiple navigator event listeners:
navigator.addOnNavigatorEvent(func)
navigator.removeOnNavigatorEvent(func)

Event listeners are removed when the screen the navigator is bound to
is unmounted, calling `removeOnNavigatorEvent` isn't mandatory.

As this api conflicts with the current api, addOnNavigatorEvent can't be used
in a screen where setOnNavigatorEvent was used before.
Guy Carmeli 6 lat temu
rodzic
commit
d71a42f15a
No account linked to committer's email address
1 zmienionych plików z 29 dodań i 2 usunięć
  1. 29
    2
      src/Screen.js

+ 29
- 2
src/Screen.js Wyświetl plik

@@ -21,6 +21,7 @@ class Navigator {
21 21
     this.screenInstanceID = screenInstanceID;
22 22
     this.navigatorEventID = navigatorEventID;
23 23
     this.navigatorEventHandler = null;
24
+    this.navigatorEventHandlers = [];
24 25
     this.navigatorEventSubscription = null;
25 26
   }
26 27
 
@@ -141,7 +142,24 @@ class Navigator {
141 142
   }
142 143
 
143 144
   setOnNavigatorEvent(callback) {
145
+    if (this.navigatorEventHandlers.length > 0) {
146
+      throw 'setOnNavigatorEvent can not be used after addOnNavigatorEvent has been called';
147
+    }
144 148
     this.navigatorEventHandler = callback;
149
+    this._registerNavigatorEvent();
150
+  }
151
+
152
+  addOnNavigatorEvent(callback) {
153
+    if (this.navigatorEventHandler) {
154
+      throw 'addOnNavigatorEvent can not be used after setOnNavigatorEvent has been called';
155
+    }
156
+    if (this.navigatorEventHandlers.indexOf(callback) === -1) {
157
+      this.navigatorEventHandlers.push(callback);
158
+    }
159
+    this._registerNavigatorEvent();
160
+  }
161
+
162
+  _registerNavigatorEvent() {
145 163
     if (!this.navigatorEventSubscription) {
146 164
       let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
147 165
       this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
@@ -149,19 +167,28 @@ class Navigator {
149 167
     }
150 168
   }
151 169
 
152
-  handleDeepLink(params = {}) {
153
-    Navigation.handleDeepLink(params);
170
+  removeOnNavigatorEvent(callback) {
171
+    const index = this.navigatorEventHandlers.indexOf(callback);
172
+    if (index !== -1) {
173
+      this.navigatorEventHandlers.splice(index, 1);
174
+    }
154 175
   }
155 176
 
156 177
   onNavigatorEvent(event) {
157 178
     if (this.navigatorEventHandler) {
158 179
       this.navigatorEventHandler(event);
159 180
     }
181
+    this.navigatorEventHandlers.forEach(handler => handler(event));
182
+  }
183
+
184
+  handleDeepLink(params = {}) {
185
+    Navigation.handleDeepLink(params);
160 186
   }
161 187
 
162 188
   cleanup() {
163 189
     if (this.navigatorEventSubscription) {
164 190
       this.navigatorEventSubscription.remove();
191
+      this.navigatorEventHandlers = [];
165 192
       Navigation.clearEventHandler(this.navigatorEventID);
166 193
     }
167 194
   }