Browse Source

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 years ago
parent
commit
d71a42f15a
No account linked to committer's email address
1 changed files with 29 additions and 2 deletions
  1. 29
    2
      src/Screen.js

+ 29
- 2
src/Screen.js View File

21
     this.screenInstanceID = screenInstanceID;
21
     this.screenInstanceID = screenInstanceID;
22
     this.navigatorEventID = navigatorEventID;
22
     this.navigatorEventID = navigatorEventID;
23
     this.navigatorEventHandler = null;
23
     this.navigatorEventHandler = null;
24
+    this.navigatorEventHandlers = [];
24
     this.navigatorEventSubscription = null;
25
     this.navigatorEventSubscription = null;
25
   }
26
   }
26
 
27
 
141
   }
142
   }
142
 
143
 
143
   setOnNavigatorEvent(callback) {
144
   setOnNavigatorEvent(callback) {
145
+    if (this.navigatorEventHandlers.length > 0) {
146
+      throw 'setOnNavigatorEvent can not be used after addOnNavigatorEvent has been called';
147
+    }
144
     this.navigatorEventHandler = callback;
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
     if (!this.navigatorEventSubscription) {
163
     if (!this.navigatorEventSubscription) {
146
       let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
164
       let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
147
       this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
165
       this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
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
   onNavigatorEvent(event) {
177
   onNavigatorEvent(event) {
157
     if (this.navigatorEventHandler) {
178
     if (this.navigatorEventHandler) {
158
       this.navigatorEventHandler(event);
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
   cleanup() {
188
   cleanup() {
163
     if (this.navigatorEventSubscription) {
189
     if (this.navigatorEventSubscription) {
164
       this.navigatorEventSubscription.remove();
190
       this.navigatorEventSubscription.remove();
191
+      this.navigatorEventHandlers = [];
165
       Navigation.clearEventHandler(this.navigatorEventID);
192
       Navigation.clearEventHandler(this.navigatorEventID);
166
     }
193
     }
167
   }
194
   }