Skip to content

[FSSDK-11509] Remove optional netinfo require #1054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const mockNetInfo = vi.hoisted(() => {
return netInfo;
});

vi.mock('../utils/import.react_native/@react-native-community/netinfo', () => {
vi.mock('@react-native-community/netinfo', () => {
return {
addEventListener: mockNetInfo.addEventListener.bind(mockNetInfo),
};
Expand Down
10 changes: 3 additions & 7 deletions lib/event_processor/batch_event_processor.react_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

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

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

start(): void {
super.start();
if (addEventListener) {
this.unsubscribeNetInfo = addEventListener(this.connectionListener.bind(this));
}
this.unsubscribeNetInfo = addEventListener(this.connectionListener.bind(this));
}

stop(): void {
if (this.unsubscribeNetInfo) {
this.unsubscribeNetInfo();
}
this.unsubscribeNetInfo?.();
super.stop();
}
}
22 changes: 2 additions & 20 deletions lib/event_processor/event_processor_factory.react_native.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ vi.mock('../utils/cache/store', () => {
vi.mock('@react-native-community/netinfo', () => {
return { NetInfoState: {}, addEventListener: vi.fn() };
});

let isNetInfoAvailable = false;
let isNetInfoAvailable = true;
let isAsyncStorageAvailable = true;

await vi.hoisted(async () => {
Expand Down Expand Up @@ -77,12 +76,10 @@ async function mockRequireNetInfo() {
import { createForwardingEventProcessor, createBatchEventProcessor } from './event_processor_factory.react_native';
import { getForwardingEventProcessor } from './forwarding_event_processor';
import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser';
import { EVENT_STORE_PREFIX, extractEventProcessor, FAILED_EVENT_RETRY_INTERVAL, getPrefixEventStore } from './event_processor_factory';
import { EVENT_STORE_PREFIX, extractEventProcessor, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
import { getOpaqueBatchEventProcessor } from './event_processor_factory';
import { AsyncStore, AsyncPrefixStore, SyncStore, SyncPrefixStore } from '../utils/cache/store';
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native';
import { BatchEventProcessor } from './batch_event_processor';
import { MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE } from '../utils/import.react_native/@react-native-async-storage/async-storage';

describe('createForwardingEventProcessor', () => {
Expand Down Expand Up @@ -119,27 +116,12 @@ describe('createBatchEventProcessor', () => {
const MockAsyncPrefixStore = vi.mocked(AsyncPrefixStore);

beforeEach(() => {
isNetInfoAvailable = false;
mockGetOpaqueBatchEventProcessor.mockClear();
MockAsyncStorageCache.mockClear();
MockSyncPrefixStore.mockClear();
MockAsyncPrefixStore.mockClear();
});

it('returns an instance of ReacNativeNetInfoEventProcessor if netinfo can be required', async () => {
isNetInfoAvailable = true;
const processor = createBatchEventProcessor({});
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][1]).toBe(ReactNativeNetInfoEventProcessor);
});

it('returns an instance of BatchEventProcessor if netinfo cannot be required', async () => {
isNetInfoAvailable = false;
const processor = createBatchEventProcessor({});
expect(Object.is(processor, mockGetOpaqueBatchEventProcessor.mock.results[0].value)).toBe(true);
expect(mockGetOpaqueBatchEventProcessor.mock.calls[0][1]).toBe(BatchEventProcessor);
});

it('uses AsyncStorageCache and AsyncPrefixStore to create eventStore if no eventStore is provided', () => {
const processor = createBatchEventProcessor({});

Expand Down
31 changes: 16 additions & 15 deletions lib/event_processor/event_processor_factory.react_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
import { getForwardingEventProcessor } from './forwarding_event_processor';
import { EventDispatcher } from './event_dispatcher/event_dispatcher';
import { EventProcessor } from './event_processor';
import defaultEventDispatcher from './event_dispatcher/default_dispatcher.browser';
import {
BatchEventProcessorOptions,
Expand All @@ -26,10 +25,9 @@ import {
} from './event_processor_factory';
import { EVENT_STORE_PREFIX, FAILED_EVENT_RETRY_INTERVAL } from './event_processor_factory';
import { AsyncPrefixStore } from '../utils/cache/store';
import { BatchEventProcessor, EventWithId } from './batch_event_processor';
import { EventWithId } from './batch_event_processor';
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native';
import { isAvailable as isNetInfoAvailable } from '../utils/import.react_native/@react-native-community/netinfo';

export const DEFAULT_EVENT_BATCH_SIZE = 10;
export const DEFAULT_EVENT_FLUSH_INTERVAL = 1_000;
Expand Down Expand Up @@ -60,17 +58,20 @@ export const createBatchEventProcessor = (
): OpaqueEventProcessor => {
const eventStore = options.eventStore ? getPrefixEventStore(options.eventStore) : getDefaultEventStore();

return getOpaqueBatchEventProcessor({
eventDispatcher: options.eventDispatcher || defaultEventDispatcher,
closingEventDispatcher: options.closingEventDispatcher,
flushInterval: options.flushInterval,
batchSize: options.batchSize,
defaultFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
defaultBatchSize: DEFAULT_EVENT_BATCH_SIZE,
retryOptions: {
maxRetries: 5,
return getOpaqueBatchEventProcessor(
{
eventDispatcher: options.eventDispatcher || defaultEventDispatcher,
closingEventDispatcher: options.closingEventDispatcher,
flushInterval: options.flushInterval,
batchSize: options.batchSize,
defaultFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
defaultBatchSize: DEFAULT_EVENT_BATCH_SIZE,
retryOptions: {
maxRetries: 5,
},
failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL,
eventStore,
},
failedEventRetryInterval: FAILED_EVENT_RETRY_INTERVAL,
eventStore,
}, isNetInfoAvailable() ? ReactNativeNetInfoEventProcessor : BatchEventProcessor);
ReactNativeNetInfoEventProcessor
);
};
38 changes: 0 additions & 38 deletions lib/utils/import.react_native/@react-native-community/netinfo.ts

This file was deleted.

21 changes: 13 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"uuid": "^9.0.1"
},
"devDependencies": {
"@react-native-async-storage/async-storage": "^1.2.0",
"@react-native-async-storage/async-storage": "^2",
"@react-native-community/netinfo": "^11.3.2",
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-node-resolve": "^7.1.1",
Expand Down Expand Up @@ -147,8 +147,8 @@
"webpack": "^5.74.0"
},
"peerDependencies": {
"@react-native-async-storage/async-storage": "^1.2.0",
"@react-native-community/netinfo": "^11.3.2",
"@react-native-async-storage/async-storage": ">=1.2.0 <3.0.0",
"@react-native-community/netinfo": ">=10.0.0 <12.0.0",
"fast-text-encoding": "^1.0.6",
"react-native-get-random-values": "^1.11.0",
"ua-parser-js": "^1.0.38"
Expand Down
Loading