update promises

This commit is contained in:
Luke Pulverenti 2015-12-30 15:25:17 -05:00
parent e340d7a40b
commit 3fc5d39784
9 changed files with 164 additions and 179 deletions

View File

@ -675,14 +675,16 @@
self.getTargets = function () {
return new Promise(function (resolve, reject) {
var targets = [];
if (castPlayer.hasReceivers) {
targets.push(self.getCurrentTargetInfo());
}
return targets;
resolve(targets);
});
};
self.getCurrentTargetInfo = function () {

View File

@ -107,11 +107,11 @@
getSavedViewSetting: function (key) {
var deferred = $.Deferred();
var val = LibraryBrowser.getSavedView(key);
return new Promise(function (resolve, reject) {
deferred.resolveWith(null, [val]);
return deferred.promise();
var val = LibraryBrowser.getSavedView(key);
resolve(val);
});
},
needsRefresh: function (elem) {

View File

@ -404,9 +404,7 @@
function getUserViews(apiClient, userId) {
var deferred = $.Deferred();
apiClient.getUserViews({}, userId).then(function (result) {
return apiClient.getUserViews({}, userId).then(function (result) {
var items = result.Items;
@ -442,10 +440,8 @@
}
}
deferred.resolveWith(null, [list]);
return list;
});
return deferred.promise();
}
function updateLibraryMenu(user) {

View File

@ -370,9 +370,11 @@
self.setDefaultPlayerActive = function () {
var player = self.getDefaultPlayer();
var target = player.getTargets()[0];
self.setActivePlayer(player, target);
player.getTargets().then(function (targets) {
self.setActivePlayer(player, targets[0]);
});
};
self.removeActivePlayer = function (name) {
@ -449,13 +451,11 @@
self.getTargets = function () {
var deferred = $.Deferred();
var promises = players.map(function (p) {
return p.getTargets();
});
Promise.all(promises).then(function (responses) {
return Promise.all(promises).then(function (responses) {
var targets = [];
@ -481,10 +481,8 @@
return aVal.localeCompare(bVal);
});
deferred.resolveWith(null, [targets]);
return targets;
});
return deferred.promise();
};
function doWithPlaybackValidation(player, fn) {
@ -831,7 +829,7 @@
self.getPlaybackInfo = function (itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId) {
var deferred = DeferredBuilder.Deferred();
return new Promise(function (resolve, reject) {
require(['localassetmanager'], function () {
@ -844,27 +842,22 @@
var playbackInfo = getPlaybackInfoFromLocalMediaSource(itemId, deviceProfile, startPosition, localMediaSource);
deferred.resolveWith(null, [playbackInfo]);
resolve(playbackInfo);
return;
}
getPlaybackInfoWithoutLocalMediaSource(itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId, deferred);
getPlaybackInfoWithoutLocalMediaSource(itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId, resolve, reject);
});
return;
}
getPlaybackInfoWithoutLocalMediaSource(itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId, deferred);
getPlaybackInfoWithoutLocalMediaSource(itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId, resolve, reject);
});
});
return deferred.promise();
}
function getPlaybackInfoWithoutLocalMediaSource(itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId, deferred) {
self.getPlaybackInfoInternal(itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId).then(function (result) {
deferred.resolveWith(null, [result]);
}, function () {
deferred.reject();
});
function getPlaybackInfoWithoutLocalMediaSource(itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId, resolve, reject) {
self.getPlaybackInfoInternal(itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId).then(resolve, reject);
}
self.getPlaybackInfoInternal = function (itemId, deviceProfile, startPosition, mediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId) {
@ -934,18 +927,18 @@
self.supportsDirectPlay = function (mediaSource) {
var deferred = $.Deferred();
return new Promise(function (resolve, reject) {
if (mediaSource.SupportsDirectPlay) {
if (mediaSource.Protocol == 'Http' && !mediaSource.RequiredHttpHeaders.length) {
// If this is the only way it can be played, then allow it
if (!mediaSource.SupportsDirectStream && !mediaSource.SupportsTranscoding) {
deferred.resolveWith(null, [true]);
resolve(true);
}
else {
var val = mediaSource.Path.toLowerCase().replace('https:', 'http').indexOf(ApiClient.serverAddress().toLowerCase().replace('https:', 'http').substring(0, 14)) == 0;
deferred.resolveWith(null, [val]);
resolve(val);
}
}
@ -955,15 +948,15 @@
LocalAssetManager.fileExists(mediaSource.Path).then(function (exists) {
console.log('LocalAssetManager.fileExists: path: ' + mediaSource.Path + ' result: ' + exists);
deferred.resolveWith(null, [exists]);
resolve(exists);
});
});
}
}
else {
deferred.resolveWith(null, [false]);
resolve(false);
}
return deferred.promise();
});
};
self.showPlayerSelection = showPlayerSelection;

View File

@ -1025,7 +1025,7 @@
elem.classList.remove('hide');
if (!browserInfo.animate) {
if (!browserInfo.animate || browserInfo.mobile) {
return;
}

View File

@ -24,6 +24,14 @@
self.getTargets = function () {
return new Promise(function (resolve, reject) {
resolve(self.getTargetsInternal());
});
};
self.getTargetsInternal = function () {
var targets = [{
name: Globalize.translate('MyDevice'),
id: AppInfo.deviceId,
@ -823,13 +831,11 @@
function getOptimalMediaSource(mediaType, versions) {
var deferred = $.Deferred();
var promises = versions.map(function (v) {
return MediaController.supportsDirectPlay(v);
});
Promise.all(promises).then(function (responses) {
return Promise.all(promises).then(function (responses) {
for (var i = 0, length = versions.length; i < length; i++) {
versions[i].enableDirectPlay = responses[i] || false;
@ -852,10 +858,8 @@
return s.SupportsTranscoding;
})[0];
deferred.resolveWith(null, [optimalVersion]);
return optimalVersion;
});
return deferred.promise();
}
self.createStreamInfo = function (type, item, mediaSource, startPosition) {
@ -1021,8 +1025,8 @@
return;
}
var onBitrateDetected = function() {
self.getDeviceProfile().then(function(deviceProfile) {
var onBitrateDetected = function () {
self.getDeviceProfile().then(function (deviceProfile) {
playOnDeviceProfileCreated(deviceProfile, item, startPosition, callback);
});
};
@ -1540,13 +1544,11 @@
self.getPlayerState = function () {
var deferred = $.Deferred();
return new Promise(function (resolve, reject) {
var result = self.getPlayerStateInternal(self.currentMediaRenderer, self.currentItem, self.currentMediaSource);
deferred.resolveWith(null, [result]);
return deferred.promise();
resolve(result);
});
};
self.getPlayerStateInternal = function (mediaRenderer, item, mediaSource) {
@ -1967,16 +1969,17 @@
self.tryPair = function (target) {
var deferred = $.Deferred();
deferred.resolve();
return deferred.promise();
return new Promise(function (resolve, reject) {
resolve();
});
};
}
window.MediaPlayer = new mediaPlayer();
window.MediaController.registerPlayer(window.MediaPlayer);
window.MediaController.setActivePlayer(window.MediaPlayer, window.MediaPlayer.getTargets()[0]);
window.MediaController.setActivePlayer(window.MediaPlayer, window.MediaPlayer.getTargetsInternal()[0]);
})(document, setTimeout, clearTimeout, screen, setInterval, window);

View File

@ -2,12 +2,12 @@
var supporterPlaybackKey = 'lastSupporterPlaybackMessage4';
function validatePlayback(deferred) {
function validatePlayback(resolve, reject) {
Dashboard.getPluginSecurityInfo().then(function (pluginSecurityInfo) {
if (pluginSecurityInfo.IsMBSupporter) {
deferred.resolve();
resolve();
} else {
var lastMessage = parseInt(appStorage.getItem(supporterPlaybackKey) || '0');
@ -16,13 +16,13 @@
// Don't show on the very first playback attempt
appStorage.setItem(supporterPlaybackKey, new Date().getTime());
deferred.resolve();
resolve();
}
else if ((new Date().getTime() - lastMessage) > 345600000) {
showPlaybackOverlay(deferred);
showPlaybackOverlay(resolve, reject);
} else {
deferred.resolve();
resolve();
}
}
});
@ -95,7 +95,7 @@
return html;
}
function showPlaybackOverlay(deferred) {
function showPlaybackOverlay(resolve, reject) {
require(['components/paperdialoghelper', 'paper-fab', 'paper-item-body', 'paper-icon-item'], function (paperDialogHelper) {
@ -135,7 +135,7 @@
dlg.addEventListener('iron-overlay-closed', function (e) {
appStorage.setItem(supporterPlaybackKey, new Date().getTime());
dlg.parentNode.removeChild(dlg);
deferred.resolve();
resolve();
});
paperDialogHelper.open(dlg);
@ -146,12 +146,12 @@
});
}
function validateSync(deferred) {
function validateSync(resolve, reject) {
Dashboard.getPluginSecurityInfo().then(function (pluginSecurityInfo) {
if (pluginSecurityInfo.IsMBSupporter) {
deferred.resolve();
resolve();
return;
}
@ -162,21 +162,19 @@
Dashboard.hideLoadingMsg();
if (registrationInfo.IsRegistered) {
deferred.resolve();
resolve();
return;
}
Dashboard.alert({
message: Globalize.translate('HeaderSyncRequiresSupporterMembership') + '<br/><p><a href="http://emby.media/premiere" target="_blank">' + Globalize.translate('ButtonLearnMore') + '</a></p>',
title: Globalize.translate('HeaderSync'),
callback: function () {
deferred.reject();
}
callback: reject
});
}, function () {
deferred.reject();
reject();
Dashboard.hideLoadingMsg();
Dashboard.alert({
@ -279,19 +277,17 @@
validateFeature: function (name) {
var deferred = DeferredBuilder.Deferred();
return new Promise(function (resolve, reject) {
if (name == 'playback') {
validatePlayback(deferred);
validatePlayback(resolve, reject);
} else if (name == 'livetv') {
deferred.resolve();
resolve();
} else if (name == 'sync') {
validateSync(deferred);
validateSync(resolve, reject);
} else {
deferred.resolve();
resolve();
}
return deferred.promise();
});
}
};

View File

@ -167,7 +167,7 @@
self.getPlayerState = function () {
var deferred = $.Deferred();
return new Promise(function (resolve, reject) {
var apiClient = window.ApiClient;
@ -186,13 +186,12 @@
session = getPlayerState(session);
}
deferred.resolveWith(null, [session]);
resolve(session);
});
} else {
deferred.resolveWith(null, [{}]);
resolve({});
}
return deferred.promise();
});
};
var pollInterval;
@ -263,7 +262,7 @@
self.getTargets = function () {
var deferred = $.Deferred();
return new Promise(function (resolve, reject) {
var sessionQuery = {
ControllableByUserId: Dashboard.getCurrentUserId()
@ -291,25 +290,25 @@
};
});
deferred.resolveWith(null, [targets]);
resolve(targets);
}, function () {
deferred.reject();
reject();
});
} else {
deferred.resolveWith(null, []);
resolve([]);
}
return deferred.promise();
});
};
self.tryPair = function(target) {
var deferred = $.Deferred();
deferred.resolve();
return deferred.promise();
return new Promise(function (resolve, reject) {
resolve();
});
};
}

View File

@ -2,9 +2,7 @@
function getUserViews(userId) {
var deferred = $.Deferred();
ApiClient.getUserViews({}, userId).then(function (result) {
return ApiClient.getUserViews({}, userId).then(function (result) {
var items = result.Items;
@ -44,10 +42,8 @@
}
}
deferred.resolveWith(null, [list]);
return list;
});
return deferred.promise();
}
function enableScrollX() {