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

Remove timer dependency from event-helpers (listen, listenOnce) #8472

Merged

Conversation

jridgewell
Copy link
Contributor

This inverts the responsibility of calling the unlistener. Now, the
caller can be given access the unlisten by passing a closure. The
closure is called with unlisten as the param, giving the caller
access. When the caller determines it is time to stop listening (due to
timeout, etc), it can manually cleanup.

Fixes #8316.

@jridgewell jridgewell force-pushed the event-helper-remove-timer-dep branch 3 times, most recently from ff6623b to 2b78971 Compare March 29, 2017 20:13
Copy link
Contributor

@dvoytenko dvoytenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but couple of q

if (unlistenLoad) {
unlistenLoad();
}
failedToLoad(eleOrWindow);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failedToLoad throws an exception, right? Not a huge fun of those methods. Could you please add a comment here and say that this method will rethrow the original error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

* @param {!EventTarget} element
* @param {string} eventType
* @param {boolean=} opt_capture
* @param {number=} opt_timeout
* @param {function(!UnlistenDef)=} opt_cancel An optional function that, when
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this is ok since this is relatively rare. But WDYT about cancellable promises? E.g. we could have a type extension with method cancel on the promise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about doing this, but it would be exposing it in the wrong object. Ie, how promise do not have a #resolve or #reject, only the resolver gets access to them. If we had a deferred type, we could easily add it there:

/**
 * @typedef {{
 *   resolve: function(*=),
 *   reject: function(*=),
 *   cancel: function(),
 *   promise: !Promise,
 * }}
 */
let DeferredDef;

@jridgewell jridgewell force-pushed the event-helper-remove-timer-dep branch from 4decc53 to 7fda6a5 Compare April 17, 2017 18:24
This inverts the responsibility of calling the unlistener. Now, the
caller can be given access the `unlisten` by passing a closure. The
closure is called with `unlisten` as the param, giving the caller
access. When the caller determines it is time to stop listening (due to
timeout, etc), it can manually cleanup.
@jridgewell jridgewell force-pushed the event-helper-remove-timer-dep branch from 7fda6a5 to d9ec050 Compare April 20, 2017 18:18
@jridgewell jridgewell force-pushed the event-helper-remove-timer-dep branch from 497c5b2 to 021f606 Compare April 20, 2017 19:02
@jridgewell jridgewell merged commit afab245 into ampproject:master Apr 21, 2017
@jridgewell jridgewell deleted the event-helper-remove-timer-dep branch April 21, 2017 19:31
DarXector pushed a commit to DarXector/amphtml that referenced this pull request Apr 25, 2017
…roject#8472)

* Remove opt_timeout from loadPromise

It’s unused

* Allow caller to call internal unlisten

This inverts the responsibility of calling the unlistener. Now, the
caller can be given access the `unlisten` by passing a closure. The
closure is called with `unlisten` as the param, giving the caller
access. When the caller determines it is time to stop listening (due to
timeout, etc), it can manually cleanup.

* lint

* fix merge conflict

* fix test

* Fix tests
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
…roject#8472)

* Remove opt_timeout from loadPromise

It’s unused

* Allow caller to call internal unlisten

This inverts the responsibility of calling the unlistener. Now, the
caller can be given access the `unlisten` by passing a closure. The
closure is called with `unlisten` as the param, giving the caller
access. When the caller determines it is time to stop listening (due to
timeout, etc), it can manually cleanup.

* lint

* fix merge conflict

* fix test

* Fix tests
KenneyE pushed a commit to spotxchange/amphtml that referenced this pull request May 3, 2017
…roject#8472)

* Remove opt_timeout from loadPromise

It’s unused

* Allow caller to call internal unlisten

This inverts the responsibility of calling the unlistener. Now, the
caller can be given access the `unlisten` by passing a closure. The
closure is called with `unlisten` as the param, giving the caller
access. When the caller determines it is time to stop listening (due to
timeout, etc), it can manually cleanup.

* lint

* fix merge conflict

* fix test

* Fix tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants