Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
runat-1128988: Add event.once
Browse files Browse the repository at this point in the history
I'm really surprised that this wasn't there already.

Signed-off-by: Joe Walker <[email protected]>
  • Loading branch information
joewalker committed Mar 19, 2015
1 parent 75877de commit 6c0a31c
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/gcli/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ exports.createEvent = function(name) {
handlers = [];
};

/**
* Fire an event just once using a promise.
*/
event.once = function() {
return new Promise(function(resolve, reject) {
var handler = function(arg) {
event.remove(handler);

This comment has been minimized.

Copy link
@bgrins

bgrins Mar 19, 2015

I could see someone thinking they could pass a callback into this function since add/remove take them.. Seems like it would be easy enough to take in func, scope and fire it appropriately in addition to returning the promise

This comment has been minimized.

Copy link
@joewalker

joewalker Mar 20, 2015

Author Owner

It would be easy to add that, but does it actually help anyone? Isn't a promise the 'right' way?
How about I add:

if (arguments.length !== 0) {
  throw new Error('event.once uses promise return values');
}

This comment has been minimized.

Copy link
@bgrins

bgrins Mar 20, 2015

I was thinking there was value in having the most-guessable API since it would match what is already here and what we use in the devtools event-emitter, but I don't care too much. Throwing an error is better than silently ignoring the argument.

resolve(arg);
};

event.add(handler);
});
},

/**
* Temporarily prevent this event from firing.
* @see resumeFire(ev)
Expand Down

0 comments on commit 6c0a31c

Please sign in to comment.