Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(345)

Unified Diff: third_party/polymer/components-chromium/core-shared-lib/core-shared-lib-extracted.js

Issue 592593002: Inline scripts were extracted from Polymer elements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: s/echo ""/echo/ Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/polymer/components-chromium/core-shared-lib/core-shared-lib-extracted.js
diff --git a/third_party/polymer/components-chromium/core-shared-lib/core-shared-lib-extracted.js b/third_party/polymer/components-chromium/core-shared-lib/core-shared-lib-extracted.js
new file mode 100644
index 0000000000000000000000000000000000000000..93627ba7b95b60c1750916e9a783a2927844fe1e
--- /dev/null
+++ b/third_party/polymer/components-chromium/core-shared-lib/core-shared-lib-extracted.js
@@ -0,0 +1,116 @@
+
+(function() {
+
+ Polymer('core-shared-lib',{
+
+ notifyEvent: 'core-shared-lib-load',
+
+ ready: function() {
+ if (!this.url && this.defaultUrl) {
+ this.url = this.defaultUrl;
+ }
+ },
+
+ urlChanged: function() {
+ require(this.url, this, this.callbackName);
+ },
+
+ provide: function() {
+ this.async('notify');
+ },
+
+ notify: function() {
+ this.fire(this.notifyEvent, arguments);
+ }
+
+ });
+
+ var apiMap = {};
+
+ function require(url, notifiee, callbackName) {
+ // make hashable string form url
+ var name = nameFromUrl(url);
+ // lookup existing loader instance
+ var loader = apiMap[name];
+ // create a loader as needed
+ if (!loader) {
+ loader = apiMap[name] = new Loader(name, url, callbackName);
+ }
+ loader.requestNotify(notifiee);
+ }
+
+ function nameFromUrl(url) {
+ return url.replace(/[\:\/\%\?\&\.\=\-]/g, '_') + '_api';
+ }
+
+ var Loader = function(name, url, callbackName) {
+ this.instances = [];
+ this.callbackName = callbackName;
+ if (this.callbackName) {
+ window[this.callbackName] = this.success.bind(this);
+ } else {
+ if (url.indexOf(this.callbackMacro) >= 0) {
+ this.callbackName = name + '_loaded';
+ window[this.callbackName] = this.success.bind(this);
+ url = url.replace(this.callbackMacro, this.callbackName);
+ } else {
+ // TODO(sjmiles): we should probably fallback to listening to script.load
+ throw 'core-shared-api: a %%callback%% parameter is required in the API url';
+ }
+ }
+ //
+ this.addScript(url);
+ };
+
+ Loader.prototype = {
+
+ callbackMacro: '%%callback%%',
+ loaded: false,
+
+ addScript: function(src) {
+ var script = document.createElement('script');
+ script.src = src;
+ script.onerror = this.error.bind(this);
+ var s = document.querySelector('script');
+ s.parentNode.insertBefore(script, s);
+ this.script = script;
+ },
+
+ removeScript: function() {
+ if (this.script.parentNode) {
+ this.script.parentNode.removeChild(this.script);
+ }
+ this.script = null;
+ },
+
+ error: function() {
+ this.cleanup();
+ },
+
+ success: function() {
+ this.loaded = true;
+ this.cleanup();
+ this.result = Array.prototype.slice.call(arguments);
+ this.instances.forEach(this.provide, this);
+ this.instances = null;
+ },
+
+ cleanup: function() {
+ delete window[this.callbackName];
+ },
+
+ provide: function(instance) {
+ instance.notify(instance, this.result);
+ },
+
+ requestNotify: function(instance) {
+ if (this.loaded) {
+ this.provide(instance);
+ } else {
+ this.instances.push(instance);
+ }
+ }
+
+ };
+
+})();

Powered by Google App Engine
This is Rietveld 408576698