Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var bicycle = {};
- bicycle.Provider = (function() {
- 'use strict';
- function Provider(timeout) {
- this.internal = {
- promises: {},
- timeout: timeout || 0
- };
- };
- Provider.prototype.require = function(key) {
- var provider = this;
- if( provider.internal.promises[key] ) {
- return provider.internal.promises[key].promise;
- };
- provider.internal.promises[key] = {
- // resolve: null,
- // reject: null,
- // promise: null,
- // timeout: null
- };
- // promise executor is called synchronously,
- // promise constructor will not return until it will be finished,
- // so it's safe, though looks tricky:
- provider.internal.promises[key].promise = new Promise(function(resolve, reject) {
- provider.internal.promises[key].resolve = resolve;
- provider.internal.promises[key].reject = reject;
- if( provider.internal.timeout ) {
- provider.internal.promises[key].timeout = setTimeout(function() {
- provider.internal.promises[key].reject(new Error('Timeout'));
- }, provider.internal.timeout);
- };
- });
- return provider.internal.promises[key].promise;
- };
- Provider.prototype.provide = function(key, value) {
- if( this.internal.promises[key] ) {
- if( this.internal.promises[key].timeout ) {
- clearTimeout(provider.internal.promises[key].timeout);
- delete this.internal.promises[key].timeout;
- };
- this.internal.promises[key].resolve(value);
- return this;
- };
- this.internal.promises[key] = {
- // promise: null
- };
- this.internal.promises[key].promise = Promise.resolve(value);
- return this;
- };
- return Provider;
- })();
- bicycle.Loader = (function() {
- 'use strict';
- function Loader() {
- this.internal = {
- promises: {}
- };
- };
- Loader.prototype.load = function(src) {
- var loader = this;
- if( this.internal.promises[src] ) {
- return this.internal.promises[src];
- };
- this.internal.promises[src] = new Promise(function(resolve, reject) {
- var script = document.createElement('script');
- script.async = true;
- script.src = src;
- script.addEventListener('load', function(event) {
- document.documentElement.removeChild(script);
- resolve(null);
- });
- script.addEventListener('error', function(event) {
- document.documentElement.removeChild(script);
- reject(null);
- });
- document.documentElement.appendChild(script);
- });
- return this.internal.promises[src];
- };
- return Loader;
- })();
- bicycle.provider = new bicycle.Provider;
- bicycle.loader = new bicycle.Loader;
- bicycle.require = function(key) {
- return bicycle.provider.require(key);
- };
- bicycle.provide = function(key, value) {
- return bicycle.provider.provide(key, value);
- };
- bicycle.load = function(src) {
- return bicycle.loader.load(src);
- };
- /*
- console.info('Loading world...');
- bicycle.load('bicycled-test.js').then(function() {
- console.info('World loaded.');
- }).catch(function() {
- console.error('Error loading world.');
- });
- bicycle.require('world').then(function(world) {
- console.info('Required world:', world);
- }).catch(function(error) {
- console.error('Error requiring world:', error);
- });
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement