Browse Source

addOnNavigatorEvent improvements (#2175)

* throwing an error instead of a string

* return a callback to remove the register handler
dror biran 7 years ago
parent
commit
02aee567fa
2 changed files with 14 additions and 3 deletions
  1. 8
    0
      docs/screen-api.md
  2. 6
    3
      src/Screen.js

+ 8
- 0
docs/screen-api.md View File

259
 ## setOnNavigatorEvent(callback)
259
 ## setOnNavigatorEvent(callback)
260
 
260
 
261
 Set a handler for navigator events (like nav button press). This would normally go in your component constructor.
261
 Set a handler for navigator events (like nav button press). This would normally go in your component constructor.
262
+Can not be used in conjuction with `addOnNavigatorEvent`.
262
 
263
 
263
 ```js
264
 ```js
264
 // this.onNavigatorEvent will be our handler
265
 // this.onNavigatorEvent will be our handler
265
 this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
266
 this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
266
 ```
267
 ```
267
 
268
 
269
+## addOnNavigatorEvent(callback)
270
+
271
+Add a handler for navigator events (like nav button press). This would normally go in your component constructor.
272
+If you choose to use `addOnNavigatorEvent` instead of `setOnNavigatorEvent` you will be able to add multiple handlers.
273
+Bear in mind that you can't use both `addOnNavigatorEvent` and `setOnNavigatorEvent`.
274
+`addOnNavigatorEvent` returns a function, that once called will remove the registered handler.
275
+
268
 # Screen Visibility
276
 # Screen Visibility
269
 
277
 
270
 `const isVisible = await this.props.navigator.screenIsCurrentlyVisible()`
278
 `const isVisible = await this.props.navigator.screenIsCurrentlyVisible()`

+ 6
- 3
src/Screen.js View File

143
 
143
 
144
   setOnNavigatorEvent(callback) {
144
   setOnNavigatorEvent(callback) {
145
     if (this.navigatorEventHandlers.length > 0) {
145
     if (this.navigatorEventHandlers.length > 0) {
146
-      throw 'setOnNavigatorEvent can not be used after addOnNavigatorEvent has been called';
146
+      throw new Error('setOnNavigatorEvent can not be used after addOnNavigatorEvent has been called');
147
     }
147
     }
148
     this.navigatorEventHandler = callback;
148
     this.navigatorEventHandler = callback;
149
     this._registerNavigatorEvent();
149
     this._registerNavigatorEvent();
151
 
151
 
152
   addOnNavigatorEvent(callback) {
152
   addOnNavigatorEvent(callback) {
153
     if (this.navigatorEventHandler) {
153
     if (this.navigatorEventHandler) {
154
-      throw 'addOnNavigatorEvent can not be used after setOnNavigatorEvent has been called';
154
+      throw new Error('addOnNavigatorEvent can not be used after setOnNavigatorEvent has been called');
155
     }
155
     }
156
     if (this.navigatorEventHandlers.indexOf(callback) === -1) {
156
     if (this.navigatorEventHandlers.indexOf(callback) === -1) {
157
       this.navigatorEventHandlers.push(callback);
157
       this.navigatorEventHandlers.push(callback);
158
     }
158
     }
159
     this._registerNavigatorEvent();
159
     this._registerNavigatorEvent();
160
+
161
+    return () => this._removeOnNavigatorEvent(callback)
162
+    
160
   }
163
   }
161
 
164
 
162
   _registerNavigatorEvent() {
165
   _registerNavigatorEvent() {
167
     }
170
     }
168
   }
171
   }
169
 
172
 
170
-  removeOnNavigatorEvent(callback) {
173
+  _removeOnNavigatorEvent(callback) {
171
     const index = this.navigatorEventHandlers.indexOf(callback);
174
     const index = this.navigatorEventHandlers.indexOf(callback);
172
     if (index !== -1) {
175
     if (index !== -1) {
173
       this.navigatorEventHandlers.splice(index, 1);
176
       this.navigatorEventHandlers.splice(index, 1);