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", "author": "Lossless GmbH",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "(tstest test/ --web)", "test": "(parcel serve test/index.html)",
"build": "(tsbuild --web)", "build": "(tsbuild --web)",
"format": "(gitzone format)" "format": "(gitzone format)"
}, },
@ -17,8 +17,15 @@
"@gitzone/tstest": "^1.0.15", "@gitzone/tstest": "^1.0.15",
"@pushrocks/tapbundle": "^3.0.7", "@pushrocks/tapbundle": "^3.0.7",
"@types/node": "^10.11.7", "@types/node": "^10.11.7",
"parcel-bundler": "^1.11.0",
"tslint": "^5.11.0", "tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.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' import * as webdetector from '../ts/index'
let testWebDetector: webdetector.WebDetector; console.log('hi');
tap.test('should create a valid instance of webdetector', async () => { const run = async () => {
testWebDetector = new webdetector.WebDetector(); const testWebDetector = new webdetector.WebDetector({
expect(testWebDetector).to.be.instanceOf(webdetector.WebDetector); checkOnlineUrl: 'https://pubapi.lossless.one'
}) });
const onlineResultBoolean = await testWebDetector.isOnline();
tap.test('should determine if we are currently online', async () => { console.log('browser is online:')
let onlineResultBoolean = await testWebDetector.isOnline(); console.log(onlineResultBoolean);
expect(onlineResultBoolean).to.be.true; testWebDetector.startPeriodicChecks();
}); testWebDetector.onlineObservable.subscribe((state) => {
console.log(state);
tap.start() })
}
run();

View File

@ -1,24 +1,56 @@
import * as plugins from './webdetector.plugins'; import * as plugins from './webdetector.plugins';
import {throttleTime } from 'rxjs/operators';
export interface IWebDetectorOptions {
checkOnlineUrl: string;
}
export class WebDetector { 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() { async isOnline() {
const navigatorOnline = window.navigator.onLine let reachesInternet: boolean = false;
let reachesGoogle: boolean = false;
if (navigatorOnline) {
const controller = new AbortController(); 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(() => { const timeout = setTimeout(() => {
controller.abort(); controller.abort();
}, 5000); }, 1000);
await fetchPromise.then(async response => { 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 { export {
removeme smartdelay,
smartrx
} }