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

feat(events): add eventStack #1733

Merged
merged 14 commits into from
Aug 27, 2017
Merged

feat(events): add eventStack #1733

merged 14 commits into from
Aug 27, 2017

Conversation

layershifter
Copy link
Member

@layershifter layershifter commented Jun 2, 2017

Rel #284.
Fixes #1657.

What is it?

This PR introduces a new event handling system that will solve following problems:

  • event priority, only top event will be emitted (will help with multiple Modals and Dropdowns);
  • solve performance problems with multiple Dropdowns;

Example

-document.addEventListener('keydown', this.openOnArrow)
-document.addEventListener('keydown', this.openOnSpace)
+eventStack.sub('keydown', [this.openOnArrow, this.openOnSpace])
-document.removeEventListener('keydown', this.openOnArrow)
-document.removeEventListener('keydown', this.openOnSpace)
-document.removeEventListener('keydown', this.moveSelectionOnKeyDown)
-document.removeEventListener('keydown', this.selectItemOnEnter)
-document.removeEventListener('keydown', this.removeItemOnBackspace)
+eventStack.unsub('keydown', [
+  this.openOnArrow,
+  this.openOnSpace,
+  this.moveSelectionOnKeyDown,
+  this.selectItemOnEnter,
+  this.removeItemOnBackspace,
+])

@layershifter
Copy link
Member Author

layershifter commented Jun 2, 2017

@levithomason Would be awesome to hear your throughts.

However, I want to finish my other open PRs before I'll start there.


unlisten = name => {
if(_.some(this.queues, name)) return
document.removeEventListener(name, this.emit(name))
Copy link
Member

Choose a reason for hiding this comment

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

The emit method returns a new function on each call. Are you sure this removes the original listener(s)?

Copy link
Member Author

Choose a reason for hiding this comment

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

@davezuko Yep, you're fully right there. I pushed some changes and tests, now it works as I described.

@codecov-io
Copy link

codecov-io commented Jun 7, 2017

Codecov Report

Merging #1733 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #1733      +/-   ##
=========================================
- Coverage    99.8%   99.8%   -0.01%     
=========================================
  Files         148     148              
  Lines        2589    2555      -34     
=========================================
- Hits         2584    2550      -34     
  Misses          5       5
Impacted Files Coverage Δ
src/modules/Search/Search.js 100% <100%> (ø) ⬆️
src/addons/Portal/Portal.js 100% <100%> (ø) ⬆️
src/modules/Dropdown/Dropdown.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 23ec465...78564ee. Read the comment docs.

}
document.addEventListener('keydown', this.removeItemOnBackspace)
Copy link
Member Author

Choose a reason for hiding this comment

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

moveSelectionOnKeyDown, selectItemOnEnter and removeItemOnBackspace should be added only if the Dropdown is open

Copy link
Member

Choose a reason for hiding this comment

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

removeItemOnBackspace works when a multiple search selection Dropdown is focused and openOnFocus={false}. The cursor focusses the Dropdown, it does not open, but the backspace listener is added so the user can remove labels:

http://g.recordit.co/XcbauX3d4V.gif

I'd prefer we don't change any logic in this PR except the adding/removing events. That said, it looks like we're missing a test for this as well. Changing that logic should have broken a test.

Copy link
Member Author

Choose a reason for hiding this comment

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

@levithomason Thanks for clarification, I've restored event handlers and added a fix. Now eventPull handles only unique events

Copy link
Member Author

@layershifter layershifter left a comment

Choose a reason for hiding this comment

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

@davezuko @levithomason I've performed final updates there, will be awesome to get a feedback there.

@levithomason
Copy link
Member

I believe this also solves #1157, correct?

@layershifter
Copy link
Member Author

No, #1157 will need an another solution, however this PR opens the road to it.

@levithomason
Copy link
Member

levithomason commented Aug 20, 2017

I have not gone into this in detail yet, but I don't want to hold it up any longer so I'd like to merge it.

One nit I have is that a pool, in my experience, is a set of shared resources where any available item may be pulled form the pool (like a db connection). I'd like to call this a "stack", which indicates they will be processed one at a time in the order they were added from the end. Is this accurate with the implementation here?

If so, let's go with eventStack over eventPool, merge, and move forward 👍

EDIT Fix stack description, thanks David.

@layershifter
Copy link
Member Author

I will change tomorrow and will sync with master 👍

@layershifter layershifter changed the title feat(events): add eventPool feat(events): add eventStack Aug 21, 2017
@layershifter
Copy link
Member Author

I merged with master, renamed to eventStack and fixed lint issues.

@dvdzkwsk
Copy link
Member

dvdzkwsk commented Aug 21, 2017

I'd like to call this a "stack", which indicates they will be processed one at a time in the order they were added. Is this accurate with the implementation here?

I think you mean a queue ;) <3

@kyrasteen
Copy link
Contributor

Hey there. Thanks for fixing this issue. I was wondering if this will be merged and released in the near future? It would help my team out with a current production bug. If there is anything I can contribute to get this merged, happy to help. :)

@levithomason
Copy link
Member

I think you mean a queue ;) <3

Whoops, I still mean a stack, however, I was lazy and inaccurate in the description. I meant to say "processed one at a time, from the end" rather than "in the order added".

When I imagine a Popup opening inside of a Modal, I think of a stack of handlers. The Popup handlers should be processed first even though they were added last. In a queue, I'd think the Modal handlers would be processed first, then the Popup (in the order they were added).

Thanks for pointing out the error @davezuko.

@levithomason
Copy link
Member

Released in [email protected]

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.

5 participants