mirror of
https://github.com/zhigang1992/react-native-web.git
synced 2026-04-24 04:25:27 +08:00
[fix] NetInfo event handler registration
This commit is contained in:
committed by
Nicolas Gallagher
parent
5598961d2c
commit
4beae0dd78
@@ -1,5 +1,33 @@
|
||||
/* eslint-env mocha */
|
||||
|
||||
import assert from 'assert';
|
||||
import NetInfo from '..';
|
||||
|
||||
suite('apis/NetInfo', () => {
|
||||
test.skip('NO TEST COVERAGE', () => {});
|
||||
suite('isConnected', () => {
|
||||
const handler = () => {};
|
||||
|
||||
teardown(() => {
|
||||
try { NetInfo.isConnected.removeEventListener('change', handler); } catch (e) {}
|
||||
});
|
||||
|
||||
suite('addEventListener', () => {
|
||||
test('throws if the provided "eventType" is not supported', () => {
|
||||
assert.throws(() => NetInfo.isConnected.addEventListener('foo', handler));
|
||||
assert.doesNotThrow(() => NetInfo.isConnected.addEventListener('change', handler));
|
||||
});
|
||||
});
|
||||
|
||||
suite('removeEventListener', () => {
|
||||
test('throws if the handler is not registered', () => {
|
||||
assert.throws(() => NetInfo.isConnected.removeEventListener('change', handler));
|
||||
});
|
||||
|
||||
test('throws if the provided "eventType" is not supported', () => {
|
||||
NetInfo.isConnected.addEventListener('change', handler);
|
||||
assert.throws(() => NetInfo.isConnected.removeEventListener('foo', handler));
|
||||
assert.doesNotThrow(() => NetInfo.isConnected.removeEventListener('change', handler));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import ExecutionEnvironment from 'fbjs/lib/ExecutionEnvironment';
|
||||
import findIndex from 'lodash/findIndex';
|
||||
import invariant from 'fbjs/lib/invariant';
|
||||
|
||||
const connection = ExecutionEnvironment.canUseDOM && (
|
||||
@@ -17,6 +18,8 @@ const connection = ExecutionEnvironment.canUseDOM && (
|
||||
|
||||
const eventTypes = [ 'change' ];
|
||||
|
||||
const connectionListeners = [];
|
||||
|
||||
/**
|
||||
* Navigator online: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine
|
||||
* Network Connection API: https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation
|
||||
@@ -56,8 +59,12 @@ const NetInfo = {
|
||||
isConnected: {
|
||||
addEventListener(type: string, handler: Function): { remove: () => void } {
|
||||
invariant(eventTypes.indexOf(type) !== -1, 'Trying to subscribe to unknown event: "%s"', type);
|
||||
window.addEventListener('online', handler.bind(null, true), false);
|
||||
window.addEventListener('offline', handler.bind(null, false), false);
|
||||
const onlineCallback = () => handler(true);
|
||||
const offlineCallback = () => handler(false);
|
||||
connectionListeners.push([ handler, onlineCallback, offlineCallback ]);
|
||||
|
||||
window.addEventListener('online', onlineCallback, false);
|
||||
window.addEventListener('offline', offlineCallback, false);
|
||||
|
||||
return {
|
||||
remove: () => NetInfo.isConnected.removeEventListener(type, handler)
|
||||
@@ -66,8 +73,15 @@ const NetInfo = {
|
||||
|
||||
removeEventListener(type: string, handler: Function): void {
|
||||
invariant(eventTypes.indexOf(type) !== -1, 'Trying to subscribe to unknown event: "%s"', type);
|
||||
window.removeEventListener('online', handler.bind(null, true), false);
|
||||
window.removeEventListener('offline', handler.bind(null, false), false);
|
||||
|
||||
const listenerIndex = findIndex(connectionListeners, (pair) => pair[0] === handler);
|
||||
invariant(listenerIndex !== -1, 'Trying to remove NetInfo connection listener for unregistered handler');
|
||||
const [ , onlineCallback, offlineCallback ] = connectionListeners[listenerIndex];
|
||||
|
||||
window.removeEventListener('online', onlineCallback, false);
|
||||
window.removeEventListener('offline', offlineCallback, false);
|
||||
|
||||
connectionListeners.splice(listenerIndex, 1);
|
||||
},
|
||||
|
||||
fetch(): Promise {
|
||||
|
||||
Reference in New Issue
Block a user