register.js 1.3 KB

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