Skip to content

Commit 9eeb831

Browse files
committed
Add parameter to cancel request
This commit adds a parameter to each operation to cancel a previous running request. Default value of this parameter is `true`. Resolves: #25
1 parent 74296cb commit 9eeb831

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

src/mustache/api.service.mustache

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ export class {{classname}} {
134134
{{#allParams}}* @param {{paramName}} {{description}}
135135
{{/allParams}}{{#useHttpClient}}* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
136136
* @param reportProgress flag to report request and response progress.{{/useHttpClient}}
137+
* @param cancelPreviousRequest set whether or not the previous request for the same operation should be cancelled if it is still running.
137138
*/
138139
{{#useHttpClient}}
139-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe?: 'body', reportProgress?: boolean): Observable<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>;
140-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
141-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
142-
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
140+
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe?: 'body', reportProgress?: boolean, cancelPreviousRequest?: boolean): Observable<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>;
141+
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe?: 'response', reportProgress?: boolean, cancelPreviousRequest?: boolean): Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
142+
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe?: 'events', reportProgress?: boolean, cancelPreviousRequest?: boolean): Observable<HttpEvent<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
143+
public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}observe: any = 'body', reportProgress: boolean = false, cancelPreviousRequest: boolean = true): Observable<any> {
143144
{{/useHttpClient}}
144145
{{^useHttpClient}}
145-
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: RequestOptionsArgs): Observable<Response> {
146+
public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: RequestOptionsArgs, cancelPreviousRequest: boolean = true): Observable<Response> {
146147
{{/useHttpClient}}
147148
{{#allParams}}
148149
{{#required}}
@@ -333,15 +334,19 @@ export class {{classname}} {
333334
reportProgress: reportProgress
334335
}
335336
);
336-
if (this.cancelMap['{{operationIdOriginal}}']) {
337-
this.cancelMap['{{operationIdOriginal}}'].next();
338-
this.cancelMap['{{operationIdOriginal}}'].complete();
339-
this.cancelMap['{{operationIdOriginal}}'] = null;
340-
}
341-
this.cancelMap['{{operationIdOriginal}}'] = '{{httpMethod}}'.toUpperCase() === 'GET' ? new Subject<any>() : null;
342-
if(this.cancelMap['{{operationIdOriginal}}']) {
343-
handle = handle.pipe(takeUntil(this.cancelMap['{{operationIdOriginal}}']));
337+
338+
if (cancelPreviousRequest) {
339+
if (this.cancelMap['{{operationIdOriginal}}']) {
340+
this.cancelMap['{{operationIdOriginal}}'].next();
341+
this.cancelMap['{{operationIdOriginal}}'].complete();
342+
this.cancelMap['{{operationIdOriginal}}'] = null;
343+
}
344+
this.cancelMap['{{operationIdOriginal}}'] = '{{httpMethod}}'.toUpperCase() === 'GET' ? new Subject<any>() : null;
345+
if(this.cancelMap['{{operationIdOriginal}}']) {
346+
handle = handle.pipe(takeUntil(this.cancelMap['{{operationIdOriginal}}']));
347+
}
344348
}
349+
345350
if(typeof this.configuration.errorHandler === 'function') {
346351
return handle.pipe(catchError(err => this.configuration.errorHandler(err, '{{operationIdOriginal}}')));
347352
}
@@ -372,14 +377,16 @@ export class {{classname}} {
372377

373378
let handle = this.http.request(`${this.configuration.basePath}{{{path}}}`, requestOptions);
374379

375-
if (this.cancelMap['{{operationIdOriginal}}']) {
376-
this.cancelMap['{{operationIdOriginal}}'].next();
377-
this.cancelMap['{{operationIdOriginal}}'].complete();
378-
this.cancelMap['{{operationIdOriginal}}'] = null;
379-
}
380-
this.cancelMap['{{operationIdOriginal}}'] = '{{httpMethod}}'.toUpperCase() === 'GET' ? new Subject<any>() : null;
381-
if(this.cancelMap['{{operationIdOriginal}}']) {
382-
handle = handle.takeUntil(this.cancelMap['{{operationIdOriginal}}']);
380+
if (cancelPreviousRequest) {
381+
if (this.cancelMap['{{operationIdOriginal}}']) {
382+
this.cancelMap['{{operationIdOriginal}}'].next();
383+
this.cancelMap['{{operationIdOriginal}}'].complete();
384+
this.cancelMap['{{operationIdOriginal}}'] = null;
385+
}
386+
this.cancelMap['{{operationIdOriginal}}'] = '{{httpMethod}}'.toUpperCase() === 'GET' ? new Subject<any>() : null;
387+
if(this.cancelMap['{{operationIdOriginal}}']) {
388+
handle = handle.takeUntil(this.cancelMap['{{operationIdOriginal}}']);
389+
}
383390
}
384391

385392
if(typeof this.configuration.errorHandler === 'function') {

src/mustache/apiCallByPartialMap.mustache

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,33 @@
99
* @param map parameters map to set partial amount of parameters easily
1010
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
1111
* @param reportProgress flag to report request and response progress.
12+
* @param cancelPreviousRequest set whether or not the previous request for the same operation should be cancelled if it is still running.
1213
*/
1314
public {{nickname}}ByMap(
1415
map: {{operationIdCamelCase}}.PartialParamMap,
1516
observe?: 'body',
16-
reportProgress?: boolean): Observable<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>;
17+
reportProgress?: boolean,
18+
cancelPreviousRequest?: boolean): Observable<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>;
1719
public {{nickname}}ByMap(
1820
map: {{operationIdCamelCase}}.PartialParamMap,
1921
observe?: 'response',
20-
reportProgress?: boolean): Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
22+
reportProgress?: boolean,
23+
cancelPreviousRequest?: boolean): Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
2124
public {{nickname}}ByMap(
2225
map: {{operationIdCamelCase}}.PartialParamMap,
2326
observe?: 'events',
24-
reportProgress?: boolean): Observable<HttpEvent<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
27+
reportProgress?: boolean,
28+
cancelPreviousRequest?: boolean): Observable<HttpEvent<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>>;
2529
public {{nickname}}ByMap(
2630
map: {{operationIdCamelCase}}.PartialParamMap,
2731
observe: any = 'body',
28-
reportProgress: boolean = false): Observable<any> {
32+
reportProgress: boolean = false,
33+
cancelPreviousRequest: boolean = true): Observable<any> {
2934
return this.{{nickname}}({{#allParams}}
3035
map.{{paramName}},{{/allParams}}
3136
observe,
32-
reportProgress
37+
reportProgress,
38+
cancelPreviousRequest
3339
);
3440
}
3541
{{/useHttpClient}}

0 commit comments

Comments
 (0)