refactor(ivy): remove duplicated flatten util (#29547)
This commit removes code duplication where we had 2 versions of a `flatten` utility. Moreover this change results in queries using a non-recursive version of `flatten` which should result in a better performance of query refresh operations. PR Close #29547
This commit is contained in:
parent
401b8eedd5
commit
dd69e4e780
|
@ -9,6 +9,7 @@
|
||||||
import {Observable} from 'rxjs';
|
import {Observable} from 'rxjs';
|
||||||
|
|
||||||
import {EventEmitter} from '../event_emitter';
|
import {EventEmitter} from '../event_emitter';
|
||||||
|
import {flatten} from '../util/array_utils';
|
||||||
import {getSymbolIterator} from '../util/symbol';
|
import {getSymbolIterator} from '../util/symbol';
|
||||||
|
|
||||||
|
|
||||||
|
@ -110,7 +111,7 @@ export class QueryList<T>/* implements Iterable<T> */ {
|
||||||
* @param resultsTree The results tree to store
|
* @param resultsTree The results tree to store
|
||||||
*/
|
*/
|
||||||
reset(resultsTree: Array<T|any[]>): void {
|
reset(resultsTree: Array<T|any[]>): void {
|
||||||
this._results = depthFirstFlatten(resultsTree);
|
this._results = flatten(resultsTree);
|
||||||
(this as{dirty: boolean}).dirty = false;
|
(this as{dirty: boolean}).dirty = false;
|
||||||
(this as{length: number}).length = this._results.length;
|
(this as{length: number}).length = this._results.length;
|
||||||
(this as{last: T}).last = this._results[this.length - 1];
|
(this as{last: T}).last = this._results[this.length - 1];
|
||||||
|
@ -131,10 +132,3 @@ export class QueryList<T>/* implements Iterable<T> */ {
|
||||||
(this.changes as EventEmitter<any>).unsubscribe();
|
(this.changes as EventEmitter<any>).unsubscribe();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function depthFirstFlatten<T>(list: Array<T|T[]>): T[] {
|
|
||||||
return list.reduce((flat: any[], item: T | T[]): T[] => {
|
|
||||||
const flatItem = Array.isArray(item) ? depthFirstFlatten(item) : item;
|
|
||||||
return (<T[]>flat).concat(flatItem);
|
|
||||||
}, []);
|
|
||||||
}
|
|
||||||
|
|
|
@ -9,7 +9,9 @@
|
||||||
import {SRCSET_ATTRS, URI_ATTRS, VALID_ATTRS, VALID_ELEMENTS, getTemplateContent} from '../sanitization/html_sanitizer';
|
import {SRCSET_ATTRS, URI_ATTRS, VALID_ATTRS, VALID_ELEMENTS, getTemplateContent} from '../sanitization/html_sanitizer';
|
||||||
import {InertBodyHelper} from '../sanitization/inert_body';
|
import {InertBodyHelper} from '../sanitization/inert_body';
|
||||||
import {_sanitizeUrl, sanitizeSrcset} from '../sanitization/url_sanitizer';
|
import {_sanitizeUrl, sanitizeSrcset} from '../sanitization/url_sanitizer';
|
||||||
|
import {addAllToArray} from '../util/array_utils';
|
||||||
import {assertDefined, assertEqual, assertGreaterThan} from '../util/assert';
|
import {assertDefined, assertEqual, assertGreaterThan} from '../util/assert';
|
||||||
|
|
||||||
import {attachPatchData} from './context_discovery';
|
import {attachPatchData} from './context_discovery';
|
||||||
import {allocExpando, createNodeAtIndex, elementAttribute, load, textBinding} from './instructions/all';
|
import {allocExpando, createNodeAtIndex, elementAttribute, load, textBinding} from './instructions/all';
|
||||||
import {LContainer, NATIVE} from './interfaces/container';
|
import {LContainer, NATIVE} from './interfaces/container';
|
||||||
|
@ -22,7 +24,6 @@ import {BINDING_INDEX, HEADER_OFFSET, LView, RENDERER, TVIEW, TView, T_HOST} fro
|
||||||
import {appendChild, createTextNode, nativeRemoveNode} from './node_manipulation';
|
import {appendChild, createTextNode, nativeRemoveNode} from './node_manipulation';
|
||||||
import {getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from './state';
|
import {getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from './state';
|
||||||
import {NO_CHANGE} from './tokens';
|
import {NO_CHANGE} from './tokens';
|
||||||
import {addAllToArray} from './util/array_utils';
|
|
||||||
import {renderStringify} from './util/misc_utils';
|
import {renderStringify} from './util/misc_utils';
|
||||||
import {getNativeByIndex, getNativeByTNode, getTNode, isLContainer} from './util/view_utils';
|
import {getNativeByIndex, getNativeByTNode, getTNode, isLContainer} from './util/view_utils';
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {devModeEqual} from '@angular/core/src/change_detection/change_detection_util';
|
import {devModeEqual} from '@angular/core/src/change_detection/change_detection_util';
|
||||||
|
|
||||||
import {flatten} from '../../src/render3/util/array_utils';
|
|
||||||
import {isDifferent} from '../../src/render3/util/misc_utils';
|
import {isDifferent} from '../../src/render3/util/misc_utils';
|
||||||
|
|
||||||
describe('util', () => {
|
describe('util', () => {
|
||||||
|
@ -67,19 +65,4 @@ describe('util', () => {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('flatten', () => {
|
|
||||||
|
|
||||||
it('should flatten an empty array', () => { expect(flatten([])).toEqual([]); });
|
|
||||||
|
|
||||||
it('should flatten a flat array', () => { expect(flatten([1, 2, 3])).toEqual([1, 2, 3]); });
|
|
||||||
|
|
||||||
it('should flatten a nested array', () => {
|
|
||||||
expect(flatten([1, [2], 3])).toEqual([1, 2, 3]);
|
|
||||||
expect(flatten([[1], 2, [3]])).toEqual([1, 2, 3]);
|
|
||||||
expect(flatten([1, [2, [3]], 4])).toEqual([1, 2, 3, 4]);
|
|
||||||
expect(flatten([1, [2, [3]], [4]])).toEqual([1, 2, 3, 4]);
|
|
||||||
expect(flatten([1, [2, [3]], [[[4]]]])).toEqual([1, 2, 3, 4]);
|
|
||||||
expect(flatten([1, [], 2])).toEqual([1, 2]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright Google Inc. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by an MIT-style license that can be
|
||||||
|
* found in the LICENSE file at https://angular.io/license
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {flatten} from '../../src/util/array_utils';
|
||||||
|
|
||||||
|
describe('flatten', () => {
|
||||||
|
|
||||||
|
it('should flatten an empty array', () => { expect(flatten([])).toEqual([]); });
|
||||||
|
|
||||||
|
it('should flatten a flat array', () => { expect(flatten([1, 2, 3])).toEqual([1, 2, 3]); });
|
||||||
|
|
||||||
|
it('should flatten a nested array depth-first', () => {
|
||||||
|
expect(flatten([1, [2], 3])).toEqual([1, 2, 3]);
|
||||||
|
expect(flatten([[1], 2, [3]])).toEqual([1, 2, 3]);
|
||||||
|
expect(flatten([1, [2, [3]], 4])).toEqual([1, 2, 3, 4]);
|
||||||
|
expect(flatten([1, [2, [3]], [4]])).toEqual([1, 2, 3, 4]);
|
||||||
|
expect(flatten([1, [2, [3]], [[[4]]]])).toEqual([1, 2, 3, 4]);
|
||||||
|
expect(flatten([1, [], 2])).toEqual([1, 2]);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue