RxJS based Pub/sub Eventing

An global eventing system is a pretty key piece of a full fledged MVC app. It lets you very easily separate concerns while maintaining communication between discrete parts of the application.

I’ve been using a simple event dispatcher for a while now. At the same time, I’ve had my eye on the excellent, and still way over my head, RxJS library. I had been using it for DOM / browser events, but I couldn’t wrap my head around how to connect event strings (“magic strings”) with an Observable and use that to execute commands or callbacks. Then I discovered this bit of sample code that demonstrated exactly what I wanted to do.

I hand’t looked at the Subject object before, but it does exactly what I need. And the callback / handler / command execute function fit right into the role of the subscribed onNext function.

Adapting the emitter example fit into my framework was straight forward and I ended up with a new Emitter module that replaces both my old EventDispatcher and EventCommandMap.

I’m keeping a simple map of subjects keyed to the event string

_subjectMap[evtStr] = {
 once: once,
 handler: handler,
 subject: new Rx.Subject()
 };

And whenever an event is published, this object is retrieved and the subject’s subscribers are notified. If the event was only supposed to occur once, it’s unmapped safely.

function publish(evtStr, data) {
  var subjObj = _subjectMap[evtStr];

  if(!subjObj) {
    return;
  }

  if(subjObj.once) {
    subjObj.subject.onCompleted();
    subjObj.subject.dispose();
    subjObj = null;
  }
}

Mapping to a command module was painless as well since we just need to map the execute() function to the subject.

function subscribeCommand(evtStr, cmdModule, once) {
  var cmd = require(cmdModule);
  if(cmd.hasOwnProperty('execute')) {
    return subscribe(evtStr, cmd.execute, once);
  } else {
    throw new Error('Emitter cannot map '+evtStr+' to command '+cmdModule+': must have execute()');
  }
}

Smart WBT Players

Just a Saturday morning thought –

Smart WBTs. I’ve always liked the idea.

A long time ago, I wrote several Flash based WBT presentation tools – content was externalized in my own XML schema, played in my nice player. These things are pretty common. Even in modern HTML5 / JavaScript land, a nice player that can present any type of content is a big efficiency. I was taking apart the Articulate Storyline 2 Player JavaScript the other day and it does the same thing. It’s fun to see how other developers tackled the problem.

But one idea I had (around 2005) was: What if the player was smart enough to not only present the content, but understand what the learner was trying to do? What if it’s a typical learner that’s mashing Next as quickly as possible to get to the test and the system noticed this (based on time per page) and prompted you to just go to the test? What if the learner sat on a question or tried 5 times and couldn’t get it right? It would notice and ask if you’d like to go back to that part of the content for a quick review?

I had it all coded up and working perfectly – but it’s hard to get business partners to buy into that when deadlines come. I couldn’t get anyone interested in the idea we just moved on without it. But I never forgot it.

I did bring the idea back with the Social Sim “engine” I built, but never completed my vision for it.

This morning I saw the Synaptic JavaScript library on GitHub. A JavaScript based neural frakin’ network. How cool would that be integrated into a WBT player? If the WBT learned how you interacted with it and altered it’s own behavior.

That would indeed be pretty cool.