refactor(core): remove deprecated rxjs signature usage in event emitter and make more minifier-friendly (#41450)

* We had a usage of `Observable.subscribe` that uses the deprecated signature with 3 arguments. These changes switch to the non-deprecated version that passes in an `Observer`.
* Avoids always creating a `complete` callback since it isn't required.
* We were repeating all of the internal callbacks twice: once for sync and once for async. These changes move them out into variables so that they're more minifier-friendly. The savings aren't huge (~100 bytes minified), but it doesn't add any maintenance effort on our end so I decided to add it.

PR Close #41450
This commit is contained in:
Kristiyan Kostadinov 2021-04-05 13:52:08 +02:00 committed by atscott
parent 7a49aa5136
commit d641542587
4 changed files with 32 additions and 44 deletions

View File

@ -8,7 +8,7 @@
/// <reference types="rxjs" />
import {Subject, Subscription} from 'rxjs';
import {PartialObserver, Subject, Subscription} from 'rxjs';
/**
* Use in components with the `@Output` directive to emit custom events
@ -115,57 +115,30 @@ class EventEmitter_ extends Subject<any> {
}
subscribe(observerOrNext?: any, error?: any, complete?: any): Subscription {
let schedulerFn: (t: any) => any;
let errorFn = (err: any): any => null;
let completeFn = (): any => null;
let nextFn = observerOrNext;
let errorFn = error || (() => null);
let completeFn = complete;
if (observerOrNext && typeof observerOrNext === 'object') {
schedulerFn = this.__isAsync ? (value: any) => {
setTimeout(() => observerOrNext.next(value));
} : (value: any) => {
observerOrNext.next(value);
};
const observer = observerOrNext as PartialObserver<unknown>;
nextFn = observer.next?.bind(observer);
errorFn = observer.error?.bind(observer);
completeFn = observer.complete?.bind(observer);
}
if (observerOrNext.error) {
errorFn = this.__isAsync ? (err) => {
setTimeout(() => observerOrNext.error(err));
} : (err) => {
observerOrNext.error(err);
};
if (this.__isAsync) {
errorFn = _wrapInTimeout(errorFn);
if (nextFn) {
nextFn = _wrapInTimeout(nextFn);
}
if (observerOrNext.complete) {
completeFn = this.__isAsync ? () => {
setTimeout(() => observerOrNext.complete());
} : () => {
observerOrNext.complete();
};
}
} else {
schedulerFn = this.__isAsync ? (value: any) => {
setTimeout(() => observerOrNext(value));
} : (value: any) => {
observerOrNext(value);
};
if (error) {
errorFn = this.__isAsync ? (err) => {
setTimeout(() => error(err));
} : (err) => {
error(err);
};
}
if (complete) {
completeFn = this.__isAsync ? () => {
setTimeout(() => complete());
} : () => {
complete();
};
if (completeFn) {
completeFn = _wrapInTimeout(completeFn);
}
}
const sink = super.subscribe(schedulerFn, errorFn, completeFn);
const sink = super.subscribe({next: nextFn, error: errorFn, complete: completeFn});
if (observerOrNext instanceof Subscription) {
observerOrNext.add(sink);
@ -175,6 +148,12 @@ class EventEmitter_ extends Subject<any> {
}
}
function _wrapInTimeout(fn: (value: unknown) => any) {
return (value: unknown) => {
setTimeout(fn, undefined, value);
};
}
/**
* @publicApi
*/

View File

@ -650,6 +650,9 @@
{
"name": "_testabilityGetter"
},
{
"name": "_wrapInTimeout"
},
{
"name": "addComponentLogic"
},

View File

@ -638,6 +638,9 @@
{
"name": "_testabilityGetter"
},
{
"name": "_wrapInTimeout"
},
{
"name": "addComponentLogic"
},

View File

@ -926,6 +926,9 @@
{
"name": "_testabilityGetter"
},
{
"name": "_wrapInTimeout"
},
{
"name": "absoluteRedirect"
},