Skip to content

Commit

Permalink
Use BehaviorSubject and reduce potential null pointer exceptions (#49)
Browse files Browse the repository at this point in the history
* Use BehaviorSubject and reduce potential null pointer exceptions

* Fix CI

* Revert dependencies

* Not sure what is wrong with node18
  • Loading branch information
henryruhs committed Sep 10, 2022
1 parent 59bddea commit 2be3d81
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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
- name: Set up Node ${{ matrix.node-version }}
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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/abort/abort.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Subject, Subscription } from 'rxjs';
import { BehaviorSubject, Subscription } from 'rxjs';
import { UniversalMethod } from '../common';

export interface Store
{
controller : Subject<AbortController>;
controller : BehaviorSubject<AbortController>;
timer : Subscription;
}

Expand Down
12 changes: 5 additions & 7 deletions src/abort/abort.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -41,10 +41,9 @@ export class AbortService
}
this.store.set(request.urlWithParams,
{
controller: new Subject<AbortController>(),
controller: new BehaviorSubject<AbortController>(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;
}

Expand All @@ -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);
}
Expand All @@ -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]>
Expand Down
2 changes: 1 addition & 1 deletion src/cache/cache.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/cache/cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
Expand Down
4 changes: 2 additions & 2 deletions src/observe/observe.interface.ts
Original file line number Diff line number Diff line change
@@ -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<ObserveStatus>;
status : BehaviorSubject<ObserveStatus>;
timer : Subscription;
}

Expand Down
9 changes: 4 additions & 5 deletions src/observe/observe.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -38,10 +38,9 @@ export class ObserveService
}
this.store.set(request.urlWithParams,
{
status: new Subject<ObserveStatus>(),
status: new BehaviorSubject<ObserveStatus>('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;
}

Expand Down Expand Up @@ -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]>
Expand Down

0 comments on commit 2be3d81

Please sign in to comment.