Skip to content

Commit 7586ab0

Browse files
authored
Merge pull request #24 from T-Systems-MMS/delay-cancel-request
Delay cancel request
2 parents 5a26bf2 + 3283bc1 commit 7586ab0

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/mustache/api.service.mustache

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import { CustomQueryEncoderHelper } from '../encoder';
2020

2121
{{^useRxJS6}}
2222
import { Observable } from 'rxjs/Observable';
23+
import { Subject } from 'rxjs/Subject';
2324
{{/useRxJS6}}
2425
{{#useRxJS6}}
25-
import { Observable } from 'rxjs';
26-
import { catchError } from 'rxjs/operators';
26+
import { Observable, Subject } from 'rxjs';
27+
import { catchError, takeUntil } from 'rxjs/operators';
2728
{{/useRxJS6}}
2829
{{^useHttpClient}}
2930
import '../rxjs-operators';
31+
import 'rxjs/add/operator/takeUntil';
3032
{{/useHttpClient}}
3133

3234
{{#imports}}
@@ -65,6 +67,7 @@ export class {{classname}} {
6567
protected basePath = '{{{basePath}}}';
6668
public defaultHeaders = new {{#useHttpClient}}Http{{/useHttpClient}}Headers();
6769
public configuration = new Configuration();
70+
private cancelMap: {[key: string]: Subject<any>} = {};
6871

6972
constructor(protected {{#useHttpClient}}httpClient: HttpClient{{/useHttpClient}}{{^useHttpClient}}http: Http{{/useHttpClient}}, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
7073
@@ -315,7 +318,7 @@ export class {{classname}} {
315318

316319
{{/hasFormParams}}
317320
{{#useHttpClient}}
318-
const handle = this.httpClient.{{httpMethod}}{{^isResponseFile}}<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>{{/isResponseFile}}(`${this.configuration.basePath}{{{path}}}`,{{#isBodyAllowed}}
321+
let handle = this.httpClient.{{httpMethod}}{{^isResponseFile}}<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>{{/isResponseFile}}(`${this.configuration.basePath}{{{path}}}`,{{#isBodyAllowed}}
319322
{{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}{{#hasFormParams}}convertFormParamsToString ? formParams.toString() : formParams{{/hasFormParams}}{{^hasFormParams}}null{{/hasFormParams}}{{/bodyParam}},{{/isBodyAllowed}}
320323
{
321324
{{#hasQueryParams}}
@@ -330,6 +333,15 @@ export class {{classname}} {
330333
reportProgress: reportProgress
331334
}
332335
);
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}}']));
344+
}
333345
if(typeof this.configuration.errorHandler === 'function') {
334346
return handle.pipe(catchError(err => this.configuration.errorHandler(err, '{{operationIdOriginal}}')));
335347
}
@@ -358,7 +370,18 @@ export class {{classname}} {
358370
requestOptions = (<any>Object).assign(requestOptions, extraHttpRequestParams);
359371
}
360372

361-
const handle = this.http.request(`${this.configuration.basePath}{{{path}}}`, requestOptions);
373+
let handle = this.http.request(`${this.configuration.basePath}{{{path}}}`, requestOptions);
374+
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}}']);
383+
}
384+
362385
if(typeof this.configuration.errorHandler === 'function') {
363386
return handle.catch(err => this.configuration.errorHandler(err, '{{operationIdOriginal}}'));
364387
}

0 commit comments

Comments
 (0)