Skip to content

Instantly share code, notes, and snippets.

@ericf
Created January 15, 2014 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericf/8445732 to your computer and use it in GitHub Desktop.
Save ericf/8445732 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
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