loader.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var REGISTRATION_KEY = '@@any-observable/REGISTRATION';
  3. var registered = null;
  4. module.exports = function (global, loadImplementation) {
  5. return function register(implementation, opts) {
  6. opts = opts || {};
  7. // global registration unless explicitly {global: false} in options (default true)
  8. var registerGlobal = opts.global !== false;
  9. // load any previous global registration
  10. if (registerGlobal && !registered) {
  11. registered = global[REGISTRATION_KEY];
  12. }
  13. if (registered && implementation && registered.implementation !== implementation) {
  14. throw new Error('any-observable already defined as "' + registered.implementation +
  15. '". You can only register an implementation before the first ' +
  16. ' call to require(\'any-observable\') and an implementation cannot be changed');
  17. }
  18. if (!registered) {
  19. // use provided implementation
  20. if (implementation && opts.Observable) {
  21. registered = {
  22. Observable: opts.Observable,
  23. implementation: implementation
  24. };
  25. } else {
  26. // require implementation if implementation is specified but not provided
  27. registered = loadImplementation(implementation || null);
  28. }
  29. if (registerGlobal) {
  30. // register preference globally in case multiple installations
  31. global[REGISTRATION_KEY] = registered;
  32. }
  33. }
  34. return registered;
  35. };
  36. };