UNPKG

ngx-network

Version:

get and tracking the network speed with simplest way ever

191 lines (180 loc) 10 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common/http'), require('@angular/core'), require('rxjs'), require('rxjs/operators')) : typeof define === 'function' && define.amd ? define('ngx-network', ['exports', '@angular/common/http', '@angular/core', 'rxjs', 'rxjs/operators'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["ngx-network"] = {}, global.ng.common.http, global.ng.core, global.rxjs, global.rxjs.operators)); })(this, (function (exports, i1, i0, rxjs, operators) { 'use strict'; function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var i1__namespace = /*#__PURE__*/_interopNamespace(i1); var i0__namespace = /*#__PURE__*/_interopNamespace(i0); exports.Units = void 0; (function (Units) { Units["kb/s"] = "kb/s"; Units["mb/s"] = "mb/s"; Units["gb/s"] = "gb/s"; Units["tb/s"] = "tb/s"; })(exports.Units || (exports.Units = {})); var NGX_NETWORK_CONFIG = 'NGX_NETWORK_CONFIG'; var NgxNetworkModule = /** @class */ (function () { function NgxNetworkModule() { } NgxNetworkModule.forRoot = function (config) { return { ngModule: NgxNetworkModule, providers: [ NgxNetworkService, { provide: NGX_NETWORK_CONFIG, useValue: { url: (config === null || config === void 0 ? void 0 : config.url) || "https://raw.githubusercontent.com/mahmoudshahin1111/ngx-network/master/src/assets/mocks/1mb.jpg", speedUnit: (config === null || config === void 0 ? void 0 : config.speedUnit) || exports.Units['mb/s'], delay: (config === null || config === void 0 ? void 0 : config.delay) || 100 } } ], }; }; return NgxNetworkModule; }()); NgxNetworkModule.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NgxNetworkModule, deps: [], target: i0__namespace.ɵɵFactoryTarget.NgModule }); NgxNetworkModule.ɵmod = i0__namespace.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NgxNetworkModule, imports: [i1.HttpClientModule] }); NgxNetworkModule.ɵinj = i0__namespace.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NgxNetworkModule, imports: [[i1.HttpClientModule]] }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NgxNetworkModule, decorators: [{ type: i0.NgModule, args: [{ imports: [i1.HttpClientModule], }] }] }); var NgxNetworkService = /** @class */ (function () { function NgxNetworkService(config, http) { this.config = config; this.http = http; this.speedChanged$ = new rxjs.BehaviorSubject({ speed: 0, unit: config.speedUnit, }); this.onSpeedChanged$ = this.speedChanged$.asObservable().pipe(operators.throttleTime(this.config.delay), operators.filter(function (payload) { return payload.speed !== 0; })); this.startSendingPayloadSubscription = new rxjs.Subscription(); this.lastPayloadSize = 0; this.lastTime = 0; } NgxNetworkService.prototype.getSpeed = function () { if (this.startSendingPayloadSubscription) { this.startSendingPayloadSubscription.unsubscribe(); } this.startSendingPayloadSubscription = this.startSendingPayload(this.config.url, false).subscribe(); return this.onSpeedChanged$; }; NgxNetworkService.prototype.onSpeedChanged = function () { if (this.startSendingPayloadSubscription) { this.startSendingPayloadSubscription.unsubscribe(); } this.startSendingPayloadSubscription = this.startSendingPayload(this.config.url, true).subscribe(); return this.onSpeedChanged$; }; NgxNetworkService.prototype.startSendingPayload = function (url, repeatRequest) { var _this = this; this.lastTime = 0; this.lastPayloadSize = 0; var randomQuery = "q=" + Math.round(Math.random() * 99959).toString(); if (!repeatRequest) { return this.sendRequest(url, randomQuery).pipe(operators.takeLast(1), operators.tap(function (payload) { _this.speedChanged$.next(payload); }), operators.map(function (e) { return null; })); } return this.sendRequest(url, randomQuery).pipe(operators.tap(function (payload) { _this.speedChanged$.next(payload); }), operators.finalize(function () { if (!repeatRequest) return; _this.startSendingPayloadSubscription.unsubscribe(); _this.startSendingPayloadSubscription = _this.startSendingPayload(url, repeatRequest).subscribe(); }), operators.map(function (e) { return null; })); }; NgxNetworkService.prototype.sendRequest = function (url, params) { var _this = this; var request = new i1.HttpRequest('GET', url, { params: params, responseType: 'arraybuffer', reportProgress: true, }); return this.http .request(request) .pipe(operators.concatMap(function (httpEvent) { return _this.handleHttpEvents(httpEvent); })); }; NgxNetworkService.prototype.handleHttpEvents = function (e) { var _this = this; return new rxjs.Observable(function (subscriber) { if (e.type === i1.HttpEventType.DownloadProgress) { var currentTime = Date.now(); var elapseTimeForEveryPayload = currentTime - _this.lastTime; var elapseTimeSecs = elapseTimeForEveryPayload / 1000; var speed = _this.speedChanged$.getValue().speed.toString(); var currentPayloadSize = e.loaded; var downloadPayloadSize = Math.abs(currentPayloadSize - _this.lastPayloadSize); switch (_this.config.speedUnit) { case exports.Units['kb/s']: speed = Number(downloadPayloadSize / 1024 / elapseTimeSecs).toFixed(3); break; case exports.Units['mb/s']: speed = Number(downloadPayloadSize / Math.pow(1024, 2) / elapseTimeSecs).toFixed(3); break; case exports.Units['gb/s']: speed = Number(downloadPayloadSize / Math.pow(1024, 3) / elapseTimeSecs).toFixed(3); break; case exports.Units['tb/s']: speed = Number(downloadPayloadSize / Math.pow(1024, 4) / elapseTimeSecs).toFixed(3); break; } _this.lastPayloadSize = e.loaded; _this.lastTime = Date.now(); subscriber.next({ speed: Number(Number(speed) === 0 ? _this.speedChanged$.getValue().speed : speed), unit: _this.config.speedUnit, }); } subscriber.complete(); }); }; return NgxNetworkService; }()); NgxNetworkService.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NgxNetworkService, deps: [{ token: NGX_NETWORK_CONFIG }, { token: i1__namespace.HttpClient }], target: i0__namespace.ɵɵFactoryTarget.Injectable }); NgxNetworkService.ɵprov = i0__namespace.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NgxNetworkService, providedIn: 'root' }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NgxNetworkService, decorators: [{ type: i0.Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: function () { return [{ type: undefined, decorators: [{ type: i0.Inject, args: [NGX_NETWORK_CONFIG] }] }, { type: i1__namespace.HttpClient }]; } }); /* * Public API Surface of ngx-network */ /** * Generated bundle index. Do not edit. */ exports.NGX_NETWORK_CONFIG = NGX_NETWORK_CONFIG; exports.NgxNetworkModule = NgxNetworkModule; exports.NgxNetworkService = NgxNetworkService; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=ngx-network.umd.js.map