Skip to content

Commit 5daa4e0

Browse files
committed
up
1 parent d40119a commit 5daa4e0

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

lib/event_processor/batch_event_processor.react_native.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {
18-
NetInfoState,
19-
addEventListener as addConnectionListener,
20-
} from '@react-native-community/netinfo';
17+
import { NetInfoState, addEventListener } from '../utils/import.react_native/@react-native-community/netinfo';
2118

2219
import { BatchEventProcessor, BatchEventProcessorConfig } from './batch_event_processor';
2320
import { Fn } from '../utils/type';
@@ -44,7 +41,9 @@ export class ReactNativeNetInfoEventProcessor extends BatchEventProcessor {
4441

4542
start(): void {
4643
super.start();
47-
this.unsubscribeNetInfo = addConnectionListener(this.connectionListener.bind(this));
44+
if (addEventListener) {
45+
this.unsubscribeNetInfo = addEventListener(this.connectionListener.bind(this));
46+
}
4847
}
4948

5049
stop(): void {

lib/event_processor/event_processor_factory.react_native.spec.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,48 @@ vi.mock('../utils/cache/cache', () => {
4141
return { SyncPrefixCache: vi.fn(), AsyncPrefixCache: vi.fn() };
4242
});
4343

44+
vi.mock('@react-native-community/netinfo', () => {
45+
return { NetInfoState: {}, addEventListener: vi.fn() };
46+
});
47+
48+
let isNetInfoAvailable = false;
49+
50+
await vi.hoisted(async () => {
51+
await mockRequireNetInfo();
52+
});
53+
54+
async function mockRequireNetInfo() {
55+
const {Module} = await import('module');
56+
const M: any = Module;
57+
58+
M._load_original = M._load;
59+
M._load = (uri: string, parent: string) => {
60+
if (uri === '@react-native-community/netinfo') {
61+
if (isNetInfoAvailable) return {};
62+
throw new Error('Module not found: @react-native-community/netinfo');
63+
}
64+
return M._load_original(uri, parent);
65+
};
66+
}
67+
4468
import { createForwardingEventProcessor, createBatchEventProcessor } from './event_processor_factory.react_native';
4569
import { getForwardingEventProcessor } from './forwarding_event_processor';
4670
import defaultEventDispatcher from './default_dispatcher.browser';
4771
import { EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
4872
import { getBatchEventProcessor } from './event_processor_factory';
4973
import { AsyncCache, AsyncPrefixCache, SyncCache, SyncPrefixCache } from '../utils/cache/cache';
5074
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
75+
import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native';
76+
import { BatchEventProcessor } from './batch_event_processor';
5177

5278
describe('createForwardingEventProcessor', () => {
5379
const mockGetForwardingEventProcessor = vi.mocked(getForwardingEventProcessor);
5480

5581
beforeEach(() => {
5682
mockGetForwardingEventProcessor.mockClear();
83+
isNetInfoAvailable = false;
5784
});
58-
85+
5986
it('returns forwarding event processor by calling getForwardingEventProcessor with the provided dispatcher', () => {
6087
const eventDispatcher = {
6188
dispatchEvent: vi.fn(),
@@ -82,12 +109,26 @@ describe('createBatchEventProcessor', () => {
82109
const MockAsyncPrefixCache = vi.mocked(AsyncPrefixCache);
83110

84111
beforeEach(() => {
112+
isNetInfoAvailable = false;
85113
mockGetBatchEventProcessor.mockClear();
86114
MockAsyncStorageCache.mockClear();
87115
MockSyncPrefixCache.mockClear();
88116
MockAsyncPrefixCache.mockClear();
89117
});
90118

119+
it('returns an instance of ReacNativeNetInfoEventProcessor if netinfo can be required', async () => {
120+
isNetInfoAvailable = true;
121+
const processor = createBatchEventProcessor({});
122+
expect(Object.is(processor, mockGetBatchEventProcessor.mock.results[0].value)).toBe(true);
123+
expect(mockGetBatchEventProcessor.mock.calls[0][1]).toBe(ReactNativeNetInfoEventProcessor);
124+
});
125+
126+
it('returns an instance of BatchEventProcessor if netinfo cannot be required', async () => {
127+
isNetInfoAvailable = false;
128+
const processor = createBatchEventProcessor({});;
129+
expect(mockGetBatchEventProcessor.mock.calls[0][1]).toBe(BatchEventProcessor);
130+
});
131+
91132
it('uses AsyncStorageCache and AsyncPrefixCache to create eventStore if no eventStore is provided', () => {
92133
const processor = createBatchEventProcessor({});
93134

lib/event_processor/event_processor_factory.react_native.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import defaultEventDispatcher from './default_dispatcher.browser';
2020
import { BatchEventProcessorOptions, getBatchEventProcessor, getPrefixEventStore } from './event_processor_factory';
2121
import { EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
2222
import { AsyncPrefixCache } from '../utils/cache/cache';
23-
import { EventWithId } from './batch_event_processor';
23+
import { BatchEventProcessor, EventWithId } from './batch_event_processor';
2424
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
25+
import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native';
26+
import { isAvailable as isNetInfoAvailable } from '../utils/import.react_native/@react-native-community/netinfo';
2527

2628
export const createForwardingEventProcessor = (
2729
eventDispatcher: EventDispatcher = defaultEventDispatcher,
@@ -59,5 +61,5 @@ export const createBatchEventProcessor = (
5961
},
6062
failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL,
6163
eventStore,
62-
});
64+
}, isNetInfoAvailable() ? ReactNativeNetInfoEventProcessor : BatchEventProcessor);
6365
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2024, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import type { NetInfoSubscription, NetInfoChangeHandler } from '@react-native-community/netinfo';
18+
import { Maybe } from '../../type';
19+
20+
export { NetInfoState } from '@react-native-community/netinfo';
21+
export type NetInfoAddEventListerType = (listener: NetInfoChangeHandler) => NetInfoSubscription;
22+
23+
let addEventListener: Maybe<NetInfoAddEventListerType> = undefined;
24+
25+
const requireNetInfo = () => {
26+
try {
27+
return require('@react-native-community/netinfo');
28+
} catch (e) {
29+
return undefined;
30+
}
31+
}
32+
33+
export const isAvailable = () => requireNetInfo() !== undefined;
34+
35+
const netinfo = requireNetInfo();
36+
addEventListener = netinfo?.addEventListener;
37+
38+
export { addEventListener };

0 commit comments

Comments
 (0)