diff --git a/lib/event_processor/batch_event_processor.react_native.spec.ts b/lib/event_processor/batch_event_processor.react_native.spec.ts index a30717d12..5e17ca966 100644 --- a/lib/event_processor/batch_event_processor.react_native.spec.ts +++ b/lib/event_processor/batch_event_processor.react_native.spec.ts @@ -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), }; diff --git a/lib/event_processor/batch_event_processor.react_native.ts b/lib/event_processor/batch_event_processor.react_native.ts index ac5110de4..28741380a 100644 --- a/lib/event_processor/batch_event_processor.react_native.ts +++ b/lib/event_processor/batch_event_processor.react_native.ts @@ -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'; @@ -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(); } } diff --git a/lib/event_processor/event_processor_factory.react_native.spec.ts b/lib/event_processor/event_processor_factory.react_native.spec.ts index 733b494d2..6065e16de 100644 --- a/lib/event_processor/event_processor_factory.react_native.spec.ts +++ b/lib/event_processor/event_processor_factory.react_native.spec.ts @@ -47,8 +47,6 @@ vi.mock('../utils/cache/store', () => { vi.mock('@react-native-community/netinfo', () => { return { NetInfoState: {}, addEventListener: vi.fn() }; }); - -let isNetInfoAvailable = false; let isAsyncStorageAvailable = true; await vi.hoisted(async () => { @@ -61,15 +59,10 @@ async function mockRequireNetInfo() { M._load_original = M._load; M._load = (uri: string, parent: string) => { - if (uri === '@react-native-community/netinfo') { - if (isNetInfoAvailable) return {}; - throw new Error("Module not found: @react-native-community/netinfo"); - } if (uri === '@react-native-async-storage/async-storage') { if (isAsyncStorageAvailable) return {}; throw new Error("Module not found: @react-native-async-storage/async-storage"); } - return M._load_original(uri, parent); }; } @@ -77,12 +70,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', () => { @@ -90,7 +81,6 @@ describe('createForwardingEventProcessor', () => { beforeEach(() => { mockGetForwardingEventProcessor.mockClear(); - isNetInfoAvailable = false; }); it('returns forwarding event processor by calling getForwardingEventProcessor with the provided dispatcher', () => { @@ -119,27 +109,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({}); diff --git a/lib/event_processor/event_processor_factory.react_native.ts b/lib/event_processor/event_processor_factory.react_native.ts index 02c0e2cf7..99019eff0 100644 --- a/lib/event_processor/event_processor_factory.react_native.ts +++ b/lib/event_processor/event_processor_factory.react_native.ts @@ -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, @@ -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; @@ -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 + ); }; diff --git a/lib/utils/import.react_native/@react-native-community/netinfo.ts b/lib/utils/import.react_native/@react-native-community/netinfo.ts deleted file mode 100644 index 434a0a1b3..000000000 --- a/lib/utils/import.react_native/@react-native-community/netinfo.ts +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright 2024, Optimizely - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import type { NetInfoSubscription, NetInfoChangeHandler } from '@react-native-community/netinfo'; -import { Maybe } from '../../type'; - -export { NetInfoState } from '@react-native-community/netinfo'; -export type NetInfoAddEventListerType = (listener: NetInfoChangeHandler) => NetInfoSubscription; - -let addEventListener: Maybe = undefined; - -const requireNetInfo = () => { - try { - return require('@react-native-community/netinfo'); - } catch (e) { - return undefined; - } -} - -export const isAvailable = (): boolean => requireNetInfo() !== undefined; - -const netinfo = requireNetInfo(); -addEventListener = netinfo?.addEventListener; - -export { addEventListener }; diff --git a/package-lock.json b/package-lock.json index 331bb0974..919143fa8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,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", @@ -68,10 +68,11 @@ "node": ">=14.0.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" + "react-native-get-random-values": "^1.11.0", + "ua-parser-js": "^1.0.38" }, "peerDependenciesMeta": { "@react-native-async-storage/async-storage": { @@ -85,6 +86,9 @@ }, "react-native-get-random-values": { "optional": true + }, + "ua-parser-js": { + "optional": true } } }, @@ -3979,15 +3983,16 @@ } }, "node_modules/@react-native-async-storage/async-storage": { - "version": "1.21.0", - "resolved": "https://registry.npmjs.org/@react-native-async-storage/async-storage/-/async-storage-1.21.0.tgz", - "integrity": "sha512-JL0w36KuFHFCvnbOXRekqVAUplmOyT/OuCQkogo6X98MtpSaJOKEAeZnYO8JB0U/RIEixZaGI5px73YbRm/oag==", + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@react-native-async-storage/async-storage/-/async-storage-2.1.2.tgz", + "integrity": "sha512-dvlNq4AlGWC+ehtH12p65+17V0Dx7IecOWl6WanF2ja38O1Dcjjvn7jVzkUHJ5oWkQBlyASurTPlTHgKXyYiow==", "dev": true, + "license": "MIT", "dependencies": { "merge-options": "^3.0.4" }, "peerDependencies": { - "react-native": "^0.0.0-0 || >=0.60 <1.0" + "react-native": "^0.0.0-0 || >=0.65 <1.0" } }, "node_modules/@react-native-community/cli": { diff --git a/package.json b/package.json index 6aaac97d4..d67a034bd 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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.0.0 <3.0.0", + "@react-native-community/netinfo": ">=5.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"