• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5var SelectionBroker = function() {
6  this.brokers = [];
7  this.dispatching = false;
8  this.lastDispatchingHandler = null;
9  this.nodePositionMap = [];
10  this.sortedPositionList = [];
11  this.positionNodeMap = [];
12};
13
14SelectionBroker.prototype.addSelectionHandler = function(handler) {
15  this.brokers.push(handler);
16}
17
18SelectionBroker.prototype.setNodePositionMap = function(map) {
19  let broker = this;
20  if (!map) return;
21  broker.nodePositionMap = map;
22  broker.positionNodeMap = [];
23  broker.sortedPositionList = [];
24  let next = 0;
25  for (let i in broker.nodePositionMap) {
26    broker.sortedPositionList[next] = Number(broker.nodePositionMap[i]);
27    broker.positionNodeMap[next++] = i;
28  }
29  broker.sortedPositionList = sortUnique(broker.sortedPositionList,
30                                       function(a,b) { return a - b; });
31  this.positionNodeMap.sort(function(a,b) {
32    let result = broker.nodePositionMap[a] - broker.nodePositionMap[b];
33    if (result != 0) return result;
34    return a - b;
35  });
36}
37
38SelectionBroker.prototype.select = function(from, locations, selected) {
39  let broker = this;
40  if (!broker.dispatching) {
41    broker.lastDispatchingHandler = from;
42    try {
43      broker.dispatching = true;
44      let enrichLocations = function(locations) {
45        result = [];
46        for (let location of locations) {
47          let newLocation = {};
48          if (location.pos_start != undefined) {
49            newLocation.pos_start = location.pos_start;
50          }
51          if (location.pos_end != undefined) {
52            newLocation.pos_end = location.pos_end;
53          }
54          if (location.node_id != undefined) {
55            newLocation.node_id = location.node_id;
56          }
57          if (location.block_id != undefined) {
58            newLocation.block_id = location.block_id;
59          }
60          if (newLocation.pos_start == undefined &&
61              newLocation.pos_end == undefined &&
62              newLocation.node_id != undefined) {
63            if (broker.nodePositionMap && broker.nodePositionMap[location.node_id]) {
64              newLocation.pos_start = broker.nodePositionMap[location.node_id];
65              newLocation.pos_end = location.pos_start + 1;
66            }
67          }
68          result.push(newLocation);
69        }
70        return result;
71      }
72      locations = enrichLocations(locations);
73      for (var b of this.brokers) {
74        if (b != from) {
75          b.brokeredSelect(locations, selected);
76        }
77      }
78    }
79    finally {
80      broker.dispatching = false;
81    }
82  }
83}
84
85SelectionBroker.prototype.clear = function(from) {
86  this.lastDispatchingHandler = null;
87  if (!this.dispatching) {
88    try {
89      this.dispatching = true;
90      this.brokers.forEach(function(b) {
91        if (b != from) {
92          b.brokeredClear();
93        }
94      });
95    } finally {
96      this.dispatching = false;
97    }
98  }
99}
100