"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/chainsaw/index.js" (11 Apr 2017, 3705 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var Traverse = require('traverse');
    2 var EventEmitter = require('events').EventEmitter;
    3 
    4 module.exports = Chainsaw;
    5 function Chainsaw (builder) {
    6     var saw = Chainsaw.saw(builder, {});
    7     var r = builder.call(saw.handlers, saw);
    8     if (r !== undefined) saw.handlers = r;
    9     saw.record();
   10     return saw.chain();
   11 };
   12 
   13 Chainsaw.light = function ChainsawLight (builder) {
   14     var saw = Chainsaw.saw(builder, {});
   15     var r = builder.call(saw.handlers, saw);
   16     if (r !== undefined) saw.handlers = r;
   17     return saw.chain();
   18 };
   19 
   20 Chainsaw.saw = function (builder, handlers) {
   21     var saw = new EventEmitter;
   22     saw.handlers = handlers;
   23     saw.actions = [];
   24 
   25     saw.chain = function () {
   26         var ch = Traverse(saw.handlers).map(function (node) {
   27             if (this.isRoot) return node;
   28             var ps = this.path;
   29 
   30             if (typeof node === 'function') {
   31                 this.update(function () {
   32                     saw.actions.push({
   33                         path : ps,
   34                         args : [].slice.call(arguments)
   35                     });
   36                     return ch;
   37                 });
   38             }
   39         });
   40 
   41         process.nextTick(function () {
   42             saw.emit('begin');
   43             saw.next();
   44         });
   45 
   46         return ch;
   47     };
   48 
   49     saw.pop = function () {
   50         return saw.actions.shift();
   51     };
   52 
   53     saw.next = function () {
   54         var action = saw.pop();
   55 
   56         if (!action) {
   57             saw.emit('end');
   58         }
   59         else if (!action.trap) {
   60             var node = saw.handlers;
   61             action.path.forEach(function (key) { node = node[key] });
   62             node.apply(saw.handlers, action.args);
   63         }
   64     };
   65 
   66     saw.nest = function (cb) {
   67         var args = [].slice.call(arguments, 1);
   68         var autonext = true;
   69 
   70         if (typeof cb === 'boolean') {
   71             var autonext = cb;
   72             cb = args.shift();
   73         }
   74 
   75         var s = Chainsaw.saw(builder, {});
   76         var r = builder.call(s.handlers, s);
   77 
   78         if (r !== undefined) s.handlers = r;
   79 
   80         // If we are recording...
   81         if ("undefined" !== typeof saw.step) {
   82             // ... our children should, too
   83             s.record();
   84         }
   85 
   86         cb.apply(s.chain(), args);
   87         if (autonext !== false) s.on('end', saw.next);
   88     };
   89 
   90     saw.record = function () {
   91         upgradeChainsaw(saw);
   92     };
   93 
   94     ['trap', 'down', 'jump'].forEach(function (method) {
   95         saw[method] = function () {
   96             throw new Error("To use the trap, down and jump features, please "+
   97                             "call record() first to start recording actions.");
   98         };
   99     });
  100 
  101     return saw;
  102 };
  103 
  104 function upgradeChainsaw(saw) {
  105     saw.step = 0;
  106 
  107     // override pop
  108     saw.pop = function () {
  109         return saw.actions[saw.step++];
  110     };
  111 
  112     saw.trap = function (name, cb) {
  113         var ps = Array.isArray(name) ? name : [name];
  114         saw.actions.push({
  115             path : ps,
  116             step : saw.step,
  117             cb : cb,
  118             trap : true
  119         });
  120     };
  121 
  122     saw.down = function (name) {
  123         var ps = (Array.isArray(name) ? name : [name]).join('/');
  124         var i = saw.actions.slice(saw.step).map(function (x) {
  125             if (x.trap && x.step <= saw.step) return false;
  126             return x.path.join('/') == ps;
  127         }).indexOf(true);
  128 
  129         if (i >= 0) saw.step += i;
  130         else saw.step = saw.actions.length;
  131 
  132         var act = saw.actions[saw.step - 1];
  133         if (act && act.trap) {
  134             // It's a trap!
  135             saw.step = act.step;
  136             act.cb();
  137         }
  138         else saw.next();
  139     };
  140 
  141     saw.jump = function (step) {
  142         saw.step = step;
  143         saw.next();
  144     };
  145 };