Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking internet connection & preload requirement #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,31 @@ export class IdlePreload /*implements PreloadingStrategy*/ {
* include zone to run outside of zone.js
*/
constructor(private _ngZone: NgZone, @Inject(REQUEST_IDLE_CALLBACK) private requestIdleCallback: any) {}
loadingRoute = new Set<Route>();
loading = true;

/*To avoid reloading the route*/
if (this.loadingRoute.has(route)) {
this.loading = false;
}
/*Checking for slow internet connection*/
const conn = typeof navigator !== 'undefined' ? (navigator as any).connection : undefined;
if (conn) {
if ((conn.effectiveType || '').includes('2g') || conn.saveData) { this.loading = false};
}

/*
* fire off preloading async modules
* fire off preloading async modules - loading only required modules
*/
preload(route: /*Route*/ any, fn: any /* () => Observable<any>*/ ): any/* Observable<any> */ {
this.requestIdleCallback(fn);
return of(null);
}

if (this.loading) {
if(route.data && !route.data.preload) {
yazharasu marked this conversation as resolved.
Show resolved Hide resolved
return null;
} else {
preload(route: /*Route*/ any, fn: any /* () => Observable<any>*/ ): any/* Observable<any> */ {
this.requestIdleCallback(fn);
return of(null);
}
}
}

/*
Expand Down