| 20 | // @spec { name, send_message } |
| 21 | // ``` |
| 22 | var module_proxy = function(spec, my) { |
| 23 | var _super = {}; |
| 24 | my = my || {}; |
| 25 | spec = spec || {}; |
| 26 | |
| 27 | my.name = spec.name || 'INVALID'; |
| 28 | my.send_message = spec.send_message; |
| 29 | |
| 30 | my.rpc_calls = {}; |
| 31 | |
| 32 | // |
| 33 | // ### _public_ |
| 34 | // |
| 35 | var call; /* call(name, args, cb_); */ |
| 36 | |
| 37 | // |
| 38 | // ### _protected_ |
| 39 | // |
| 40 | var rpc_reply; /* rpc_reply(err,res); */ |
| 41 | |
| 42 | // |
| 43 | // ### _that_ |
| 44 | // |
| 45 | var that = new events.EventEmitter(); |
| 46 | |
| 47 | /****************************************************************************/ |
| 48 | /* PROTECTED METHODS */ |
| 49 | /****************************************************************************/ |
| 50 | // ### rpc_reply |
| 51 | // |
| 52 | // Method called when an `rpc_reply` message is received. |
| 53 | // ``` |
| 54 | // @oid {number} original message id for the `rpc_call` |
| 55 | // @err {Error} Javascript error if an error occurred |
| 56 | // @res {object} JSON serializable result object |
| 57 | // ``` |
| 58 | rpc_reply = function(oid, err, res) { |
| 59 | if(my.rpc_calls[oid]) { |
| 60 | my.rpc_calls[oid](err, res); |
| 61 | delete my.rpc_calls[oid]; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | /****************************************************************************/ |
| 66 | /* PUBLIC METHODS */ |
| 67 | /****************************************************************************/ |
| 68 | // ### |
| 69 | // |
| 70 | // Calls a remote procedure. It generates a message and sends it through the |
| 71 | // `send_message` method. When the reply is received, `rpc_reply` will be |
| 72 | // triggered, and eventually the callback called |
| 73 | // ``` |
| 74 | // @proc {string} the procedure name |
| 75 | // @args {object} serializable JSON arguments |
| 76 | // @cb_ {function(err, res)} the callback when the rpc completes |
| 77 | // ``` |
| 78 | call = function(proc, args, cb_) { |
| 79 | if(my.name === 'INVALID' || my.name === '__ALL__') { |