flat-tree.mjs 596 B

123456789101112131415161718192021222324
  1. import { addUniqueItem, removeItem } from 'motion-utils';
  2. import { compareByDepth } from './compare-by-depth.mjs';
  3. class FlatTree {
  4. constructor() {
  5. this.children = [];
  6. this.isDirty = false;
  7. }
  8. add(child) {
  9. addUniqueItem(this.children, child);
  10. this.isDirty = true;
  11. }
  12. remove(child) {
  13. removeItem(this.children, child);
  14. this.isDirty = true;
  15. }
  16. forEach(callback) {
  17. this.isDirty && this.children.sort(compareByDepth);
  18. this.isDirty = false;
  19. this.children.forEach(callback);
  20. }
  21. }
  22. export { FlatTree };