-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@aws-amplify/datastore) Adding socket disconnection detection (#5086
- Loading branch information
Showing
5 changed files
with
119 additions
and
19 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,56 @@ | ||
import * as Observable from 'zen-observable'; | ||
import { ConsoleLogger as Logger, Reachability } from '@aws-amplify/core'; | ||
|
||
const logger = new Logger('DataStore'); | ||
|
||
const RECONNECTING_IN = 5000; // 5s this may be configurable in the future | ||
|
||
type ConnectionStatus = { | ||
// Might add other params in the future | ||
online: boolean; | ||
}; | ||
|
||
export default class DataStoreConnectivity { | ||
private connectionStatus: ConnectionStatus; | ||
private observer: ZenObservable.SubscriptionObserver<ConnectionStatus>; | ||
constructor() { | ||
this.connectionStatus = { | ||
online: false, | ||
}; | ||
} | ||
|
||
status(): Observable<ConnectionStatus> { | ||
if (this.observer) { | ||
throw new Error('Subscriber already exists'); | ||
} | ||
return new Observable(observer => { | ||
this.observer = observer; | ||
// Will be used to forward socket connection changes, enhancing Reachability | ||
|
||
const subs = new Reachability() | ||
.networkMonitor() | ||
.subscribe(({ online }) => { | ||
this.connectionStatus.online = online; | ||
|
||
const observerResult = { ...this.connectionStatus }; // copyOf status | ||
|
||
observer.next(observerResult); | ||
}); | ||
|
||
return () => { | ||
subs.unsubscribe(); | ||
}; | ||
}); | ||
} | ||
|
||
socketDisconnected() { | ||
if (this.observer && typeof this.observer.next === 'function') { | ||
this.observer.next({ online: false }); // Notify network issue from the socket | ||
|
||
setTimeout(() => { | ||
const observerResult = { ...this.connectionStatus }; // copyOf status | ||
this.observer.next(observerResult); | ||
}, RECONNECTING_IN); // giving time for socket cleanup and network status stabilization | ||
} | ||
} | ||
} |
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