Skip to content

Commit 1e5c8f9

Browse files
Stefan SchubertStefan Schubert
authored andcommitted
call after handle every time to complete cancel subject
* add take(1) for listing to cancellation for afterHandler of configuration
1 parent d323318 commit 1e5c8f9

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

src/mustache/api.service.mustache

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ import { Subject } from 'rxjs/Subject'
2424
{{/useRxJS6}}
2525
{{#useRxJS6}}
2626
import { Observable, Subject } from 'rxjs';
27-
import { catchError, takeUntil, tap } from 'rxjs/operators';
27+
import { catchError, takeUntil, tap, take } from 'rxjs/operators';
2828
{{/useRxJS6}}
2929
{{^useHttpClient}}
3030
import '../rxjs-operators';
3131
import 'rxjs/add/operator/takeUntil';
3232
import 'rxjs/add/operator/tap';
33+
import 'rxjs/add/operator/take';
3334
{{/useHttpClient}}
3435

3536
{{#imports}}
@@ -340,7 +341,7 @@ export class {{classname}} {
340341
if (this.cancelMap['{{operationIdOriginal}}']) {
341342
this.cancelMap['{{operationIdOriginal}}'].next();
342343
this.cancelMap['{{operationIdOriginal}}'].complete();
343-
this.cancelMap['{{operationIdOriginal}}'] = null;
344+
delete this.cancelMap['{{operationIdOriginal}}'];
344345
}
345346
this.cancelMap['{{operationIdOriginal}}'] = '{{httpMethod}}'.toUpperCase() === 'GET' ? new Subject<any>() : null;
346347
if(this.cancelMap['{{operationIdOriginal}}']) {
@@ -351,19 +352,19 @@ export class {{classname}} {
351352
if (typeof this.configuration.beforeHandler === 'function') {
352353
this.configuration.beforeHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase());
353354
}
354-
if (typeof this.configuration.afterHandler === 'function') {
355-
const afterHandler = (result: any, stopCancellation = true) => {
356-
if (stopCancellation && cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
357-
// don't call next to prevent second call
358-
this.cancelMap['{{operationIdOriginal}}'].complete();
359-
this.cancelMap['{{operationIdOriginal}}'] = null;
360-
}
355+
const afterHandler = (result: any, stopCancellation = true) => {
356+
// stop cancelation to prevent calling afterHandler on next service call
357+
if (stopCancellation && cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
358+
this.cancelMap['{{operationIdOriginal}}'].complete();
359+
delete this.cancelMap['{{operationIdOriginal}}'];
360+
}
361+
if (typeof this.configuration.afterHandler === 'function') {
361362
this.configuration.afterHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase(), result);
362-
};
363-
handle = handle.pipe(tap(afterHandler, afterHandler));
364-
if (cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
365-
this.cancelMap['{{operationIdOriginal}}'].asObservable().subscribe(() => afterHandler(new Error('CANCELLED'), false));
366363
}
364+
};
365+
handle = handle.pipe(tap(afterHandler, afterHandler));
366+
if (typeof this.configuration.afterHandler === 'function' && cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
367+
this.cancelMap['{{operationIdOriginal}}'].asObservable().pipe(take(1)).subscribe(() => afterHandler(new Error('CANCELLED'), false));
367368
}
368369

369370
if (typeof this.configuration.errorHandler === 'function') {
@@ -400,7 +401,7 @@ export class {{classname}} {
400401
if (this.cancelMap['{{operationIdOriginal}}']) {
401402
this.cancelMap['{{operationIdOriginal}}'].next();
402403
this.cancelMap['{{operationIdOriginal}}'].complete();
403-
this.cancelMap['{{operationIdOriginal}}'] = null;
404+
delete this.cancelMap['{{operationIdOriginal}}'];
404405
}
405406
this.cancelMap['{{operationIdOriginal}}'] = '{{httpMethod}}'.toUpperCase() === 'GET' ? new Subject<any>() : null;
406407
if(this.cancelMap['{{operationIdOriginal}}']) {
@@ -411,19 +412,19 @@ export class {{classname}} {
411412
if (typeof this.configuration.beforeHandler === 'function') {
412413
this.configuration.beforeHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase());
413414
}
414-
if (typeof this.configuration.afterHandler === 'function') {
415-
const afterHandler = (result: any, stopCancellation = true) => {
416-
if (stopCancellation && cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
417-
// don't call next to prevent second call
418-
this.cancelMap['{{operationIdOriginal}}'].complete();
419-
this.cancelMap['{{operationIdOriginal}}'] = null;
420-
}
415+
const afterHandler = (result: any, stopCancellation = true) => {
416+
// stop cancelation to prevent calling afterHandler on next service call
417+
if (stopCancellation && cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
418+
this.cancelMap['{{operationIdOriginal}}'].complete();
419+
delete this.cancelMap['{{operationIdOriginal}}'];
420+
}
421+
if (typeof this.configuration.afterHandler === 'function') {
421422
this.configuration.afterHandler('{{operationIdOriginal}}', '{{httpMethod}}'.toUpperCase(), result);
422-
};
423-
handle = handle.tap(afterHandler, afterHandler);
424-
if (cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
425-
this.cancelMap['{{operationIdOriginal}}'].asObservable().subscribe(() => afterHandler(new Error('CANCELLED'), false));
426423
}
424+
};
425+
handle = handle.tap(afterHandler, afterHandler);
426+
if (typeof this.configuration.afterHandler === 'function' && cancelPreviousRequest && this.cancelMap['{{operationIdOriginal}}']) {
427+
this.cancelMap['{{operationIdOriginal}}'].asObservable().take(1).subscribe(() => afterHandler(new Error('CANCELLED'), false));
427428
}
428429

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

0 commit comments

Comments
 (0)