Skip to content

Commit

Permalink
chore: emit VisibleElementsChanged whenever an action happens which c…
Browse files Browse the repository at this point in the history
…hanges the visible elements
  • Loading branch information
himanshusinghs committed Jul 10, 2024
1 parent dea3187 commit 628c98d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
5 changes: 5 additions & 0 deletions packages/hadron-document/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export class Document extends EventEmitter {
insertBeginning(key: string | number, value: BSONValue): Element {
const newElement = this.elements.insertBeginning(key, value);
newElement._bubbleUp(ElementEvents.Added, newElement, this);
this.emit(Events.VisibleElementsChanged, this);
return newElement;
}

Expand All @@ -280,6 +281,7 @@ export class Document extends EventEmitter {
insertEnd(key: string | number, value: BSONValue): Element {
const newElement = this.elements.insertEnd(key, value);
newElement._bubbleUp(ElementEvents.Added, newElement, this);
this.emit(Events.VisibleElementsChanged, this);
return newElement;
}

Expand All @@ -299,6 +301,7 @@ export class Document extends EventEmitter {
): Element | undefined {
const newElement = this.elements.insertAfter(element, key, value);
newElement?._bubbleUp(ElementEvents.Added, newElement, this);
this.emit(Events.VisibleElementsChanged, this);
return newElement;
}

Expand Down Expand Up @@ -403,6 +406,7 @@ export class Document extends EventEmitter {
element.expand(true);
}
this.emit(Events.Expanded);
this.emit(Events.VisibleElementsChanged, this);
}

/**
Expand All @@ -414,6 +418,7 @@ export class Document extends EventEmitter {
element.collapse();
}
this.emit(Events.Collapsed);
this.emit(Events.VisibleElementsChanged, this);
}

getVisibleElements() {
Expand Down
31 changes: 24 additions & 7 deletions packages/hadron-document/src/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { TypeCastTypes } from 'hadron-type-checker';
import type { ObjectId } from 'bson';
import type { BSONArray, BSONObject, BSONValue } from './utils';
import { getDefaultValueForType } from './utils';
import { ElementEvents } from '.';
import { DocumentEvents, ElementEvents } from '.';

export const DATE_FORMAT = 'YYYY-MM-DD HH:mm:ss.SSS';
export { Events };
Expand Down Expand Up @@ -303,6 +303,7 @@ export class Element extends EventEmitter {
}
const newElement = this.elements.insertAfter(element, key, value);
newElement!._bubbleUp(Events.Added, newElement, this);
this.emitVisibleElementsChanged();
return newElement!;
}

Expand All @@ -320,6 +321,7 @@ export class Element extends EventEmitter {
}
const newElement = this.elements.insertEnd(key, value, true);
this._bubbleUp(Events.Added, newElement);
this.emitVisibleElementsChanged();
return newElement;
}

Expand Down Expand Up @@ -692,6 +694,7 @@ export class Element extends EventEmitter {
this.removed = true;
if (this.parent) {
this._bubbleUp(Events.Removed, this, this.parent);
this.emitVisibleElementsChanged(this.parent);
}
}

Expand All @@ -702,6 +705,9 @@ export class Element extends EventEmitter {
if (this.isAdded()) {
this.parent?.elements?.remove(this);
this._bubbleUp(Events.Removed, this, this.parent);
if (this.parent) {
this.emitVisibleElementsChanged(this.parent);
}
delete (this as any).parent;
} else {
if (this.originalExpandableValue) {
Expand Down Expand Up @@ -739,7 +745,8 @@ export class Element extends EventEmitter {
element.expand(expandChildren);
}
}
this._bubbleUp(ElementEvents.Expanded, this);
this.emit(ElementEvents.Expanded, this);
this.emitVisibleElementsChanged();
}

/**
Expand All @@ -756,7 +763,8 @@ export class Element extends EventEmitter {
element.collapse();
}
}
this._bubbleUp(ElementEvents.Collapsed, this);
this.emit(ElementEvents.Collapsed, this);
this.emitVisibleElementsChanged();
}

/**
Expand Down Expand Up @@ -851,10 +859,7 @@ export class Element extends EventEmitter {
return;
}
this.maxVisibleElementsCount = newCount;

if (this.expanded) {
this._bubbleUp(Events.VisibleElementsChanged, this, this.getRoot());
}
this.emitVisibleElementsChanged();
}

getTotalVisibleElementsCount(): number {
Expand All @@ -871,6 +876,18 @@ export class Element extends EventEmitter {
);
}

private emitVisibleElementsChanged(targetElement: Element | Document = this) {
if (targetElement.isRoot()) {
targetElement.emit(DocumentEvents.VisibleElementsChanged, targetElement);
} else if (targetElement.expanded) {
targetElement._bubbleUp(
Events.VisibleElementsChanged,
targetElement,
targetElement.getRoot()
);
}
}

/**
* @deprecated Use ElementEvents import instead
*/
Expand Down

0 comments on commit 628c98d

Please sign in to comment.