update
This commit is contained in:
parent
0b4af4331c
commit
bcd7b5cf90
7
dist/index.html
vendored
Normal file
7
dist/index.html
vendored
Normal 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
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
1
dist/test.8d1fc802.map
vendored
Normal file
File diff suppressed because one or more lines are too long
6764
package-lock.json
generated
6764
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
11
package.json
11
package.json
@ -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
7
test/index.html
Normal file
@ -0,0 +1,7 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<script src="./test.ts"></script>
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
27
test/test.ts
27
test/test.ts
@ -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;
|
||||
});
|
||||
|
||||
tap.start()
|
||||
const run = async () => {
|
||||
const testWebDetector = new webdetector.WebDetector({
|
||||
checkOnlineUrl: 'https://pubapi.lossless.one'
|
||||
});
|
||||
const onlineResultBoolean = await testWebDetector.isOnline();
|
||||
console.log('browser is online:')
|
||||
console.log(onlineResultBoolean);
|
||||
testWebDetector.startPeriodicChecks();
|
||||
testWebDetector.onlineObservable.subscribe((state) => {
|
||||
console.log(state);
|
||||
})
|
||||
}
|
||||
run();
|
||||
|
56
ts/index.ts
56
ts/index.ts
@ -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) {
|
||||
const controller = new AbortController();
|
||||
const fetchPromise = fetch('https://google.com', { signal: controller.signal });
|
||||
const timeout = setTimeout(() => {
|
||||
controller.abort();
|
||||
}, 5000);
|
||||
let reachesInternet: boolean = false;
|
||||
const controller = new AbortController();
|
||||
const fetchPromise = fetch(this.options.checkOnlineUrl, { signal: controller.signal });
|
||||
const timeout = setTimeout(() => {
|
||||
controller.abort();
|
||||
}, 1000);
|
||||
|
||||
await fetchPromise.then(async response => {
|
||||
reachesGoogle = true
|
||||
});
|
||||
await fetchPromise.then(async response => {
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,4 +1,7 @@
|
||||
const removeme = {};
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartrx from '@pushrocks/smartrx';
|
||||
|
||||
export {
|
||||
removeme
|
||||
smartdelay,
|
||||
smartrx
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user