6 Commits

Author SHA1 Message Date
d24352a289 2.0.3 2023-06-11 16:09:07 +02:00
e6c15b2d81 fix(core): update 2023-06-11 16:09:06 +02:00
4d6ae3ba87 2.0.2 2023-06-10 16:43:21 +02:00
68ac5e6ec2 fix(core): update 2023-06-10 16:43:20 +02:00
d4401172e6 2.0.1 2023-06-10 16:38:25 +02:00
1887645625 fix(core): update 2023-06-10 16:38:25 +02:00
8 changed files with 110 additions and 67 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/webdetector",
"version": "2.0.0",
"version": "2.0.3",
"private": false,
"description": "detect different environments within the browser",
"main": "dist_ts/index.js",

View File

@ -1,10 +1,10 @@
import { tap, expect } from '@pushrocks/tapbundle';
import * as webdetector from '../ts/index';
import * as webdetector from '../ts/webdetector.classes.webdetector.js';
let testWebDetector: webdetector.WebDetector;
tap.test('first test', async () => {
const testWebDetector = new webdetector.WebDetector({
testWebDetector = new webdetector.WebDetector({
checkOnlineUrl: 'https://pubapi.lossless.one',
});
@ -19,6 +19,17 @@ tap.test('should be online', async () => {
testWebDetector.onlineObservable.subscribe((state) => {
console.log(state);
});
testWebDetector.stopPeriodicChecks();
});
tap.test('should detect the platform', async () => {
const platform = testWebDetector.platform.detectPlatform();
console.log(platform);
});
tap.test('should detect if the app is installed', async () => {
const isInstalled = testWebDetector.pwa.isInstalled();
expect(isInstalled).toBeFalse();
});
console.log('hi');

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@pushrocks/webdetector',
version: '2.0.0',
version: '2.0.3',
description: 'detect different environments within the browser'
}

View File

@ -1,62 +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;
}
async startPeriodicChecks() {
while (true) {
await this.isOnline();
await plugins.smartdelay.delayFor(3000);
}
}
}
export * from './webdetector.classes.webdetector.js';

View File

@ -2,7 +2,7 @@ export type TWebPlatform = 'android' | 'ios' | 'windows' | 'mac' | 'linux' | 'un
export class Platform {
public detectPlatform(): TWebPlatform {
const userAgent = navigator?.userAgent?.toLowerCase();
const userAgent = globalThis?.navigator?.userAgent?.toLowerCase();
if (!userAgent) {
return 'unknown';

View File

@ -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;
}
}

View File

@ -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;
}
}

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "nodenext",
"esModuleInterop": true
}
}