commit-queue.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { LinkedList } from '../mol-data/generic';
  7. import { GraphicsRenderObject } from './render-object';
  8. type N = LinkedList.Node<GraphicsRenderObject>
  9. export class CommitQueue {
  10. private removeList = LinkedList<GraphicsRenderObject>();
  11. private removeMap = new Map<GraphicsRenderObject, N>();
  12. private addList = LinkedList<GraphicsRenderObject>();
  13. private addMap = new Map<GraphicsRenderObject, N>();
  14. get isEmpty() {
  15. return this.removeList.count === 0 && this.addList.count === 0;
  16. }
  17. add(o: GraphicsRenderObject) {
  18. if (this.removeMap.has(o)) {
  19. const a = this.removeMap.get(o)!;
  20. this.removeMap.delete(o);
  21. this.removeList.remove(a);
  22. }
  23. if (this.addMap.has(o)) return;
  24. const b = this.addList.addLast(o);
  25. this.addMap.set(o, b);
  26. }
  27. remove(o: GraphicsRenderObject) {
  28. if (this.addMap.has(o)) {
  29. const a = this.addMap.get(o)!;
  30. this.addMap.delete(o);
  31. this.addList.remove(a);
  32. }
  33. if (this.removeMap.has(o)) return;
  34. const b = this.removeList.addLast(o);
  35. this.removeMap.set(o, b);
  36. }
  37. tryGetRemove() {
  38. const o = this.removeList.removeFirst();
  39. if (o) this.removeMap.delete(o);
  40. return o;
  41. }
  42. tryGetAdd() {
  43. const o = this.addList.removeFirst();
  44. if (o) this.addMap.delete(o);
  45. return o;
  46. }
  47. }