Advertisement
Guest User

Untitled

a guest
Jun 30th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. part of NuclearSummer;
  2.  
  3. class PathWhitelist {
  4. static List<String> paths = new List<String>();
  5. static addPath (String file) {
  6. paths.add(file);
  7. }
  8. static canRequest(String file) {
  9. return paths.contains(file);
  10. }
  11. }
  12.  
  13. // Trying to simulate non async language else it gets really confusing if the grabber for lets say money updates before the actual data does
  14. class TemplateFiller {
  15. static Map<String, String> HTML_FILE_CACHE = new Map<String,String>();
  16. Map <String, Function> _callbacks = new Map<String, Function>();
  17. Map <String, String> _outputs = new Map<String, dynamic>();
  18. int _currCallback = 0;
  19. int _waitingFor = 0;
  20. bool newFile = false;
  21. String fPath = "";
  22. TemplateFiller (File file) {
  23. if (!HTML_FILE_CACHE.containsKey(file.path)) {
  24. newFile = true;
  25. fPath = file.path;
  26. this.queueCallback("HTMLFILE", () {
  27. return file.readAsString(encoding: Encoding.ASCII);
  28. });
  29. }
  30. else {
  31. _outputs["HTMLFILE"] = HTML_FILE_CACHE[file.path];
  32. }
  33. }
  34. void queueCallback (String namedParameter, Function fun()) {
  35. if (!_callbacks.containsKey(namedParameter)) {
  36. _waitingFor++;
  37. _callbacks[namedParameter] = fun;
  38. }
  39. else {
  40. throw "Trying to load a named parameter that already exists";
  41. }
  42. }
  43. void knownValue (String namedParameter, String output) {
  44. if (!_callbacks.containsKey(namedParameter)) {
  45. _outputs[namedParameter] = output;
  46. }
  47. else {
  48. throw "Trying to load a named parameter that already exists";
  49. }
  50. }
  51. void _loadInOrder (Completer c) {
  52. // Well I think this will probably be regarded as completely disgusting :P But you know what, its easier.
  53. if (_callbacks.length > 0) {
  54. _callbacks.values.elementAt(_currCallback)().then((val) {
  55. this._outputs[_callbacks.keys.elementAt(this._currCallback)] = val;
  56. this._currCallback++;
  57.  
  58. if (this._waitingFor != this._currCallback) {
  59. this._loadInOrder(c);
  60. }
  61. else {
  62. c.complete(this._outputs);
  63. }
  64. });
  65. }
  66. else {
  67. c.complete(this._outputs);
  68. }
  69. }
  70. Future<Map<String,dynamic>> _getOutputs () {
  71. Completer comp = new Completer();
  72. _loadInOrder(comp);
  73. return comp.future;
  74. }
  75.  
  76. Future<String> templateOutput () {
  77. Completer comp = new Completer();
  78. this._getOutputs().then((Map<String, String> outputs) {
  79. if (newFile == true) {
  80. HTML_FILE_CACHE[fPath] = outputs["HTMLFILE"];
  81. }
  82. RegExp regy = new RegExp(r"<%<([a-zA-Z0-9_]+?)>%>");
  83. comp.complete(outputs["HTMLFILE"].replaceAllMapped(regy,(match) {
  84. if (outputs.containsKey(match.group(1))) {
  85. return outputs[match.group(1)];
  86. }
  87. else {
  88. return "";
  89. }
  90. }));
  91. });
  92. return comp.future;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement