reflex-dom icon indicating copy to clipboard operation
reflex-dom copied to clipboard

Location.manageHistory: Consider dispatching the event for changes to route

Open 3noch opened this issue 7 years ago • 2 comments

manageHistory currently maintains changes to the route internally but any other JS code listening for route events won't be notified when these updates happen. We can dispatch this event and rely on the browser to update the Dynamic for us. There are a few choices to consider, however, namely if we want the event to bubble, etc.

dispatchEvent' :: Dom.Window -> JSM ()
dispatchEvent' window = do
  obj@(JS.Object o) <- JS.create
  JS.objSetPropertyByName obj (t_ "cancelable") True
  JS.objSetPropertyByName obj (t_ "bubbles")    True
  JS.objSetPropertyByName obj (t_ "view")       window
  event <- newPopStateEvent (t_ "popstate") $ Just $ pFromJSVal o
  dispatchEvent_ window event

browserHistoryWith only provides half of the story that manageHistory offers. However, if the browser were handling the updates for us, we could write manageHistory in terms of browserHistoryWith.

3noch avatar Jan 17 '18 18:01 3noch

This is an interesting idea, but I'm not sure exactly when it should be done or what other ramifications it might have. Do JS libraries usually do this - i.e. is there an established best-practice?

Separately, shouldn't something like this be used to construct the event?

ryantrinkle avatar Jan 17 '18 19:01 ryantrinkle

That code is actually from reflex-dom-contrib. You're right that it can likely be modernized.

Good question about best practices. It could even be that we should add this to HistoryCommand so that the user can decide.

3noch avatar Jan 17 '18 19:01 3noch