rootInstance.js 767 B

1234567891011121314151617181920212223242526272829303132
  1. export const holder = new WeakMap();
  2. export const rootInstanceSymbol = Symbol('rootInstance');
  3. /**
  4. * Register an object as a root instance.
  5. *
  6. * @param {Object} object An object to associate with root instance flag.
  7. */
  8. export function registerAsRootInstance(object) {
  9. holder.set(object, true);
  10. }
  11. /**
  12. * Check if the source of the root indication call is valid.
  13. *
  14. * @param {Symbol} rootSymbol A symbol as a source of truth.
  15. * @return {Boolean}
  16. */
  17. export function hasValidParameter(rootSymbol) {
  18. return rootSymbol === rootInstanceSymbol;
  19. }
  20. /**
  21. * Check if passed an object was flagged as a root instance.
  22. *
  23. * @param {Object} object An object to check.
  24. * @return {Boolean}
  25. */
  26. export function isRootInstance(object) {
  27. return holder.has(object);
  28. }