Skip to content

Commit

Permalink
fix(request): updated request stats observables
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanGerbeth committed Jan 10, 2025
1 parent 683164c commit 8c666a7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
18 changes: 9 additions & 9 deletions packages/operators/src/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),

Check warning on line 181 in packages/operators/src/request.test.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

Unexpected console statement
complete: () => console.log('complete')

Check warning on line 182 in packages/operators/src/request.test.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

Unexpected console statement
});

const progressDownload = Progress();
const progressDownload = new Progress();
progressDownload.subscribe({
next: e => console.log('DOWNLOAD', e),

Check warning on line 187 in packages/operators/src/request.test.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

Unexpected console statement
complete: () => console.log('complete')

Check warning on line 188 in packages/operators/src/request.test.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

Unexpected console statement
Expand All @@ -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) });

Check warning on line 209 in packages/operators/src/request.test.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

Unexpected console statement

const byteRate = TransferRate(MBYTE, SECOND);
const byteRate = new TransferRate(MBYTE, SECOND);
byteRate.subscribe({ next: e => console.log('RATE', e) });

Check warning on line 212 in packages/operators/src/request.test.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

Unexpected console statement

const estimateTime = EstimateTime(SECOND);
const estimateTime = new EstimateTime(SECOND);
estimateTime.subscribe({ next: e => console.log('ESTIMATE', e) });

Check warning on line 215 in packages/operators/src/request.test.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

Unexpected console statement

const fileMap = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 8c666a7

Please sign in to comment.