-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move offline logic into a plugin
- Loading branch information
Showing
8 changed files
with
136 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
packages/analytics-browser/src/plugins/network-checker-plugin.ts
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
packages/analytics-browser/src/plugins/network-connectivity-checker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { getGlobalScope } from '@amplitude/analytics-client-common'; | ||
import { BeforePlugin, BrowserClient } from '@amplitude/analytics-types'; | ||
import { BrowserConfig } from 'src/config'; | ||
|
||
interface EventListener { | ||
type: 'online' | 'offline'; | ||
handler: () => void; | ||
} | ||
|
||
export const networkConnectivityCheckerPlugin = (): BeforePlugin => { | ||
const name = '@amplitude/plugin-network-checker-browser'; | ||
const type = 'before' as const; | ||
const globalScope = getGlobalScope(); | ||
let eventListeners: EventListener[] = []; | ||
|
||
const addNetworkListener = (type: 'online' | 'offline', handler: () => void) => { | ||
if (globalScope) { | ||
globalScope.addEventListener(type, handler); | ||
eventListeners.push({ | ||
type, | ||
handler, | ||
}); | ||
} | ||
}; | ||
|
||
const removeNetworkListeners = () => { | ||
eventListeners.forEach(({ type, handler }) => { | ||
if (globalScope) { | ||
globalScope.removeEventListener(type, handler); | ||
} | ||
}); | ||
eventListeners = []; | ||
}; | ||
|
||
const setup = async (config: BrowserConfig, amplitude: BrowserClient) => { | ||
config.offline = !navigator.onLine; | ||
|
||
addNetworkListener('online', () => { | ||
config.offline = false; | ||
// Flush immediately will cause ERR_NETWORK_CHANGED | ||
setTimeout(() => { | ||
amplitude.flush(); | ||
}, config.flushIntervalMillis); | ||
}); | ||
|
||
addNetworkListener('offline', () => { | ||
config.offline = true; | ||
}); | ||
}; | ||
|
||
const teardown = async () => { | ||
removeNetworkListeners(); | ||
}; | ||
|
||
return { | ||
name, | ||
type, | ||
setup, | ||
teardown, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/analytics-browser/test/plugins/network-connectivity-checker.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
|
||
import { createAmplitudeMock, createConfigurationMock } from '../helpers/mock'; | ||
import { networkConnectivityCheckerPlugin } from '../../src/plugins/network-connectivity-checker'; | ||
|
||
describe('networkConnectivityCheckerPlugin', () => { | ||
const amplitude = createAmplitudeMock(); | ||
const config = createConfigurationMock(); | ||
|
||
test('should set up correctly when online', async () => { | ||
const plugin = networkConnectivityCheckerPlugin(); | ||
jest.spyOn(navigator, 'onLine', 'get').mockReturnValue(true); | ||
const addEventListenerSpy = jest.spyOn(window, 'addEventListener'); | ||
|
||
await plugin.setup?.(config, amplitude); | ||
|
||
expect(config.offline).toEqual(false); | ||
expect(addEventListenerSpy).toHaveBeenCalledWith('online', expect.any(Function)); | ||
expect(addEventListenerSpy).toHaveBeenCalledWith('offline', expect.any(Function)); | ||
addEventListenerSpy.mockRestore(); | ||
}); | ||
|
||
test('should set up correctly when offline', async () => { | ||
const plugin = networkConnectivityCheckerPlugin(); | ||
jest.spyOn(navigator, 'onLine', 'get').mockReturnValue(false); | ||
const addEventListenerSpy = jest.spyOn(window, 'addEventListener'); | ||
|
||
await plugin.setup?.(config, amplitude); | ||
|
||
expect(config.offline).toEqual(true); | ||
expect(addEventListenerSpy).toHaveBeenCalledWith('online', expect.any(Function)); | ||
expect(addEventListenerSpy).toHaveBeenCalledWith('offline', expect.any(Function)); | ||
addEventListenerSpy.mockRestore(); | ||
}); | ||
|
||
test('should teardown plugin', async () => { | ||
const plugin = networkConnectivityCheckerPlugin(); | ||
const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener'); | ||
|
||
await plugin.setup?.(createConfigurationMock(), amplitude); | ||
await plugin.teardown?.(); | ||
|
||
expect(removeEventListenerSpy).toHaveBeenCalledWith('online', expect.any(Function)); | ||
expect(removeEventListenerSpy).toHaveBeenCalledWith('offline', expect.any(Function)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters