@@ -24,13 +24,13 @@ import { Subject } from 'rxjs/Subject'
2424{ {/useRxJS6} }
2525{ {#useRxJS6} }
2626import { Observable, Subject } from 'rxjs';
27- import { catchError, takeUntil, tap, take } from 'rxjs/operators';
27+ import { catchError, takeUntil, tap, share } from 'rxjs/operators';
2828{ {/useRxJS6} }
2929{ {^useHttpClient} }
3030import '../rxjs-operators';
3131import 'rxjs/add/operator/takeUntil';
3232import 'rxjs/add/operator/tap';
33- import 'rxjs/add/operator/take ';
33+ import 'rxjs/add/operator/share ';
3434{ {/useHttpClient} }
3535
3636{ {#imports} }
@@ -340,8 +340,6 @@ export class {{classname}} {
340340 if (cancelPreviousRequest) {
341341 if (this.cancelMap[' {{operationIdOriginal}}' ]) {
342342 this.cancelMap[' {{operationIdOriginal}}' ].next();
343- this.cancelMap[' {{operationIdOriginal}}' ].complete();
344- delete this.cancelMap[' {{operationIdOriginal}}' ];
345343 }
346344 this.cancelMap['{ {operationIdOriginal} }'] = '{ {httpMethod} }'.toUpperCase() === 'GET' ? new Subject<any >() : null;
347345 if(this.cancelMap['{ {operationIdOriginal} }']) {
@@ -352,23 +350,30 @@ export class {{classname}} {
352350 if (typeof this.configuration.beforeHandler === 'function') {
353351 this.configuration.beforeHandler(' {{operationIdOriginal}}' , ' {{httpMethod}}' .toUpperCase());
354352 }
353+ let afterSubscription: Subscription;
355354 const afterHandler = (result: any) => {
355+ if (afterSubscription) {
356+ afterSubscription.unsubscribe();
357+ }
356358 // stop cancellation to prevent calling afterHandler on next service call
357359 if (cancelPreviousRequest && this.cancelMap['{ {operationIdOriginal} }']) {
358360 this.cancelMap[' {{operationIdOriginal}}' ].complete();
359361 delete this.cancelMap[' {{operationIdOriginal}}' ];
360362 }
361363 if (typeof this.configuration.afterHandler === 'function') {
362- this.configuration.afterHandler(' {{operationIdOriginal}}' , ' {{httpMethod}}' .toUpperCase(), result);
364+ this.configuration.afterHandler(
365+ ' {{operationIdOriginal}}' ,
366+ ' {{httpMethod}}' .toUpperCase(),
367+ result ? result : new Error(' CANCELLED' )
368+ );
363369 }
364370 };
365- handle = handle.pipe(tap(afterHandler, afterHandler));
366- if (typeof this.configuration.afterHandler === 'function' && cancelPreviousRequest && this.cancelMap['{ {operationIdOriginal} }']) {
367- this.cancelMap[' {{operationIdOriginal}}' ]
368- .asObservable()
369- .pipe(take(1))
370- .subscribe(() => this.configuration.afterHandler(' {{operationIdOriginal}}' , ' {{httpMethod}}' .toUpperCase(), new Error(' CANCELLED' )));
371- }
371+ handle = handle.pipe(share());
372+ afterSubscription = handle.subscribe(
373+ result => afterHandler(result),
374+ err => afterHandler(err),
375+ () => afterHandler()
376+ )
372377
373378 if (typeof this.configuration.errorHandler === 'function') {
374379 return handle.pipe(catchError(err => this.configuration.errorHandler(err, ' {{operationIdOriginal}}' )));
@@ -415,23 +420,30 @@ export class {{classname}} {
415420 if (typeof this.configuration.beforeHandler === 'function') {
416421 this.configuration.beforeHandler(' {{operationIdOriginal}}' , ' {{httpMethod}}' .toUpperCase());
417422 }
423+ let afterSubscription: Subscription;
418424 const afterHandler = (result: any) => {
425+ if (afterSubscription) {
426+ afterSubscription.unsubscribe();
427+ }
419428 // stop cancellation to prevent calling afterHandler on next service call
420429 if (cancelPreviousRequest && this.cancelMap['{ {operationIdOriginal} }']) {
421430 this.cancelMap[' {{operationIdOriginal}}' ].complete();
422431 delete this.cancelMap[' {{operationIdOriginal}}' ];
423432 }
424433 if (typeof this.configuration.afterHandler === 'function') {
425- this.configuration.afterHandler(' {{operationIdOriginal}}' , ' {{httpMethod}}' .toUpperCase(), result);
434+ this.configuration.afterHandler(
435+ ' {{operationIdOriginal}}' ,
436+ ' {{httpMethod}}' .toUpperCase(),
437+ result ? result : new Error(' CANCELLED' )
438+ );
426439 }
427440 };
428- handle = handle.tap(afterHandler, afterHandler);
429- if (typeof this.configuration.afterHandler === 'function' && cancelPreviousRequest && this.cancelMap['{ {operationIdOriginal} }']) {
430- this.cancelMap[' {{operationIdOriginal}}' ]
431- .asObservable()
432- .take(1)
433- .subscribe(() => this.configuration.afterHandler(' {{operationIdOriginal}}' , ' {{httpMethod}}' .toUpperCase(), new Error(' CANCELLED' )));
434- }
441+ handle = handle.share();
442+ afterSubscription = handle.subscribe(
443+ result => afterHandler(result),
444+ err => afterHandler(err),
445+ () => afterHandler()
446+ )
435447
436448 if (typeof this.configuration.errorHandler === 'function') {
437449 return handle.catch(err => this.configuration.errorHandler(err, ' {{operationIdOriginal}}' ));
0 commit comments