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

33
LINES

< > BotCompany Repo | #1034193 // SnippetInstaCache - always has latest contents of selected snippets in memory

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

Libraryless. Click here for Pure Java version (19647L/120K).

// only works within a module
// has mild race conditions
sclass SnippetInstaCache is AutoCloseable {
  // snippet ID to text
  SS texts = syncMap();
  AutoCloseable updater;

  *(S... snippetIDs) { this(asList(snippetIDs)); }
  
  *(Iterable<S> snippetIDs) {
    updater = dm_onSnippetEdited(voidfunc(S id) {
      bool contained = texts.containsKey(id);
      //print("SnippetInstaCache: snippet edited " + id + ", contained: " + contained);
      if (contained) {
        S text = loadSnippet(id);
        print(text := shorten(text));
        texts.put(id, text);
      }
    });
    
    fOr (snippetID : map fsI(snippetIDs))
      texts.put(snippetID, loadSnippet(snippetID));
  }
  
  // get snippet text
  S get(S snippetID) {
    ret texts.get(fsI(snippetID));
  }
  
  close { dispose updater; }
  
  Cl<S> cachedSnippetIDs() { ret cloneKeys(texts); }
}

Author comment

Began life as a copy of #1032676

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034193
Snippet name: SnippetInstaCache - always has latest contents of selected snippets in memory
Eternal ID of this version: #1034193/9
Text MD5: 9d0fdea4a2cc20b8333b812e4065427c
Transpilation MD5: c716b293c5d72ab9e193e0c6f6b5e038
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-30 17:19:33
Source code size: 897 bytes / 33 lines
Pitched / IR pitched: No / No
Views / Downloads: 104 / 217
Version history: 8 change(s)
Referenced in: [show references]