commit-queue.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. get size() {
  18. return this.removeMap.size + this.addMap.size;
  19. }
  20. add(o: GraphicsRenderObject) {
  21. if (this.removeMap.has(o)) {
  22. const a = this.removeMap.get(o)!;
  23. this.removeMap.delete(o);
  24. this.removeList.remove(a);
  25. }
  26. if (this.addMap.has(o)) return;
  27. const b = this.addList.addLast(o);
  28. this.addMap.set(o, b);
  29. }
  30. remove(o: GraphicsRenderObject) {
  31. if (this.addMap.has(o)) {
  32. const a = this.addMap.get(o)!;
  33. this.addMap.delete(o);
  34. this.addList.remove(a);
  35. }
  36. if (this.removeMap.has(o)) return;
  37. const b = this.removeList.addLast(o);
  38. this.removeMap.set(o, b);
  39. }
  40. tryGetRemove() {
  41. const o = this.removeList.removeFirst();
  42. if (o) this.removeMap.delete(o);
  43. return o;
  44. }
  45. tryGetAdd() {
  46. const o = this.addList.removeFirst();
  47. if (o) this.addMap.delete(o);
  48. return o;
  49. }
  50. }