Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit c45f2ae

Browse files
CaerusKaruThomasBurleson
authored andcommitted
refactor(core): create generic sort operations (#996)
* improve performance with `MediaObserver::media$` property
1 parent 47248b1 commit c45f2ae

File tree

11 files changed

+47
-38
lines changed

11 files changed

+47
-38
lines changed

src/lib/core/breakpoints/break-point-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {Injectable, Inject} from '@angular/core';
99

1010
import {BreakPoint} from './break-point';
1111
import {BREAKPOINTS} from './break-points-token';
12-
import {sortAscendingPriority} from './breakpoint-tools';
12+
import {sortAscendingPriority} from '../utils/sort';
1313

1414
export type OptionalBreakPoint = BreakPoint | null;
1515

src/lib/core/breakpoints/breakpoint-tools.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
9-
import {OptionalBreakPoint} from './break-point-registry';
108
import {BreakPoint} from './break-point';
119
import {extendObject} from '../../utils/object-extend';
1210

@@ -64,16 +62,3 @@ export function mergeByAlias(defaults: BreakPoint[], custom: BreakPoint[] = []):
6462

6563
return validateSuffixes(Object.keys(dict).map(k => dict[k]));
6664
}
67-
68-
/** HOF to sort the breakpoints by priority */
69-
export function sortDescendingPriority(a: OptionalBreakPoint, b: OptionalBreakPoint): number {
70-
const priorityA = a ? a.priority || 0 : 0;
71-
const priorityB = b ? b.priority || 0 : 0;
72-
return priorityB - priorityA;
73-
}
74-
75-
export function sortAscendingPriority(a: BreakPoint, b: BreakPoint): number {
76-
const pA = a.priority || 0;
77-
const pB = b.priority || 0;
78-
return pA - pB;
79-
}

src/lib/core/breakpoints/data/break-points.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {TestBed, inject} from '@angular/core/testing';
9-
import {sortDescendingPriority} from '../breakpoint-tools';
9+
import {sortDescendingPriority} from '../../utils/sort';
1010

1111
import {BreakPoint} from '../break-point';
1212
import {BREAKPOINTS} from '../break-points-token';

src/lib/core/breakpoints/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,3 @@ export * from './data/orientation-break-points';
1212
export * from './break-point';
1313
export * from './break-point-registry';
1414
export * from './break-points-token';
15-
16-
export {
17-
sortDescendingPriority,
18-
sortAscendingPriority
19-
} from './breakpoint-tools';

src/lib/core/media-change.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class MediaChange {
1919
* @param mediaQuery e.g. (min-width: 600px) and (max-width: 959px)
2020
* @param mqAlias e.g. gt-sm, md, gt-lg
2121
* @param suffix e.g. GtSM, Md, GtLg
22+
* @param priority the priority of activation for the given breakpoint
2223
*/
2324
constructor(public matches = false,
2425
public mediaQuery = 'all',

src/lib/core/media-marshaller/media-marshaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {merge, Observable, Subject, Subscription} from 'rxjs';
1111
import {filter, tap} from 'rxjs/operators';
1212

1313
import {BreakPoint} from '../breakpoints/break-point';
14-
import {sortDescendingPriority} from '../breakpoints/breakpoint-tools';
14+
import {sortDescendingPriority} from '../utils/sort';
1515
import {BreakPointRegistry} from '../breakpoints/break-point-registry';
1616
import {MatchMedia} from '../match-media/match-media';
1717
import {MediaChange} from '../media-change';

src/lib/core/media-marshaller/print-hook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {MediaChange} from '../media-change';
1212
import {BreakPoint} from '../breakpoints/break-point';
1313
import {LAYOUT_CONFIG, LayoutConfigOptions} from '../tokens/library-config';
1414
import {BreakPointRegistry, OptionalBreakPoint} from '../breakpoints/break-point-registry';
15-
import {sortDescendingPriority} from '../breakpoints/breakpoint-tools';
15+
import {sortDescendingPriority} from '../utils/sort';
1616

1717
/**
1818
* Interface to apply PrintHook to call anonymous `target.updateStyles()`

src/lib/core/media-observer/media-observer.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {MediaChange} from '../media-change';
1414
import {MatchMedia} from '../match-media/match-media';
1515
import {PrintHook} from '../media-marshaller/print-hook';
1616
import {BreakPointRegistry, OptionalBreakPoint} from '../breakpoints/break-point-registry';
17+
import {sortDescendingPriority} from '../utils/sort';
1718

1819
/**
1920
* MediaObserver enables applications to listen for 1..n mediaQuery activations and to determine
@@ -64,12 +65,7 @@ export class MediaObserver {
6465
* @breaking-change 7.0.0-beta.24
6566
* @deletion-target v7.0.0-beta.25
6667
*/
67-
get media$(): Observable<MediaChange> {
68-
return this._media$.pipe(
69-
filter((changes: MediaChange[]) => changes.length > 0),
70-
map((changes: MediaChange[]) => changes[0])
71-
);
72-
}
68+
readonly media$: Observable<MediaChange>;
7369

7470
/** Filter MediaChange notifications for overlapping breakpoints */
7571
filterOverlaps = false;
@@ -78,6 +74,10 @@ export class MediaObserver {
7874
protected matchMedia: MatchMedia,
7975
protected hook: PrintHook) {
8076
this._media$ = this.watchActivations();
77+
this.media$ = this._media$.pipe(
78+
filter((changes: MediaChange[]) => changes.length > 0),
79+
map((changes: MediaChange[]) => changes[0])
80+
);
8181
}
8282

8383

@@ -176,10 +176,10 @@ export class MediaObserver {
176176
.map(query => new MediaChange(true, query))
177177
.map(replaceWithPrintAlias)
178178
.map(mergeMQAlias)
179-
.sort(sortChangesByPriority);
179+
.sort(sortDescendingPriority);
180180
}
181181

182-
private _media$: Observable<MediaChange[]>;
182+
private readonly _media$: Observable<MediaChange[]>;
183183
}
184184

185185
/**
@@ -190,9 +190,3 @@ function toMediaQuery(query: string, locator: BreakPointRegistry) {
190190
return bp ? bp.mediaQuery : query;
191191
}
192192

193-
/** HOF to sort the breakpoints by priority */
194-
export function sortChangesByPriority(a: MediaChange, b: MediaChange): number {
195-
const priorityA = a ? a.priority || 0 : 0;
196-
const priorityB = b ? b.priority || 0 : 0;
197-
return priorityB - priorityA;
198-
}

src/lib/core/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export * from './base/index';
1717
export * from './breakpoints/index';
1818
export * from './match-media/index';
1919
export * from './media-observer/index';
20+
export * from './utils/index';
2021

2122
export * from './style-utils/style-utils';
2223
export * from './style-builder/style-builder';

src/lib/core/utils/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
export * from './sort';

0 commit comments

Comments
 (0)