-----------------------------------------------------------------------------
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE LambdaCase                 #-}
{-# LANGUAGE CPP                        #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Miso.EventSource
-- Copyright   :  (C) 2016-2025 David M. Johnson
-- License     :  BSD3-style (see the file LICENSE)
-- Maintainer  :  David M. Johnson <code@dmj.io>
-- Stability   :  experimental
-- Portability :  non-portable
----------------------------------------------------------------------------
module Miso.EventSource
  ( -- *** EventSource
    connectText
  , connectJSON
  , close
  , socketState
  -- *** Defaults
  , emptyEventSource
  -- *** Types
  , EventSource (..)
  , URL
  -- *** Re-exports
  , Payload (..)
  ) where
-----------------------------------------------------------------------------
import           Data.Aeson
-----------------------------------------------------------------------------
import           Miso.Effect
import           Miso.Runtime
import           Miso.String
-----------------------------------------------------------------------------
-- | <https://developer.mozilla.org/en-US/docs/Web/API/EventSource>
connectText
  :: URL
  -> (EventSource -> action)
  -- ^ onOpen
  -> (MisoString -> action)
  -- ^ onMessage
  -> (MisoString -> action)
  -- ^ onError
  -> Effect parent model action
connectText :: forall action parent model.
URL
-> (EventSource -> action)
-> (URL -> action)
-> (URL -> action)
-> Effect parent model action
connectText = URL
-> (EventSource -> action)
-> (URL -> action)
-> (URL -> action)
-> Effect parent model action
forall action parent model.
URL
-> (EventSource -> action)
-> (URL -> action)
-> (URL -> action)
-> Effect parent model action
eventSourceConnectText
-----------------------------------------------------------------------------
-- | <https://developer.mozilla.org/en-US/docs/Web/API/EventSource>
connectJSON
  :: FromJSON value
  => URL
  -> (EventSource -> action)
  -- ^ onOpen
  -> (value -> action)
  -- ^ onMessage
  -> (MisoString -> action)
  -- ^ onError
  -> Effect parent model action
connectJSON :: forall value action parent model.
FromJSON value =>
URL
-> (EventSource -> action)
-> (value -> action)
-> (URL -> action)
-> Effect parent model action
connectJSON = URL
-> (EventSource -> action)
-> (value -> action)
-> (URL -> action)
-> Effect parent model action
forall value action parent model.
FromJSON value =>
URL
-> (EventSource -> action)
-> (value -> action)
-> (URL -> action)
-> Effect parent model action
eventSourceConnectJSON
-----------------------------------------------------------------------------
-- | <https://developer.mozilla.org/en-US/docs/Web/API/EventSource/close>
close
  :: EventSource
  -> Effect parent model action
close :: forall parent model action.
EventSource -> Effect parent model action
close = EventSource -> Effect parent model action
forall parent model action.
EventSource -> Effect parent model action
eventSourceClose
-----------------------------------------------------------------------------