diff --git a/packages/operators/src/request.test.js b/packages/operators/src/request.test.js index 9c0d82a..85adbfd 100644 --- a/packages/operators/src/request.test.js +++ b/packages/operators/src/request.test.js @@ -9,10 +9,10 @@ import { test, describe, beforeEach, expect, vi, afterAll, beforeAll } from 'vit import { log, logResult } from './log.js'; import { resolveBlob, resolveJSON } from './response.js'; -import { EstimateTime } from './stream/observables/EstimateTime.js'; -import { Progress } from './stream/observables/Progress.js'; -import { TransferRate } from './stream/observables/TransferRate.js'; -import { MBYTE, SECOND } from './stream/observables/utils.js'; +import { EstimateTime } from './stream/stats/EstimateTime.js'; +import { Progress } from './stream/stats/Progress.js'; +import { TransferRate } from './stream/stats/TransferRate.js'; +import { MBYTE, SECOND } from './stream/stats/utils.js'; describe.skip('request', () => { let testScheduler; @@ -176,13 +176,13 @@ describe.skip('request - demo ', () => { body: formData }); - const progressUpload = Progress(); + const progressUpload = new Progress(); progressUpload.subscribe({ next: e => console.log('UPLOAD', e), complete: () => console.log('complete') }); - const progressDownload = Progress(); + const progressDownload = new Progress(); progressDownload.subscribe({ next: e => console.log('DOWNLOAD', e), complete: () => console.log('complete') @@ -205,13 +205,13 @@ describe('test', () => { test('progress on download', async () => { const { request } = await import('./request.js'); - const progress = Progress(); + const progress = new Progress(); progress.subscribe({ next: e => console.log('DOWNLOAD', e) }); - const byteRate = TransferRate(MBYTE, SECOND); + const byteRate = new TransferRate(MBYTE, SECOND); byteRate.subscribe({ next: e => console.log('RATE', e) }); - const estimateTime = EstimateTime(SECOND); + const estimateTime = new EstimateTime(SECOND); estimateTime.subscribe({ next: e => console.log('ESTIMATE', e) }); const fileMap = { diff --git a/packages/operators/src/stream/observables/EstimateTime.js b/packages/operators/src/stream/stats/EstimateTime.js similarity index 59% rename from packages/operators/src/stream/observables/EstimateTime.js rename to packages/operators/src/stream/stats/EstimateTime.js index 1891a51..de19caf 100644 --- a/packages/operators/src/stream/observables/EstimateTime.js +++ b/packages/operators/src/stream/stats/EstimateTime.js @@ -2,15 +2,18 @@ import { concatWith, distinctUntilChanged, map, of, Subject } from 'rxjs'; import { calcReceivedStats, MSECOND } from './utils'; -export const EstimateTime = (timeUnit = MSECOND) => { - return new Subject().pipe( - calcReceivedStats(), - calcEstimatedTime(), - concatWith(of(0)), - distinctUntilChanged(), - convertEstimedTimeTo(timeUnit) - ); -}; +export class EstimateTime extends Subject { + constructor(timeUnit = MSECOND) { + super(); + return this.pipe( + calcReceivedStats(), + calcEstimatedTime(), + concatWith(of(0)), + distinctUntilChanged(), + convertEstimedTimeTo(timeUnit) + ); + } +} const calcEstimatedTime = () => { return source => diff --git a/packages/operators/src/stream/observables/Progress.js b/packages/operators/src/stream/stats/Progress.js similarity index 53% rename from packages/operators/src/stream/observables/Progress.js rename to packages/operators/src/stream/stats/Progress.js index 31082b0..6c1d2e5 100644 --- a/packages/operators/src/stream/observables/Progress.js +++ b/packages/operators/src/stream/stats/Progress.js @@ -2,14 +2,17 @@ import { concatWith, distinctUntilChanged, map, of, Subject } from 'rxjs'; import { calcReceivedStats } from './utils'; -export const Progress = () => { - return new Subject().pipe( - calcReceivedStats(), - calcPercentageProgress(), - concatWith(of(100)), - distinctUntilChanged() - ); -}; +export class Progress extends Subject { + constructor() { + super(); + return this.pipe( + calcReceivedStats(), + calcPercentageProgress(), + concatWith(of(100)), + distinctUntilChanged() + ); + } +} const calcPercentageProgress = () => { return source => source.pipe(map(({ value, total }) => Math.floor((value / total) * 100))); diff --git a/packages/operators/src/stream/observables/TransferRate.js b/packages/operators/src/stream/stats/TransferRate.js similarity index 58% rename from packages/operators/src/stream/observables/TransferRate.js rename to packages/operators/src/stream/stats/TransferRate.js index 71e105e..a1c42e5 100644 --- a/packages/operators/src/stream/observables/TransferRate.js +++ b/packages/operators/src/stream/stats/TransferRate.js @@ -2,13 +2,16 @@ import { map, Subject } from 'rxjs'; import { calcReceivedStats, MBIT, SECOND } from './utils'; -export const TransferRate = (byteUnit = MBIT, timeUnit = SECOND) => { - return new Subject().pipe( - calcReceivedStats(), - calcAverageByteLengthPerTimeUnit(timeUnit), - calcTransferRate(byteUnit) - ); -}; +export class TransferRate extends Subject { + constructor(byteUnit = MBIT, timeUnit = SECOND) { + super(); + return this.pipe( + calcReceivedStats(), + calcAverageByteLengthPerTimeUnit(timeUnit), + calcTransferRate(byteUnit) + ); + } +} const calcAverageByteLengthPerTimeUnit = timeRatio => { return source => source.pipe(map(({ value, period }) => (value / period) * timeRatio)); diff --git a/packages/operators/src/stream/observables/utils.js b/packages/operators/src/stream/stats/utils.js similarity index 100% rename from packages/operators/src/stream/observables/utils.js rename to packages/operators/src/stream/stats/utils.js