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

60
LINES

< > BotCompany Repo | #1024286 // ConceptsLoadedOnDemand - uses the "case" mechanism (subdirs of program dir)

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

Libraryless. Click here for Pure Java version (12396L/84K).

// TODO: run dbBot()

sclass ConceptsLoadedOnDemand {
  new Map<S, Concepts> loaded; // key = just the case ID. value can be null in case of error
  L onCaseLoaded; // L<voidfunc(S caseID, O extraInfo, Concepts concepts)>
  L onUnloadingCase; // L<voidfunc(S caseID, Concepts concepts)>
  Lock lock = lock();
  
  Concepts get(S caseID, O extraInfo) { ret conceptsForCase(caseID, extraInfo); }
  
  Concepts getIfLoaded(S caseID) {
    lock lock;
    ret loaded.get(unnull(caseID));
  }
  
  // caseID may be empty, then you get the program's main DB
  Concepts conceptsForCase(S caseID, O extraInfo) {
    lock lock;
    //assertNempty(+caseID);
    caseID = unnull(caseID);
    
    if (loaded.containsKey(caseID))
      ret assertNotNull("Concepts load error", loaded.get(caseID));

    S id = dbID(caseID);
    print("Loading concepts for case " + id);
    Concepts cc = new Concepts(id);
    cc.persist();
    loaded.put(caseID, cc);
    pcallFAll(onCaseLoaded, caseID, extraInfo, cc);
    ret cc;
  }
  
  S dbID(S caseID) {
    ret combineWithSlash(getDBProgramID(), caseID);
  }
  
  void cleanMeUp {
    cleanUpAndClear(loaded);
  }
  
  void onCaseLoaded(VF3<S, O, Concepts> f) {
    onCaseLoaded = listCreateAndAdd(onCaseLoaded, f);
  }
  
  void onUnloadingCase(VF2<S, Concepts> f) {
    onUnloadingCase = listCreateAndAdd(onUnloadingCase, f);
  }
  
  void unloadCase(S caseID) {
    lock lock;
    caseID = unnull(caseID);
    Concepts cc = loaded.get(caseID);
    if (cc != null) {
      pcallFAll(onUnloadingCase, caseID, cc);
      cleanUp(cc);
      loaded.remove(caseID);
    }
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1024286
Snippet name: ConceptsLoadedOnDemand - uses the "case" mechanism (subdirs of program dir)
Eternal ID of this version: #1024286/20
Text MD5: ec71c8839e689508b283e188e2520135
Transpilation MD5: 627c417ec5748fde740edfa2f3f66522
Author: stefan
Category: eleu
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-04 15:19:31
Source code size: 1661 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 286 / 785
Version history: 19 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)