@samitouri / QOS-React / commits / 52652f3456

Toggle method no longer emits 'mutated' event unless it mutated the store

Brian Vaughn committed Apr 21, 2019 at 12:20 UTC 52652f3456ab00581939f0b12327d0fae6d8fc66
1 file changed +50 -43
src/devtools/store.js
+50 -43
@@ -449,6 +449,8 @@ export default class Store extends EventEmitter {
449
450 // TODO Maybe split this into two methods: expand() and collapse()
451 toggleIsCollapsed(id: number, isCollapsed: boolean): void {
452 + let didMutate = false;
453 +
454 const element = this.getElementByID(id);
455 if (element !== null) {
456 if (isCollapsed) {
@@ -456,24 +458,21 @@ export default class Store extends EventEmitter {
458 throw Error('Root nodes cannot be collapsed');
459 }
460
459 - if (element.isCollapsed) {
460 - // There's nothing to change in this case.
461 - // We can exit early (without even emiting a "mutated" event).
462 - return;
463 - }
464 -
465 - element.isCollapsed = true;
461 + if (!element.isCollapsed) {
462 + didMutate = true;
463 + element.isCollapsed = true;
464
467 - const weightDelta = 1 - element.weight;
465 + const weightDelta = 1 - element.weight;
466
469 - let parentElement = ((this._idToElement.get(
470 - element.parentID
471 - ): any): Element);
472 - while (parentElement != null) {
473 - // We don't need to break on a collapsed parent in the same way as the expand case below.
474 - // That's because collapsing a node doesn't "bubble" and affect its parents.
475 - parentElement.weight += weightDelta;
476 - parentElement = this._idToElement.get(parentElement.parentID);
467 + let parentElement = ((this._idToElement.get(
468 + element.parentID
469 + ): any): Element);
470 + while (parentElement != null) {
471 + // We don't need to break on a collapsed parent in the same way as the expand case below.
472 + // That's because collapsing a node doesn't "bubble" and affect its parents.
473 + parentElement.weight += weightDelta;
474 + parentElement = this._idToElement.get(parentElement.parentID);
475 + }
476 }
477 } else {
478 let currentElement = element;
@@ -481,24 +480,29 @@ export default class Store extends EventEmitter {
480 const oldWeight = currentElement.isCollapsed
481 ? 1
482 : currentElement.weight;
484 - currentElement.isCollapsed = false;
485 - const newWeight = currentElement.isCollapsed
486 - ? 1
487 - : currentElement.weight;
488 - const weightDelta = newWeight - oldWeight;
483
490 - let parentElement = ((this._idToElement.get(
491 - currentElement.parentID
492 - ): any): Element);
493 - while (parentElement != null) {
494 - parentElement.weight += weightDelta;
495 - if (parentElement.isCollapsed) {
496 - // It's important to break on a collapsed parent when expanding nodes.
497 - // That's because expanding a node "bubbles" up and expands all parents as well.
498 - // Breaking in this case prevents us from over-incrementing the expanded weights.
499 - break;
484 + if (currentElement.isCollapsed) {
485 + didMutate = true;
486 + currentElement.isCollapsed = false;
487 +
488 + const newWeight = currentElement.isCollapsed
489 + ? 1
490 + : currentElement.weight;
491 + const weightDelta = newWeight - oldWeight;
492 +
493 + let parentElement = ((this._idToElement.get(
494 + currentElement.parentID
495 + ): any): Element);
496 + while (parentElement != null) {
497 + parentElement.weight += weightDelta;
498 + if (parentElement.isCollapsed) {
499 + // It's important to break on a collapsed parent when expanding nodes.
500 + // That's because expanding a node "bubbles" up and expands all parents as well.
501 + // Breaking in this case prevents us from over-incrementing the expanded weights.
502 + break;
503 + }
504 + parentElement = this._idToElement.get(parentElement.parentID);
505 }
501 - parentElement = this._idToElement.get(parentElement.parentID);
506 }
507
508 currentElement =
@@ -508,17 +512,20 @@ export default class Store extends EventEmitter {
512 }
513 }
514
511 - let weightAcrossRoots = 0;
512 - this._roots.forEach(rootID => {
513 - const { weight } = ((this.getElementByID(rootID): any): Element);
514 - weightAcrossRoots += weight;
515 - });
516 - this._weightAcrossRoots = weightAcrossRoots;
517 -
518 - // The Tree context's search reducer expects an explicit list of ids for nodes that were added or removed.
519 - // In this case, we can pass it empty arrays since nodes in a collapsed tree are still there (just hidden).
520 - // Updating the selected search index later may require auto-expanding a collapsed subtree though.
521 - this.emit('mutated', [[], []]);
515 + // Only re-calculate weights and emit an "update" event if the store was mutated.
516 + if (didMutate) {
517 + let weightAcrossRoots = 0;
518 + this._roots.forEach(rootID => {
519 + const { weight } = ((this.getElementByID(rootID): any): Element);
520 + weightAcrossRoots += weight;
521 + });
522 + this._weightAcrossRoots = weightAcrossRoots;
523 +
524 + // The Tree context's search reducer expects an explicit list of ids for nodes that were added or removed.
525 + // In this case, we can pass it empty arrays since nodes in a collapsed tree are still there (just hidden).
526 + // Updating the selected search index later may require auto-expanding a collapsed subtree though.
527 + this.emit('mutated', [[], []]);
528 + }
529 }
530 }
531