context.js 518 B

123456789101112131415161718192021222324
  1. const contexts = new WeakMap();
  2. const ID_PREFIX = 'r';
  3. function getContext(rendererContextResult) {
  4. if (contexts.has(rendererContextResult)) {
  5. return contexts.get(rendererContextResult);
  6. }
  7. const ctx = {
  8. currentIndex: 0,
  9. get id() {
  10. return ID_PREFIX + this.currentIndex.toString();
  11. },
  12. };
  13. contexts.set(rendererContextResult, ctx);
  14. return ctx;
  15. }
  16. export function incrementId(rendererContextResult) {
  17. const ctx = getContext(rendererContextResult);
  18. const id = ctx.id;
  19. ctx.currentIndex++;
  20. return id;
  21. }