cujojs / meld

AOP for JS with before, around, on, afterReturning, afterThrowing, after advice, and pointcuts
Other
644 stars 65 forks source link

async before/after functions supported? #13

Closed 0xgeert closed 11 years ago

0xgeert commented 11 years ago

Are async before/after functions supported?

Allow me to explain the need:

I'm designing a modular system (similar to Backbone Marionette) in which modules may have extra (often crosscutting) functionality mixed-in. These modules are managed by the app and have a strict livecycle, existing of solely a 'create', 'stage', 'destage' and 'teardown' method which are inherited from a baseClass module.

Modules can't directly communicate to eachother to keep modules strictly separated, i.e: everything is done over pub/sub.

The idea (borrowed from Twitter Flight) is to have a Module and it's optional Mixins to implement logic using before('create'), after('appear), etc. I.e: the 'create' method is never extended (in the classical OOP sense) but 'aspected'. I plan on using Meld.js for this.

These mixed-in methods may be async in nature: e.g.: a Template Module which needs to lazy-fetch it's mustache-template from a remote location.

Besides this, every module will also by definition publish livecycle events: beforeCreate, afterAppear, etc. to communicate state to the app and other modules that might be listening in.

The problem is in the afterX events, which should be called after:

Is such a usecase supported with Meld.js out of the box?

If not, I could perhaps (?) utilize Cujojs/When somehow for this issue.

Any help clarifying this much appreciated.

briancavalier commented 11 years ago

Hey @gbrits, there is nothing out-of-the-box in meld right now for working with promises. However, as you suspected, it is possible to use meld.js and when.js together to do it. In fact, wire.js (cujo's IOC container) does just that in it's wire/aop plugin. It provides new advice types: afterFulfilling, afterRejecting, and afterPromise.

The code is really simple, so have a look and hopefully that will give some ideas.

We're considering creating some sort of meld add-on that provides this functionality, but we haven't decided exactly how we want to package it up yet.

Let me know if you have any questions!