From e6c15b2d811d91915840400a17761481ad80e1b7 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sun, 11 Jun 2023 16:09:06 +0200 Subject: [PATCH] fix(core): update --- test/test.both.ts | 7 ++- ts/00_commitinfo_data.ts | 2 +- ts/index.ts | 70 +------------------------- ts/webdetector.classes.pwa.ts | 12 +++++ ts/webdetector.classes.webdetector.ts | 71 +++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 71 deletions(-) create mode 100644 ts/webdetector.classes.pwa.ts create mode 100644 ts/webdetector.classes.webdetector.ts diff --git a/test/test.both.ts b/test/test.both.ts index dd458c5..a40e13a 100644 --- a/test/test.both.ts +++ b/test/test.both.ts @@ -1,6 +1,6 @@ import { tap, expect } from '@pushrocks/tapbundle'; -import * as webdetector from '../ts/index.js'; +import * as webdetector from '../ts/webdetector.classes.webdetector.js'; let testWebDetector: webdetector.WebDetector; tap.test('first test', async () => { @@ -27,6 +27,11 @@ tap.test('should detect the platform', async () => { console.log(platform); }); +tap.test('should detect if the app is installed', async () => { + const isInstalled = testWebDetector.pwa.isInstalled(); + expect(isInstalled).toBeFalse(); +}); + console.log('hi'); tap.start(); diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 263a041..a7d9065 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@pushrocks/webdetector', - version: '2.0.2', + version: '2.0.3', description: 'detect different environments within the browser' } diff --git a/ts/index.ts b/ts/index.ts index fa055b0..ef891f0 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,69 +1 @@ -import * as plugins from './webdetector.plugins.js'; - -import { Platform } from './webdetector.classes.platform.js'; - -export interface IWebDetectorOptions { - checkOnlineUrl: string; -} - -export class WebDetector { - // subclasses - public platform = new Platform(); - - options: IWebDetectorOptions; - private onlineObservableIntake = new plugins.smartrx.ObservableIntake(); - public onlineObservable = this.onlineObservableIntake.observable.pipe( - plugins.smartrx.rxjs.ops.throttleTime(10000) - ); - latestState: 'online' | 'offline' = 'online'; - - constructor(optionsArg: IWebDetectorOptions) { - this.options = optionsArg; - } - - /** - * - */ - async isOnline() { - 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) => { - 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; - } - - private periodicChecksRunning = false; - - public async startPeriodicChecks() { - this.periodicChecksRunning = true; - while (this.periodicChecksRunning) { - await this.isOnline(); - await plugins.smartdelay.delayFor(3000); - } - } - - public async stopPeriodicChecks() { - this.periodicChecksRunning = false; - } -} +export * from './webdetector.classes.webdetector.js'; diff --git a/ts/webdetector.classes.pwa.ts b/ts/webdetector.classes.pwa.ts new file mode 100644 index 0000000..e89e521 --- /dev/null +++ b/ts/webdetector.classes.pwa.ts @@ -0,0 +1,12 @@ +export class Pwa { + public isInstalled(): boolean { + let isInstalled: boolean; + if (globalThis?.matchMedia?.('(display-mode: standalone)').matches) { + isInstalled = true; + console.log('PWA installed'); + } else { + isInstalled = false; + } + return isInstalled; + } +} \ No newline at end of file diff --git a/ts/webdetector.classes.webdetector.ts b/ts/webdetector.classes.webdetector.ts new file mode 100644 index 0000000..620e439 --- /dev/null +++ b/ts/webdetector.classes.webdetector.ts @@ -0,0 +1,71 @@ +import * as plugins from './webdetector.plugins.js'; + +import { Platform } from './webdetector.classes.platform.js'; +import { Pwa } from './webdetector.classes.pwa.js'; + +export interface IWebDetectorOptions { + checkOnlineUrl: string; +} + +export class WebDetector { + // subclasses + public platform = new Platform(); + public pwa = new Pwa(); + + options: IWebDetectorOptions; + private onlineObservableIntake = new plugins.smartrx.ObservableIntake(); + public onlineObservable = this.onlineObservableIntake.observable.pipe( + plugins.smartrx.rxjs.ops.throttleTime(10000) + ); + latestState: 'online' | 'offline' = 'online'; + + constructor(optionsArg: IWebDetectorOptions) { + this.options = optionsArg; + } + + /** + * + */ + async isOnline() { + 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) => { + 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; + } + + private periodicChecksRunning = false; + + public async startPeriodicChecks() { + this.periodicChecksRunning = true; + while (this.periodicChecksRunning) { + await this.isOnline(); + await plugins.smartdelay.delayFor(3000); + } + } + + public async stopPeriodicChecks() { + this.periodicChecksRunning = false; + } +}