Publisher

This testbed shows how Dustin Diaz’ publisher framework can be imported and used. It is structured like a unit test and may be converted into one as the infrastructure for that is made more accessible.


// import com.dustindiaz.publisher

// SETUP

// The number of observed yawns and a yawning function.
var yawnCount = 0;
var yawnListener = function yawnListener(event) {
    alert(”yawn at joke ” + event.id);
    yawnCount++;
}

// The number of observed giggles and a giggle function.
var giggleCount = 0;
var giggleListener = function giggleListener(event) {
    alert(”giggle at joke ” + event.id);
    giggleCount++;
}

/** Asserts the number of observed yawns and giggles and resets counters. */
var assertCounts = function assertCounts(expectedYawns, expectedGiggles) {
    if (yawnCount != expectedYawns) {
        alert(”Expected ” + expectedYawns + ” yawns, but got ” + yawnCount);
    }
    yawnCount = 0;
    if (giggleCount != expectedGiggles) {
        alert(”Expected ” + expectedGiggles + ” giggles, but got ” + giggleCount);
    }
    giggleCount = 0;
}

/** Test event passing using publisher library */
var testPublisher = function() {
    /** Comedian object with joke and sneeze publishers.*/
    var comedian = {
        joke: new Publisher(),
        sneeze: new Publisher()
    };

    var assertListenerCount = function assertListenerCount(event, expectedCount) {
        var count = comedian[event].subscribers.length;
        if (count != expectedCount) {
            alert(”Expected ” + expectedCount + ” listeners for ” + event + “, but found ” + count);
        }
    };

    assertListenerCount(”joke”, 0);

    comedian.joke.deliver({id:1});
// no response
    assertCounts(0, 0);

    yawnListener.subscribe(comedian.joke);
    assertListenerCount(”joke”, 1);

    comedian.joke.deliver({id:2})
// yawn at 2
    assertCounts(1, 0);

    giggleListener.subscribe(comedian.joke);
    assertListenerCount(”joke”, 2);
    comedian.joke.deliver({id:3})
// yawn at 3 and giggle at 3
    assertCounts(1, 1);

    comedian.sneeze.deliver({id:4})
// no response
    assertCounts(0, 0);

    giggleListener.unsubscribe(comedian.joke);
    assertListenerCount(”joke”, 1);
    comedian.joke.deliver({id:5})
// yawn at 5
    assertCounts(1, 0);

    yawnListener.unsubscribe(comedian.joke);
    assertListenerCount(”joke”, 0);
    comedian.joke.deliver({id:6})
// no response
    assertCounts(0, 0);
}

testPublisher();
alert(”done”);

5 Responses to “Publisher”

  1. logi says:

    There is a code tag around the code. It just doesn’t maintain the indentation.

  2. logi says:

    The code indentation is all screwed up. Does anyone know how to fix that? Is it a theme issue?

  3. logi says:

    Fixored. I replaced the code block with a pre block and all is well again.

  4. nando says:

    I think it’s a theme thing… we could get a code tag to surround code snippets and make it look prettier :)

  5. logi says:

    oh, and I changed the time-zone setting so Nando’s post got pushed completely out of order. It was posted an hour earlier than it appears to have been.

Leave a Reply

You must be logged in to post a comment.