Advertisement
Guest User

Untitled

a guest
May 27th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. var bicycle = {};
  2.  
  3.  
  4. bicycle.Provider = (function() {
  5. 'use strict';
  6.  
  7.  
  8. function Provider(timeout) {
  9. this.internal = {
  10. promises: {},
  11. timeout: timeout || 0
  12. };
  13. };
  14.  
  15.  
  16. Provider.prototype.require = function(key) {
  17. var provider = this;
  18.  
  19. if( provider.internal.promises[key] ) {
  20. return provider.internal.promises[key].promise;
  21. };
  22.  
  23. provider.internal.promises[key] = {
  24. // resolve: null,
  25. // reject: null,
  26. // promise: null,
  27. // timeout: null
  28. };
  29.  
  30. // promise executor is called synchronously,
  31. // promise constructor will not return until it will be finished,
  32. // so it's safe, though looks tricky:
  33. provider.internal.promises[key].promise = new Promise(function(resolve, reject) {
  34.  
  35. provider.internal.promises[key].resolve = resolve;
  36. provider.internal.promises[key].reject = reject;
  37.  
  38. if( provider.internal.timeout ) {
  39. provider.internal.promises[key].timeout = setTimeout(function() {
  40. provider.internal.promises[key].reject(new Error('Timeout'));
  41. }, provider.internal.timeout);
  42. };
  43.  
  44. });
  45.  
  46. return provider.internal.promises[key].promise;
  47. };
  48.  
  49.  
  50. Provider.prototype.provide = function(key, value) {
  51.  
  52. if( this.internal.promises[key] ) {
  53.  
  54. if( this.internal.promises[key].timeout ) {
  55. clearTimeout(provider.internal.promises[key].timeout);
  56. delete this.internal.promises[key].timeout;
  57. };
  58.  
  59. this.internal.promises[key].resolve(value);
  60. return this;
  61. };
  62.  
  63. this.internal.promises[key] = {
  64. // promise: null
  65. };
  66.  
  67. this.internal.promises[key].promise = Promise.resolve(value);
  68.  
  69. return this;
  70. };
  71.  
  72.  
  73. return Provider;
  74. })();
  75.  
  76.  
  77. bicycle.Loader = (function() {
  78. 'use strict';
  79.  
  80.  
  81. function Loader() {
  82. this.internal = {
  83. promises: {}
  84. };
  85. };
  86.  
  87.  
  88. Loader.prototype.load = function(src) {
  89. var loader = this;
  90.  
  91. if( this.internal.promises[src] ) {
  92. return this.internal.promises[src];
  93. };
  94.  
  95. this.internal.promises[src] = new Promise(function(resolve, reject) {
  96.  
  97. var script = document.createElement('script');
  98.  
  99. script.async = true;
  100. script.src = src;
  101.  
  102. script.addEventListener('load', function(event) {
  103. document.documentElement.removeChild(script);
  104. resolve(null);
  105. });
  106.  
  107. script.addEventListener('error', function(event) {
  108. document.documentElement.removeChild(script);
  109. reject(null);
  110. });
  111.  
  112. document.documentElement.appendChild(script);
  113.  
  114. });
  115.  
  116. return this.internal.promises[src];
  117. };
  118.  
  119.  
  120. return Loader;
  121. })();
  122.  
  123.  
  124. bicycle.provider = new bicycle.Provider;
  125. bicycle.loader = new bicycle.Loader;
  126.  
  127.  
  128. bicycle.require = function(key) {
  129. return bicycle.provider.require(key);
  130. };
  131.  
  132.  
  133. bicycle.provide = function(key, value) {
  134. return bicycle.provider.provide(key, value);
  135. };
  136.  
  137.  
  138. bicycle.load = function(src) {
  139. return bicycle.loader.load(src);
  140. };
  141.  
  142.  
  143. /*
  144.  
  145. console.info('Loading world...');
  146.  
  147. bicycle.load('bicycled-test.js').then(function() {
  148. console.info('World loaded.');
  149. }).catch(function() {
  150. console.error('Error loading world.');
  151. });
  152.  
  153. bicycle.require('world').then(function(world) {
  154. console.info('Required world:', world);
  155. }).catch(function(error) {
  156. console.error('Error requiring world:', error);
  157. });
  158.  
  159. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement