| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 if (!content) { | 231 if (!content) { |
| 232 console.error("Failed to load resource: " + path); | 232 console.error("Failed to load resource: " + path); |
| 233 return; | 233 return; |
| 234 } | 234 } |
| 235 var sourceURL = appendSourceURL ? Runtime.resolveSourceURL(path) : ""; | 235 var sourceURL = appendSourceURL ? Runtime.resolveSourceURL(path) : ""; |
| 236 Runtime.cachedResources[path] = content + sourceURL; | 236 Runtime.cachedResources[path] = content + sourceURL; |
| 237 } | 237 } |
| 238 } | 238 } |
| 239 | 239 |
| 240 /** | 240 /** |
| 241 * @return {boolean} | |
| 242 */ | |
| 243 Runtime.isReleaseMode = function() | |
| 244 { | |
| 245 return !!allDescriptors.length; | |
| 246 } | |
| 247 | |
| 248 /** | |
| 249 * @param {string} appName | 241 * @param {string} appName |
| 250 * @return {!Promise.<undefined>} | 242 * @return {!Promise.<undefined>} |
| 251 */ | 243 */ |
| 252 Runtime.startApplication = function(appName) | 244 Runtime.startApplication = function(appName) |
| 253 { | 245 { |
| 254 console.timeStamp("Runtime.startApplication"); | 246 console.timeStamp("Runtime.startApplication"); |
| 255 | 247 |
| 256 var allDescriptorsByName = {}; | 248 var allDescriptorsByName = {}; |
| 257 for (var i = 0; Runtime.isReleaseMode() && i < allDescriptors.length; ++i) { | 249 for (var i = 0; i < allDescriptors.length; ++i) { |
| 258 var d = allDescriptors[i]; | 250 var d = allDescriptors[i]; |
| 259 allDescriptorsByName[d["name"]] = d; | 251 allDescriptorsByName[d["name"]] = d; |
| 260 } | 252 } |
| 261 | 253 |
| 262 var applicationPromise; | 254 var applicationPromise; |
| 263 if (applicationDescriptor) | 255 if (applicationDescriptor) |
| 264 applicationPromise = Promise.resolve(applicationDescriptor); | 256 applicationPromise = Promise.resolve(applicationDescriptor); |
| 265 else | 257 else |
| 266 applicationPromise = loadResourcePromise(appName + ".json").then(JSON.pa
rse.bind(JSON)); | 258 applicationPromise = loadResourcePromise(appName + ".json").then(JSON.pa
rse.bind(JSON)); |
| 267 | 259 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 289 } | 281 } |
| 290 | 282 |
| 291 return Promise.all(moduleJSONPromises).then(instantiateRuntime); | 283 return Promise.all(moduleJSONPromises).then(instantiateRuntime); |
| 292 | 284 |
| 293 /** | 285 /** |
| 294 * @param {!Array.<!Object>} moduleDescriptors | 286 * @param {!Array.<!Object>} moduleDescriptors |
| 295 * @return {!Promise.<undefined>} | 287 * @return {!Promise.<undefined>} |
| 296 */ | 288 */ |
| 297 function instantiateRuntime(moduleDescriptors) | 289 function instantiateRuntime(moduleDescriptors) |
| 298 { | 290 { |
| 299 for (var i = 0; !Runtime.isReleaseMode() && i < moduleDescriptors.le
ngth; ++i) { | 291 for (var i = 0; i < moduleDescriptors.length; ++i) { |
| 300 moduleDescriptors[i]["name"] = configuration[i]["name"]; | 292 moduleDescriptors[i].name = configuration[i]["name"]; |
| 301 moduleDescriptors[i]["condition"] = configuration[i]["condition"
]; | 293 moduleDescriptors[i].condition = configuration[i]["condition"]; |
| 294 moduleDescriptors[i].remote = configuration[i]["type"] === "remo
te"; |
| 302 } | 295 } |
| 303 self.runtime = new Runtime(moduleDescriptors); | 296 self.runtime = new Runtime(moduleDescriptors); |
| 304 if (coreModuleNames) | 297 if (coreModuleNames) |
| 305 return /** @type {!Promise<undefined>} */ (self.runtime._loadAut
oStartModules(coreModuleNames)); | 298 return /** @type {!Promise<undefined>} */ (self.runtime._loadAut
oStartModules(coreModuleNames)); |
| 306 return Promise.resolve(); | 299 return Promise.resolve(); |
| 307 } | 300 } |
| 308 } | 301 } |
| 309 } | 302 } |
| 310 | 303 |
| 311 /** | 304 /** |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 return this._modulesMap[moduleName]._loadPromise(); | 483 return this._modulesMap[moduleName]._loadPromise(); |
| 491 }, | 484 }, |
| 492 | 485 |
| 493 /** | 486 /** |
| 494 * @param {!Array.<string>} moduleNames | 487 * @param {!Array.<string>} moduleNames |
| 495 * @return {!Promise.<!Array.<*>>} | 488 * @return {!Promise.<!Array.<*>>} |
| 496 */ | 489 */ |
| 497 _loadAutoStartModules: function(moduleNames) | 490 _loadAutoStartModules: function(moduleNames) |
| 498 { | 491 { |
| 499 var promises = []; | 492 var promises = []; |
| 500 for (var i = 0; i < moduleNames.length; ++i) { | 493 for (var i = 0; i < moduleNames.length; ++i) |
| 501 if (Runtime.isReleaseMode()) | 494 promises.push(this.loadModulePromise(moduleNames[i])); |
| 502 this._modulesMap[moduleNames[i]]._loaded = true; | |
| 503 else | |
| 504 promises.push(this.loadModulePromise(moduleNames[i])); | |
| 505 } | |
| 506 return Promise.all(promises); | 495 return Promise.all(promises); |
| 507 }, | 496 }, |
| 508 | 497 |
| 509 /** | 498 /** |
| 510 * @param {!Runtime.Extension} extension | 499 * @param {!Runtime.Extension} extension |
| 511 * @param {?function(function(new:Object)):boolean} predicate | 500 * @param {?function(function(new:Object)):boolean} predicate |
| 512 * @return {boolean} | 501 * @return {boolean} |
| 513 */ | 502 */ |
| 514 _checkExtensionApplicability: function(extension, predicate) | 503 _checkExtensionApplicability: function(extension, predicate) |
| 515 { | 504 { |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 * @type {!Array.<string>|undefined} | 680 * @type {!Array.<string>|undefined} |
| 692 */ | 681 */ |
| 693 this.dependencies; | 682 this.dependencies; |
| 694 | 683 |
| 695 /** | 684 /** |
| 696 * @type {!Array.<string>} | 685 * @type {!Array.<string>} |
| 697 */ | 686 */ |
| 698 this.scripts; | 687 this.scripts; |
| 699 | 688 |
| 700 /** | 689 /** |
| 690 * @type {string|undefined} |
| 691 */ |
| 692 this.condition; |
| 693 |
| 694 /** |
| 701 * @type {boolean|undefined} | 695 * @type {boolean|undefined} |
| 702 */ | 696 */ |
| 703 this.remote; | 697 this.remote; |
| 704 } | 698 } |
| 705 | 699 |
| 706 /** | 700 /** |
| 707 * @constructor | 701 * @constructor |
| 708 */ | 702 */ |
| 709 Runtime.ExtensionDescriptor = function() | 703 Runtime.ExtensionDescriptor = function() |
| 710 { | 704 { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 770 if (!content) | 764 if (!content) |
| 771 throw new Error(fullName + " not preloaded. Check module.json"); | 765 throw new Error(fullName + " not preloaded. Check module.json"); |
| 772 return content; | 766 return content; |
| 773 }, | 767 }, |
| 774 | 768 |
| 775 /** | 769 /** |
| 776 * @return {!Promise.<undefined>} | 770 * @return {!Promise.<undefined>} |
| 777 */ | 771 */ |
| 778 _loadPromise: function() | 772 _loadPromise: function() |
| 779 { | 773 { |
| 780 if (this._loaded) | |
| 781 return Promise.resolve(); | |
| 782 | |
| 783 if (!this.enabled()) | 774 if (!this.enabled()) |
| 784 return Promise.reject(new Error("Module " + this._name + " is not en
abled")); | 775 return Promise.reject(new Error("Module " + this._name + " is not en
abled")); |
| 785 | 776 |
| 786 if (this._pendingLoadPromise) | 777 if (this._pendingLoadPromise) |
| 787 return this._pendingLoadPromise; | 778 return this._pendingLoadPromise; |
| 788 | 779 |
| 789 var dependencies = this._descriptor.dependencies; | 780 var dependencies = this._descriptor.dependencies; |
| 790 var dependencyPromises = []; | 781 var dependencyPromises = []; |
| 791 for (var i = 0; dependencies && i < dependencies.length; ++i) | 782 for (var i = 0; dependencies && i < dependencies.length; ++i) |
| 792 dependencyPromises.push(this._manager._modulesMap[dependencies[i]]._
loadPromise()); | 783 dependencyPromises.push(this._manager._modulesMap[dependencies[i]]._
loadPromise()); |
| 793 | 784 |
| 794 this._pendingLoadPromise = Promise.all(dependencyPromises) | 785 this._pendingLoadPromise = Promise.all(dependencyPromises) |
| 795 .then(this._loadResources.bind(this)) | 786 .then(this._loadResources.bind(this)) |
| 796 .then(this._loadScripts.bind(this)) | 787 .then(this._loadScripts.bind(this)) |
| 797 .then(markAsLoaded.bind(this)); | 788 .then(() => this._loadedForTest = true); |
| 798 | 789 |
| 799 return this._pendingLoadPromise; | 790 return this._pendingLoadPromise; |
| 800 | |
| 801 /** | |
| 802 * @this {Runtime.Module} | |
| 803 */ | |
| 804 function markAsLoaded() | |
| 805 { | |
| 806 delete this._pendingLoadPromise; | |
| 807 this._loaded = true; | |
| 808 } | |
| 809 }, | 791 }, |
| 810 | 792 |
| 811 /** | 793 /** |
| 812 * @return {!Promise.<undefined>} | 794 * @return {!Promise.<undefined>} |
| 813 * @this {Runtime.Module} | 795 * @this {Runtime.Module} |
| 814 */ | 796 */ |
| 815 _loadResources: function() | 797 _loadResources: function() |
| 816 { | 798 { |
| 817 var resources = this._descriptor["resources"]; | 799 var resources = this._descriptor["resources"]; |
| 818 if (!resources) | 800 if (!resources || !resources.length) |
| 819 return Promise.resolve(); | 801 return Promise.resolve(); |
| 820 var promises = []; | 802 var promises = []; |
| 821 for (var i = 0; i < resources.length; ++i) { | 803 for (var i = 0; i < resources.length; ++i) { |
| 822 var url = this._modularizeURL(resources[i]); | 804 var url = this._modularizeURL(resources[i]); |
| 823 promises.push(Runtime.loadResourceIntoCache(url, true)); | 805 promises.push(Runtime.loadResourceIntoCache(url, true)); |
| 824 } | 806 } |
| 825 return Promise.all(promises).then(undefined); | 807 return Promise.all(promises).then(undefined); |
| 826 }, | 808 }, |
| 827 | 809 |
| 828 /** | 810 /** |
| 829 * @return {!Promise.<undefined>} | 811 * @return {!Promise.<undefined>} |
| 830 */ | 812 */ |
| 831 _loadScripts: function() | 813 _loadScripts: function() |
| 832 { | 814 { |
| 833 if (!this._descriptor.scripts) | 815 if (!this._descriptor.scripts || !this._descriptor.scripts.length) |
| 834 return Promise.resolve(); | 816 return Promise.resolve(); |
| 835 | 817 return loadScriptsPromise(this._descriptor.scripts.map(this._modularizeU
RL, this), this._remoteBase()); |
| 836 if (Runtime.isReleaseMode()) | |
| 837 return loadScriptsPromise([this._name + "_module.js"], this._remoteB
ase()); | |
| 838 | |
| 839 return loadScriptsPromise(this._descriptor.scripts.map(this._modularizeU
RL, this)); | |
| 840 }, | 818 }, |
| 841 | 819 |
| 842 /** | 820 /** |
| 843 * @param {string} resourceName | 821 * @param {string} resourceName |
| 844 */ | 822 */ |
| 845 _modularizeURL: function(resourceName) | 823 _modularizeURL: function(resourceName) |
| 846 { | 824 { |
| 847 return normalizePath(this._name + "/" + resourceName); | 825 return normalizePath(this._name + "/" + resourceName); |
| 848 }, | 826 }, |
| 849 | 827 |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1211 { | 1189 { |
| 1212 var sourceURL = self.location.href; | 1190 var sourceURL = self.location.href; |
| 1213 if (self.location.search) | 1191 if (self.location.search) |
| 1214 sourceURL = sourceURL.replace(self.location.search, ""); | 1192 sourceURL = sourceURL.replace(self.location.search, ""); |
| 1215 sourceURL = sourceURL.substring(0, sourceURL.lastIndexOf("/") + 1) + path; | 1193 sourceURL = sourceURL.substring(0, sourceURL.lastIndexOf("/") + 1) + path; |
| 1216 return "\n/*# sourceURL=" + sourceURL + " */"; | 1194 return "\n/*# sourceURL=" + sourceURL + " */"; |
| 1217 } | 1195 } |
| 1218 | 1196 |
| 1219 /** @type {!Runtime} */ | 1197 /** @type {!Runtime} */ |
| 1220 var runtime; | 1198 var runtime; |
| OLD | NEW |