Skip to content

Commit 414ce88

Browse files
authored
Merge pull request #33 from T-Systems-MMS/bugfix/call_after_handler_for_cancelled_requests
add call of after handler for cancelled requests
2 parents 7c76631 + 0d45390 commit 414ce88

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

src/mustache/api.service.mustache

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ import { CustomQueryEncoderHelper } from '../encoder';
2121
{{^useRxJS6}}
2222
import { Observable } from 'rxjs/Observable';
2323
import { Subject } from 'rxjs/Subject';
24+
import { Subscription } from 'rxjs/Subscription';
2425
{{/useRxJS6}}
2526
{{#useRxJS6}}
26-
import { Observable, Subject } from 'rxjs';
27-
import { catchError, takeUntil, tap } from 'rxjs/operators';
27+
import { Observable, Subject, Subscription } from 'rxjs';
28+
import { catchError, takeUntil, tap, share } from 'rxjs/operators';
2829
{{/useRxJS6}}
2930
{{^useHttpClient}}
3031
import '../rxjs-operators';
3132
import 'rxjs/add/operator/takeUntil';
3233
import 'rxjs/add/operator/tap';
34+
import 'rxjs/add/operator/share';
3335
{{/useHttpClient}}
3436

3537
{{#imports}}
@@ -339,8 +341,6 @@ export class {{classname}} {
339341
if (cancelPreviousRequest) {
340342
if (this.cancelMap['{{operationIdOriginal}}']) {
341343
this.cancelMap['{{operationIdOriginal}}'].next();
342-
this.cancelMap['{{operationIdOriginal}}'].complete();
343-
this.cancelMap['{{operationIdOriginal}}'] = null;
344344
}
345345
this.cancelMap['{{operationIdOriginal}}'] = '{{httpMethod}}'.toUpperCase() === 'GET' ? new Subject<any>() : null;
346346
if(this.cancelMap['{{operationIdOriginal}}']) {
@@ -351,12 +351,30 @@ export class {{classname}} {
351351
if (typeof this.configuration.beforeHandler === 'function') {
352352
this.configuration.beforeHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase());
353353
}
354-
if (typeof this.configuration.afterHandler === 'function') {
355-
handle = handle.pipe(tap(
356-
result => this.configuration.afterHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase(), result),
357-
err => this.configuration.afterHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase(), err)
358-
));
359-
}
354+
let afterSubscription: Subscription;
355+
const afterHandler = (result: any = null) => {
356+
if (afterSubscription) {
357+
afterSubscription.unsubscribe();
358+
}
359+
// stop cancellation to prevent calling afterHandler on next service call
360+
if (cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
361+
this.cancelMap['{{operationIdOriginal}}'].complete();
362+
delete this.cancelMap['{{operationIdOriginal}}'];
363+
}
364+
if (typeof this.configuration.afterHandler === 'function') {
365+
this.configuration.afterHandler(
366+
'{{operationIdOriginal}}',
367+
'{{httpMethod}}'.toUpperCase(),
368+
result ? result : new Error('CANCELLED')
369+
);
370+
}
371+
};
372+
handle = handle.pipe(share());
373+
afterSubscription = handle.subscribe(
374+
result => afterHandler(result),
375+
err => afterHandler(err),
376+
() => afterHandler()
377+
);
360378

361379
if (typeof this.configuration.errorHandler === 'function') {
362380
return handle.pipe(catchError(err => this.configuration.errorHandler(err, '{{operationIdOriginal}}')));
@@ -391,8 +409,6 @@ export class {{classname}} {
391409
if (cancelPreviousRequest) {
392410
if (this.cancelMap['{{operationIdOriginal}}']) {
393411
this.cancelMap['{{operationIdOriginal}}'].next();
394-
this.cancelMap['{{operationIdOriginal}}'].complete();
395-
this.cancelMap['{{operationIdOriginal}}'] = null;
396412
}
397413
this.cancelMap['{{operationIdOriginal}}'] = '{{httpMethod}}'.toUpperCase() === 'GET' ? new Subject<any>() : null;
398414
if(this.cancelMap['{{operationIdOriginal}}']) {
@@ -403,12 +419,30 @@ export class {{classname}} {
403419
if (typeof this.configuration.beforeHandler === 'function') {
404420
this.configuration.beforeHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase());
405421
}
406-
if (typeof this.configuration.afterHandler === 'function') {
407-
handle = handle.tap(
408-
result => this.configuration.afterHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase(), result),
409-
err => this.configuration.afterHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase(), err)
410-
);
411-
}
422+
let afterSubscription: Subscription;
423+
const afterHandler = (result: any = null) => {
424+
if (afterSubscription) {
425+
afterSubscription.unsubscribe();
426+
}
427+
// stop cancellation to prevent calling afterHandler on next service call
428+
if (cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
429+
this.cancelMap['{{operationIdOriginal}}'].complete();
430+
delete this.cancelMap['{{operationIdOriginal}}'];
431+
}
432+
if (typeof this.configuration.afterHandler === 'function') {
433+
this.configuration.afterHandler(
434+
'{{operationIdOriginal}}',
435+
'{{httpMethod}}'.toUpperCase(),
436+
result ? result : new Error('CANCELLED')
437+
);
438+
}
439+
};
440+
handle = handle.share();
441+
afterSubscription = handle.subscribe(
442+
result => afterHandler(result),
443+
err => afterHandler(err),
444+
() => afterHandler()
445+
);
412446

413447
if (typeof this.configuration.errorHandler === 'function') {
414448
return handle.catch(err => this.configuration.errorHandler(err, '{{operationIdOriginal}}'));

0 commit comments

Comments
 (0)