rework some initial dependency loading

This commit is contained in:
Luke Pulverenti 2015-12-01 15:48:55 -05:00
parent 9227ac0989
commit 5200b37787
13 changed files with 121 additions and 467 deletions

View File

@ -1,314 +0,0 @@
(function (global) {
function isArray(arr) {
return Object.prototype.toString.call(arr) === '[object Array]';
}
function foreach(arr, handler) {
if (isArray(arr)) {
for (var i = 0; i < arr.length; i++) {
handler(arr[i]);
}
}
else
handler(arr);
}
function D(fn) {
var status = 'pending',
doneFuncs = [],
failFuncs = [],
progressFuncs = [],
resultArgs = null,
promise = {
done: function () {
for (var i = 0; i < arguments.length; i++) {
// skip any undefined or null arguments
if (!arguments[i]) {
continue;
}
if (isArray(arguments[i])) {
var arr = arguments[i];
for (var j = 0; j < arr.length; j++) {
// immediately call the function if the deferred has been resolved
if (status === 'resolved') {
arr[j].apply(this, resultArgs);
}
doneFuncs.push(arr[j]);
}
}
else {
// immediately call the function if the deferred has been resolved
if (status === 'resolved') {
arguments[i].apply(this, resultArgs);
}
doneFuncs.push(arguments[i]);
}
}
return this;
},
fail: function () {
for (var i = 0; i < arguments.length; i++) {
// skip any undefined or null arguments
if (!arguments[i]) {
continue;
}
if (isArray(arguments[i])) {
var arr = arguments[i];
for (var j = 0; j < arr.length; j++) {
// immediately call the function if the deferred has been resolved
if (status === 'rejected') {
arr[j].apply(this, resultArgs);
}
failFuncs.push(arr[j]);
}
}
else {
// immediately call the function if the deferred has been resolved
if (status === 'rejected') {
arguments[i].apply(this, resultArgs);
}
failFuncs.push(arguments[i]);
}
}
return this;
},
always: function () {
return this.done.apply(this, arguments).fail.apply(this, arguments);
},
progress: function () {
for (var i = 0; i < arguments.length; i++) {
// skip any undefined or null arguments
if (!arguments[i]) {
continue;
}
if (isArray(arguments[i])) {
var arr = arguments[i];
for (var j = 0; j < arr.length; j++) {
// immediately call the function if the deferred has been resolved
if (status === 'pending') {
progressFuncs.push(arr[j]);
}
}
}
else {
// immediately call the function if the deferred has been resolved
if (status === 'pending') {
progressFuncs.push(arguments[i]);
}
}
}
return this;
},
then: function () {
// fail callbacks
if (arguments.length > 1 && arguments[1]) {
this.fail(arguments[1]);
}
// done callbacks
if (arguments.length > 0 && arguments[0]) {
this.done(arguments[0]);
}
// notify callbacks
if (arguments.length > 2 && arguments[2]) {
this.progress(arguments[2]);
}
},
promise: function (obj) {
if (obj == null) {
return promise;
} else {
for (var i in promise) {
obj[i] = promise[i];
}
return obj;
}
},
state: function () {
return status;
},
debug: function () {
console.log('[debug]', doneFuncs, failFuncs, status);
},
isRejected: function () {
return status === 'rejected';
},
isResolved: function () {
return status === 'resolved';
},
pipe: function (done, fail, progress) {
return D(function (def) {
foreach(done, function (func) {
// filter function
if (typeof func === 'function') {
deferred.done(function () {
var returnval = func.apply(this, arguments);
// if a new deferred/promise is returned, its state is passed to the current deferred/promise
if (returnval && typeof returnval === 'function') {
returnval.promise().then(def.resolve, def.reject, def.notify);
}
else { // if new return val is passed, it is passed to the piped done
def.resolve(returnval);
}
});
}
else {
deferred.done(def.resolve);
}
});
foreach(fail, function (func) {
if (typeof func === 'function') {
deferred.fail(function () {
var returnval = func.apply(this, arguments);
if (returnval && typeof returnval === 'function') {
returnval.promise().then(def.resolve, def.reject, def.notify);
} else {
def.reject(returnval);
}
});
}
else {
deferred.fail(def.reject);
}
});
}).promise();
}
},
deferred = {
resolveWith: function (context) {
if (status === 'pending') {
status = 'resolved';
var args = resultArgs = (arguments.length > 1) ? arguments[1] : [];
for (var i = 0; i < doneFuncs.length; i++) {
doneFuncs[i].apply(context, args);
}
}
return this;
},
rejectWith: function (context) {
if (status === 'pending') {
status = 'rejected';
var args = resultArgs = (arguments.length > 1) ? arguments[1] : [];
for (var i = 0; i < failFuncs.length; i++) {
failFuncs[i].apply(context, args);
}
}
return this;
},
notifyWith: function (context) {
if (status === 'pending') {
var args = resultArgs = (arguments.length > 1) ? arguments[1] : [];
for (var i = 0; i < progressFuncs.length; i++) {
progressFuncs[i].apply(context, args);
}
}
return this;
},
resolve: function () {
return this.resolveWith(this, arguments);
},
reject: function () {
return this.rejectWith(this, arguments);
},
notify: function () {
return this.notifyWith(this, arguments);
}
}
var obj = promise.promise(deferred);
if (fn) {
fn.apply(obj, [obj]);
}
return obj;
}
D.when = function () {
if (arguments.length < 2) {
var obj = arguments.length ? arguments[0] : undefined;
if (obj && (typeof obj.isResolved === 'function' && typeof obj.isRejected === 'function')) {
return obj.promise();
}
else {
return D().resolve(obj).promise();
}
}
else {
return (function (args) {
var df = D(),
size = args.length,
done = 0,
rp = new Array(size); // resolve params: params of each resolve, we need to track down them to be able to pass them in the correct order if the master needs to be resolved
for (var i = 0; i < args.length; i++) {
(function (j) {
var obj = null;
if (args[j].done) {
args[j].done(function () { rp[j] = (arguments.length < 2) ? arguments[0] : arguments; if (++done == size) { df.resolve.apply(df, rp); } })
.fail(function () { df.reject(arguments); });
} else {
obj = args[j];
args[j] = new Deferred();
args[j].done(function () { rp[j] = (arguments.length < 2) ? arguments[0] : arguments; if (++done == size) { df.resolve.apply(df, rp); } })
.fail(function () { df.reject(arguments); }).resolve(obj);
}
})(i);
}
return df.promise();
})(arguments);
}
}
global.Deferred = D;
})(window);
(function (globalScope) {
globalScope.DeferredBuilder = {
Deferred: function () {
return new globalScope.Deferred();
},
when: function (promises) {
return globalScope.Deferred.when(promises);
}
};
})(window);

View File

@ -1148,7 +1148,7 @@
return;
}
require(['connectservice'], function () {
require(['connectservice', 'cryptojs-md5'], function () {
var md5 = self.getConnectPasswordHash(password);
@ -1208,7 +1208,7 @@
return;
}
require(['connectservice'], function () {
require(['connectservice', 'cryptojs-md5'], function () {
var md5 = self.getConnectPasswordHash(password);

View File

@ -1,35 +0,0 @@
(function (globalScope) {
if (!globalScope.MediaBrowser) {
globalScope.MediaBrowser = {};
}
globalScope.MediaBrowser.generateDeviceId = function (keyName, seed) {
keyName = keyName || 'randomId';
var keys = [];
keys.push(navigator.userAgent);
keys.push((navigator.cpuClass || ""));
if (seed) {
keys.push(seed);
}
var randomId = '';
// Since the above is not guaranteed to be unique per device, add a little more
randomId = appStorage.getItem(keyName);
if (!randomId) {
randomId = new Date().getTime();
appStorage.setItem(keyName, randomId.toString());
}
keys.push(randomId);
return CryptoJS.SHA1(keys.join('|')).toString();
};
})(window);

View File

@ -39,6 +39,6 @@
"commit": "cec8e49744a1e18b14a711eea77e201bb70de544"
},
"_source": "git://github.com/desandro/doc-ready.git",
"_target": "1.0.x",
"_target": "~1.0.4",
"_originalSource": "doc-ready"
}

View File

@ -28,14 +28,14 @@
"iron-component-page": "polymerelements/iron-component-page#^1.0.0"
},
"ignore": [],
"homepage": "https://github.com/polymerelements/iron-flex-layout",
"homepage": "https://github.com/PolymerElements/iron-flex-layout",
"_release": "1.2.2",
"_resolution": {
"type": "version",
"tag": "v1.2.2",
"commit": "41c4f35be1368afb770312b907a258175565dbdf"
},
"_source": "git://github.com/polymerelements/iron-flex-layout.git",
"_source": "git://github.com/PolymerElements/iron-flex-layout.git",
"_target": "^1.0.0",
"_originalSource": "polymerelements/iron-flex-layout"
"_originalSource": "PolymerElements/iron-flex-layout"
}

View File

@ -36,7 +36,7 @@
"tag": "v1.0.8",
"commit": "e9a66727f3da0446f04956d4e4f1dcd51cdec2ff"
},
"_source": "git://github.com/PolymerElements/iron-selector.git",
"_source": "git://github.com/polymerelements/iron-selector.git",
"_target": "^1.0.0",
"_originalSource": "PolymerElements/iron-selector"
"_originalSource": "polymerelements/iron-selector"
}

View File

@ -178,6 +178,8 @@
window.ImageStore = self;
}
new imageFileStore();
require(['cryptojs-sha1'], function () {
new imageFileStore();
});
})();

View File

@ -130,15 +130,20 @@
return;
}
testDeviceId(device.uuid).then(function (isUnlocked) {
var legacyDeviceId = NativeIapManager.getLegacyDeviceId();
if (legacyDeviceId) {
testDeviceId(legacyDeviceId).then(function(isUnlocked) {
if (isUnlocked) {
deferred.resolveWith(null, [true]);
return;
}
if (isUnlocked) {
deferred.resolveWith(null, [true]);
return;
}
deferred.resolveWith(null, [false]);
});
} else {
deferred.resolveWith(null, [false]);
});
}
});
}

View File

@ -149,6 +149,8 @@
window.ImageStore = self;
}
new indexedDbBlobImageStore();
require(['cryptojs-sha1'], function () {
new indexedDbBlobImageStore();
});
})();

View File

@ -1308,7 +1308,7 @@ $(document).on('pageshow', "#dashboardPage", DashboardPage.onPageShow).on('pageb
function takeTour(page, userId) {
Dashboard.loadSwipebox().then(function () {
require(['swipebox'], function () {
$.swipebox([
{ href: 'css/images/tour/dashboard/dashboard.png', title: Globalize.translate('DashboardTourDashboard') },

View File

@ -165,7 +165,7 @@
function takeTour(page, userId) {
Dashboard.loadSwipebox().then(function () {
require(['swipebox'], function () {
$.swipebox([
{ href: 'css/images/tour/web/tourcontent.jpg', title: Globalize.translate('WebClientTourContent') },

View File

@ -154,7 +154,7 @@
index = 0;
}
Dashboard.loadSwipebox().then(function () {
require(['swipebox'], function () {
$.swipebox(slideshowItems, {
initialIndexOnArray: index,

View File

@ -1522,71 +1522,6 @@ var Dashboard = {
}
},
getAppInfo: function (appName, appVersion, deviceId, deviceName) {
function generateDeviceName() {
var name;
if (browserInfo.chrome) {
name = "Chrome";
} else if (browserInfo.edge) {
name = "Edge";
} else if (browserInfo.mozilla) {
name = "Firefox";
} else if (browserInfo.msie) {
name = "Internet Explorer";
} else {
name = "Web Browser";
}
if (browserInfo.version) {
name += " " + browserInfo.version;
}
if (browserInfo.ipad) {
name += " Ipad";
} else if (browserInfo.iphone) {
name += " Iphone";
} else if (browserInfo.android) {
name += " Android";
}
return name;
}
appVersion = appVersion || window.dashboardVersion;
appName = appName || "Emby Web Client";
deviceName = deviceName || generateDeviceName();
var seed = [];
var keyName = 'randomId';
deviceId = deviceId || MediaBrowser.generateDeviceId(keyName, seed.join(','));
return {
appName: appName,
appVersion: appVersion,
deviceName: deviceName,
deviceId: deviceId
};
},
loadSwipebox: function () {
var deferred = DeferredBuilder.Deferred();
Dashboard.importCss('bower_components/swipebox/src/css/swipebox.min.css');
require([
'bower_components/swipebox/src/js/jquery.swipebox.min'
], function () {
deferred.resolve();
});
return deferred.promise();
},
ready: function (fn) {
Dashboard.initPromise.then(fn);
@ -1915,9 +1850,11 @@ var AppInfo = {};
paths: paths
});
define("cryptojs-sha1", ["apiclient/sha1"]);
define("cryptojs-md5", ["apiclient/md5"]);
}
function init(promiseResolve, capabilities, appName, appVersion, deviceId, deviceName) {
function init(promiseResolve, hostingAppInfo) {
if (Dashboard.isRunningInCordova() && browserInfo.android) {
define("appstorage", ["cordova/android/appstorage"]);
@ -2019,7 +1956,7 @@ var AppInfo = {};
return Hammer;
});
define("cryptojs-sha1", ["apiclient/sha1"]);
define("swipebox", ['bower_components/swipebox/src/js/jquery.swipebox.min', "css!bower_components/swipebox/src/css/swipebox.min.css"]);
define("contentuploader", ["apiclient/sync/contentuploader"]);
define("serversync", ["apiclient/sync/serversync"]);
@ -2037,10 +1974,6 @@ var AppInfo = {};
var deps = [];
if (!deviceId) {
deps.push('cryptojs-sha1');
}
if (!window.fetch) {
deps.push('bower_components/fetch/fetch');
}
@ -2053,16 +1986,15 @@ var AppInfo = {};
require(deps, function () {
var baseInfo = Dashboard.getAppInfo(appName, appVersion, deviceId, deviceName);
for (var i in baseInfo) {
AppInfo[i] = baseInfo[i];
for (var i in hostingAppInfo) {
AppInfo[i] = hostingAppInfo[i];
}
initAfterDependencies(promiseResolve, capabilities);
initAfterDependencies(promiseResolve);
});
}
function initAfterDependencies(promiseResolve, capabilities) {
function initAfterDependencies(promiseResolve) {
var drawer = document.querySelector('.mainDrawerPanel');
drawer.classList.remove('mainDrawerPanelPreInit');
@ -2093,7 +2025,6 @@ var AppInfo = {};
deps.push('apiclient/connectionmanager');
deps.push('apiclient/deferred');
deps.push('apiclient/credentials');
deps.push('apiclient/md5');
deps.push('thirdparty/jquerymobile-1.4.5/jquery.mobile.custom.js');
@ -2132,6 +2063,8 @@ var AppInfo = {};
}
}
var capabilities = Dashboard.capabilities();
capabilities.DeviceProfile = MediaPlayer.getDeviceProfile(Math.max(screen.height, screen.width));
var connectionManagerPromise = createConnectionManager(capabilities);
@ -2273,34 +2206,97 @@ var AppInfo = {};
}
}
function initCordovaWithDeviceId(deferred, deviceId) {
function getCordovaHostingAppInfo() {
cordova.getAppVersion.getVersionNumber(function (appVersion) {
var capablities = Dashboard.capabilities();
return new Promise(function (resolve, reject) {
var name = browserInfo.android ? "Emby for Android Mobile" : (browserInfo.safari ? "Emby for iOS" : "Emby Mobile");
document.addEventListener("deviceready", function () {
// Remove special characters
var cleanDeviceName = device.model.replace(/[^\w\s]/gi, '');
cordova.getAppVersion.getVersionNumber(function (appVersion) {
init(deferred, capablities, name, appVersion, deviceId, cleanDeviceName);
var name = browserInfo.android ? "Emby for Android Mobile" : (browserInfo.safari ? "Emby for iOS" : "Emby Mobile");
// Remove special characters
var cleanDeviceName = device.model.replace(/[^\w\s]/gi, '');
resolve({
deviceId: device.uuid,
deviceName: cleanDeviceName,
appName: name,
appVersion: appVersion
});
});
}, false);
});
}
function initCordova(deferred) {
function getWebHostingAppInfo() {
document.addEventListener("deviceready", function () {
return new Promise(function (resolve, reject) {
window.plugins.uniqueDeviceID.get(function (uuid) {
var deviceName;
initCordovaWithDeviceId(deferred, uuid);
if (browserInfo.chrome) {
deviceName = "Chrome";
} else if (browserInfo.edge) {
deviceName = "Edge";
} else if (browserInfo.mozilla) {
deviceName = "Firefox";
} else if (browserInfo.msie) {
deviceName = "Internet Explorer";
} else {
deviceName = "Web Browser";
}
}, function () {
if (browserInfo.version) {
deviceName += " " + browserInfo.version;
}
// Failure. Use cordova uuid
initCordovaWithDeviceId(deferred, device.uuid);
});
}, false);
if (browserInfo.ipad) {
deviceName += " Ipad";
} else if (browserInfo.iphone) {
deviceName += " Iphone";
} else if (browserInfo.android) {
deviceName += " Android";
}
function onDeviceAdAcquired(id) {
resolve({
deviceId: id,
deviceName: deviceName,
appName: "Emby Web Client",
appVersion: window.dashboardVersion
});
}
var deviceId = appStorage.getItem('_deviceId');
if (deviceId) {
onDeviceAdAcquired(deviceId);
} else {
require(['cryptojs-md5'], function () {
var keys = [];
keys.push(navigator.userAgent);
keys.push((navigator.cpuClass || ""));
var randomId = CryptoJS.SHA1(keys.join('|')).toString();
appStorage.setItem('_deviceId', randomId);
onDeviceAdAcquired(randomId);
});
}
});
}
function getHostingAppInfo() {
if (Dashboard.isRunningInCordova()) {
return getCordovaHostingAppInfo();
}
return getWebHostingAppInfo();
}
function setBrowserInfo(isMobile) {
@ -2372,7 +2368,6 @@ var AppInfo = {};
initialDependencies.push('isMobile');
initialDependencies.push('apiclient/logger');
initialDependencies.push('apiclient/store');
initialDependencies.push('apiclient/device');
initialDependencies.push('scripts/extensions');
var supportsNativeWebComponents = 'registerElement' in document && 'content' in document.createElement('template');
@ -2395,11 +2390,10 @@ var AppInfo = {};
link.rel = 'import';
link.onload = function () {
if (Dashboard.isRunningInCordova()) {
initCordova(resolve);
} else {
init(resolve, Dashboard.capabilities());
}
getHostingAppInfo().then(function (hostingAppInfo) {
init(resolve, hostingAppInfo);
});
};
link.href = "vulcanize-out.html?v=" + window.dashboardVersion;
document.head.appendChild(link);