cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 396 forks source link

Join concurrency guard #433

Open ChrisEineke opened 9 years ago

ChrisEineke commented 9 years ago

This guard ensures that while there is a pending promise for a function, subsequent calls will return the pending promise until the pending promise either rejects or resolves.

briancavalier commented 9 years ago

Thanks, @ceineke, this seems pretty interesting. Can you describe scenarios in which you've needed/used it?

ChrisEineke commented 9 years ago

This comes in handy when you have a long-running function that isn't re-entrant and has a lot of side effects on the system.

Let's say you have a long-running method X that is run when you receive two different AMQP messages A and B. When you receive message A, you invoke method X. If you then receive message B while the method runs, you can join the result. Vice versa, when you receive message B, you invoke method X. If you then receive message A while the method runs, you can join the result.

briancavalier commented 9 years ago

@ceineke Thanks for the explanation. If I understand correctly, the first call to the join-guard will trigger an actual call to underlying guarded function. While that call is inflight, that is until the promise returned by the first call settles, all subsequent calls will return a promise that's equivalent (settles in the same way) to that first one. Am I getting that right?

briancavalier commented 9 years ago

See my comment about refactoring guard(). Do you think that would help to make the join guard simpler and avoid the sneaky use of the reject channel?

As we work through this, it'd be great to add unit tests and docs as well. We may also have to find a name other than join, since when.join already exists and having two things with the same name will probably be confusing.

ChrisEineke commented 9 years ago

I agree with your explanations. I can take a look at refactoring guard(), providing unit tests and documentation, and suggest a different name. How about merge?

briancavalier commented 9 years ago

I can take a look at refactoring guard(), providing unit tests and documentation

Cool, thanks!

How about merge?

I've been trying to think of a short phrase that describes what this guard does. The best I've come up with so far are "memoize while in flight" and "memoize until settled". To me, "merge" doesn't quite capture those phrases. Can you think of alternative short phrases that describe this guard? Maybe they will help us come up with a name.

ChrisEineke commented 9 years ago

Based on this article, how does autoreset sound?

briancavalier commented 9 years ago

Thanks. Yeah, autoreset is an interesting name, esp since it has a relationship to semaphores. It (and the article) also made me think of debounce (see http://whatis.techtarget.com/definition/debouncing), which is a common concept with circuits/signals and event programming. Those both seem like good candidates.

Pinging @unscriptable to see if has ideas.

unscriptable commented 9 years ago

One of the meanings of "enjoin" seems appropriate: "to be performed or adopted". If we're using adjectives, the word "shared" seems like it could lead in the right direction.

"memoize" isn't quite right, unfortunately. :)

What about "coincide", "conform", or "accord" (verb sense)?