VulcanJS / Vulcan

🌋 A toolkit to quickly build apps with React, GraphQL & Meteor
http://vulcanjs.org
MIT License
7.98k stars 1.88k forks source link

Callbacks API 2.0 #1999

Closed SachaG closed 5 years ago

SachaG commented 6 years ago

Currently:

runCallbacks(callbackName, iterator, arg1, arg2, arg3…);

Proposal:

runCallbacks({ 
  name,  // the name of the callback
  iterator,  // the object that will be iterated on
  properties, // arguments passed down to each iteration (i.e. { arg1, arg2, arg3… })
  options, // callback options such as sync/async/concurrent
});
SachaG commented 6 years ago

Ping @ochicf

SachaG commented 6 years ago

Oh also I think it makes sense to use the type name instead of the collection name:

movies.edit.async -> movie.update.async

mattblackdev commented 6 years ago
runCallbacks({
  // properties
  arguments, // [arg1, arg2] 
SachaG commented 6 years ago

@mattblackdev the reason I didn't use arguments is because it's a reserved word in JS.

mattblackdev commented 6 years ago

@SachaG 🙈

x5engine commented 6 years ago

@SachaG 🙈

ochicf commented 6 years ago

Hi! Sorry for the late response, I've been kind of busy this week and I wanted to do it properly.

I like the proposal of having a single object as parameter for runCallbacks. Some comments/questions:

So, the signature would look like this:

runCallbacks({ 
  name,  // the name of the callback
  iterator,  // the object that will be iterated on
  parameters, // or `args`, `params`. arguments passed down to each iteration (i.e. { arg1, arg2, arg3… })
  ...options, // callback options such as sync/async/concurrent
});

As for options, I'll refer to the last discussion we had about this topic, and more specific to my post in https://github.com/VulcanJS/Vulcan/issues/1949#issuecomment-389111037. Long story short, the options I think we have:

An option would be to have different specific runners, being consecutive.sync, consecutive.async, concurrent the ones that cover the requirements exposed above.

runCallbacks({ 
  name,
  iterator,
  parameters, 
  runs: 'consecutive.sync|consecutive.async|concurrent', // defaults to 'consecutive.sync'?
  ...otherOptions, // if any
});

That gives me an idea to maybe offer a simple API to add custom runners? That way anyone could add their callback runner if they need to. I think it's kind of complex so I'm not going to enter this rabbit hole right now, but if you guys think it is a good idea tell me and I'll ellaborate.

About the signature of an added callback, I'm guessing we would keep the same as we have right now, being function addedCallback(iterator, ...parameters) { return iterator; }.

Tell me what you guys think!

SachaG commented 6 years ago

properties would be an object or an array? I'm guessing an array, since it fits better with how parameters in functions are handled.

An object I think, otherwise we'll just end up with the same backwards compatibility issues when something changes (i.e. the second argument was modifier but it's now data).

following on renaming properties, since arguments is reserved, maybe args, parameters or params would fit better? I think they better represent of what they actually are.

Good point. Maybe args then?

ochicf commented 6 years ago

So, the added callbacks would be like this then, function callbackName(iterator, argsAsObject) {}, right?

SachaG commented 6 years ago

Yes exactly, the callbacks would be the same as now except that all args would be "compressed" into the second argument.