binary.tap handler never called if word* method not called just after instantiation #16

Open
ruquay opened this Issue Sep 11, 2012 · 1 comment

Comments

Projects
None yet
1 participant

ruquay commented Sep 11, 2012

If an instance of binary is created, but no word* methods are called until a later event loop, the tap handler never gets called. This happens whether a word* method is called in process.nextTick, or setTimeout.

Via process.nextTick

var binary = require("binary");
var bin = binary();
process.nextTick(function() {
  bin.word8("myInt");
  bin.tap(function(vars) {
    // this never shows
    console.log(vars);
  });
  bin.write(new Buffer([42]));
});

Via setTimeout

var binary = require("binary");
var bin = binary();
setTimeout(function() {
  bin.word8("myInt");
  bin.tap(function(vars) {
    // this never shows
    console.log(vars);
  });
  bin.write(new Buffer([42]));
}, 10);

This one works

var binary = require("binary");
var bin = binary().word8("myInt");
process.nextTick(function() {
  bin.tap(function(vars) {
    // this DOES show
    console.log(vars);
  });
  bin.write(new Buffer([42]));
});

A use case for the expected behavior might be to create a long-lived instance of binary attached to a client TCP stream when you don't know which word* method to call until some specific client behavior has been triggered. And the workaround for such a case is to lazily instantiate the binary until you do know this.

ruquay commented Sep 14, 2012

I have since discovered that this doesn't happen only right after instantiation. Keeping a reference to an instance of binary, while not assigning an pending word, and then letting the event loop finish its run, will always reproduce this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment