Guest User

Application-specific event loops in JavaScript UIs

a guest
Jul 19th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. (A longer version of https://news.ycombinator.com/item?id=6074054
  2. re application-specific event loops in JavaScript UIs.)
  3.  
  4. From a complexity point of view, if your UI can easily be modeled in
  5. terms of a stream of mouse-clicks, keypresses, and so on, then this
  6. design gains you nothing. It just duplicates the browser loop at a
  7. higher (i.e. slower) level. But if you're trying to make, oh I don't
  8. know, say a spreadsheet, where the UI can't easily be so expressed,
  9. there's a lot to be gained from a single interception point that takes
  10. the lower-level events and translates them into app-specific ones.
  11. That way you can move just once (and in just one portion of the code)
  12. from the lower level to the higher, instead of writing the entire UI
  13. in terms of both and suffering a thousand conflicts between them.
  14.  
  15. That might sound obvious, but it was a revelation to me when I saw
  16. that you could write a UI like this; it is a way out of the
  17. event-handler spider webs that I always used to get trapped in. I
  18. think the reason I didn't see it before is that that interception
  19. point has to be put right at the root of the connection between the
  20. browser and the app; that means digging deeper and writing code that
  21. one normally thinks of as outside the scope of an app UI. It's as if
  22. one is laying a deeper foundation in order to erect a larger building.
  23.  
  24. This design is particularly powerful when what's going on in one part
  25. of the UI needs to change something that's typically handled
  26. elsewhere--something that comes up frequently in complex UIs, which
  27. after all are highly stateful. The obvious way to do it is to just let
  28. the one thing call the other, but now you've got coupling which when
  29. compounded will lead to spaghetti. Another way is to generate fake
  30. low-level events (e.g. "pretend to click the mouse here") and let
  31. normal event handlers pick it up. But translating between levels like
  32. that is a real pain, and this approach never works very well anyway
  33. (i.e. such pretend clicks never seem to behave like a real one would
  34. have). In the above design, you just make an application event and add
  35. it to the queue and it's guaranteed to be handled the same way as any
  36. other. If you've worked out a good model for your UI then these events
  37. are basically messages in a language that the entire UI shares. That
  38. reduces the coupling enough to get you off the path to spaghetti.
  39.  
  40. From a performance point of view: if your code is doing things in
  41. event handlers that make your app sluggish, that's a really bad
  42. position to be in. For example, you can't (well, I couldn't) easily
  43. drop scroll events in a way that lightens the browser's load and makes
  44. it faster. But if your browser event handlers do nothing but trivially
  45. append to a queue, they basically can't bog down. And since you're
  46. running your own control loop on a timer, you now get to decide how
  47. much of anything to do and when. You can make these decisions in an
  48. app-specific way instead of relying on the browser which has no choice
  49. but to decide things the same way for everybody. This opens a new
  50. world of strategies for keeping UI latency low. It also gives you
  51. simple ways to simulate concurrency.
  52.  
  53. I know there are a great many ways to solve these kinds of problems,
  54. but this one pleases me considerably for two reasons: it saved us from
  55. some horrifying (potentially fatal) performance problems, and it
  56. transcends the usual engineering tradeoff between abstraction and
  57. speed—always a very interesting thing to run across.
Advertisement
Add Comment
Please, Sign In to add comment