Skip to content

Commit

Permalink
Merge pull request #71 from basics/feature/fetch-sonar-optimization
Browse files Browse the repository at this point in the history
Feature/fetch sonar optimization
  • Loading branch information
StephanGerbeth authored Oct 25, 2024
2 parents 2ebafdc + 9063f6e commit d6bdc01
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
8 changes: 8 additions & 0 deletions packages/operators/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Project Changelog

# [@rxjs-collection/operators-v1.0.4](https://github.com/basics/rxjs-collection/compare/@rxjs-collection/operators-v1.0.3...@rxjs-collection/operators-v1.0.4) (2024-10-25)


### Bug Fixes

* **fetch:** missed optimized code segmentation ([36d7543](https://github.com/basics/rxjs-collection/commit/36d7543e17508c27d4925f3854391e3f65eb6b47))
* **fetch:** optimized code segmentation ([9bcd2f3](https://github.com/basics/rxjs-collection/commit/9bcd2f317b8ee540f0a01e1b21733095a3c836e1))

# [@rxjs-collection/operators-v1.0.4-beta.1](https://github.com/basics/rxjs-collection/compare/@rxjs-collection/operators-v1.0.3...@rxjs-collection/operators-v1.0.4-beta.1) (2024-10-25)


Expand Down
2 changes: 1 addition & 1 deletion packages/operators/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rxjs-collection/operators",
"version": "1.0.4-beta.1",
"version": "1.0.4",
"description": "rxjs operators",
"license": "MIT",
"contributors": [
Expand Down
24 changes: 13 additions & 11 deletions packages/operators/src/fetch/autoPagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import { request } from './request';
export const autoPagination = ({ resolveRoute }) => {
return source =>
source.pipe(
concatMap(({ url }) => {
return from(resolveRoute(url)).pipe(
request(),
expand(resp =>
from(resolveRoute(url, resp)).pipe(
filter(url => !!url),
request()
)
)
);
}),
concatMap(({ url }) => from(resolveRoute(url)).pipe(request(), getNext(resolveRoute, url))),
map(resp => resp.clone())
);
};

const getNext = (resolveRoute, url) => {
return source =>
source.pipe(
expand(resp =>
from(resolveRoute(url, resp)).pipe(
filter(url => !!url),
request()
)
)
);
};
25 changes: 14 additions & 11 deletions packages/operators/src/fetch/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ export const networkRetry = ({ timeout = defaultTimeout, count } = {}) => {
concatMap(resp => (!resp.ok && throwError(() => new Error('invalid request'))) || of(resp)),
retry({
count,
delay: () =>
combineLatest([connectionObservable]).pipe(
// all defined observables have to be valid
map(values => values.every(v => v === true)),
// reset counter if one observable is invalid
tap(valid => (counter = counter * valid)),
// continue only if all observables are valid
filter(valid => valid),
tap(() => console.log(`retry: request - next: ${counter} in ${timeout(counter)}s`)),
delay(timeout(counter++) || timeout)
)
delay: () => determineDelayWhenOnline(timeout, ++counter)
}),
catchError(e => console.error(e))

Check warning on line 28 in packages/operators/src/fetch/retry.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

Unexpected console statement
);
};
};

const determineDelayWhenOnline = (timeout, counter) => {
return combineLatest([connectionObservable]).pipe(
// all defined observables have to be valid
map(values => values.every(v => v === true)),
// reset counter if one observable is invalid
tap(valid => (counter = counter * valid)),
// continue only if all observables are valid
filter(valid => valid),
tap(() => console.log(`retry: request - next: ${counter} in ${timeout(counter)}s`)),

Check warning on line 41 in packages/operators/src/fetch/retry.js

View workflow job for this annotation

GitHub Actions / Install (ubuntu-latest, 20)

Unexpected console statement
delay(timeout(counter) || timeout)
);
};
2 changes: 1 addition & 1 deletion packages/operators/src/fetch/retry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('request retry', function () {
let counter = 0;

const mockObservable = of(null).pipe(
map(() => ({ ok: !(++counter < 3) })),
map(() => ({ ok: ++counter >= 3 })),
networkRetry({ timeout: () => 1000 })
);

Expand Down
12 changes: 6 additions & 6 deletions packages/operators/src/log.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Observable } from 'rxjs';

export const log = (active = true) => {
return source => {
if (active) {
if (active) {
return source => {
return new Observable(observer => {
return source.subscribe(
val => {
Expand All @@ -19,8 +19,8 @@ export const log = (active = true) => {
}
);
});
} else {
return source;
}
};
};
} else {
return source => source;
}
};
1 change: 1 addition & 0 deletions packages/operators/vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineProject } from 'vitest/config';
export default defineProject({
test: {
setupFiles: ['../../setup.js'],
testTimeout: 10000,
environment: 'happy-dom'
}
});

0 comments on commit d6bdc01

Please sign in to comment.