Add an exponential backoff to rechecking the app list doodle.
[chromium-blink-merge.git] / third_party / polymer / components-chromium / core-shared-lib / core-shared-lib-extracted.js
blobcfb46066f9c0cf61302400fc29a239636196c522
2 (function() {
4 Polymer('core-shared-lib',{
6 notifyEvent: 'core-shared-lib-load',
8 ready: function() {
9 if (!this.url && this.defaultUrl) {
10 this.url = this.defaultUrl;
14 urlChanged: function() {
15 require(this.url, this, this.callbackName);
18 provide: function() {
19 this.async('notify');
22 notify: function() {
23 this.fire(this.notifyEvent, arguments);
26 });
28 var apiMap = {};
30 function require(url, notifiee, callbackName) {
31 // make hashable string form url
32 var name = nameFromUrl(url);
33 // lookup existing loader instance
34 var loader = apiMap[name];
35 // create a loader as needed
36 if (!loader) {
37 loader = apiMap[name] = new Loader(name, url, callbackName);
39 loader.requestNotify(notifiee);
42 function nameFromUrl(url) {
43 return url.replace(/[\:\/\%\?\&\.\=\-\,]/g, '_') + '_api';
46 var Loader = function(name, url, callbackName) {
47 this.instances = [];
48 this.callbackName = callbackName;
49 if (this.callbackName) {
50 window[this.callbackName] = this.success.bind(this);
51 } else {
52 if (url.indexOf(this.callbackMacro) >= 0) {
53 this.callbackName = name + '_loaded';
54 window[this.callbackName] = this.success.bind(this);
55 url = url.replace(this.callbackMacro, this.callbackName);
56 } else {
57 // TODO(sjmiles): we should probably fallback to listening to script.load
58 throw 'core-shared-api: a %%callback%% parameter is required in the API url';
62 this.addScript(url);
65 Loader.prototype = {
67 callbackMacro: '%%callback%%',
68 loaded: false,
70 addScript: function(src) {
71 var script = document.createElement('script');
72 script.src = src;
73 script.onerror = this.error.bind(this);
74 var s = document.querySelector('script');
75 s.parentNode.insertBefore(script, s);
76 this.script = script;
79 removeScript: function() {
80 if (this.script.parentNode) {
81 this.script.parentNode.removeChild(this.script);
83 this.script = null;
86 error: function() {
87 this.cleanup();
90 success: function() {
91 this.loaded = true;
92 this.cleanup();
93 this.result = Array.prototype.slice.call(arguments);
94 this.instances.forEach(this.provide, this);
95 this.instances = null;
98 cleanup: function() {
99 delete window[this.callbackName];
102 provide: function(instance) {
103 instance.notify(instance, this.result);
106 requestNotify: function(instance) {
107 if (this.loaded) {
108 this.provide(instance);
109 } else {
110 this.instances.push(instance);
116 })();