Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

59
LINES

< > BotCompany Repo | #1027845 // tok_eventFunctions

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (7900L) is out of date.

1  
/* event change; =>
2  
   transient LinkedHashSet<Runnable> onChange;
3  
   public selfType onChange(Runnable r) { onChange = syncAddOrCreate(onChange, r); this; }
4  
   void change() { if (onChange != null) for (listener : onChange) pcallF_typed(listener); }
5  
; }
6  
*/
7  
svoid tok_eventFunctions(LS tok) {
8  
  int i;
9  
10  
  while ((i = jfind(tok, "event <id>", nIdx -> !eqGet(tok, nIdx+3, "aka"))) >= 0) {
11  
    int iModifiers = tok_leftScanModifiers(tok, i);
12  
    S modifiers = joinSubList(tok, iModifiers, i-1);
13  
    int iSemicolon = i+4;
14  
    LS tokArgs = null;
15  
    if (eqGet(tok, i+4, "(")) {
16  
      int argsFrom = i+6;
17  
      int argsTo = findCodeTokens(tok, argsFrom, false, ")");
18  
      tokArgs = subList(tok, argsFrom-1, argsTo);
19  
      iSemicolon = argsTo+2;
20  
    }
21  
22  
    int iEnd = iSemicolon+1;
23  
    S body = "";
24  
    if (eqGet(tok, iSemicolon, "{")) {
25  
      iEnd = tok_findEndOfBlock(tok, iSemicolon);
26  
      body = joinSubList(tok, iSemicolon+1, iEnd-1);
27  
    } else if (neqGet(tok, iSemicolon, ";"))
28  
      fail("Semicolon expected at end: " + joinSubList(tok, i, iSemicolon+1));
29  
    
30  
    S change = tok.get(i+2), prefix = "";
31  
    if (startsWithCamelCaseWord(change, "on"))
32  
      change = firstToLower(dropFirst(2, change));
33  
    else if (startsWithCamelCaseWord(change, "fire")) {
34  
      prefix = "fire";
35  
      change = firstToLower(dropFirst(4, change));
36  
    }
37  
    S onChange = "on" + firstToUpper(change);
38  
    S removeListener = "remove" + firstToUpper(change) + "Listener";
39  
    
40  
    LPairS args = tok_typesAndNamesOfParams(tokArgs);
41  
    LS types = pairsA(args);
42  
    S args1 = join(dropFirstAndLast(tokArgs));
43  
    S args2 = joinWithComma(pairsB(args));
44  
    S typeParams = joinWithComma(map tok_toNonPrimitiveTypes(types));      S listenerType = empty(args) ? "Runnable" : "IVF" + l(args) + "<" + typeParams + ">";
45  
    S r = empty(args) ? "r" : "f";
46  
    S fireChange = empty(prefix) ? change : prefix + firstToUpper(change);
47  
   
48  
    replaceTokens_reTok(tok, iModifiers, iEnd, 
49  
      "\*modifiers*/ transient Set<\*listenerType*/> \*onChange*/;\n" +
50  
      "public \*modifiers*/ selfType \*onChange*/(\*listenerType*/ \*r*/) { \*onChange*/ = createOrAddToSyncLinkedHashSet(\*onChange*/, \*r*/); this; }\n" +
51  
      "public \*modifiers*/ selfType \*removeListener*/(\*listenerType*/ \*r*/) { main remove(\*onChange*/, \*r*/); this; }\n" +
52  
      "public \*modifiers*/  void \*fireChange*/(\*args1*/) { \*body*/ " +
53  
        "if (\*onChange*/ != null) for (listener : \*onChange*/) " +
54  
        "pcallF_typed(" +
55  
          joinNemptiesWithComma("listener", args2) + "); " +
56  
      "}"
57  
    );
58  
  }
59  
}

Author comment

Began life as a copy of #1012742

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027845
Snippet name: tok_eventFunctions
Eternal ID of this version: #1027845/28
Text MD5: f180f25b41d210c1e2c7b83e7c61a2e7
Author: stefan
Category: javax / parsing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-26 17:59:33
Source code size: 2625 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 297 / 548
Version history: 27 change(s)
Referenced in: [show references]