• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3let $module
4
5/*
6  let contextProto = this.context;
7  while (contextProto = Object.getPrototypeOf(contextProto)) {
8    completionGroups.push(Object.getOwnPropertyNames(contextProto));
9  }
10*/
11
12
13function handle (data) {
14  let idx      = data.idx
15    , child    = data.child
16    , method   = data.method
17    , args     = data.args
18    , callback = function () {
19        let _args = Array.prototype.slice.call(arguments)
20        if (_args[0] instanceof Error) {
21          let e = _args[0]
22          _args[0] = {
23              '$error'  : '$error'
24            , 'type'    : e.constructor.name
25            , 'message' : e.message
26            , 'stack'   : e.stack
27          }
28          Object.keys(e).forEach(function(key) {
29            _args[0][key] = e[key]
30          })
31        }
32        process.send({ owner: 'farm', idx: idx, child: child, args: _args })
33      }
34    , exec
35
36  if (method == null && typeof $module == 'function')
37    exec = $module
38  else if (typeof $module[method] == 'function')
39    exec = $module[method]
40
41  if (!exec)
42    return console.error('NO SUCH METHOD:', method)
43
44  exec.apply(null, args.concat([ callback ]))
45}
46
47
48process.on('message', function (data) {
49  if (data.owner !== 'farm') {
50    return;
51  }
52
53  if (!$module) return $module = require(data.module)
54  if (data.event == 'die') return process.exit(0)
55  handle(data)
56})
57