Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 15th, 2012  |  syntax: JavaScript  |  size: 1.74 KB  |  views: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. var requirejs = require("requirejs");
  3. requirejs.config({
  4.         nodeRequire: require
  5. });
  6.  
  7. require = requirejs;
  8.  
  9. requirejs(["streamline/lib/callbacks/transform", "streamline/lib/callbacks/runtime"], function(transform, runtime) {
  10.         "use strict";
  11.  
  12.         var __rt = runtime.runtime("test"),
  13.                 __func = __rt.__func,
  14.                 __cb = __rt.__cb,
  15.                 __trap = __rt.__trap;
  16.  
  17.         function streamline(js) {
  18.                 var streamlinejs = transform.transform(js, {
  19.                         noHelpers: true,
  20.                         lines: "ignore"
  21.                 });
  22.                 return new Function("data", "resolvePartial", "resolveVariable", "callback", "__func", "__cb", "__trap", streamlinejs);
  23.         }
  24.  
  25.         function resolvePartial(name, callback) {
  26.                 var template = "";
  27.                 template += "var res = '';\n";
  28.                 template += "res += 'partial:" + name + "\n';\n";
  29.                 template += "callback(null, res);\n";
  30.                 var fn = streamline(template);
  31.                 callback(null, fn);
  32.         }
  33.  
  34.         function resolveVariable(name, callback) {
  35.                 setTimeout(function() {
  36.                         callback(null, 'variable:' + name + '\n');
  37.                 }, 500);
  38.         }
  39.  
  40.         var template = "";
  41.         template += "var res = '';\n";
  42.         template += "if (data.a && data.a > 0) {\n";
  43.         template += "   setTimeout(_, 1000);\n";
  44.         template += "   res += 'a > 0\n';\n";
  45.         template += "   if (data.a === 1) {\n";
  46.         template += "           res += 'a = 1\n';\n";
  47.         template += "   }\n";
  48.         template += "} else {\n";
  49.         template += "   res += 'a < 0\n';\n";
  50.         template += "}\n";
  51.         template += "res += resolvePartial('test', _)(data, resolvePartial, resolveVariable, _);\n";
  52.         template += "res += resolveVariable('test', _);\n";
  53.         template += "res += 'bye!'\n";
  54.         template += "callback(null, res);\n";
  55.  
  56.         var fn = streamline(template);
  57.         var data = {a: 1};
  58.         fn(data, resolvePartial, resolveVariable, function(err, res) {
  59.                 console.log("done:\n" + res);
  60.         }, __func, __cb, __trap);
  61. });