Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major rewrite to Docs (do not merge this, only for review) #14

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions code-snippets/StaticBuilder.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE CPP #-}

import Reflex.Dom
import Data.Text as T
import Data.Text.Encoding as T
import Data.ByteString.Char8 as BS8

main = do
#if defined (ghcjs_HOST_OS)
-- Use the widget normally as the main entry point
mainWidget topWidget
return ()
#else
(_,bs) <- renderStatic $ topWidget
-- Use the generated bytestring as the body of page
-- And include the all.js in the end
BS8.putStrLn bs
#endif

-- Widget supporting Static Rendering
topWidget :: ((MonadHold t m,
PostBuild t m,
Prerender js m,
DomBuilder t m,
TriggerEvent t m,
PerformEvent t m))
=> m ()
topWidget = do
el "h1" $ text "Some heading"

-- Use constDyn for widget which need Dynamic values
elDynAttr "div" (constDyn ("id" =: "mydiv")) $ text "hello"

-- The Events will only fire in the Immediate DomBuilder
ev <- button "Click to test"

divClass "static-text" $ widgetHold (text "Initial text")
(text "Changed text" <$ ev)

-- The MonadHold widgets have to be put inside prerender
c <- prerender (return $ constDyn 0) (Reflex.Dom.count ev)
display c

message <- getWebSocketMessage ("some message sent to websocket" <$ ev)

divClass "message" $ widgetHold (text "Waiting For message")
(text <$> message)

return ()

getWebSocketMessage ::
(_)
=> Event t Text
-> m (Event t Text)
getWebSocketMessage messageEv = prerender (return never) $ do
let sendEv = (\m -> [T.encodeUtf8 m]) <$> messageEv
ws <- webSocket "ws://echo.websocket.org" $ WebSocketConfig sendEv never False
return (T.decodeUtf8 <$> _webSocket_recv ws)
24 changes: 12 additions & 12 deletions code-snippets/nested_dynamics.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
-- A slightly contrived example just to demonstrate use of nested dynamics
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has been added to reflex-examples repo reflex-frp/reflex-examples#17
So it will not be a part of docs here.

-- This example also has a nested state machine,
-- By using foldDynM, we could use foldDyn inside of it.
--
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}

import Reflex.Dom
import Data.Text as T
import Data.Monoid

-- A slightly contrived example just to demonstrate use of nested dynamics
-- This example also has a nested state machine,
-- By using foldDynM, we could use foldDyn inside of it.
--
-- A ScoreCard can either display some info/updates or the current score
data ScoreCard t =
Info (Dynamic t Text)
Expand All @@ -33,11 +33,11 @@ main = mainWidget $ do
-- gameEv :: (Reflex t) => Event t GameEvent
gameEv = leftmost [newGame, move1, move2, move3, move4]

newGame = const NewGame <$> newGameEv
move1 = const (DoTurn "Move 1" 1) <$> m1
move2 = const (DoTurn "Move 2" 20) <$> m2
move3 = const (DoTurn "Move 3" 10) <$> m3
move4 = const (DoTurn "Move 4" 5) <$> m4
newGame = NewGame <$ newGameEv
move1 = (DoTurn "Move 1" 1) <$ m1
move2 = (DoTurn "Move 2" 20) <$ m2
move3 = (DoTurn "Move 3" 10) <$ m3
move4 = (DoTurn "Move 4" 5) <$ m4

-- Capture the score, in a Dynamic independent of ScoreCard
-- This will maintain its value irrespective of the state of ScoreCard
Expand All @@ -47,9 +47,9 @@ main = mainWidget $ do

foldDyn handleGameEvent 0 gameEv

let
let
initCard = Score scoreDyn

eventHandler _ (Info _) = return (Score scoreDyn)
eventHandler _ (Score _) = do
let handleGameEvent (NewGame) _ = "New Game!"
Expand All @@ -62,7 +62,7 @@ main = mainWidget $ do
-- So this will be reset whenever you toggle the display of score card
textDyn <- foldDyn handleGameEvent "" gameEv
return (Info textDyn)

-- external state machine using foldDynM
-- Here the (ScoreCard t) itself contains a Dynamic value
-- scoreCardDyn :: Dynamic t (ScoreCard t)
Expand Down
82 changes: 0 additions & 82 deletions doc/advanced_topics.rst

This file was deleted.

Loading