From dd69e4e780f7cc41a5df7d507ee932fdc674f205 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Wed, 27 Mar 2019 17:40:34 +0100 Subject: [PATCH] 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 --- packages/core/src/linker/query_list.ts | 10 ++------ packages/core/src/render3/i18n.ts | 3 ++- .../src/{render3 => }/util/array_utils.ts | 0 packages/core/test/render3/util_spec.ts | 17 ------------- packages/core/test/util/array_utils_spec.ts | 25 +++++++++++++++++++ 5 files changed, 29 insertions(+), 26 deletions(-) rename packages/core/src/{render3 => }/util/array_utils.ts (100%) create mode 100644 packages/core/test/util/array_utils_spec.ts diff --git a/packages/core/src/linker/query_list.ts b/packages/core/src/linker/query_list.ts index ae483f8f26..5152125116 100644 --- a/packages/core/src/linker/query_list.ts +++ b/packages/core/src/linker/query_list.ts @@ -9,6 +9,7 @@ import {Observable} from 'rxjs'; import {EventEmitter} from '../event_emitter'; +import {flatten} from '../util/array_utils'; import {getSymbolIterator} from '../util/symbol'; @@ -110,7 +111,7 @@ export class QueryList/* implements Iterable */ { * @param resultsTree The results tree to store */ reset(resultsTree: Array): void { - this._results = depthFirstFlatten(resultsTree); + this._results = flatten(resultsTree); (this as{dirty: boolean}).dirty = false; (this as{length: number}).length = this._results.length; (this as{last: T}).last = this._results[this.length - 1]; @@ -131,10 +132,3 @@ export class QueryList/* implements Iterable */ { (this.changes as EventEmitter).unsubscribe(); } } - -function depthFirstFlatten(list: Array): T[] { - return list.reduce((flat: any[], item: T | T[]): T[] => { - const flatItem = Array.isArray(item) ? depthFirstFlatten(item) : item; - return (flat).concat(flatItem); - }, []); -} diff --git a/packages/core/src/render3/i18n.ts b/packages/core/src/render3/i18n.ts index 0ab46519f8..0c2b4ba8ec 100644 --- a/packages/core/src/render3/i18n.ts +++ b/packages/core/src/render3/i18n.ts @@ -9,7 +9,9 @@ import {SRCSET_ATTRS, URI_ATTRS, VALID_ATTRS, VALID_ELEMENTS, getTemplateContent} from '../sanitization/html_sanitizer'; import {InertBodyHelper} from '../sanitization/inert_body'; import {_sanitizeUrl, sanitizeSrcset} from '../sanitization/url_sanitizer'; +import {addAllToArray} from '../util/array_utils'; import {assertDefined, assertEqual, assertGreaterThan} from '../util/assert'; + import {attachPatchData} from './context_discovery'; import {allocExpando, createNodeAtIndex, elementAttribute, load, textBinding} from './instructions/all'; 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 {getIsParent, getLView, getPreviousOrParentTNode, setIsParent, setPreviousOrParentTNode} from './state'; import {NO_CHANGE} from './tokens'; -import {addAllToArray} from './util/array_utils'; import {renderStringify} from './util/misc_utils'; import {getNativeByIndex, getNativeByTNode, getTNode, isLContainer} from './util/view_utils'; diff --git a/packages/core/src/render3/util/array_utils.ts b/packages/core/src/util/array_utils.ts similarity index 100% rename from packages/core/src/render3/util/array_utils.ts rename to packages/core/src/util/array_utils.ts diff --git a/packages/core/test/render3/util_spec.ts b/packages/core/test/render3/util_spec.ts index 36290be8b2..5b8673e8bf 100644 --- a/packages/core/test/render3/util_spec.ts +++ b/packages/core/test/render3/util_spec.ts @@ -7,8 +7,6 @@ */ 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'; 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]); - }); - }); }); diff --git a/packages/core/test/util/array_utils_spec.ts b/packages/core/test/util/array_utils_spec.ts new file mode 100644 index 0000000000..9754b10f1b --- /dev/null +++ b/packages/core/test/util/array_utils_spec.ts @@ -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]); + }); +}); \ No newline at end of file