Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model eventbus #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

Ako-Njang
Copy link
Contributor

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

pmlopes commented Apr 19, 2022

@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
Copy link
Contributor Author

Ako-Njang commented Apr 19, 2022

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

pmlopes commented Apr 22, 2022

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

Ako-Njang commented Jun 7, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants