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).

1  
// TODO: run dbBot()
2  
3  
sclass ConceptsLoadedOnDemand {
4  
  new Map<S, Concepts> loaded; // key = just the case ID. value can be null in case of error
5  
  L onCaseLoaded; // L<voidfunc(S caseID, O extraInfo, Concepts concepts)>
6  
  L onUnloadingCase; // L<voidfunc(S caseID, Concepts concepts)>
7  
  Lock lock = lock();
8  
  
9  
  Concepts get(S caseID, O extraInfo) { ret conceptsForCase(caseID, extraInfo); }
10  
  
11  
  Concepts getIfLoaded(S caseID) {
12  
    lock lock;
13  
    ret loaded.get(unnull(caseID));
14  
  }
15  
  
16  
  // caseID may be empty, then you get the program's main DB
17  
  Concepts conceptsForCase(S caseID, O extraInfo) {
18  
    lock lock;
19  
    //assertNempty(+caseID);
20  
    caseID = unnull(caseID);
21  
    
22  
    if (loaded.containsKey(caseID))
23  
      ret assertNotNull("Concepts load error", loaded.get(caseID));
24  
25  
    S id = dbID(caseID);
26  
    print("Loading concepts for case " + id);
27  
    Concepts cc = new Concepts(id);
28  
    cc.persist();
29  
    loaded.put(caseID, cc);
30  
    pcallFAll(onCaseLoaded, caseID, extraInfo, cc);
31  
    ret cc;
32  
  }
33  
  
34  
  S dbID(S caseID) {
35  
    ret combineWithSlash(getDBProgramID(), caseID);
36  
  }
37  
  
38  
  void cleanMeUp {
39  
    cleanUpAndClear(loaded);
40  
  }
41  
  
42  
  void onCaseLoaded(VF3<S, O, Concepts> f) {
43  
    onCaseLoaded = listCreateAndAdd(onCaseLoaded, f);
44  
  }
45  
  
46  
  void onUnloadingCase(VF2<S, Concepts> f) {
47  
    onUnloadingCase = listCreateAndAdd(onUnloadingCase, f);
48  
  }
49  
  
50  
  void unloadCase(S caseID) {
51  
    lock lock;
52  
    caseID = unnull(caseID);
53  
    Concepts cc = loaded.get(caseID);
54  
    if (cc != null) {
55  
      pcallFAll(onUnloadingCase, caseID, cc);
56  
      cleanUp(cc);
57  
      loaded.remove(caseID);
58  
    }
59  
  }
60  
}

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: 287 / 786
Version history: 19 change(s)
Referenced in: [show references]