basant307/AI_Governance_Project
048
1"use strict"2 // global key for user preferred registration3var REGISTRATION_KEY = '@@any-promise/REGISTRATION',4 // Prior registration (preferred or detected)5 registered = null6 7/**8 * Registers the given implementation. An implementation must9 * be registered prior to any call to `require("any-promise")`,10 * typically on application load.11 *12 * If called with no arguments, will return registration in13 * following priority:14 *15 * For Node.js:16 *17 * 1. Previous registration18 * 2. global.Promise if node.js version >= 0.1219 * 3. Auto detected promise based on first sucessful require of20 * known promise libraries. Note this is a last resort, as the21 * loaded library is non-deterministic. node.js >= 0.12 will22 * always use global.Promise over this priority list.23 * 4. Throws error.24 *25 * For Browser:26 *27 * 1. Previous registration28 * 2. window.Promise29 * 3. Throws error.30 *31 * Options:32 *33 * Promise: Desired Promise constructor34 * global: Boolean - Should the registration be cached in a global variable to35 * allow cross dependency/bundle registration? (default true)36 */37module.exports = function(root, loadImplementation){38 return function register(implementation, opts){39 implementation = implementation || null40 opts = opts || {}41 // global registration unless explicitly {global: false} in options (default true)42 var registerGlobal = opts.global !== false;43 44 // load any previous global registration45 if(registered === null && registerGlobal){46 registered = root[REGISTRATION_KEY] || null47 }48 49 if(registered !== null50 && implementation !== null51 && registered.implementation !== implementation){52 // Throw error if attempting to redefine implementation53 throw new Error('any-promise already defined as "'+registered.implementation+54 '". You can only register an implementation before the first '+55 ' call to require("any-promise") and an implementation cannot be changed')56 }57 58 if(registered === null){59 // use provided implementation60 if(implementation !== null && typeof opts.Promise !== 'undefined'){61 registered = {62 Promise: opts.Promise,63 implementation: implementation64 }65 } else {66 // require implementation if implementation is specified but not provided67 registered = loadImplementation(implementation)68 }69 70 if(registerGlobal){71 // register preference globally in case multiple installations72 root[REGISTRATION_KEY] = registered73 }74 }75 76 return registered77 }78}79 