forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
squashed commits from aws-amplify#12907
- Loading branch information
1 parent
4840d0a
commit 9c32e1b
Showing
28 changed files
with
837 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Observable, Observer } from 'rxjs'; | ||
import { NetworkConnectionMonitor, Reachability } from '../src/Reachability'; | ||
|
||
describe('NetworkConnectionMonitor', () => { | ||
let reachabilityObserver: Observer<{ online: boolean }>; | ||
const eventHandler = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest | ||
.spyOn(Reachability.prototype, 'networkMonitor') | ||
.mockImplementationOnce(() => { | ||
return new Observable(observer => { | ||
reachabilityObserver = observer; | ||
}); | ||
}) | ||
// Twice because we subscribe to get the initial state then again to monitor reachability | ||
.mockImplementationOnce(() => { | ||
return new Observable(observer => { | ||
reachabilityObserver = observer; | ||
}); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
eventHandler.mockReset(); | ||
}); | ||
|
||
it('should execute event handler exactly once if device is already online', () => { | ||
const netMon = new NetworkConnectionMonitor(); | ||
netMon.enableNetworkMonitoringFor(eventHandler); | ||
expect(eventHandler).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not execute event handler if device is already offline', () => { | ||
jest | ||
.spyOn(Reachability.prototype, 'isOnline') | ||
.mockImplementationOnce(() => { | ||
return false; | ||
}); | ||
const netMon = new NetworkConnectionMonitor(); | ||
netMon.enableNetworkMonitoringFor(eventHandler); | ||
expect(eventHandler).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should subscribe to network event when device is offline, execute the event handler when device comes online then unsubscribe', () => { | ||
jest | ||
.spyOn(Reachability.prototype, 'isOnline') | ||
.mockImplementationOnce(() => { | ||
return false; | ||
}); | ||
|
||
const netMon = new NetworkConnectionMonitor(); | ||
netMon.enableNetworkMonitoringFor(eventHandler); | ||
|
||
// Should have been called 0 times at this stage since device is offline | ||
expect(eventHandler).toHaveBeenCalledTimes(0); | ||
|
||
// Replicating device coming online the first time | ||
reachabilityObserver?.next?.({ online: true }); | ||
|
||
// Should be called exactly one time when the device comes online | ||
expect(eventHandler).toHaveBeenCalledTimes(1); | ||
eventHandler.mockReset(); | ||
|
||
// Replicating device coming online the second time | ||
reachabilityObserver?.next?.({ online: true }); | ||
|
||
// Should not be called since it should have unsubscribed after executing the event handler once | ||
expect(eventHandler).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |
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: 22 additions & 0 deletions
22
packages/core/__tests__/utils/deviceId/getDeviceId.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,22 @@ | ||
import { Cache } from '../../../src'; | ||
import { getDeviceId } from '../../../src/utils/deviceId/getDeviceId'; | ||
|
||
describe('getDeviceId: ', () => { | ||
const testDeviceId = 'test-device-id'; | ||
it('should return the device id if already present in Cache', async () => { | ||
jest.spyOn(Cache, 'getItem').mockImplementationOnce(key => { | ||
return Promise.resolve(testDeviceId); | ||
}); | ||
const setItemSpy = jest.spyOn(Cache, 'setItem'); | ||
expect(await getDeviceId()).toBe(testDeviceId); | ||
expect(setItemSpy).toHaveBeenCalledTimes(0); | ||
}); | ||
it('should create a new device id if not available and store it in Cache', async () => { | ||
jest.spyOn(Cache, 'getItem').mockImplementationOnce(key => { | ||
return Promise.resolve(undefined); | ||
}); | ||
const setItemSpy = jest.spyOn(Cache, 'setItem'); | ||
await getDeviceId(); | ||
expect(setItemSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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
54 changes: 54 additions & 0 deletions
54
packages/core/src/Reachability/ReachabilityMonitor/NetworkConnectionMonitor.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,54 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Observable, Subscription } from 'rxjs'; | ||
import { Reachability } from '..'; | ||
import { AsyncReturnType, NetworkStatus } from '../types'; | ||
|
||
export class NetworkConnectionMonitor { | ||
/** | ||
* @private | ||
*/ | ||
private _networkMonitoringSubscriptions?: Subscription; | ||
private _networkMonitor: Observable<NetworkStatus>; | ||
private _reachability: Reachability; | ||
|
||
constructor() { | ||
this._reachability = new Reachability(); | ||
this._networkMonitor = this._reachability.networkMonitor(); | ||
} | ||
|
||
/** | ||
* Turn network state monitoring on if it isn't on already | ||
*/ | ||
public enableNetworkMonitoringFor( | ||
eventHandler: (...args: any) => Promise<any> | ||
): AsyncReturnType<typeof eventHandler> { | ||
if (this._reachability.isOnline()) { | ||
return eventHandler(); | ||
} else { | ||
return new Promise((resolve, _) => { | ||
const subscription = this._networkMonitor.subscribe(({ online }) => { | ||
if (online) { | ||
const eventHandlerResult = eventHandler(); | ||
subscription.unsubscribe(); | ||
resolve(eventHandlerResult); | ||
} | ||
}); | ||
if (!this._networkMonitoringSubscriptions) { | ||
this._networkMonitoringSubscriptions = subscription; | ||
return; | ||
} | ||
this._networkMonitoringSubscriptions.add(subscription); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Turn network state monitoring off if it isn't off already | ||
*/ | ||
public disableNetworkMonitoring() { | ||
this._networkMonitoringSubscriptions?.unsubscribe(); | ||
this._networkMonitoringSubscriptions = undefined; | ||
} | ||
} |
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
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
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,24 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO: add native impl of this | ||
import { Cache } from '../../Cache'; | ||
import { amplifyUuid } from '../amplifyUuid'; | ||
|
||
/** | ||
* Local storage key to store the device id | ||
*/ | ||
const _localStorageKey = 'amplify-device-id'; | ||
|
||
/** | ||
* Utility to generate or return cached deviceId | ||
*/ | ||
export async function getDeviceId(): Promise<string | undefined> { | ||
let deviceId = (await Cache.getItem(_localStorageKey)) as string | undefined; | ||
if (!!deviceId) { | ||
return deviceId; | ||
} | ||
deviceId = amplifyUuid(); | ||
await Cache.setItem(_localStorageKey, deviceId); | ||
return deviceId; | ||
} |
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,4 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { getDeviceId } from './getDeviceId'; |
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
Oops, something went wrong.