ponyfill.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. export default function symbolObservablePonyfill(root) {
  2. var result;
  3. var Symbol = root.Symbol;
  4. if (typeof Symbol === 'function') {
  5. if (Symbol.observable) {
  6. result = Symbol.observable;
  7. } else {
  8. if (typeof Symbol.for === 'function') {
  9. // This just needs to be something that won't trample other user's Symbol.for use
  10. // It also will guide people to the source of their issues, if this is problematic.
  11. // META: It's a resource locator!
  12. result = Symbol.for('https://github.com/benlesh/symbol-observable');
  13. } else {
  14. // Symbol.for didn't exist! The best we can do at this point is a totally
  15. // unique symbol. Note that the string argument here is a descriptor, not
  16. // an identifier. This symbol is unique.
  17. result = Symbol('https://github.com/benlesh/symbol-observable');
  18. }
  19. try {
  20. Symbol.observable = result;
  21. } catch (err) {
  22. // Do nothing. In some environments, users have frozen `Symbol` for security reasons,
  23. // if it is frozen assigning to it will throw. In this case, we don't care, because
  24. // they will need to use the returned value from the ponyfill.
  25. }
  26. }
  27. } else {
  28. result = '@@observable';
  29. }
  30. return result;
  31. };