{-|
Module: IHP.ServerSideComponent.Types
Description: Types & Data Structures for IHP SSC
Copyright: (c) digitally induced GmbH, 2021
-}
module IHP.ServerSideComponent.Types where

import IHP.ViewPrelude
import qualified Network.WebSockets as WebSocket

class Component state action | state -> action where
    initialState :: state
    render :: state -> Html
    action ::
        ( ?instanceRef :: IORef (ComponentInstance state)
        , ?connection :: WebSocket.Connection
        , ?context :: ControllerContext
        , ?modelContext :: ModelContext
        ) => state -> action -> IO state

    componentDidMount ::
        ( ?instanceRef :: IORef (ComponentInstance state)
        , ?connection :: WebSocket.Connection
        , ?context :: ControllerContext
        , ?modelContext :: ModelContext
        ) => state -> IO state
    componentDidMount state
state = state -> IO state
forall (f :: * -> *) a. Applicative f => a -> f a
pure state
state

data ComponentsController components
    = ComponentsController
    deriving (ComponentsController components
-> ComponentsController components -> Bool
(ComponentsController components
 -> ComponentsController components -> Bool)
-> (ComponentsController components
    -> ComponentsController components -> Bool)
-> Eq (ComponentsController components)
forall components.
ComponentsController components
-> ComponentsController components -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ComponentsController components
-> ComponentsController components -> Bool
$c/= :: forall components.
ComponentsController components
-> ComponentsController components -> Bool
== :: ComponentsController components
-> ComponentsController components -> Bool
$c== :: forall components.
ComponentsController components
-> ComponentsController components -> Bool
Eq, Int -> ComponentsController components -> ShowS
[ComponentsController components] -> ShowS
ComponentsController components -> String
(Int -> ComponentsController components -> ShowS)
-> (ComponentsController components -> String)
-> ([ComponentsController components] -> ShowS)
-> Show (ComponentsController components)
forall components. Int -> ComponentsController components -> ShowS
forall components. [ComponentsController components] -> ShowS
forall components. ComponentsController components -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ComponentsController components] -> ShowS
$cshowList :: forall components. [ComponentsController components] -> ShowS
show :: ComponentsController components -> String
$cshow :: forall components. ComponentsController components -> String
showsPrec :: Int -> ComponentsController components -> ShowS
$cshowsPrec :: forall components. Int -> ComponentsController components -> ShowS
Show, Typeable (ComponentsController components)
Constr
DataType
Typeable (ComponentsController components)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> ComponentsController components
    -> c (ComponentsController components))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r)
    -> Constr
    -> c (ComponentsController components))
-> (ComponentsController components -> Constr)
-> (ComponentsController components -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d))
    -> Maybe (c (ComponentsController components)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (ComponentsController components)))
-> ((forall b. Data b => b -> b)
    -> ComponentsController components
    -> ComponentsController components)
-> (forall r r'.
    (r -> r' -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> ComponentsController components
    -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r
    -> (forall d. Data d => d -> r')
    -> ComponentsController components
    -> r)
-> (forall u.
    (forall d. Data d => d -> u)
    -> ComponentsController components -> [u])
-> (forall u.
    Int
    -> (forall d. Data d => d -> u)
    -> ComponentsController components
    -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> ComponentsController components
    -> m (ComponentsController components))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> ComponentsController components
    -> m (ComponentsController components))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> ComponentsController components
    -> m (ComponentsController components))
-> Data (ComponentsController components)
ComponentsController components -> Constr
ComponentsController components -> DataType
(forall d. Data d => c (t d))
-> Maybe (c (ComponentsController components))
(forall b. Data b => b -> b)
-> ComponentsController components
-> ComponentsController components
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> ComponentsController components
-> c (ComponentsController components)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c (ComponentsController components)
forall components.
Data components =>
Typeable (ComponentsController components)
forall components.
Data components =>
ComponentsController components -> Constr
forall components.
Data components =>
ComponentsController components -> DataType
forall components.
Data components =>
(forall b. Data b => b -> b)
-> ComponentsController components
-> ComponentsController components
forall components u.
Data components =>
Int
-> (forall d. Data d => d -> u)
-> ComponentsController components
-> u
forall components u.
Data components =>
(forall d. Data d => d -> u)
-> ComponentsController components -> [u]
forall components r r'.
Data components =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> ComponentsController components
-> r
forall components r r'.
Data components =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> ComponentsController components
-> r
forall components (m :: * -> *).
(Data components, Monad m) =>
(forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
forall components (m :: * -> *).
(Data components, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
forall components (c :: * -> *).
Data components =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c (ComponentsController components)
forall components (c :: * -> *).
Data components =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> ComponentsController components
-> c (ComponentsController components)
forall components (t :: * -> *) (c :: * -> *).
(Data components, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (ComponentsController components))
forall components (t :: * -> * -> *) (c :: * -> *).
(Data components, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (ComponentsController components))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int
-> (forall d. Data d => d -> u)
-> ComponentsController components
-> u
forall u.
(forall d. Data d => d -> u)
-> ComponentsController components -> [u]
forall r r'.
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> ComponentsController components
-> r
forall r r'.
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> ComponentsController components
-> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c (ComponentsController components)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> ComponentsController components
-> c (ComponentsController components)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d))
-> Maybe (c (ComponentsController components))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (ComponentsController components))
$cComponentsController :: Constr
$tComponentsController :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
$cgmapMo :: forall components (m :: * -> *).
(Data components, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
gmapMp :: (forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
$cgmapMp :: forall components (m :: * -> *).
(Data components, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
gmapM :: (forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
$cgmapM :: forall components (m :: * -> *).
(Data components, Monad m) =>
(forall d. Data d => d -> m d)
-> ComponentsController components
-> m (ComponentsController components)
gmapQi :: Int
-> (forall d. Data d => d -> u)
-> ComponentsController components
-> u
$cgmapQi :: forall components u.
Data components =>
Int
-> (forall d. Data d => d -> u)
-> ComponentsController components
-> u
gmapQ :: (forall d. Data d => d -> u)
-> ComponentsController components -> [u]
$cgmapQ :: forall components u.
Data components =>
(forall d. Data d => d -> u)
-> ComponentsController components -> [u]
gmapQr :: (r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> ComponentsController components
-> r
$cgmapQr :: forall components r r'.
Data components =>
(r' -> r -> r)
-> r
-> (forall d. Data d => d -> r')
-> ComponentsController components
-> r
gmapQl :: (r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> ComponentsController components
-> r
$cgmapQl :: forall components r r'.
Data components =>
(r -> r' -> r)
-> r
-> (forall d. Data d => d -> r')
-> ComponentsController components
-> r
gmapT :: (forall b. Data b => b -> b)
-> ComponentsController components
-> ComponentsController components
$cgmapT :: forall components.
Data components =>
(forall b. Data b => b -> b)
-> ComponentsController components
-> ComponentsController components
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (ComponentsController components))
$cdataCast2 :: forall components (t :: * -> * -> *) (c :: * -> *).
(Data components, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (ComponentsController components))
dataCast1 :: (forall d. Data d => c (t d))
-> Maybe (c (ComponentsController components))
$cdataCast1 :: forall components (t :: * -> *) (c :: * -> *).
(Data components, Typeable t) =>
(forall d. Data d => c (t d))
-> Maybe (c (ComponentsController components))
dataTypeOf :: ComponentsController components -> DataType
$cdataTypeOf :: forall components.
Data components =>
ComponentsController components -> DataType
toConstr :: ComponentsController components -> Constr
$ctoConstr :: forall components.
Data components =>
ComponentsController components -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c (ComponentsController components)
$cgunfold :: forall components (c :: * -> *).
Data components =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r)
-> Constr
-> c (ComponentsController components)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> ComponentsController components
-> c (ComponentsController components)
$cgfoldl :: forall components (c :: * -> *).
Data components =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> ComponentsController components
-> c (ComponentsController components)
$cp1Data :: forall components.
Data components =>
Typeable (ComponentsController components)
Data)

data ComponentInstance state
    = ComponentInstance { ComponentInstance state -> state
state :: state } -- ^ If you wondered why the current rendered HTML doesn't need to be stored here for later diffing it: As our render functions are pure we can just re-render the HTML based on the state when we want to do our diffing

instance (SetField "state" (ComponentInstance state) state) where
    setField :: state -> ComponentInstance state -> ComponentInstance state
setField state
state ComponentInstance state
componentInstance = ComponentInstance state
componentInstance { state
state :: state
$sel:state:ComponentInstance :: state
state }