/*jshint -W098 */
(function(root, factory) {
    if (typeof module === "object" && module.exports) {
        // Node, or CommonJS-Like environments
        module.exports = function(postal) {
            factory(require("lodash"), require("conduit"), postal, this);
        };
    } else if (typeof define === "function" && define.amd) {
        // AMD. Register as an anonymous module.
        define(["lodash", "conduit", "postal"], function(_, Conduit, postal) {
            return factory(_, Conduit, postal, root);
        });
    } else {
        // Browser globals
        root.postal = factory(root._, root.Conduit, root.postal, root);
    }
}(this, function(_, Conduit, postal, global, undefined) {

    var plugin = postal.preserve = {
        store: {},
        expiring: []
    };
    var system = postal.channel(postal.configuration.SYSTEM_CHANNEL);
    var dtSort = function(a, b) {
        return b.expires - a.expires;
    };
    var tap = postal.addWireTap(function(d, e) {
        var channel = e.channel;
        var topic = e.topic;
        if (e.headers && e.headers.preserve) {
            plugin.store[channel] = plugin.store[channel] || {};
            plugin.store[channel][topic] = plugin.store[channel][topic] || [];
            plugin.store[channel][topic].push(e);
            // a bit harder to read, but trying to make
            // traversing expired messages faster than
            // iterating the store object's multiple arrays
            if (e.headers.expires) {
                plugin.expiring.push({
                    expires: e.headers.expires,
                    purge: function() {
                        plugin.store[channel][topic] = _.without(plugin.store[channel][topic], e);
                        plugin.expiring = _.without(plugin.expiring, this);
                    }
                });
                plugin.expiring.sort(dtSort);
            }
        }
    });

    function purgeExpired() {
        var dt = new Date();
        var expired = _.filter(plugin.expiring, function(x) {
            return x.expires < dt;
        });
        console.log("Expired message count: " + expired.length);
        while (expired.length) {
            expired.pop().purge();
        }
    }

    if (!postal.subscribe.after) {
        var orig = postal.subscribe;
        postal.subscribe = new Conduit.Sync({
            context: postal,
            target: orig
        });
    }

    postal.SubscriptionDefinition.prototype.enlistPreserved = function() {
        var channel = this.channel;
        var binding = this.topic;
        var self = this;
        purgeExpired(true);
        if (plugin.store[channel]) {
            _.each(plugin.store[channel], function(msgs, topic) {
                if (postal.configuration.resolver.compare(binding, topic)) {
                    _.each(msgs, function(env) {
                        self.callback.call(
                            self.context || (self.callback.context && self.callback.context()) || this,
                            env.data,
                            env);
                    });
                }
            });
        }
    };

    return postal;
}));
@@ -240,16 +240,18 @@
 %22conduit
+js
 %22), post
@@ -416,16 +416,18 @@
 %22conduit
+js
 %22, %22post
@@ -2094,73 +2094,8 @@
 %7D);%0A
-        console.log(%22Expired message count: %22 + expired.length);%0A
     

remove console.log() and change require() and define() "conduit" dependencies to match the way the current postal.js has it, "conduitjs"