Skip to content

Instantly share code, notes, and snippets.

@ericf ericf/gist:8445732
Created Jan 15, 2014

Embed
What would you like to do?
Exposed.prototype._getApplicableNamespaces = function () {
var namespaces = this.__namespaces__.concat(),
proto = Object.getPrototypeOf(this);
function isApplicable(namespace) {
return !namespaces.some(function (ns) {
var nsRegex = new RegExp('^' + ns + '(?:$|\\..+)');
return nsRegex.test(namespace);
});
}
while (Exposed.isExposed(proto)) {
namespaces.unshift.apply(namespaces,
proto.__namespaces__.filter(isApplicable));
proto = Object.getPrototypeOf(proto);
}
return namespaces;
};
@ezequiel

This comment has been minimized.

Copy link

ezequiel commented Jan 15, 2014

I know, performance probably doesn't matter. So, for laughs:

Exposed.prototype._getApplicableNamespaces = function () {
    var namespaces = this.__namespaces__;
        proto      = Object.getPrototypeOf(this);

    function isApplicable(namespace) {
        return !namespaces.some(function (ns) {
            var nsRegex = new RegExp('^' + ns + '(?:$|\\..+)');
            return nsRegex.test(namespace);
        });
    }

    // couldn't think of a better name
    var namespacez = [];
    while (Exposed.isExposed(proto)) {
        namespacez.push.apply(namespacez,
                proto.__namespaces__.filter(isApplicable));

        proto = Object.getPrototypeOf(proto);
    }

    return namespacez.reverse().concat(namespaces)
};

I don't know for certain if this actually works, nor if it really is faster. Both assumptions should be true... in theory.

@ericf

This comment has been minimized.

Copy link
Owner Author

ericf commented Jan 15, 2014

Oh this actually has to check the whole chain. There's some semantics behind this which aren't obvious, but I documented them in the real code: https://github.com/yahoo/express-state/blob/cache/lib/exposed.js#L118

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.