@code-art/rx-helpers
Version:
A library with Rxjs operators that can be used in your Angular 8 projects.
214 lines (206 loc) • 7.28 kB
JavaScript
import { Subject, Subscriber, defer } from 'rxjs';
import { takeUntil, startWith, switchMap, shareReplay } from 'rxjs/operators';
import { __extends } from 'tslib';
var DESTROY_SUBJECT_SYMBOL = Symbol();
var DECORATOR_APPLIED_SYMBOL = Symbol();
function isComponentType(type) {
return type && !!type.ɵcmp;
}
function isDirectiveType(type) {
return type && !!type.ɵdir;
}
function isPipeType(type) {
return type && !!type.ɵpipe;
}
function isProviderType(type) {
return type && !!type.ɵprov;
}
function doOnDestroy() {
var subject = this[DESTROY_SUBJECT_SYMBOL];
if (subject) {
subject.next(0);
subject.complete();
this[DESTROY_SUBJECT_SYMBOL] = undefined;
}
}
function applyOnDestroyHook(def, handler) {
var oldOnDestroy = def.onDestroy;
def.onDestroy = function () {
if (oldOnDestroy) {
oldOnDestroy.apply(this);
}
handler.apply(this);
};
}
function applyOnDestroyToProvider(def, handler) {
var oldOnDestroy = def.prototype.ngOnDestroy;
def.prototype.ngOnDestroy = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (oldOnDestroy) {
oldOnDestroy.apply(this, args);
}
handler.apply(this);
};
}
function applyOnDestroyToInstance(instance) {
if (!instance[DESTROY_SUBJECT_SYMBOL]) {
instance[DESTROY_SUBJECT_SYMBOL] = new Subject();
var constructor = instance.constructor;
if (typeof constructor === 'function') {
if (constructor[DECORATOR_APPLIED_SYMBOL]) {
return;
}
if (isPipeType(constructor)
|| isComponentType(constructor)
|| isDirectiveType(constructor)
|| isProviderType(constructor)) {
throw new Error("To use " + takeUntilDestroyed.name + " with Components, Pipes, Directives and Services add the @" + TakeUntilDestroyed.name + ".");
}
}
if (typeof instance.ngOnDestroy !== 'function') {
throw new Error(takeUntilDestroyed.name + " can only be applied to Components, Pipes, Directives and Services with @Injectable decorator or "
+ " to object instance implementing OnDestroy interface.");
}
var oldOnDestroy_1 = instance.ngOnDestroy;
instance.ngOnDestroy = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
oldOnDestroy_1.apply(instance, args);
doOnDestroy.apply(this);
};
}
}
function applyOnDestroyToClass(target) {
if (isComponentType(target)) {
applyOnDestroyHook(target.ɵcmp, doOnDestroy);
}
else if (isDirectiveType(target)) {
applyOnDestroyHook(target.ɵdir, doOnDestroy);
}
else if (isPipeType(target)) {
applyOnDestroyHook(target.ɵpipe, doOnDestroy);
}
else {
applyOnDestroyToProvider(target, doOnDestroy);
}
target[DECORATOR_APPLIED_SYMBOL] = true;
}
function TakeUntilDestroyed() {
return applyOnDestroyToClass;
}
function takeUntilDestroyed(instance) {
return function (source) {
var c = instance;
// const c = (component as any) as ComponentWithTakeUntilDestroy;
applyOnDestroyToInstance(c);
return source.pipe(takeUntil(c[DESTROY_SUBJECT_SYMBOL]));
};
}
var ZoneSubscriber = /** @class */ (function (_super) {
__extends(ZoneSubscriber, _super);
function ZoneSubscriber(_zone, destination) {
var _this = _super.call(this, destination) || this;
_this._zone = _zone;
return _this;
}
ZoneSubscriber.prototype._next = function (value) {
var _this = this;
this._zone.run(function () {
if (typeof _this.destination.next === 'function') {
_this.destination.next(value);
}
});
};
ZoneSubscriber.prototype._error = function (error) {
var _this = this;
this._zone.run(function () {
if (typeof _this.destination.error === 'function') {
_this.destination.error(error);
}
});
};
ZoneSubscriber.prototype._complete = function () {
var _this = this;
this._zone.run(function () {
if (typeof _this.destination.complete === 'function') {
_this.destination.complete();
}
});
};
return ZoneSubscriber;
}(Subscriber));
var ZoneOperator = /** @class */ (function () {
function ZoneOperator(_zone) {
this._zone = _zone;
}
ZoneOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new ZoneSubscriber(this._zone, subscriber));
};
return ZoneOperator;
}());
function withZone(zone) {
return function (source) { return source.lift(new ZoneOperator(zone)); };
}
var OutsideZoneSubscriber = /** @class */ (function (_super) {
__extends(OutsideZoneSubscriber, _super);
function OutsideZoneSubscriber(_zone, destination) {
var _this = _super.call(this, destination) || this;
_this._zone = _zone;
return _this;
}
OutsideZoneSubscriber.prototype._next = function (value) {
var _this = this;
this._zone.runOutsideAngular(function () {
if (typeof _this.destination.next === 'function') {
_this.destination.next(value);
}
});
};
OutsideZoneSubscriber.prototype._error = function (error) {
var _this = this;
this._zone.runOutsideAngular(function () {
if (typeof _this.destination.error === 'function') {
_this.destination.error(error);
}
});
};
OutsideZoneSubscriber.prototype._complete = function () {
var _this = this;
this._zone.runOutsideAngular(function () {
if (typeof _this.destination.complete === 'function') {
_this.destination.complete();
}
});
};
return OutsideZoneSubscriber;
}(Subscriber));
var OutsideZoneOperator = /** @class */ (function () {
function OutsideZoneOperator(_zone) {
this._zone = _zone;
}
OutsideZoneOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new OutsideZoneSubscriber(this._zone, subscriber));
};
return OutsideZoneOperator;
}());
function outsideZone(zone) {
return function (source) { return source.lift(new OutsideZoneOperator(zone)); };
}
function cacheUntil(observable) {
return function (source) {
return observable.pipe(startWith(0), switchMap(function () { return defer(function () { return source; }); }), shareReplay(1));
};
}
/*
* Public API Surface of rx-helpers
*/
/**
* Generated bundle index. Do not edit.
*/
export { TakeUntilDestroyed, cacheUntil, outsideZone, takeUntilDestroyed, withZone };
//# sourceMappingURL=code-art-rx-helpers.js.map