Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- part of NuclearSummer;
- class PathWhitelist {
- static List<String> paths = new List<String>();
- static addPath (String file) {
- paths.add(file);
- }
- static canRequest(String file) {
- return paths.contains(file);
- }
- }
- // Trying to simulate non async language else it gets really confusing if the grabber for lets say money updates before the actual data does
- class TemplateFiller {
- static Map<String, String> HTML_FILE_CACHE = new Map<String,String>();
- Map <String, Function> _callbacks = new Map<String, Function>();
- Map <String, String> _outputs = new Map<String, dynamic>();
- int _currCallback = 0;
- int _waitingFor = 0;
- bool newFile = false;
- String fPath = "";
- TemplateFiller (File file) {
- if (!HTML_FILE_CACHE.containsKey(file.path)) {
- newFile = true;
- fPath = file.path;
- this.queueCallback("HTMLFILE", () {
- return file.readAsString(encoding: Encoding.ASCII);
- });
- }
- else {
- _outputs["HTMLFILE"] = HTML_FILE_CACHE[file.path];
- }
- }
- void queueCallback (String namedParameter, Function fun()) {
- if (!_callbacks.containsKey(namedParameter)) {
- _waitingFor++;
- _callbacks[namedParameter] = fun;
- }
- else {
- throw "Trying to load a named parameter that already exists";
- }
- }
- void knownValue (String namedParameter, String output) {
- if (!_callbacks.containsKey(namedParameter)) {
- _outputs[namedParameter] = output;
- }
- else {
- throw "Trying to load a named parameter that already exists";
- }
- }
- void _loadInOrder (Completer c) {
- // Well I think this will probably be regarded as completely disgusting :P But you know what, its easier.
- if (_callbacks.length > 0) {
- _callbacks.values.elementAt(_currCallback)().then((val) {
- this._outputs[_callbacks.keys.elementAt(this._currCallback)] = val;
- this._currCallback++;
- if (this._waitingFor != this._currCallback) {
- this._loadInOrder(c);
- }
- else {
- c.complete(this._outputs);
- }
- });
- }
- else {
- c.complete(this._outputs);
- }
- }
- Future<Map<String,dynamic>> _getOutputs () {
- Completer comp = new Completer();
- _loadInOrder(comp);
- return comp.future;
- }
- Future<String> templateOutput () {
- Completer comp = new Completer();
- this._getOutputs().then((Map<String, String> outputs) {
- if (newFile == true) {
- HTML_FILE_CACHE[fPath] = outputs["HTMLFILE"];
- }
- RegExp regy = new RegExp(r"<%<([a-zA-Z0-9_]+?)>%>");
- comp.complete(outputs["HTMLFILE"].replaceAllMapped(regy,(match) {
- if (outputs.containsKey(match.group(1))) {
- return outputs[match.group(1)];
- }
- else {
- return "";
- }
- }));
- });
- return comp.future;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement