Sidebar.purs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. module PureTabs.Sidebar where
  2. import Browser.Runtime as Runtime
  3. import Browser.Windows (getCurrent)
  4. import Control.Alt (void)
  5. import Control.Alternative (pure)
  6. import Control.Coroutine as CR
  7. import Control.Coroutine.Aff (emit)
  8. import Control.Coroutine.Aff as CRA
  9. import Control.Monad.Error.Class (throwError)
  10. import Data.Function (($))
  11. import Data.Maybe (Maybe(..))
  12. import Data.Unit (Unit, unit)
  13. import Effect (Effect)
  14. import Effect.Aff (Aff, error)
  15. import Effect.Class (liftEffect)
  16. import Halogen as H
  17. import Halogen.Aff as HA
  18. import Halogen.VDom.Driver (runUI)
  19. import Prelude (bind, discard)
  20. import PureTabs.Model.Events (BackgroundEvent(..), SidebarEvent(..))
  21. import PureTabs.Sidebar.Bar as Bar
  22. import PureTabs.Sidebar.Tabs as Tabs
  23. import Web.DOM.ParentNode (QuerySelector(..))
  24. main :: Effect Unit
  25. main = do
  26. port <- Runtime.connect
  27. HA.runHalogenAff do
  28. currentWindow <- getCurrent
  29. liftEffect $ Runtime.postMessageJson port $ SbHasWindowId currentWindow.id
  30. content' <- HA.selectElement (QuerySelector "#content")
  31. io <- case content' of
  32. Nothing -> throwError (error "Could not find #content")
  33. Just content -> runUI Bar.component unit content
  34. io.subscribe $ onSidebarMsg port
  35. CR.runProcess ((onBackgroundMsgProducer port) CR.$$ onBackgroundMsgConsumer io.query)
  36. onBackgroundMsgProducer :: Runtime.Port -> CR.Producer BackgroundEvent Aff Unit
  37. onBackgroundMsgProducer port =
  38. CRA.produce \emitter ->
  39. liftEffect $ void $ Runtime.onMessageJsonAddListener port (emit emitter)
  40. onBackgroundMsgConsumer :: (forall a. Tabs.Query a -> Aff (Maybe a)) -> CR.Consumer BackgroundEvent Aff Unit
  41. onBackgroundMsgConsumer query =
  42. CR.consumer
  43. $ case _ of
  44. BgInitialTabList tabs -> do
  45. void $ query $ H.tell $ Tabs.InitialTabList tabs
  46. pure Nothing
  47. BgTabCreated tab -> do
  48. void $ query $ H.tell $ Tabs.TabCreated tab
  49. pure Nothing
  50. BgTabDeleted tabId -> do
  51. void $ query $ H.tell $ Tabs.TabDeleted tabId
  52. pure Nothing
  53. BgTabActivated prev next -> do
  54. void $ query $ H.tell $ Tabs.TabActivated prev next
  55. pure Nothing
  56. BgTabMoved tabId prev next -> do
  57. void $ query $ H.tell $ Tabs.TabMoved tabId prev next
  58. pure Nothing
  59. BgTabUpdated tabId cinfo tab -> do
  60. void $ query $ H.tell $ Tabs.TabInfoChanged tabId cinfo
  61. pure Nothing
  62. BgTabDetached tabId -> do
  63. void $ query $ H.tell $ Tabs.TabDetached tabId
  64. pure Nothing
  65. _ -> pure Nothing
  66. onSidebarMsg :: Runtime.Port -> CR.Consumer SidebarEvent Aff Unit
  67. onSidebarMsg port =
  68. CR.consumer \(msg) -> do
  69. liftEffect $ Runtime.postMessageJson port msg
  70. pure Nothing