Skip to content

Commit 2b97b44

Browse files
[FSSDK-11509] Remove optional netinfo require (#1054)
1 parent 4d2a4e1 commit 2b97b44

7 files changed

+37
-98
lines changed

lib/event_processor/batch_event_processor.react_native.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const mockNetInfo = vi.hoisted(() => {
3939
return netInfo;
4040
});
4141

42-
vi.mock('../utils/import.react_native/@react-native-community/netinfo', () => {
42+
vi.mock('@react-native-community/netinfo', () => {
4343
return {
4444
addEventListener: mockNetInfo.addEventListener.bind(mockNetInfo),
4545
};

lib/event_processor/batch_event_processor.react_native.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { NetInfoState, addEventListener } from '../utils/import.react_native/@react-native-community/netinfo';
17+
import { NetInfoState, addEventListener } from '@react-native-community/netinfo';
1818

1919
import { BatchEventProcessor, BatchEventProcessorConfig } from './batch_event_processor';
2020
import { Fn } from '../utils/type';
@@ -41,15 +41,11 @@ export class ReactNativeNetInfoEventProcessor extends BatchEventProcessor {
4141

4242
start(): void {
4343
super.start();
44-
if (addEventListener) {
45-
this.unsubscribeNetInfo = addEventListener(this.connectionListener.bind(this));
46-
}
44+
this.unsubscribeNetInfo = addEventListener(this.connectionListener.bind(this));
4745
}
4846

4947
stop(): void {
50-
if (this.unsubscribeNetInfo) {
51-
this.unsubscribeNetInfo();
52-
}
48+
this.unsubscribeNetInfo?.();
5349
super.stop();
5450
}
5551
}

lib/event_processor/event_processor_factory.react_native.spec.ts

+1-26
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ vi.mock('../utils/cache/store', () => {
4747
vi.mock('@react-native-community/netinfo', () => {
4848
return { NetInfoState: {}, addEventListener: vi.fn() };
4949
});
50-
51-
let isNetInfoAvailable = false;
5250
let isAsyncStorageAvailable = true;
5351

5452
await vi.hoisted(async () => {
@@ -61,36 +59,28 @@ async function mockRequireNetInfo() {
6159

6260
M._load_original = M._load;
6361
M._load = (uri: string, parent: string) => {
64-
if (uri === '@react-native-community/netinfo') {
65-
if (isNetInfoAvailable) return {};
66-
throw new Error("Module not found: @react-native-community/netinfo");
67-
}
6862
if (uri === '@react-native-async-storage/async-storage') {
6963
if (isAsyncStorageAvailable) return {};
7064
throw new Error("Module not found: @react-native-async-storage/async-storage");
7165
}
72-
7366
return M._load_original(uri, parent);
7467
};
7568
}
7669

7770
import { createForwardingEventProcessor, createBatchEventProcessor } from './event_processor_factory.react_native';
7871
import { getForwardingEventProcessor } from './forwarding_event_processor';
7972
import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser';
80-
import { EVENT_STORE_PREFIX, extractEventProcessor, FAILED_EVENT_RETRY_INTERVAL, getPrefixEventStore } from './event_processor_factory';
73+
import { EVENT_STORE_PREFIX, extractEventProcessor, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
8174
import { getOpaqueBatchEventProcessor } from './event_processor_factory';
8275
import { AsyncStore, AsyncPrefixStore, SyncStore, SyncPrefixStore } from '../utils/cache/store';
8376
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
84-
import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native';
85-
import { BatchEventProcessor } from './batch_event_processor';
8677
import { MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE } from '../utils/import.react_native/@react-native-async-storage/async-storage';
8778

8879
describe('createForwardingEventProcessor', () => {
8980
const mockGetForwardingEventProcessor = vi.mocked(getForwardingEventProcessor);
9081

9182
beforeEach(() => {
9283
mockGetForwardingEventProcessor.mockClear();
93-
isNetInfoAvailable = false;
9484
});
9585

9686
it('returns forwarding event processor by calling getForwardingEventProcessor with the provided dispatcher', () => {
@@ -119,27 +109,12 @@ describe('createBatchEventProcessor', () => {
119109
const MockAsyncPrefixStore = vi.mocked(AsyncPrefixStore);
120110

121111
beforeEach(() => {
122-
isNetInfoAvailable = false;
123112
mockGetOpaqueBatchEventProcessor.mockClear();
124113
MockAsyncStorageCache.mockClear();
125114
MockSyncPrefixStore.mockClear();
126115
MockAsyncPrefixStore.mockClear();
127116
});
128117

129-
it('returns an instance of ReacNativeNetInfoEventProcessor if netinfo can be required', async () => {
130-
isNetInfoAvailable = true;
131-
const processor = createBatchEventProcessor({});
132-
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
133-
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][1]).toBe(ReactNativeNetInfoEventProcessor);
134-
});
135-
136-
it('returns an instance of BatchEventProcessor if netinfo cannot be required', async () => {
137-
isNetInfoAvailable = false;
138-
const processor = createBatchEventProcessor({});
139-
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
140-
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][1]).toBe(BatchEventProcessor);
141-
});
142-
143118
it('uses AsyncStorageCache and AsyncPrefixStore to create eventStore if no eventStore is provided', () => {
144119
const processor = createBatchEventProcessor({});
145120

lib/event_processor/event_processor_factory.react_native.ts

+16-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
import { getForwardingEventProcessor } from './forwarding_event_processor';
1717
import { EventDispatcher } from './event_dispatcher/event_dispatcher';
18-
import { EventProcessor } from './event_processor';
1918
import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser';
2019
import {
2120
BatchEventProcessorOptions,
@@ -26,10 +25,9 @@ import {
2625
} from './event_processor_factory';
2726
import { EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
2827
import { AsyncPrefixStore } from '../utils/cache/store';
29-
import { BatchEventProcessor, EventWithId } from './batch_event_processor';
28+
import { EventWithId } from './batch_event_processor';
3029
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
3130
import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native';
32-
import { isAvailable as isNetInfoAvailable } from '../utils/import.react_native/@react-native-community/netinfo';
3331

3432
export const DEFAULT_EVENT_BATCH_SIZE = 10;
3533
export const DEFAULT_EVENT_FLUSH_INTERVAL = 1_000;
@@ -60,17 +58,20 @@ export const createBatchEventProcessor = (
6058
): OpaqueEventProcessor => {
6159
const eventStore = options.eventStore ? getPrefixEventStore(options.eventStore) : getDefaultEventStore();
6260

63-
return getOpaqueBatchEventProcessor({
64-
eventDispatcher: options.eventDispatcher || defaultEventDispatcher,
65-
closingEventDispatcher: options.closingEventDispatcher,
66-
flushInterval: options.flushInterval,
67-
batchSize: options.batchSize,
68-
defaultFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
69-
defaultBatchSize: DEFAULT_EVENT_BATCH_SIZE,
70-
retryOptions: {
71-
maxRetries: 5,
61+
return getOpaqueBatchEventProcessor(
62+
{
63+
eventDispatcher: options.eventDispatcher || defaultEventDispatcher,
64+
closingEventDispatcher: options.closingEventDispatcher,
65+
flushInterval: options.flushInterval,
66+
batchSize: options.batchSize,
67+
defaultFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
68+
defaultBatchSize: DEFAULT_EVENT_BATCH_SIZE,
69+
retryOptions: {
70+
maxRetries: 5,
71+
},
72+
failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL,
73+
eventStore,
7274
},
73-
failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL,
74-
eventStore,
75-
}, isNetInfoAvailable() ? ReactNativeNetInfoEventProcessor : BatchEventProcessor);
75+
ReactNativeNetInfoEventProcessor
76+
);
7677
};

lib/utils/import.react_native/@react-native-community/netinfo.ts

-38
This file was deleted.

package-lock.json

+13-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"uuid": "^9.0.1"
9898
},
9999
"devDependencies": {
100-
"@react-native-async-storage/async-storage": "^1.2.0",
100+
"@react-native-async-storage/async-storage": "^2",
101101
"@react-native-community/netinfo": "^11.3.2",
102102
"@rollup/plugin-commonjs": "^11.0.2",
103103
"@rollup/plugin-node-resolve": "^7.1.1",
@@ -147,8 +147,8 @@
147147
"webpack": "^5.74.0"
148148
},
149149
"peerDependencies": {
150-
"@react-native-async-storage/async-storage": "^1.2.0",
151-
"@react-native-community/netinfo": "^11.3.2",
150+
"@react-native-async-storage/async-storage": ">=1.0.0 <3.0.0",
151+
"@react-native-community/netinfo": ">=5.0.0 <12.0.0",
152152
"fast-text-encoding": "^1.0.6",
153153
"react-native-get-random-values": "^1.11.0",
154154
"ua-parser-js": "^1.0.38"

0 commit comments

Comments
 (0)