From 59e84fe0f76b29a4082825fcd11f8f26c64892ae Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Fri, 25 Oct 2024 12:28:12 +0000 Subject: [PATCH 1/2] chore(release): 1.0.4 [skip ci] # [@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)) --- packages/operators/CHANGELOG.md | 8 ++++++++ packages/operators/package.json | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/operators/CHANGELOG.md b/packages/operators/CHANGELOG.md index ae2ed7a..2ffe427 100644 --- a/packages/operators/CHANGELOG.md +++ b/packages/operators/CHANGELOG.md @@ -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) diff --git a/packages/operators/package.json b/packages/operators/package.json index d8aae52..d8a1827 100644 --- a/packages/operators/package.json +++ b/packages/operators/package.json @@ -1,6 +1,6 @@ { "name": "@rxjs-collection/operators", - "version": "1.0.4-beta.1", + "version": "1.0.4", "description": "rxjs operators", "license": "MIT", "contributors": [ From 9063f6e9fc85f5d544af4e6d92d1d60e32cccf94 Mon Sep 17 00:00:00 2001 From: StephanGerbeth Date: Fri, 25 Oct 2024 15:07:32 +0200 Subject: [PATCH 2/2] fix(fetch): implemented sonar optimizations --- .../operators/src/fetch/autoPagination.js | 24 ++++++++++-------- packages/operators/src/fetch/retry.js | 25 +++++++++++-------- packages/operators/src/fetch/retry.test.js | 2 +- packages/operators/src/log.js | 12 ++++----- packages/operators/vitest.config.js | 1 + 5 files changed, 35 insertions(+), 29 deletions(-) diff --git a/packages/operators/src/fetch/autoPagination.js b/packages/operators/src/fetch/autoPagination.js index c9878e4..94b2512 100644 --- a/packages/operators/src/fetch/autoPagination.js +++ b/packages/operators/src/fetch/autoPagination.js @@ -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() + ) + ) + ); +}; diff --git a/packages/operators/src/fetch/retry.js b/packages/operators/src/fetch/retry.js index 81f67c7..18d551a 100644 --- a/packages/operators/src/fetch/retry.js +++ b/packages/operators/src/fetch/retry.js @@ -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)) ); }; }; + +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`)), + delay(timeout(counter) || timeout) + ); +}; diff --git a/packages/operators/src/fetch/retry.test.js b/packages/operators/src/fetch/retry.test.js index f9854b2..fee16e3 100644 --- a/packages/operators/src/fetch/retry.test.js +++ b/packages/operators/src/fetch/retry.test.js @@ -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 }) ); diff --git a/packages/operators/src/log.js b/packages/operators/src/log.js index 535d267..553bf54 100644 --- a/packages/operators/src/log.js +++ b/packages/operators/src/log.js @@ -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 => { @@ -19,8 +19,8 @@ export const log = (active = true) => { } ); }); - } else { - return source; - } - }; + }; + } else { + return source => source; + } }; diff --git a/packages/operators/vitest.config.js b/packages/operators/vitest.config.js index b34b1fe..42a246b 100644 --- a/packages/operators/vitest.config.js +++ b/packages/operators/vitest.config.js @@ -3,6 +3,7 @@ import { defineProject } from 'vitest/config'; export default defineProject({ test: { setupFiles: ['../../setup.js'], + testTimeout: 10000, environment: 'happy-dom' } });