2024-05-11 10:51:20 +00:00
|
|
|
import * as plugins from './plugins.js';
|
2024-05-24 23:24:02 +00:00
|
|
|
import { ServiceWorker } from './classes.serviceworker.js';
|
2024-05-11 10:51:20 +00:00
|
|
|
|
|
|
|
export class NetworkManager {
|
2024-05-23 13:28:41 +00:00
|
|
|
public serviceWorkerRef: ServiceWorker;
|
2024-05-11 10:51:20 +00:00
|
|
|
public webRequest: plugins.webrequest.WebRequest;
|
|
|
|
|
|
|
|
public previousState: string;
|
|
|
|
|
2024-05-23 13:28:41 +00:00
|
|
|
constructor(serviceWorkerRefArg: ServiceWorker) {
|
2024-05-11 10:51:20 +00:00
|
|
|
this.serviceWorkerRef = serviceWorkerRefArg;
|
|
|
|
this.webRequest = new plugins.webrequest.WebRequest();
|
|
|
|
this.getConnection()?.addEventListener('change', () => {
|
|
|
|
this.updateConnectionStatus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets the connection
|
|
|
|
*/
|
|
|
|
public getConnection() {
|
|
|
|
const navigatorLocal: any = self.navigator;
|
|
|
|
return navigatorLocal?.connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getEffectiveType() {
|
2024-05-11 10:52:42 +00:00
|
|
|
return this.getConnection()?.effectiveType || '4g';
|
2024-05-11 10:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public updateConnectionStatus() {
|
|
|
|
console.log(`Connection type changed from ${this.previousState} to ${this.getEffectiveType()}`);
|
|
|
|
}
|
|
|
|
}
|