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 all 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
49 changes: 49 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,49 @@
import { Observable, Subject } from 'rxjs';
import { OnDestroy } from '@angular/core';
import { merge } from 'rxjs/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) {
return source;
}

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

if (!(<EnhancedOnDestroy>instance).__ngOnDestroySource__) {
(<EnhancedOnDestroy>instance).__ngOnDestroySource__ = new Subject();
(<EnhancedOnDestroy>instance).__ngOnDestroy__ = 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.

I think this might be a good place to use Symbol, as I don't think we want this property to be enumerable. plus it will make sure there are no collisions.

Copy link
Contributor Author

@diamond-darrell diamond-darrell Jun 14, 2018

Choose a reason for hiding this comment

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

hmm, never used it before :)

const ngOnDestroySource = Symbol('ngOnDestroySource');
const ngOnDestroy = Symbol('ngOnDestroy');

interface EnhancedOnDestroy extends OnDestroy {
    [ngOnDestroySource]: Subject<string>;
    [ngOnDestroy]: () => void;
}
//...
if (!instance[ngOnDestroySource]) {
     instance[ngOnDestroySource] = new Subject();
     instance[ngOnDestroy] = instance.ngOnDestroy;
//...

something like this, right? (I haven't tried it in the runtime yet, just guessing in the comments)

Copy link
Contributor

Choose a reason for hiding this comment

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

exactly. The symbol data structure is built for use cases like these. I would consult @esakal first, as I think it requires a polyfill for IE.

Copy link

Choose a reason for hiding this comment

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

Hi, It is nice talking about advanced techniques :)

I think this might be a good place to use Symbol, as I don't think we want this property to be enumerable. plus it will make sure there are no collisions.

It will prevent collisions as you mentioned and will be ignored so it fit the purpose.

I think it requires a polyfill for IE

We already have this polifill enabled here

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, apparently it doesn't work yet in typescript v2.5.3 we have in kaltura-ng.
image
It relates to this issue microsoft/TypeScript#5579 and it was closed by this PR microsoft/TypeScript#15473 which was included into v2.7

@esakal We can leave it as a plain property for now or wait for upgrade of the TS (if it was planned), what do you prefer?


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

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

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

return () => {
if (subscription) {
subscription.unsubscribe();
subscription = null;
}
}
});
}
}
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';
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;
}
}
});
}