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

Add purs-tidy formatting #14

Merged
merged 2 commits into from
Oct 23, 2024
Merged
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
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "source",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "last",
"unicode": "never",
"width": null
}
4 changes: 2 additions & 2 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
services:
dev:
image: ghcr.io/flipstone/purescript-tools:debian-stable-purescript-0.15.15-2024-09-05-15466b3
image: ghcr.io/flipstone/purescript-tools:debian-stable-purescript-0.15.15-2024-10-21-d530b53
environment:
IN_CONTAINER: 'true'
stdin_open: true
tty: true
volumes:
- .:/purescript-erumu
command: spago build
command: ./scripts/build
working_dir: /purescript-erumu

6 changes: 3 additions & 3 deletions scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set -e

source "${BASH_SOURCE%/*}/lib/run-in-container.sh"

echo "====="
echo "===== $0 $* ====="
echo "====="
echo "Formatting with purs-tidy"
./scripts/format-repo

echo "Building"
spago build
15 changes: 15 additions & 0 deletions scripts/format-repo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -e

source "${BASH_SOURCE%/*}/../scripts/lib/run-in-container.sh"

cd $(git rev-parse --show-toplevel)

if [ "$CI" ]; then
MODE=check
else
MODE=format-in-place
fi

purs-tidy $MODE --config-require "./src/**/*.purs"
163 changes: 80 additions & 83 deletions spago.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,86 @@ workspace:
packages:
erumu:
path: ./
core:
dependencies:
- arrays
- bifunctors
- console
- effect
- either
- enums
- foldable-traversable
- foreign
- foreign-object
- identity
- lists
- maybe
- newtype
- parallel
- partial
- prelude
- refs
- strings
- unsafe-coerce
- web-dom
- web-events
- web-file
- web-html
build_plan:
- arrays
- bifunctors
- console
- const
- contravariant
- control
- datetime
- distributive
- effect
- either
- enums
- exceptions
- exists
- foldable-traversable
- foreign
- foreign-object
- functions
- functors
- gen
- identity
- integers
- invariant
- js-date
- lazy
- lists
- maybe
- media-types
- newtype
- nonempty
- now
- nullable
- numbers
- ordered-collections
- orders
- parallel
- partial
- prelude
- profunctor
- refs
- safe-coerce
- st
- strings
- tailrec
- transformers
- tuples
- type-equality
- typelevel-prelude
- unfoldable
- unsafe-coerce
- web-dom
- web-events
- web-file
- web-html
- web-storage
test:
dependencies: []
build_plan: []
dependencies:
- arrays
- bifunctors
- console
- effect
- either
- enums
- foldable-traversable
- foreign
- foreign-object
- identity
- lists
- maybe
- newtype
- parallel
- partial
- prelude
- refs
- strings
- unsafe-coerce
- web-dom
- web-events
- web-file
- web-html
test_dependencies: []
build_plan:
- arrays
- bifunctors
- console
- const
- contravariant
- control
- datetime
- distributive
- effect
- either
- enums
- exceptions
- exists
- foldable-traversable
- foreign
- foreign-object
- functions
- functors
- gen
- identity
- integers
- invariant
- js-date
- lazy
- lists
- maybe
- media-types
- newtype
- nonempty
- now
- nullable
- numbers
- ordered-collections
- orders
- parallel
- partial
- prelude
- profunctor
- refs
- safe-coerce
- st
- strings
- tailrec
- transformers
- tuples
- type-equality
- typelevel-prelude
- unfoldable
- unsafe-coerce
- web-dom
- web-events
- web-file
- web-html
- web-storage
package_set:
address:
registry: 58.0.1
Expand Down
119 changes: 65 additions & 54 deletions src/DOM/Erumu/App.purs
Original file line number Diff line number Diff line change
Expand Up @@ -46,100 +46,111 @@ type RenderFn model msg = model -> HTML msg
-- * Dispatching a message is always "safe"
-- - e.g. you can save a dispatch function for later call it at any time.
--
newApp :: forall m model msg.
(model -> HTML msg)
-> UpdateFn m model msg
-> model
-> (m Unit -> Effect Unit)
-> (Effect Unit -> m Unit)
-> Effect (App m model msg)
newApp ::
forall m model msg.
(model -> HTML msg) ->
UpdateFn m model msg ->
model ->
(m Unit -> Effect Unit) ->
(Effect Unit -> m Unit) ->
Effect (App m model msg)
newApp render update init execM liftM = do
virtualApp <- Virtual.newApp (node "div" [] [])
stRef <- Ref.new { model: init
, messages: Nil
, processing: false
}
stRef <- Ref.new
{ model: init
, messages: Nil
, processing: false
}

let app = App { update, render, stRef, virtualApp, execM, liftM }

renderApp app
pure app

mountApp :: forall m model msg.
String
-> App m model msg
-> Effect (Maybe String)
mountApp ::
forall m model msg.
String ->
App m model msg ->
Effect (Maybe String)
mountApp containerId (App { virtualApp }) =
Virtual.mountApp containerId virtualApp


dispatch :: forall m model msg.
App m model msg
-> msg
-> Effect Unit
dispatch ::
forall m model msg.
App m model msg ->
msg ->
Effect Unit
dispatch app@(App { stRef }) msg = do
pushMessage stRef msg
processMessages app

renderApp :: forall m model msg.
App m model msg
-> Effect Unit
renderApp ::
forall m model msg.
App m model msg ->
Effect Unit
renderApp app@(App { virtualApp, render, stRef }) = do
st <- Ref.read stRef
Virtual.rerenderApp (toVTree (render st.model) (dispatch app))
virtualApp
virtualApp

tryTakeMessages :: forall model msg.
Ref (DispatchState model msg)
-> Effect (Maybe (List msg))
tryTakeMessages ::
forall model msg.
Ref (DispatchState model msg) ->
Effect (Maybe (List msg))
tryTakeMessages stRef =
(flip Ref.modify') stRef $
\st ->
if st.processing
then { state: st, value: Nothing }
else { state: st { messages = Nil }
, value: Just (reverse st.messages)
}

setProcessing :: forall model msg.
Ref (DispatchState model msg)
-> Boolean
-> Effect Unit
if st.processing then { state: st, value: Nothing }
else
{ state: st { messages = Nil }
, value: Just (reverse st.messages)
}

setProcessing ::
forall model msg.
Ref (DispatchState model msg) ->
Boolean ->
Effect Unit
setProcessing stRef processing = void $
Ref.modify (_ { processing = processing }) stRef

pushMessage :: forall model msg.
Ref (DispatchState model msg)
-> msg
-> Effect Unit
pushMessage ::
forall model msg.
Ref (DispatchState model msg) ->
msg ->
Effect Unit
pushMessage stRef msg = void $
Ref.modify (\st -> st { messages = Cons msg st.messages }) stRef

processMessages :: forall m msg model.
App m model msg
-> Effect Unit
processMessages ::
forall m msg model.
App m model msg ->
Effect Unit
processMessages app@(App { stRef }) = void $ do
taken <- tryTakeMessages stRef

case taken of
Nothing -> pure unit -- messages are already being processed
Just Nil -> pure unit -- there are no messages to process
Nothing -> pure unit -- messages are already being processed
Just Nil -> pure unit -- there are no messages to process
Just msgs -> do
setProcessing stRef true
traverse_ (processMessage app) msgs
setProcessing stRef false
processMessages app

processMessage :: forall m msg model.
App m model msg
-> msg
-> Effect Unit
processMessage ::
forall m msg model.
App m model msg ->
msg ->
Effect Unit
processMessage app@(App { update, stRef, execM, liftM }) msg = void $ do
command <- (flip Ref.modify') stRef $ \st ->
let r = update msg st.model
in { state: st { model = r.model }
, value: r.command
}
let
r = update msg st.model
in
{ state: st { model = r.model }
, value: r.command
}

renderApp app
runcmd command (liftM <<< dispatch app) execM
Expand Down
Loading