Skip to content

Commit 8c666a7

Browse files
fix(request): updated request stats observables
1 parent 683164c commit 8c666a7

File tree

5 files changed

+42
-33
lines changed

5 files changed

+42
-33
lines changed

packages/operators/src/request.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { test, describe, beforeEach, expect, vi, afterAll, beforeAll } from 'vit
99

1010
import { log, logResult } from './log.js';
1111
import { resolveBlob, resolveJSON } from './response.js';
12-
import { EstimateTime } from './stream/observables/EstimateTime.js';
13-
import { Progress } from './stream/observables/Progress.js';
14-
import { TransferRate } from './stream/observables/TransferRate.js';
15-
import { MBYTE, SECOND } from './stream/observables/utils.js';
12+
import { EstimateTime } from './stream/stats/EstimateTime.js';
13+
import { Progress } from './stream/stats/Progress.js';
14+
import { TransferRate } from './stream/stats/TransferRate.js';
15+
import { MBYTE, SECOND } from './stream/stats/utils.js';
1616

1717
describe.skip('request', () => {
1818
let testScheduler;
@@ -176,13 +176,13 @@ describe.skip('request - demo ', () => {
176176
body: formData
177177
});
178178

179-
const progressUpload = Progress();
179+
const progressUpload = new Progress();
180180
progressUpload.subscribe({
181181
next: e => console.log('UPLOAD', e),
182182
complete: () => console.log('complete')
183183
});
184184

185-
const progressDownload = Progress();
185+
const progressDownload = new Progress();
186186
progressDownload.subscribe({
187187
next: e => console.log('DOWNLOAD', e),
188188
complete: () => console.log('complete')
@@ -205,13 +205,13 @@ describe('test', () => {
205205
test('progress on download', async () => {
206206
const { request } = await import('./request.js');
207207

208-
const progress = Progress();
208+
const progress = new Progress();
209209
progress.subscribe({ next: e => console.log('DOWNLOAD', e) });
210210

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

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

217217
const fileMap = {

packages/operators/src/stream/observables/EstimateTime.js renamed to packages/operators/src/stream/stats/EstimateTime.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import { concatWith, distinctUntilChanged, map, of, Subject } from 'rxjs';
22

33
import { calcReceivedStats, MSECOND } from './utils';
44

5-
export const EstimateTime = (timeUnit = MSECOND) => {
6-
return new Subject().pipe(
7-
calcReceivedStats(),
8-
calcEstimatedTime(),
9-
concatWith(of(0)),
10-
distinctUntilChanged(),
11-
convertEstimedTimeTo(timeUnit)
12-
);
13-
};
5+
export class EstimateTime extends Subject {
6+
constructor(timeUnit = MSECOND) {
7+
super();
8+
return this.pipe(
9+
calcReceivedStats(),
10+
calcEstimatedTime(),
11+
concatWith(of(0)),
12+
distinctUntilChanged(),
13+
convertEstimedTimeTo(timeUnit)
14+
);
15+
}
16+
}
1417

1518
const calcEstimatedTime = () => {
1619
return source =>

packages/operators/src/stream/observables/Progress.js renamed to packages/operators/src/stream/stats/Progress.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import { concatWith, distinctUntilChanged, map, of, Subject } from 'rxjs';
22

33
import { calcReceivedStats } from './utils';
44

5-
export const Progress = () => {
6-
return new Subject().pipe(
7-
calcReceivedStats(),
8-
calcPercentageProgress(),
9-
concatWith(of(100)),
10-
distinctUntilChanged()
11-
);
12-
};
5+
export class Progress extends Subject {
6+
constructor() {
7+
super();
8+
return this.pipe(
9+
calcReceivedStats(),
10+
calcPercentageProgress(),
11+
concatWith(of(100)),
12+
distinctUntilChanged()
13+
);
14+
}
15+
}
1316

1417
const calcPercentageProgress = () => {
1518
return source => source.pipe(map(({ value, total }) => Math.floor((value / total) * 100)));

packages/operators/src/stream/observables/TransferRate.js renamed to packages/operators/src/stream/stats/TransferRate.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import { map, Subject } from 'rxjs';
22

33
import { calcReceivedStats, MBIT, SECOND } from './utils';
44

5-
export const TransferRate = (byteUnit = MBIT, timeUnit = SECOND) => {
6-
return new Subject().pipe(
7-
calcReceivedStats(),
8-
calcAverageByteLengthPerTimeUnit(timeUnit),
9-
calcTransferRate(byteUnit)
10-
);
11-
};
5+
export class TransferRate extends Subject {
6+
constructor(byteUnit = MBIT, timeUnit = SECOND) {
7+
super();
8+
return this.pipe(
9+
calcReceivedStats(),
10+
calcAverageByteLengthPerTimeUnit(timeUnit),
11+
calcTransferRate(byteUnit)
12+
);
13+
}
14+
}
1215

1316
const calcAverageByteLengthPerTimeUnit = timeRatio => {
1417
return source => source.pipe(map(({ value, period }) => (value / period) * timeRatio));

0 commit comments

Comments
 (0)