Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
d24352a289 | |||
e6c15b2d81 | |||
4d6ae3ba87 | |||
68ac5e6ec2 | |||
d4401172e6 | |||
1887645625 | |||
9e8b30391a | |||
817a660662 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/webdetector",
|
"name": "@pushrocks/webdetector",
|
||||||
"version": "1.0.6",
|
"version": "2.0.3",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "detect different environments within the browser",
|
"description": "detect different environments within the browser",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -22,7 +22,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^3.0.1",
|
"@pushrocks/smartdelay": "^3.0.1",
|
||||||
"@pushrocks/smartrx": "^3.0.0"
|
"@pushrocks/smartrx": "^3.0.1"
|
||||||
},
|
},
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 version"
|
"last 1 version"
|
||||||
|
17
pnpm-lock.yaml
generated
17
pnpm-lock.yaml
generated
@ -5,8 +5,8 @@ dependencies:
|
|||||||
specifier: ^3.0.1
|
specifier: ^3.0.1
|
||||||
version: 3.0.1
|
version: 3.0.1
|
||||||
'@pushrocks/smartrx':
|
'@pushrocks/smartrx':
|
||||||
specifier: ^3.0.0
|
specifier: ^3.0.1
|
||||||
version: 3.0.0
|
version: 3.0.1
|
||||||
|
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@gitzone/tsbuild':
|
'@gitzone/tsbuild':
|
||||||
@ -547,7 +547,7 @@ packages:
|
|||||||
'@pushrocks/smartdelay': 2.0.13
|
'@pushrocks/smartdelay': 2.0.13
|
||||||
'@pushrocks/smartmatch': 2.0.0
|
'@pushrocks/smartmatch': 2.0.0
|
||||||
'@pushrocks/smartpromise': 3.1.10
|
'@pushrocks/smartpromise': 3.1.10
|
||||||
'@pushrocks/smartrx': 3.0.0
|
'@pushrocks/smartrx': 3.0.1
|
||||||
'@pushrocks/smarttime': 4.0.1
|
'@pushrocks/smarttime': 4.0.1
|
||||||
'@types/minimatch': 5.1.2
|
'@types/minimatch': 5.1.2
|
||||||
'@types/symbol-tree': 3.2.2
|
'@types/symbol-tree': 3.2.2
|
||||||
@ -815,6 +815,7 @@ packages:
|
|||||||
|
|
||||||
/@pushrocks/smartpromise@3.1.10:
|
/@pushrocks/smartpromise@3.1.10:
|
||||||
resolution: {integrity: sha512-VeTurbZ1+ZMxBDJk1Y1LV8SN9xLI+oDXKVeCFw41FAGEKOUEqordqFpi6t+7Vhe/TXUZzCVpZ5bXxAxrGf8yTQ==}
|
resolution: {integrity: sha512-VeTurbZ1+ZMxBDJk1Y1LV8SN9xLI+oDXKVeCFw41FAGEKOUEqordqFpi6t+7Vhe/TXUZzCVpZ5bXxAxrGf8yTQ==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
/@pushrocks/smartpromise@4.0.2:
|
/@pushrocks/smartpromise@4.0.2:
|
||||||
resolution: {integrity: sha512-bqorOaGXPOuiOSV81luTKrTghg4O4NBRD0zyv7TIqmrMGf4a0uoozaUMp1X8vQdZW+y0gTzUJP9wkzAE6Cci0g==}
|
resolution: {integrity: sha512-bqorOaGXPOuiOSV81luTKrTghg4O4NBRD0zyv7TIqmrMGf4a0uoozaUMp1X8vQdZW+y0gTzUJP9wkzAE6Cci0g==}
|
||||||
@ -851,10 +852,10 @@ packages:
|
|||||||
rxjs: 7.8.1
|
rxjs: 7.8.1
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/@pushrocks/smartrx@3.0.0:
|
/@pushrocks/smartrx@3.0.1:
|
||||||
resolution: {integrity: sha512-PwWmgEC3nKLoZYvOOfQhCQeesFOLNtbutxvkr/H06BfK3UgBFgnjMrVycjeaQlEWh+fgcwV3ZxZSgzMBsba8qg==}
|
resolution: {integrity: sha512-iQdrnCp4aIJ3kNAXTYyrfJFVBuPEVX2wUfonrknltEwYqtCXoG6fRhIpAONUtJQwkHGqovkNR6xGyNpBOUL+AQ==}
|
||||||
dependencies:
|
dependencies:
|
||||||
'@pushrocks/smartpromise': 3.1.10
|
'@pushrocks/smartpromise': 4.0.2
|
||||||
rxjs: 7.8.1
|
rxjs: 7.8.1
|
||||||
|
|
||||||
/@pushrocks/smartshell@2.0.30:
|
/@pushrocks/smartshell@2.0.30:
|
||||||
@ -894,7 +895,7 @@ packages:
|
|||||||
'@pushrocks/smartjson': 5.0.6
|
'@pushrocks/smartjson': 5.0.6
|
||||||
'@pushrocks/smartlog': 3.0.2
|
'@pushrocks/smartlog': 3.0.2
|
||||||
'@pushrocks/smartpromise': 3.1.10
|
'@pushrocks/smartpromise': 3.1.10
|
||||||
'@pushrocks/smartrx': 3.0.0
|
'@pushrocks/smartrx': 3.0.1
|
||||||
'@pushrocks/smarttime': 4.0.1
|
'@pushrocks/smarttime': 4.0.1
|
||||||
engine.io: 6.3.1
|
engine.io: 6.3.1
|
||||||
socket.io: 4.5.4
|
socket.io: 4.5.4
|
||||||
@ -1039,7 +1040,7 @@ packages:
|
|||||||
'@pushrocks/smartenv': 5.0.5
|
'@pushrocks/smartenv': 5.0.5
|
||||||
'@pushrocks/smartjson': 5.0.6
|
'@pushrocks/smartjson': 5.0.6
|
||||||
'@pushrocks/smartpromise': 4.0.2
|
'@pushrocks/smartpromise': 4.0.2
|
||||||
'@pushrocks/smartrx': 3.0.0
|
'@pushrocks/smartrx': 3.0.1
|
||||||
fake-indexeddb: 4.0.1
|
fake-indexeddb: 4.0.1
|
||||||
idb: 7.1.1
|
idb: 7.1.1
|
||||||
dev: true
|
dev: true
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import { tap, expect } from '@pushrocks/tapbundle';
|
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;
|
let testWebDetector: webdetector.WebDetector;
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('first test', async () => {
|
||||||
const testWebDetector = new webdetector.WebDetector({
|
testWebDetector = new webdetector.WebDetector({
|
||||||
checkOnlineUrl: 'https://pubapi.lossless.one',
|
checkOnlineUrl: 'https://pubapi.lossless.one',
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -19,6 +19,17 @@ tap.test('should be online', async () => {
|
|||||||
testWebDetector.onlineObservable.subscribe((state) => {
|
testWebDetector.onlineObservable.subscribe((state) => {
|
||||||
console.log(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');
|
console.log('hi');
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@pushrocks/webdetector',
|
name: '@pushrocks/webdetector',
|
||||||
version: '1.0.6',
|
version: '2.0.3',
|
||||||
description: 'detect different environments within the browser'
|
description: 'detect different environments within the browser'
|
||||||
}
|
}
|
||||||
|
63
ts/index.ts
63
ts/index.ts
@ -1,62 +1 @@
|
|||||||
import * as plugins from './webdetector.plugins.js';
|
export * from './webdetector.classes.webdetector.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -2,7 +2,7 @@ export type TWebPlatform = 'android' | 'ios' | 'windows' | 'mac' | 'linux' | 'un
|
|||||||
|
|
||||||
export class Platform {
|
export class Platform {
|
||||||
public detectPlatform(): TWebPlatform {
|
public detectPlatform(): TWebPlatform {
|
||||||
const userAgent = navigator?.userAgent?.toLowerCase();
|
const userAgent = globalThis?.navigator?.userAgent?.toLowerCase();
|
||||||
|
|
||||||
if (!userAgent) {
|
if (!userAgent) {
|
||||||
return 'unknown';
|
return 'unknown';
|
||||||
|
12
ts/webdetector.classes.pwa.ts
Normal file
12
ts/webdetector.classes.pwa.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
71
ts/webdetector.classes.webdetector.ts
Normal file
71
ts/webdetector.classes.webdetector.ts
Normal 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
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user