Browse Source

expose navigation handleDeepLink as a static method (#727)

Artal Druk 8 years ago
parent
commit
430d5de59a
2 changed files with 27 additions and 13 deletions
  1. 24
    1
      src/Navigation.js
  2. 3
    12
      src/Screen.js

+ 24
- 1
src/Navigation.js View File

7
 import PropRegistry from './PropRegistry';
7
 import PropRegistry from './PropRegistry';
8
 
8
 
9
 const registeredScreens = {};
9
 const registeredScreens = {};
10
+const _allNavigatorEventHandlers = {};
10
 
11
 
11
 function registerScreen(screenID, generator) {
12
 function registerScreen(screenID, generator) {
12
   registeredScreens[screenID] = generator;
13
   registeredScreens[screenID] = generator;
134
   return platformSpecific.startSingleScreenApp(params);
135
   return platformSpecific.startSingleScreenApp(params);
135
 }
136
 }
136
 
137
 
138
+function setEventHandler(navigatorEventID, eventHandler) {
139
+  _allNavigatorEventHandlers[navigatorEventID] = eventHandler;
140
+}
141
+
142
+function clearEventHandler(navigatorEventID) {
143
+  delete _allNavigatorEventHandlers[navigatorEventID];
144
+}
145
+
146
+function handleDeepLink(params = {}) {
147
+  if (!params.link) return;
148
+  const event = {
149
+    type: 'DeepLink',
150
+    link: params.link
151
+  };
152
+  for (let i in _allNavigatorEventHandlers) {
153
+    _allNavigatorEventHandlers[i](event);
154
+  }
155
+}
156
+
137
 export default {
157
 export default {
138
   getRegisteredScreen,
158
   getRegisteredScreen,
139
   registerComponent,
159
   registerComponent,
145
   showInAppNotification: showInAppNotification,
165
   showInAppNotification: showInAppNotification,
146
   dismissInAppNotification: dismissInAppNotification,
166
   dismissInAppNotification: dismissInAppNotification,
147
   startTabBasedApp: startTabBasedApp,
167
   startTabBasedApp: startTabBasedApp,
148
-  startSingleScreenApp: startSingleScreenApp
168
+  startSingleScreenApp: startSingleScreenApp,
169
+  setEventHandler: setEventHandler,
170
+  clearEventHandler: clearEventHandler,
171
+  handleDeepLink: handleDeepLink
149
 };
172
 };

+ 3
- 12
src/Screen.js View File

8
 import platformSpecific from './deprecated/platformSpecificDeprecated';
8
 import platformSpecific from './deprecated/platformSpecificDeprecated';
9
 import Navigation from './Navigation';
9
 import Navigation from './Navigation';
10
 
10
 
11
-const _allNavigatorEventHandlers = {};
12
-
13
 const NavigationSpecific = {
11
 const NavigationSpecific = {
14
   push: platformSpecific.navigatorPush,
12
   push: platformSpecific.navigatorPush,
15
   pop: platformSpecific.navigatorPop,
13
   pop: platformSpecific.navigatorPop,
123
     if (!this.navigatorEventSubscription) {
121
     if (!this.navigatorEventSubscription) {
124
       let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
122
       let Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
125
       this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
123
       this.navigatorEventSubscription = Emitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
126
-      _allNavigatorEventHandlers[this.navigatorEventID] = (event) => this.onNavigatorEvent(event);
124
+      Navigation.setEventHandler(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
127
     }
125
     }
128
   }
126
   }
129
 
127
 
130
   handleDeepLink(params = {}) {
128
   handleDeepLink(params = {}) {
131
-    if (!params.link) return;
132
-    const event = {
133
-      type: 'DeepLink',
134
-      link: params.link
135
-    };
136
-    for (let i in _allNavigatorEventHandlers) {
137
-      _allNavigatorEventHandlers[i](event);
138
-    }
129
+    Navigation.handleDeepLink(params);
139
   }
130
   }
140
 
131
 
141
   onNavigatorEvent(event) {
132
   onNavigatorEvent(event) {
147
   cleanup() {
138
   cleanup() {
148
     if (this.navigatorEventSubscription) {
139
     if (this.navigatorEventSubscription) {
149
       this.navigatorEventSubscription.remove();
140
       this.navigatorEventSubscription.remove();
150
-      delete _allNavigatorEventHandlers[this.navigatorEventID];
141
+      Navigation.clearEventHandler(this.navigatorEventID);
151
     }
142
     }
152
   }
143
   }
153
 }
144
 }