cujojs/meld

Add advise for all functions in an object

Closed this issue · 2 comments

Would it make sense to add a method which will be able to add the different advice's for all the methods/function of an object. A use case that comes to mind immediately is to log the error thrown by function.

Hi @nitinkcv. I don't think there's a single definition of "all methods" in current JavaScript. It probably means different things to different people in different use cases. For example, there are owned and inherited properties (including methods inherited by all objects, like hasOwnProperty), enumerable/non-enumerable, writable/non-writable, configurable/non-configurable, etc. So, there's a large number of possibilities for what "all methods" means.

Meld provides the ability to advise methods of an object matching a predicate. The predicate can be a single string, array of strings, regex, or function. For the particular definition of "all methods" fits your use case, you can write your own helper function to collect (using Object.keys, getOwnPropertyNames, getOwnPropertyDescriptor, or by doing prototype chain traversal with getPrototypeOf, etc.) those names into an array, and use it with the with meld's existing API.

meld.afterThrowing(myObject, myFindAllMethodNamesHelper(myObject), function(thrownError) { ... })

So, rather that build in a notion of "all methods", I think it makes sense for meld's core API to remain flexible and allow callers to determine the set of methods that fit their use case. I could certainly see someone writing another helper package that provides some convenience functions for common cases.

Closing due to lack of activity. If there's a need to reopen, please do.