Home | History | Annotate | Download | only in src
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 import {SourceResolver, sourcePositionValid} from "./source-resolver.js"
      6 
      7 export class SelectionBroker {
      8   sourceResolver: SourceResolver;
      9   sourcePositionHandlers: Array<SelectionHandler>;
     10   nodeHandlers: Array<NodeSelectionHandler>;
     11   blockHandlers: Array<BlockSelectionHandler>;
     12 
     13   constructor(sourceResolver) {
     14     this.sourcePositionHandlers = [];
     15     this.nodeHandlers = [];
     16     this.blockHandlers = [];
     17     this.sourceResolver = sourceResolver;
     18   };
     19 
     20   addSourcePositionHandler(handler) {
     21     this.sourcePositionHandlers.push(handler);
     22   }
     23 
     24   addNodeHandler(handler) {
     25     this.nodeHandlers.push(handler);
     26   }
     27 
     28   addBlockHandler(handler) {
     29     this.blockHandlers.push(handler);
     30   }
     31 
     32   broadcastSourcePositionSelect(from, sourcePositions, selected) {
     33     let broker = this;
     34     sourcePositions = sourcePositions.filter((l) => {
     35       if (!sourcePositionValid(l)) {
     36         console.log("Warning: invalid source position");
     37         return false;
     38       }
     39       return true;
     40     });
     41     for (const b of this.sourcePositionHandlers) {
     42       if (b != from) b.brokeredSourcePositionSelect(sourcePositions, selected);
     43     }
     44     const nodes = this.sourceResolver.sourcePositionsToNodeIds(sourcePositions);
     45     for (const b of this.nodeHandlers) {
     46       if (b != from) b.brokeredNodeSelect(nodes, selected);
     47     }
     48   }
     49 
     50   broadcastNodeSelect(from, nodes, selected) {
     51     let broker = this;
     52     for (const b of this.nodeHandlers) {
     53       if (b != from) b.brokeredNodeSelect(nodes, selected);
     54     }
     55     const sourcePositions = this.sourceResolver.nodeIdsToSourcePositions(nodes);
     56     for (const b of this.sourcePositionHandlers) {
     57       if (b != from) b.brokeredSourcePositionSelect(sourcePositions, selected);
     58     }
     59   }
     60 
     61   broadcastBlockSelect(from, blocks, selected) {
     62     let broker = this;
     63     for (var b of this.blockHandlers) {
     64       if (b != from) b.brokeredBlockSelect(blocks, selected);
     65     }
     66   }
     67 
     68   broadcastClear(from) {
     69     this.sourcePositionHandlers.forEach(function (b) {
     70       if (b != from) b.brokeredClear();
     71     });
     72     this.nodeHandlers.forEach(function (b) {
     73       if (b != from) b.brokeredClear();
     74     });
     75     this.blockHandlers.forEach(function (b) {
     76       if (b != from) b.brokeredClear();
     77     });
     78   }
     79 }
     80