diff --git a/dist/index.html b/dist/index.html new file mode 100644 index 0000000..bc57ea1 --- /dev/null +++ b/dist/index.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/dist/test.8d1fc802.js b/dist/test.8d1fc802.js new file mode 100644 index 0000000..80ba5d5 --- /dev/null +++ b/dist/test.8d1fc802.js @@ -0,0 +1,27783 @@ +// modules are defined as an array +// [ module function, map of requires ] +// +// map of requires is short require name -> numeric require +// +// anything defined in a previous bundle is accessed via the +// orig method which is the require for previous bundles + +// eslint-disable-next-line no-global-assign +parcelRequire = (function (modules, cache, entry, globalName) { + // Save the require from previous bundle to this closure if any + var previousRequire = typeof parcelRequire === 'function' && parcelRequire; + var nodeRequire = typeof require === 'function' && require; + + function newRequire(name, jumped) { + if (!cache[name]) { + if (!modules[name]) { + // if we cannot find the module within our internal map or + // cache jump to the current global require ie. the last bundle + // that was added to the page. + var currentRequire = typeof parcelRequire === 'function' && parcelRequire; + if (!jumped && currentRequire) { + return currentRequire(name, true); + } + + // If there are other bundles on this page the require from the + // previous one is saved to 'previousRequire'. Repeat this as + // many times as there are bundles until the module is found or + // we exhaust the require chain. + if (previousRequire) { + return previousRequire(name, true); + } + + // Try the node require function if it exists. + if (nodeRequire && typeof name === 'string') { + return nodeRequire(name); + } + + var err = new Error('Cannot find module \'' + name + '\''); + err.code = 'MODULE_NOT_FOUND'; + throw err; + } + + localRequire.resolve = resolve; + localRequire.cache = {}; + + var module = cache[name] = new newRequire.Module(name); + + modules[name][0].call(module.exports, localRequire, module, module.exports, this); + } + + return cache[name].exports; + + function localRequire(x){ + return newRequire(localRequire.resolve(x)); + } + + function resolve(x){ + return modules[name][1][x] || x; + } + } + + function Module(moduleName) { + this.id = moduleName; + this.bundle = newRequire; + this.exports = {}; + } + + newRequire.isParcelRequire = true; + newRequire.Module = Module; + newRequire.modules = modules; + newRequire.cache = cache; + newRequire.parent = previousRequire; + newRequire.register = function (id, exports) { + modules[id] = [function (require, module) { + module.exports = exports; + }, {}]; + }; + + for (var i = 0; i < entry.length; i++) { + newRequire(entry[i]); + } + + if (entry.length) { + // Expose entry point to Node, AMD or browser globals + // Based on https://github.com/ForbesLindesay/umd/blob/master/template.js + var mainExports = newRequire(entry[entry.length - 1]); + + // CommonJS + if (typeof exports === "object" && typeof module !== "undefined") { + module.exports = mainExports; + + // RequireJS + } else if (typeof define === "function" && define.amd) { + define(function () { + return mainExports; + }); + + // + + + diff --git a/test/test.ts b/test/test.ts index 1c0009d..de76518 100644 --- a/test/test.ts +++ b/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(); diff --git a/ts/index.ts b/ts/index.ts index db5a0e6..395f9c4 100644 --- a/ts/index.ts +++ b/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; } } \ No newline at end of file diff --git a/ts/webdetector.plugins.ts b/ts/webdetector.plugins.ts index d00ae0c..f3ca435 100644 --- a/ts/webdetector.plugins.ts +++ b/ts/webdetector.plugins.ts @@ -1,4 +1,7 @@ -const removeme = {}; +import * as smartdelay from '@pushrocks/smartdelay'; +import * as smartrx from '@pushrocks/smartrx'; + export { - removeme + smartdelay, + smartrx }