| 1 |
'use strict'; |
| 2 |
module.exports = require('./loader')(global, loadImplementation); |
| 3 |
|
| 4 |
function loadImplementation(implementation) { |
| 5 |
let finalImplementation; |
| 6 |
|
| 7 |
if (implementation === 'global.Observable') { |
| 8 |
// If no implementation or env specified use global.Observable |
| 9 |
finalImplementation = { |
| 10 |
Observable: global.Observable, |
| 11 |
implementation: 'global.Observable' |
| 12 |
}; |
| 13 |
} else if (implementation) { |
| 14 |
// If implementation specified, require it |
| 15 |
const package_ = require(implementation); |
| 16 |
|
| 17 |
finalImplementation = { |
| 18 |
Observable: package_.Observable || package_.default || package_, |
| 19 |
implementation |
| 20 |
}; |
| 21 |
} else { |
| 22 |
// Try to auto detect implementation. This is non-deterministic |
| 23 |
// and should prefer other branches, but this is our last chance |
| 24 |
// to load something without throwing error. |
| 25 |
finalImplementation = tryAutoDetect(); |
| 26 |
} |
| 27 |
|
| 28 |
if (!finalImplementation) { |
| 29 |
throw new Error('Cannot find any-observable implementation nor `global.Observable`. You must install polyfill or call `require(\'any-observable/register\') with your preferred implementation, for example, `require(\'any-observable/register\')(\'rxjs\')` on app load prior to any `require(\'any-observable\').'); |
| 30 |
} |
| 31 |
|
| 32 |
return finalImplementation; |
| 33 |
} |
| 34 |
|
| 35 |
function tryAutoDetect() { |
| 36 |
const packages = [ |
| 37 |
'rxjs', |
| 38 |
'zen-observable' |
| 39 |
]; |
| 40 |
|
| 41 |
for (const package_ of packages) { |
| 42 |
try { |
| 43 |
return loadImplementation(package_); |
| 44 |
} catch (_) {} |
| 45 |
} |
| 46 |
} |