Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
purescript 0.12 - hello io
Browse files Browse the repository at this point in the history
  • Loading branch information
grmble committed Jun 23, 2018
1 parent 3a828aa commit 9eb8bf9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ node_js: lts/*
install:
- npm install -g bower purescript pulp
script:
- npm install
- bower install
- pulp test
10 changes: 5 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"output"
],
"dependencies": {
"purescript-prelude": "^3.1.1",
"purescript-console": "^3.0.0",
"purescript-aff": "^4.0.2",
"purescript-foreign": "^4.0.1"
"purescript-prelude": "^4.0.1",
"purescript-console": "^4.1.0",
"purescript-aff": "^5.0.0",
"purescript-foreign": "^5.0.0"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"
"purescript-psci-support": "^4.0.0"
}
}
36 changes: 16 additions & 20 deletions src/Bonsai/DOM.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-- |
-- | This DOM package uses F for the DOM access.
-- | This is much nicer to work with than some
-- | `Eff eff (Maybe Element)`. You have to run
-- | `Effect eff (Maybe Element)`. You have to run
-- | the underlying except though, convenience
-- | helpers `effF` and `affF` are provided.
-- |
Expand All @@ -19,8 +19,7 @@
-- | take a Window or Document argument instead
-- | of using a global.
module Bonsai.DOM
( DOM
, Document(..)
( Document(..)
, Element(..)
, ElementId(..)
, Window(..)
Expand Down Expand Up @@ -54,19 +53,17 @@ where

import Prelude

import Control.Monad.Aff (Aff, liftEff')
import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Eff.Exception (EXCEPTION, throw)
import Control.Monad.Except (runExcept)
import Data.Either (Either(..))
import Data.Foldable (class Foldable, intercalate)
import Data.Foreign (F, Foreign, ForeignError(..), fail, isNull, isUndefined, readString, renderForeignError)
import Data.Foreign.Index ((!))
import Data.Function.Uncurried (Fn1, Fn2, Fn4, runFn1, runFn2, runFn4)
import Data.Newtype (class Newtype, unwrap, wrap)

-- | Effect for conversion to Eff/Aff
foreign import data DOM :: Effect
import Effect (Effect)
import Effect.Class (liftEffect)
import Effect.Aff (Aff)
import Effect.Exception (throw)
import Foreign (F, Foreign, ForeignError(..), fail, isNull, isUndefined, readString, renderForeignError)
import Foreign.Index ((!))


-- | The type for the global javascript document
Expand Down Expand Up @@ -103,19 +100,19 @@ derive instance newtypeRequestAnimationFrameId :: Newtype RequestAnimationFrameI

-- primitive methods from the native module
-- note that simple property access is done in F - no native code needed
foreign import primitives :: forall eff.
foreign import primitives ::
{ window :: Foreign
, elementById :: Fn2 ElementId Document Foreign
, appendChild :: Fn2 Element Element Unit
, addEventListener :: Fn4 ListenerOptions String (Foreign -> Eff (dom::DOM|eff) Unit) Element Unit
, addEventListener :: Fn4 ListenerOptions String (Foreign -> Effect Unit) Element Unit
, clearElement :: Fn1 Element Unit
, copyFakeArray :: Fn1 Foreign (Array Foreign)
, focusElement :: Fn1 Element Unit
, selectElement :: Fn1 Element Unit
, setLocationHash :: Fn2 String Foreign Unit
, querySelector :: Fn2 String Foreign Foreign
, querySelectorAll :: Fn2 String Foreign Foreign
, requestAnimationFrame :: Fn2 (Eff eff Unit) Window RequestAnimationFrameId
, requestAnimationFrame :: Fn2 (Effect Unit) Window RequestAnimationFrameId
}

-- | Event listener options.
Expand Down Expand Up @@ -151,7 +148,7 @@ foreignErrorMsg err =
-- | Run the F in Eff
-- |
-- | Errors will be thrown as exception
effF :: forall eff a. F a -> Eff (dom::DOM,exception::EXCEPTION|eff) a
effF :: forall a. F a -> Effect a
effF fa =
case runExcept fa of
Left err ->
Expand All @@ -161,9 +158,9 @@ effF fa =


-- | Run the F in Aff
affF :: forall eff a. F a -> Aff (dom::DOM|eff) a
affF :: forall a. F a -> Aff a
affF fa =
liftEff' $ effF fa
liftEffect $ effF fa

-- | Get the global javascript Window object
window :: F Window
Expand Down Expand Up @@ -203,8 +200,7 @@ elementById id doc =
-- | By combining this with issueCommand, you can have external
-- | elements issue commands to your bonsai program.
addEventListener
:: forall eff
. ListenerOptions -> String -> (Foreign -> Eff (dom::DOM|eff) Unit) -> Element -> F Unit
:: ListenerOptions -> String -> (Foreign -> Effect Unit) -> Element -> F Unit
addEventListener opts event fn elem = do
let _ = runFn4 primitives.addEventListener opts event fn elem
pure unit
Expand Down Expand Up @@ -328,6 +324,6 @@ ownerDocument (Element elem) =


-- | Request animation frame.
requestAnimationFrame :: forall eff. Eff eff Unit -> Window -> F RequestAnimationFrameId
requestAnimationFrame :: Effect Unit -> Window -> F RequestAnimationFrameId
requestAnimationFrame eff =
pure <<< runFn2 primitives.requestAnimationFrame eff
7 changes: 4 additions & 3 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Test.Main where

import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

main :: forall e. Eff (console :: CONSOLE | e) Unit
import Effect (Effect)
import Effect.Console (log)

main :: Effect Unit
main = do
log "Tested in purescript-bonsai-jsdom."

0 comments on commit 9eb8bf9

Please sign in to comment.