| 1 | "use strict"
|
|---|
| 2 | // global key for user preferred registration
|
|---|
| 3 | var REGISTRATION_KEY = '@@any-promise/REGISTRATION',
|
|---|
| 4 | // Prior registration (preferred or detected)
|
|---|
| 5 | registered = null
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * Registers the given implementation. An implementation must
|
|---|
| 9 | * 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 in
|
|---|
| 13 | * following priority:
|
|---|
| 14 | *
|
|---|
| 15 | * For Node.js:
|
|---|
| 16 | *
|
|---|
| 17 | * 1. Previous registration
|
|---|
| 18 | * 2. global.Promise if node.js version >= 0.12
|
|---|
| 19 | * 3. Auto detected promise based on first sucessful require of
|
|---|
| 20 | * known promise libraries. Note this is a last resort, as the
|
|---|
| 21 | * loaded library is non-deterministic. node.js >= 0.12 will
|
|---|
| 22 | * always use global.Promise over this priority list.
|
|---|
| 23 | * 4. Throws error.
|
|---|
| 24 | *
|
|---|
| 25 | * For Browser:
|
|---|
| 26 | *
|
|---|
| 27 | * 1. Previous registration
|
|---|
| 28 | * 2. window.Promise
|
|---|
| 29 | * 3. Throws error.
|
|---|
| 30 | *
|
|---|
| 31 | * Options:
|
|---|
| 32 | *
|
|---|
| 33 | * Promise: Desired Promise constructor
|
|---|
| 34 | * global: Boolean - Should the registration be cached in a global variable to
|
|---|
| 35 | * allow cross dependency/bundle registration? (default true)
|
|---|
| 36 | */
|
|---|
| 37 | module.exports = function(root, loadImplementation){
|
|---|
| 38 | return function register(implementation, opts){
|
|---|
| 39 | implementation = implementation || null
|
|---|
| 40 | 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 registration
|
|---|
| 45 | if(registered === null && registerGlobal){
|
|---|
| 46 | registered = root[REGISTRATION_KEY] || null
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | if(registered !== null
|
|---|
| 50 | && implementation !== null
|
|---|
| 51 | && registered.implementation !== implementation){
|
|---|
| 52 | // Throw error if attempting to redefine implementation
|
|---|
| 53 | 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 implementation
|
|---|
| 60 | if(implementation !== null && typeof opts.Promise !== 'undefined'){
|
|---|
| 61 | registered = {
|
|---|
| 62 | Promise: opts.Promise,
|
|---|
| 63 | implementation: implementation
|
|---|
| 64 | }
|
|---|
| 65 | } else {
|
|---|
| 66 | // require implementation if implementation is specified but not provided
|
|---|
| 67 | registered = loadImplementation(implementation)
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if(registerGlobal){
|
|---|
| 71 | // register preference globally in case multiple installations
|
|---|
| 72 | root[REGISTRATION_KEY] = registered
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | return registered
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|