RagConnectObserver.jadd 2.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
aspect RagConnectObserver {
  class RagConnectObserver implements ASTState.Trace.Receiver {
    ASTState.Trace.Receiver oldReceiver;
    class RagConnectObserverEntry {
      ASTNode node;
      String attributeString;
      Runnable attributeCall;
      RagConnectObserverEntry(ASTNode node, String attributeString, Runnable attributeCall) {
        this.node = node;
        this.attributeString = attributeString;
        this.attributeCall = attributeCall;
      }
    }
    java.util.List<RagConnectObserverEntry> observedNodes = new java.util.ArrayList<>();
    RagConnectObserver(ASTNode node) {
      // set the receiver. potentially dangerous because overriding existing receiver!
      oldReceiver = node.trace().getReceiver();
      node.trace().setReceiver(this);
    }
    void add(ASTNode node, String attributeString, Runnable attributeCall) {
      System.out.println("** observer add " + node + " on " + attributeString);
      observedNodes.add(new RagConnectObserverEntry(node, attributeString, attributeCall));
    }
    void remove(ASTNode node, String attributeString, Runnable attributeCall) {
      observedNodes.remove(new RagConnectObserverEntry(node, attributeString, attributeCall));
    }
    @Override
    public void accept(ASTState.Trace.Event event, ASTNode node, String attribute, Object params, Object value) {
      oldReceiver.accept(event, node, attribute, params, value);
      // ignore all events but INC_FLUSH_ATTR
      if (event != ASTState.Trace.Event.INC_FLUSH_ATTR) {
        return;
      }
      System.out.println("** observer check INC_FLUSH_ATTR event");
      // iterate through list, if matching pair. could maybe be more efficient.
      for (RagConnectObserverEntry entry : observedNodes) {
        if (entry.node.equals(node) && entry.attributeString.equals(attribute)) {
          // hit. call the attribute/nta-token
          System.out.println("** observer hit " + entry.node + " on " + entry.attributeString);
          entry.attributeCall.run();
        }
      }
    }
  }

  private static RagConnectObserver ASTNode._ragConnectObserverInstance;
  RagConnectObserver ASTNode._ragConnectObserver() {
    if (_ragConnectObserverInstance == null) {
      // does not matter, which node is used to create the observer as ASTState/tracing is also static
      _ragConnectObserverInstance = new RagConnectObserver(this);
    }
    return _ragConnectObserverInstance;
  }
}