This commit is contained in:
Philipp Kunz 2018-12-19 17:17:25 +01:00
parent 0b4af4331c
commit bcd7b5cf90
9 changed files with 34614 additions and 51 deletions

7
dist/index.html vendored Normal file
View File

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src="/test.8d1fc802.js"></script>
</head>
<body></body>
</html>

27783
dist/test.8d1fc802.js vendored Normal file

File diff suppressed because one or more lines are too long

1
dist/test.8d1fc802.map vendored Normal file

File diff suppressed because one or more lines are too long

6764
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/ --web)",
"test": "(parcel serve test/index.html)",
"build": "(tsbuild --web)",
"format": "(gitzone format)"
},
@ -17,8 +17,15 @@
"@gitzone/tstest": "^1.0.15",
"@pushrocks/tapbundle": "^3.0.7",
"@types/node": "^10.11.7",
"parcel-bundler": "^1.11.0",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0"
},
"dependencies": {}
"dependencies": {
"@pushrocks/smartdelay": "^2.0.2",
"@pushrocks/smartrx": "^2.0.3"
},
"browserslist": [
"last 1 version"
]
}

7
test/index.html Normal file
View File

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src="./test.ts"></script>
</head>
<body></body>
</html>

View File

@ -1,16 +1,17 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as webdetector from '../ts/index'
let testWebDetector: webdetector.WebDetector;
console.log('hi');
tap.test('should create a valid instance of webdetector', async () => {
testWebDetector = new webdetector.WebDetector();
expect(testWebDetector).to.be.instanceOf(webdetector.WebDetector);
})
tap.test('should determine if we are currently online', async () => {
let onlineResultBoolean = await testWebDetector.isOnline();
expect(onlineResultBoolean).to.be.true;
const run = async () => {
const testWebDetector = new webdetector.WebDetector({
checkOnlineUrl: 'https://pubapi.lossless.one'
});
tap.start()
const onlineResultBoolean = await testWebDetector.isOnline();
console.log('browser is online:')
console.log(onlineResultBoolean);
testWebDetector.startPeriodicChecks();
testWebDetector.onlineObservable.subscribe((state) => {
console.log(state);
})
}
run();

View File

@ -1,24 +1,56 @@
import * as plugins from './webdetector.plugins';
import {throttleTime } from 'rxjs/operators';
export interface IWebDetectorOptions {
checkOnlineUrl: string;
}
export class WebDetector {
options: IWebDetectorOptions;
private onlineObservableIntake = new plugins.smartrx.ObservableIntake();
onlineObservable = this.onlineObservableIntake.observable.pipe(throttleTime(10000));
latestState: 'online' | 'offline' = 'online';
constructor(optionsArg: IWebDetectorOptions) {
this.options = optionsArg;
}
/**
*
*/
async isOnline() {
const navigatorOnline = window.navigator.onLine
let reachesGoogle: boolean = false;
if (navigatorOnline) {
let reachesInternet: boolean = false;
const controller = new AbortController();
const fetchPromise = fetch('https://google.com', { signal: controller.signal });
const fetchPromise = fetch(this.options.checkOnlineUrl, { signal: controller.signal });
const timeout = setTimeout(() => {
controller.abort();
}, 5000);
}, 1000);
await fetchPromise.then(async response => {
reachesGoogle = true
reachesInternet = true
}).catch(err => {
// console.log(`request to ${this.options.checkOnlineUrl} failed}`)
});
const latestLocalState = (() => {
if(reachesInternet) {
return 'online'
} else {
return 'offline'
}
})();
if(latestLocalState !== this.latestState) {
this.onlineObservableIntake.push(this.latestState);
}
this.latestState = latestLocalState;
return reachesInternet;
}
async startPeriodicChecks() {
while (true) {
await this.isOnline();
await plugins.smartdelay.delayFor(3000);
}
return reachesGoogle;
}
}

View File

@ -1,4 +1,7 @@
const removeme = {};
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartrx from '@pushrocks/smartrx';
export {
removeme
smartdelay,
smartrx
}