Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pipable cancelOnDestroy and tag operators #117

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions kaltura-common/src/rxjs/operators/cancel-on-destroy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Observable } from 'rxjs/Observable';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creation method should be imported like so:
import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent } from 'rxjs';
see:
https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md#import-paths

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correcting my previous comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge is not available for import from 'rxjs' in v5.5.6 yet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I thought this work was done as part of v6 migration. my bad.

import { Subject } from 'rxjs/Subject';
import { OnDestroy } from '@angular/core';
import 'rxjs/add/observable/merge';

interface EnhancedOnDestroy extends OnDestroy {
__ngOnDestroySource__: Subject<string>;
__ngOnDestroy__: () => void;
}

export function cancelOnDestroy<T>(instance: OnDestroy, manualDestroy?: Observable<any>): (source: Observable<T>) => Observable<T> {
return (source: Observable<T>) => {
if (instance.ngOnDestroy) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really a review comment, more of an advise, feel free to ignore:
I usually prefer to use a more defensive programming approach using guard clauses:
https://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html

can remove one level of indentation by replacing the if logic like so:

if (!instance.ngOnDestroy) {
   return source;
}
.... rest of code here, no need else after a return.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, I don't mind

return Observable.create(observer => {
let subscription = source.subscribe(observer);

if (!(<EnhancedOnDestroy>instance).__ngOnDestroySource__) {
(<EnhancedOnDestroy>instance).__ngOnDestroySource__ = new Subject();
(<EnhancedOnDestroy>instance).__ngOnDestroy__ = instance.ngOnDestroy;

instance.ngOnDestroy = function (this: EnhancedOnDestroy) {
this.__ngOnDestroy__.apply(this, arguments);
this.__ngOnDestroySource__.next();
this.__ngOnDestroySource__.complete();
};
}

const sources = manualDestroy
? Observable.merge(manualDestroy, (<EnhancedOnDestroy>instance).__ngOnDestroySource__)
: (<EnhancedOnDestroy>instance).__ngOnDestroySource__.asObservable();

sources.subscribe(() => {
if (subscription) {
subscription.unsubscribe();
subscription = null;
}
});

return () => {
if (subscription) {
subscription.unsubscribe();
subscription = null;
}
}
});
} else {
return source;
}
}
}
2 changes: 2 additions & 0 deletions kaltura-common/src/rxjs/operators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { cancelOnDestroy } from './cancel-on-destroy';
export { tag } from './tag';
44 changes: 44 additions & 0 deletions kaltura-common/src/rxjs/operators/tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Observable } from 'rxjs/Observable';
import { OperationTagStoreMediator } from '../../operation-tag/operation-tag-store-mediator';

export function tag<T>(action: string): (source: Observable<T>) => Observable<T> {
let isDecreased = false;
let closed = false;

return (source: Observable<T>) => Observable.create(observer => {
OperationTagStoreMediator.increase(action);

let subscription = source.subscribe(
(value) => {
observer.next(value);
},
error => {
if (action && !isDecreased) {
isDecreased = true;
OperationTagStoreMediator.decrease(action);
}
observer.error(error);
},
() => {
if (action && !isDecreased) {
isDecreased = true;
OperationTagStoreMediator.decrease(action);
}
observer.complete();
}
);

return () => {
if (!closed && action && !isDecreased) {
isDecreased = true;
OperationTagStoreMediator.decrease(action);
}

if (subscription) {
subscription.unsubscribe();
subscription = null;
closed = true;
}
}
});
}