refactor(core): cleanup code with side-effects which was preventing tree-shaking (#30580)

PR Close #30580
This commit is contained in:
Misko Hevery
2019-05-20 22:50:30 -07:00
parent a981dd2aab
commit fcdd784667
13 changed files with 72 additions and 197 deletions

View File

@ -12,6 +12,9 @@ import {EventEmitter} from '../event_emitter';
import {flatten} from '../util/array_utils';
import {getSymbolIterator} from '../util/symbol';
function symbolIterator<T>(this: QueryList<T>): Iterator<T> {
return ((this as any as{_results: Array<T>})._results as any)[getSymbolIterator()]();
}
/**
* An unmodifiable list of items that Angular keeps up to date when the state
@ -50,6 +53,16 @@ export class QueryList<T>/* implements Iterable<T> */ {
// TODO(issue/24571): remove '!'.
readonly last !: T;
constructor() {
// This function should be declared on the prototype, but doing so there will cause the class
// declaration to have side-effects and become not tree-shakable. For this reason we do it in
// the constructor.
// [getSymbolIterator()](): Iterator<T> { ... }
const symbol = getSymbolIterator();
const proto = QueryList.prototype as any;
if (!proto[symbol]) proto[symbol] = symbolIterator;
}
/**
* See
* [Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
@ -99,8 +112,6 @@ export class QueryList<T>/* implements Iterable<T> */ {
*/
toArray(): T[] { return this._results.slice(); }
[getSymbolIterator()](): Iterator<T> { return (this._results as any)[getSymbolIterator()](); }
toString(): string { return this._results.toString(); }
/**