Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix(mdMedia): avoid unnecessary digest and make changes apply quicker
Browse files Browse the repository at this point in the history
Previously, the `updateAll()` method relied on `$timeout` for scheduling a
digest. This made the possible changes apply after the next rendering and
could possibly lead to extra digests.

This commit, replaces the call to `$timeout` with a call to `$evalAsync()`,
which avoids an extra digest if one is already in progress and also applies
the changes before the next rendering.
  • Loading branch information
gkalpak committed Dec 24, 2014
1 parent a0b66f6 commit 0bdb399
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/core/util/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ angular.module('material.core')
* @example $mdMedia('(min-width: 1200px)') == true if device-width >= 1200px
* @example $mdMedia('max-width: 300px') == true if device-width <= 300px (sanitizes input, adding parens)
*/
function mdMediaFactory($window, $mdUtil, $timeout, $mdConstant) {
function mdMediaFactory($mdConstant, $mdUtil, $rootScope, $window) {
var queriesCache = $mdUtil.cacheFactory('$mdMedia:queries', {capacity: 15});
var resultsCache = $mdUtil.cacheFactory('$mdMedia:results', {capacity: 15});

Expand Down Expand Up @@ -42,13 +42,16 @@ function mdMediaFactory($window, $mdUtil, $timeout, $mdConstant) {
}

function updateAll() {
var keys = cache.keys();
if (keys.length) {
for (var i = 0, ii = keys.length; i < ii; i++) {
cache.put(keys[i], !!$window.matchMedia(keys[i]).matches);
var keys = resultsCache.keys();
var len = keys.length;

if (len) {
for (var i = 0; i < len; i++) {
add(keys[i]);
}
// trigger a $digest()
$timeout(angular.noop);

// Trigger a $digest() if not already in progress
$rootScope.$evalAsync();
}
}
}

0 comments on commit 0bdb399

Please sign in to comment.