arsalan0c / cdp-hs

Haskell library for the Chrome DevTools Protocol
https://hackage.haskell.org/package/cdp
MIT License
10 stars 4 forks source link

Improve events interface - deconstruction of value #10

Closed arsalan0c closed 2 years ago

arsalan0c commented 2 years ago

The set of all event results are represented as a tagged union (EventReturn). For a particular event received, deconstruction is required in the user's code, in order to obtain the result.

For example:

f (CDP.EventReturnPageWindowOpen v) = v

An event handler is defined as: type Handler = EventReturn -> IO () The deconstruction function can be used as part of the handler: doSomethingWithValue . f

Ideally, f would not be required of a user.

arsalan0c commented 2 years ago

Possible solutions (as suggested by Jasper):

  1. Typeclasses:
    
    data PageWindowOpen = ...

class Event a where eventName :: Proxy a -> String

instance Event PageWindowOpen where eventName _ = "pageWindowOpen"

subscribe :: Event a => ? -> (a -> IO b) -> IO ..


2. GADTs
```hs
data EventName a where
  PageWindowOpenName :: EventName PageWindowOpen
  ...

data PageWindowOpen = ...

subscribe :: ? -> EventName a -> (a -> IO b) -> IO ...
arsalan0c commented 2 years ago

Standealone example of a solution: https://gist.github.com/jaspervdj/d1a1ee08e7fab819e2631180185ad62d

arsalan0c commented 2 years ago

Implemented in this commit with the following: