vert-x3 / vertx-eventbus-bridge-clients

Home for various EventBus Bridge clients
Other
65 stars 32 forks source link

model eventbus #69

Open Ako-Njang opened 2 years ago

Ako-Njang commented 2 years ago

@pmlopes here is simple modeling of the EventBus object I did. I also did some refactoring to suit ES6 with respect to variables and function declarations. While working on creating promises to replace the callbacks can you have any suggestions?

pmlopes commented 2 years ago

@Ako-Njang thank you, this is a great start.

I think you can keep improving this even further:

For example, all functions that have a final argument: callback, could be transformed to a promise like function. Let me illustrate:

// original
function someFunction(a, b, callback) {
  // some code
  if (success)
    callback(null, result);
  } else {
    callback(error);
  }
}

Can be transformed to:

// original
function someFunction(a, b) {
  return new Promise(onSuccess, onFailure) => {      
    // some code
    if (success)
      onSuccess(result);
    } else {
      onFailure(error);
    }
  });
}

This means that later users can use this new API as:

let reply = await eventBus.send(x, y);

As the return of the function is a promise users can write simpler code with async/await (or if they need to support older browsers), then chaining promises is also possible and still more readable than the older callback style.

Ako-Njang commented 2 years ago

@Ako-Njang thank you, this is a great start.

I think you can keep improving this even further:

For example, all functions that have a final argument: callback, could be transformed to a promise like function. Let me illustrate:

// original
function someFunction(a, b, callback) {
  // some code
  if (success)
    callback(null, result);
  } else {
    callback(error);
  }
}

Can be transformed to:

// original
function someFunction(a, b) {
  return new Promise(onSuccess, onFailure) => {      
    // some code
    if (success)
      onSuccess(result);
    } else {
      onFailure(error);
    }
  });
}

This means that later users can use this new API as:

let reply = await eventBus.send(x, y);

As the return of the function is a promise users can write simpler code with async/await (or if they need to support older browsers), then chaining promises is also possible and still more readable than the older callback style.

Thank you for this. I was a little confused about how the promises would be used. I will keep in mind the promises are for users who will install the library later in their projects. Thanks for clearing my doubts. I will begin this phase. I appreciate the feedback.

pmlopes commented 2 years ago

Yes, promises can be used with any browser (except really old ones or IE10) but those don't support webrtc either.

Old browsers may not support the async / await style but as Promises are supported they work like this:

// new browser:
let reply = await eventBus.send(a, b);

// older (on the same code)
eventBus.send(a, b)
  .then(reply => {
    // code that uses the reply...
   });
Ako-Njang commented 2 years ago

This week ( 06 - 10/06/2022 )

Next week (13 - 17/06/2022)

Message ID: <vert-x3/vertx-eventbus-bridge-clients/pull/69/c1106751098@

github.com>