-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
abort.service.ts
154 lines (136 loc) · 3.33 KB
/
abort.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { HttpContextToken, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { from, Observable, Subject } from 'rxjs';
import { ContextInterface, StoreInterface } from './abort.interface';
@Injectable()
export class AbortService
{
protected defaultContext : ContextInterface =
{
method: null,
lifetime: null
};
protected token : HttpContextToken<ContextInterface> = new HttpContextToken<ContextInterface>(() => this.defaultContext);
protected store : Map<string, StoreInterface> = new Map();
/**
* get the token of the context
*
* @since 6.0.0
*
* @return {HttpContextToken<ContextInterface>} token of the context
*/
public getToken() : HttpContextToken<ContextInterface>
{
return this.token;
}
/**
* get the signal of the request
*
* @since 4.0.0
*
* @param {HttpRequest<T>} request instance of the http request
*
* @return {Observable<boolean>} signal of the request
*/
public get<T>(request : HttpRequest<T>) : Observable<boolean>
{
if (!this.has(request))
{
this.set(request);
}
return this.store.get(request.urlWithParams).signal;
}
/**
* set the signal and timeout for the request
*
* @since 4.0.0
*
* @param {HttpRequest<T>} request instance of the http request
*
* @return {this} instance of the service
*/
public set<T>(request : HttpRequest<T>) : this
{
const context : ContextInterface = request.context.get(this.getToken());
if (this.has(request))
{
clearTimeout(this.store.get(request.urlWithParams).timeout);
}
this.store.set(request.urlWithParams,
{
signal: new Subject<boolean>(),
timeout: context.lifetime > 0 ? setTimeout(() => this.abort(request.urlWithParams), context.lifetime) : null
});
this.store.get(request.urlWithParams).signal.next(true);
return this;
}
/**
* has a signal and timeout for the request
*
* @since 4.0.0
*
* @param {HttpRequest<T>} request instance of the http request
*
* @return {boolean}
*/
public has<T>(request : HttpRequest<T>) : boolean
{
return this.store.has(request.urlWithParams);
}
/**
* abort a single request for enabled services
*
* @since 4.0.0
*
* @param {string} urlWithParams url with parameters
*
* @return {this} instance of the service
*/
public abort(urlWithParams : string) : this
{
if (this.store.has(urlWithParams))
{
clearTimeout(this.store.get(urlWithParams).timeout);
this.store.get(urlWithParams).signal.next(false);
this.store.delete(urlWithParams);
}
return this;
}
/**
* abort many requests for enabled services
*
* @since 4.1.0
*
* @param {string} url url of the request
*
* @return {this} instance of the service
*/
public abortMany(url : string) : this
{
this.store.forEach((value, urlWithParams) => urlWithParams.startsWith(url) ? this.abort(urlWithParams) : null);
return this;
}
/**
* abort all requests for enabled services
*
* @since 4.0.0
*
* @return {this} instance of the service
*/
public abortAll() : this
{
this.store.forEach((value, urlWithParams) => this.abort(urlWithParams));
return this;
}
/**
* observe all requests for enabled services
*
* @since 4.1.0
*
* @return {Observable<[string, StoreInterface]>} collection of signal and timeout
*/
public observeAll() : Observable<[string, StoreInterface]>
{
return from(this.store);
}
}