MOBILE-4270 messages: Decrease chances of offline messages duplicated
This is because Android seems to duplicate the request if the type of connection changes while the request is done. E.g. when leaving flight mode the device could connect to mobile data first and then to WiFi.main
parent
a128ae8929
commit
1f3d72cfbb
|
@ -87,7 +87,7 @@ const preferencesRoutes: Routes = [
|
||||||
CoreUserDelegate.registerHandler(AddonMessagesSendMessageUserHandler.instance);
|
CoreUserDelegate.registerHandler(AddonMessagesSendMessageUserHandler.instance);
|
||||||
|
|
||||||
// Sync some discussions when device goes online.
|
// Sync some discussions when device goes online.
|
||||||
CoreNetwork.onConnect().subscribe(() => {
|
CoreNetwork.onConnectShouldBeStable().subscribe(() => {
|
||||||
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
||||||
NgZone.run(() => {
|
NgZone.run(() => {
|
||||||
AddonMessagesSync.syncAllDiscussions(undefined, true);
|
AddonMessagesSync.syncAllDiscussions(undefined, true);
|
||||||
|
|
|
@ -16,9 +16,12 @@ import { CoreCronDelegate } from '@services/cron';
|
||||||
import { NgZone } from '@singletons';
|
import { NgZone } from '@singletons';
|
||||||
import { CoreNetwork } from '@services/network';
|
import { CoreNetwork } from '@services/network';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializer function.
|
||||||
|
*/
|
||||||
export default function(): void {
|
export default function(): void {
|
||||||
// When the app is re-connected, start network handlers that were stopped.
|
// When the app is re-connected, start network handlers that were stopped.
|
||||||
CoreNetwork.onConnect().subscribe(() => {
|
CoreNetwork.onConnectShouldBeStable().subscribe(() => {
|
||||||
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
||||||
NgZone.run(() => CoreCronDelegate.startNetworkHandlers());
|
NgZone.run(() => CoreCronDelegate.startNetworkHandlers());
|
||||||
});
|
});
|
||||||
|
|
|
@ -150,7 +150,7 @@ export class CoreFilepoolProvider {
|
||||||
this.checkQueueProcessing();
|
this.checkQueueProcessing();
|
||||||
|
|
||||||
// Start queue when device goes online.
|
// Start queue when device goes online.
|
||||||
CoreNetwork.onConnect().subscribe(() => {
|
CoreNetwork.onConnectShouldBeStable().subscribe(() => {
|
||||||
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
||||||
NgZone.run(() => this.checkQueueProcessing());
|
NgZone.run(() => this.checkQueueProcessing());
|
||||||
});
|
});
|
||||||
|
|
|
@ -38,9 +38,11 @@ export class CoreNetworkService extends Network {
|
||||||
type!: string;
|
type!: string;
|
||||||
|
|
||||||
protected connectObservable = new Subject<'connected'>();
|
protected connectObservable = new Subject<'connected'>();
|
||||||
|
protected connectStableObservable = new Subject<'connected'>();
|
||||||
protected disconnectObservable = new Subject<'disconnected'>();
|
protected disconnectObservable = new Subject<'disconnected'>();
|
||||||
protected forceConnectionMode?: CoreNetworkConnection;
|
protected forceConnectionMode?: CoreNetworkConnection;
|
||||||
protected online = false;
|
protected online = false;
|
||||||
|
protected connectStableTimeout?: number;
|
||||||
|
|
||||||
get connectionType(): CoreNetworkConnection {
|
get connectionType(): CoreNetworkConnection {
|
||||||
if (this.forceConnectionMode !== undefined) {
|
if (this.forceConnectionMode !== undefined) {
|
||||||
|
@ -146,6 +148,7 @@ export class CoreNetworkService extends Network {
|
||||||
/**
|
/**
|
||||||
* Returns an observable to notify when the app is connected.
|
* Returns an observable to notify when the app is connected.
|
||||||
* It will also be fired when connection type changes.
|
* It will also be fired when connection type changes.
|
||||||
|
* If you're going to perform network requests once the device is connected, please use onConnectShouldBeStable instead.
|
||||||
*
|
*
|
||||||
* @returns Observable.
|
* @returns Observable.
|
||||||
*/
|
*/
|
||||||
|
@ -153,6 +156,18 @@ export class CoreNetworkService extends Network {
|
||||||
return this.connectObservable;
|
return this.connectObservable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an observable to notify when the app is connected and it should already be a stable a connection.
|
||||||
|
* E.g. when leaving flight mode the device could connect to mobile network first and then to WiFi.
|
||||||
|
* If you're going to perform network requests once the device is connected, it's recommended to use this function instead of
|
||||||
|
* onConnect because some OS (e.g. Android) duplicate a request if the type of connection changes while the request is done.
|
||||||
|
*
|
||||||
|
* @returns Observable.
|
||||||
|
*/
|
||||||
|
onConnectShouldBeStable(): Observable<'connected'> {
|
||||||
|
return this.connectStableObservable;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an observable to notify when the app is disconnected.
|
* Returns an observable to notify when the app is disconnected.
|
||||||
*
|
*
|
||||||
|
@ -166,10 +181,14 @@ export class CoreNetworkService extends Network {
|
||||||
* Fires the correct observable depending on the connection status.
|
* Fires the correct observable depending on the connection status.
|
||||||
*/
|
*/
|
||||||
protected fireObservable(): void {
|
protected fireObservable(): void {
|
||||||
|
clearTimeout(this.connectStableTimeout);
|
||||||
this.checkOnline();
|
this.checkOnline();
|
||||||
|
|
||||||
if (this.online) {
|
if (this.online) {
|
||||||
this.connectObservable.next('connected');
|
this.connectObservable.next('connected');
|
||||||
|
this.connectStableTimeout = window.setTimeout(() => {
|
||||||
|
this.connectStableObservable.next('connected');
|
||||||
|
}, 5000);
|
||||||
} else {
|
} else {
|
||||||
this.disconnectObservable.next('disconnected');
|
this.disconnectObservable.next('disconnected');
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,7 +76,7 @@ export class CoreIframeUtilsProvider {
|
||||||
this.addOfflineWarning(element, src, isSubframe);
|
this.addOfflineWarning(element, src, isSubframe);
|
||||||
|
|
||||||
// If the network changes, check it again.
|
// If the network changes, check it again.
|
||||||
const subscription = CoreNetwork.onConnect().subscribe(() => {
|
const subscription = CoreNetwork.onConnectShouldBeStable().subscribe(() => {
|
||||||
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
// Execute the callback in the Angular zone, so change detection doesn't stop working.
|
||||||
NgZone.run(() => {
|
NgZone.run(() => {
|
||||||
if (!this.checkOnlineFrameInOffline(element, isSubframe)) {
|
if (!this.checkOnlineFrameInOffline(element, isSubframe)) {
|
||||||
|
|
Loading…
Reference in New Issue