SymbolTreeNode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. module.exports = class SymbolTreeNode {
  3. constructor() {
  4. this.parent = null;
  5. this.previousSibling = null;
  6. this.nextSibling = null;
  7. this.firstChild = null;
  8. this.lastChild = null;
  9. /** This value is incremented anytime a children is added or removed */
  10. this.childrenVersion = 0;
  11. /** The last child object which has a cached index */
  12. this.childIndexCachedUpTo = null;
  13. /** This value represents the cached node index, as long as
  14. * cachedIndexVersion matches with the childrenVersion of the parent */
  15. this.cachedIndex = -1;
  16. this.cachedIndexVersion = NaN; // NaN is never equal to anything
  17. }
  18. get isAttached() {
  19. return Boolean(this.parent || this.previousSibling || this.nextSibling);
  20. }
  21. get hasChildren() {
  22. return Boolean(this.firstChild);
  23. }
  24. childrenChanged() {
  25. /* jshint -W016 */
  26. // integer wrap around
  27. this.childrenVersion = (this.childrenVersion + 1) & 0xFFFFFFFF;
  28. this.childIndexCachedUpTo = null;
  29. }
  30. getCachedIndex(parentNode) {
  31. // (assumes parentNode is actually the parent)
  32. if (this.cachedIndexVersion !== parentNode.childrenVersion) {
  33. this.cachedIndexVersion = NaN;
  34. // cachedIndex is no longer valid
  35. return -1;
  36. }
  37. return this.cachedIndex; // -1 if not cached
  38. }
  39. setCachedIndex(parentNode, index) {
  40. // (assumes parentNode is actually the parent)
  41. this.cachedIndexVersion = parentNode.childrenVersion;
  42. this.cachedIndex = index;
  43. }
  44. };