PrivateSky / swarmcore

Swarm 2.0 implementation
Other
15 stars 4 forks source link

Error in callback in swarm phase #4

Closed jwulf closed 9 years ago

jwulf commented 9 years ago

I am getting this error:

Error: Executing invalid code from callbacks. Please use asynchron/swait to keep proper track for execution contexts

How do I do this?

In a swarm:

"swarmPhase" : {
     node: "someAdapter",
    code: function () {
       me = this;
       executeAdapter function (this.email, function adapterFunctionCallback (err, result) {
         if (!err) {
            me.property = result;
            me.swarm("nextPhase");
      });
   }
}
jwulf commented 9 years ago

Found it here: https://github.com/salboaie/asynchron

          var me = this;
          var result = executeAdapterFunction.async(this.email);
          (function (result){
            console.log('Swarm callback');
            console.log(result);
            me.property = result;
            me.swarm("Notification")
          }).swait(result);

This works.

salboaie commented 9 years ago

Unfortunately I'm a bit behind with documentation. In the SwarmESB 2.0 we track all the callback calls to ensure at least once semantic so all the callbacks that have to be called in phase code should be used with .swait approach or warapped in a S function call (that creates a context bounded ). Global S function is a shortcut for createSwarmCallback .

Now, I'm curios, you seems that are digging deep in swarmESB, can you tell me about your project? SwarmESB is still a research project, but I managed to use in some real life project so it is possible but there could be some hops from place to place :) Any additional help with better documentation and testing will be very great. Thank you!

jwulf commented 9 years ago

I'm integrating the backend systems for our Yoga Academy (https://www.atmayoga.com.au), and Node.js and Javascript is my language.

I get that SwarmCore is still experimental and in development, but I was about to implement something like an ESB myself and I thought: "Hang on, let me see if something like an ESB already exists for Node.js"

So I found SwarmESB and I couldn't understand it at first. But I was intrigued, especially by the trees and bees analogy. So I read some more and I think it's great.

jwulf commented 9 years ago

OK, so using createSwarmCallback (or the alias function name 'S'), I wrap the callback like this:

"swarmPhase" : {
     node: "someAdapter",
    code: function () {
       me = this;
       executeAdapterFunction(this.email, createSwarmCallback(adapterFunctionCallback (err, result) {
         if (!err) {
            me.property = result;
            me.swarm("nextPhase");
      }));
   }
}

That works. It looks like I still have to assign me=this to get a reference to the swarm in the closure.

salboaie commented 9 years ago

Yeap, should do the magic. Yeap, you have to assign a self or me variable, is normal JS...