Node.js adaptation - EventEmitters pass multiple arguments
michaelsbradleyjr opened this issue · 0 comments
I've been experimenting with usage of flapjax in the context of node.js:
https://github.com/ry/node
This involves changing the local copy of flapjax-2.1.js so that references to addEventListener
-> addListener
and removeEventListener
-> removeListener
, per the node.js API.
Also node.js scripts don't have a window
object by default but a stand-in can easily be created: var window = {}
. Then the global setTimeout can be referenced to to the window object: window.setTimeout = setTimeout
.
With those changes in place, flapjax works just fine inside a node.js script:
require('./flapjax-2.1.js')
var myReceiver = receiverE()
...
One thing I have discovered is that while instances of the node.js EventEmitter class can emit an arbitrary number of arguments (i.e. objects) to a callback function, the $E
function in flapjax will pass only one of the emitted objects.
So for example:
require('./flapjax-2.1.js')
var events = require('events')
var myEmitter = new events.EventEmitter()
var myE = $E(myEmitter, 'eventA')
myE.mapE(function (arg1, arg2) { console.log(arg1, arg2) })
myEmitter.emit('eventA', '1', '2')
If I save that script to myScript.js
and run it from bash with node myScript.js
the the following is printed to the terminal: 1 undefined
.
In other words, $E
is effectively ignoring the second argument-object being emitted on eventA
.
Now, I can "fix" it in the following manner:
...
var myR = receiverE()
myEmitter.on('eventA', function (arg1, arg2) {
myR.sendEvent({ arg1 : arg1, arg2 : arg2 })
})
myR.mapE(function (evt) { console.log(evt.arg1, evt.arg2) })
myEmitter.emit('eventA', '1', '2')
When I run it this time I get what I wanted: 1 2
is printed to the terminal.
However, this workaround introduces additional complexity into the program. Certainly, in larger scripts, once the proper receivers have been setup and the emitters "remapped" to $E
, mapE
, etc., one still benefits from the clarity that flapjax brings to reasoning about the evented dataflow.
I'm curious though how hard it would be to adapt flapjax so that it can pass an arbitrary number of emitted objects from an EventEmitter instead of one (the first one) only. I spent an hour+ poking around in the source, looking for some candidate function that I could modify to do just this, but I came up empty handed (so far).
Your thoughts on this would most appreciated.