From 2be3d81c8090649aa8bc1cb81585e233936fc575 Mon Sep 17 00:00:00 2001 From: Henry Ruhs Date: Sat, 10 Sep 2022 11:11:46 +0200 Subject: [PATCH] Use BehaviorSubject and reduce potential null pointer exceptions (#49) * Use BehaviorSubject and reduce potential null pointer exceptions * Fix CI * Revert dependencies * Not sure what is wrong with node18 --- .github/workflows/ci.yml | 4 ++-- package.json | 2 +- src/abort/abort.interface.ts | 4 ++-- src/abort/abort.service.ts | 12 +++++------- src/cache/cache.interceptor.ts | 2 +- src/cache/cache.service.ts | 4 ++-- src/observe/observe.interface.ts | 4 ++-- src/observe/observe.service.ts | 9 ++++----- 8 files changed, 19 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1292c2c..e506ea7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [ 15, 16, 17, 18 ] + node-version: [ 15, 16, 17 ] steps: - name: Checkout uses: actions/checkout@v3 @@ -37,7 +37,7 @@ jobs: uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - - run: npm install --legacy-peer-deps + - run: npm install - run: npm run test report: needs: test diff --git a/package.json b/package.json index d16b9eb..e2367b9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ngx-crud", "description": "CRUD services in Angular with effortless aborting, caching and observing", - "version": "12.0.0", + "version": "12.1.0-beta.1", "homepage": "https://ngx-crud.com", "license": "MIT", "type": "module", diff --git a/src/abort/abort.interface.ts b/src/abort/abort.interface.ts index 096fd9c..b946c6d 100644 --- a/src/abort/abort.interface.ts +++ b/src/abort/abort.interface.ts @@ -1,9 +1,9 @@ -import { Subject, Subscription } from 'rxjs'; +import { BehaviorSubject, Subscription } from 'rxjs'; import { UniversalMethod } from '../common'; export interface Store { - controller : Subject; + controller : BehaviorSubject; timer : Subscription; } diff --git a/src/abort/abort.service.ts b/src/abort/abort.service.ts index 139051f..975aa45 100644 --- a/src/abort/abort.service.ts +++ b/src/abort/abort.service.ts @@ -1,6 +1,6 @@ import { HttpContextToken, HttpRequest } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import { Observable, Subject, Subscription, filter, from, timer, mergeMap } from 'rxjs'; +import { Observable, BehaviorSubject, Subscription, filter, from, timer, mergeMap } from 'rxjs'; import { ReactiveMap } from 'rxjs-collection'; import { Context, Store } from './abort.interface'; import { stripUrlParams } from '../common'; @@ -41,10 +41,9 @@ export class AbortService } this.store.set(request.urlWithParams, { - controller: new Subject(), + controller: new BehaviorSubject(new AbortController()), timer: context.lifetime > 0 ? timer(context.lifetime).subscribe(() => this.abort(request.urlWithParams)) : new Subscription() }); - this.store.get(request.urlWithParams).controller.next(new AbortController()); return this; } @@ -57,8 +56,7 @@ export class AbortService { if (this.store.has(urlWithParams)) { - this.store.get(urlWithParams).controller.subscribe(controller => controller.abort()); - this.store.get(urlWithParams).controller.next(new AbortController()); + this.store.get(urlWithParams).controller.getValue().abort(); this.store.get(urlWithParams).timer.unsubscribe(); this.store.delete(urlWithParams); } @@ -79,12 +77,12 @@ export class AbortService observe(urlWithParams : string) : Observable<[string, Store]> { - return this.observeAll().pipe(filter(value => value[0] === urlWithParams)); + return this.observeAll().pipe(filter(([ value ] : [ string, Store ]) => value === urlWithParams)); } observeMany(url : string) : Observable<[string, Store]> { - return this.observeAll().pipe(filter(value => stripUrlParams(value[0]) === url)); + return this.observeAll().pipe(filter(([ value ] : [ string, Store ]) => stripUrlParams(value) === url)); } observeAll() : Observable<[string, Store]> diff --git a/src/cache/cache.interceptor.ts b/src/cache/cache.interceptor.ts index 1f9ad58..514a4c1 100644 --- a/src/cache/cache.interceptor.ts +++ b/src/cache/cache.interceptor.ts @@ -7,7 +7,7 @@ import HttpResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; -import { Observable, of, ReplaySubject } from 'rxjs'; +import { Observable, ReplaySubject, of } from 'rxjs'; import { filter, share, tap } from 'rxjs/operators'; import { Context } from './cache.interface'; import { CacheService } from './cache.service'; diff --git a/src/cache/cache.service.ts b/src/cache/cache.service.ts index 6051365..0b91691 100644 --- a/src/cache/cache.service.ts +++ b/src/cache/cache.service.ts @@ -76,12 +76,12 @@ export class CacheService observe(urlWithParams : string) : Observable<[string, Store]> { - return this.observeAll().pipe(filter(value => value[0] === urlWithParams)); + return this.observeAll().pipe(filter(([ value ] : [ string, Store ]) => value === urlWithParams)); } observeMany(url : string) : Observable<[string, Store]> { - return this.observeAll().pipe(filter(value => stripUrlParams(value[0]) === url)); + return this.observeAll().pipe(filter(([ value ] : [ string, Store ]) => stripUrlParams(value) === url)); } observeAll() : Observable<[string, Store]> diff --git a/src/observe/observe.interface.ts b/src/observe/observe.interface.ts index dc9e5ed..a61d210 100644 --- a/src/observe/observe.interface.ts +++ b/src/observe/observe.interface.ts @@ -1,11 +1,11 @@ import { HttpErrorResponse, HttpRequest, HttpResponse } from '@angular/common/http'; -import { Subject, Subscription } from 'rxjs'; +import { BehaviorSubject, Subscription } from 'rxjs'; import { UniversalMethod } from '../common'; import { ObserveStatus } from './observe.type'; export interface Store { - status : Subject; + status : BehaviorSubject; timer : Subscription; } diff --git a/src/observe/observe.service.ts b/src/observe/observe.service.ts index 1f3503d..fb62b44 100644 --- a/src/observe/observe.service.ts +++ b/src/observe/observe.service.ts @@ -1,6 +1,6 @@ import { HttpContextToken, HttpErrorResponse, HttpRequest, HttpResponse } from '@angular/common/http'; import { Optional, Inject, Injectable } from '@angular/core'; -import { Observable, Subject, Subscription, filter, from, timer, mergeMap } from 'rxjs'; +import { Observable, BehaviorSubject, Subscription, filter, from, timer, mergeMap } from 'rxjs'; import { ReactiveMap } from 'rxjs-collection'; import { ObserveAfterEffect, ObserveBeforeEffect, Context, Store } from './observe.interface'; import { OBSERVE_EFFECT } from './observe.token'; @@ -38,10 +38,9 @@ export class ObserveService } this.store.set(request.urlWithParams, { - status: new Subject(), + status: new BehaviorSubject('STARTED'), timer: context.lifetime > 0 ? timer(context.lifetime).subscribe(() => this.complete(request.urlWithParams)) : new Subscription() }); - this.store.get(request.urlWithParams).status.next('STARTED'); return this; } @@ -102,12 +101,12 @@ export class ObserveService observe(urlWithParams : string) : Observable<[string, Store]> { - return this.observeAll().pipe(filter(value => value[0] === urlWithParams)); + return this.observeAll().pipe(filter(([ value ] : [ string, Store ]) => value === urlWithParams)); } observeMany(url : string) : Observable<[string, Store]> { - return this.observeAll().pipe(filter(value => stripUrlParams(value[0]) === url)); + return this.observeAll().pipe(filter(([ value ] : [ string, Store ]) => stripUrlParams(value) === url)); } observeAll() : Observable<[string, Store]>