Elmish.Dispatch
- Package
- purescript-elmish
- Repository
- collegevine/purescript-elmish
#DispatchMsgFn Source
newtype DispatchMsgFn msg
Represents a function that a view can use to report both errors and
messages originating from JS/DOM. Underneath it's just a function that
takes an Either
, but it is wrapped in a newtype in order to provide class
instances for it.
Constructors
DispatchMsgFn (Either DispatchError msg -> Effect Unit)
Instances
#DispatchMsg Source
type DispatchMsg = Effect Unit
#DispatchError Source
type DispatchError = String
#dispatchMsgFn Source
dispatchMsgFn :: forall msg. (DispatchError -> Effect Unit) -> (msg -> Effect Unit) -> DispatchMsgFn msg
Construct a DispatchMsgFn
out of "on error" and "on message" handlers
#issueError Source
issueError :: forall msg. DispatchMsgFn msg -> DispatchError -> Effect Unit
Report an error via the given dispatch function
#issueMsg Source
issueMsg :: forall msg. DispatchMsgFn msg -> msg -> Effect Unit
Issue a message via the given dispatch function
#ignoreMsg Source
ignoreMsg :: forall msg2 msg1. DispatchMsgFn msg1 -> DispatchMsgFn msg2
Creates a new DispatchMsgFn
that relays errors from the given
DispatchMsgFn
, but throws away messages
#cmapMaybe Source
cmapMaybe :: forall msg2 msg1. (msg2 -> Maybe msg1) -> DispatchMsgFn msg1 -> DispatchMsgFn msg2
Allows to optionally convert the message to another type, swallowing the message when conversion fails.
#handle Source
handle :: forall effFn fn msg. MkEventHandler msg fn effFn => MkJsCallback effFn => DispatchMsgFn msg -> fn -> JsCallback effFn
Creates a JsCallback
that uses the given DispatchMsgFn
to either issue
a message or report an error. The fn
parameter is either a message or a
function that produces a message. When the JS code calls the resulting
JsCallback
, its parameters are validated, then the fn
function is
called to produce a message, which is then reported via the given
DispatchMsgFn
, unless the parameters passed from JS cannot be decoded, in
which case an error is reported via DispatchMsgFn
.
Example of intended usage with elmish-html
:
-- PureScript
data Message = A | B Int
view state dispatch =
div {}
[ button { onClick: handle dispatch A } "Click A"
, button { onClick: handle dispatch $ B 42 } "Click B"
]
Example with FFIed component used as the view:
-- PureScript
data Message = A | B Int | C String Boolean
view state dispatch = createElement' ffiComponent_
{ foo: "bar"
, onA: handle dispatch A
, onB: handle dispatch B
, onC: handle dispatch C
, onBaz: handle dispatch \x y -> B (x+y)
}
// JSX:
export const ffiComponent_ = args =>
<div>
Foo is {args.bar}<br />
<button onClick={args.onA}>A</button>
<button onClick={() => args.onB(42)}>B</button>
<button onClick={() => args.onC("hello", true)}>C</button>
<button onClick={() => args.onBaz(21, 21)}>Baz</button>
</div>
#handleMaybe Source
handleMaybe :: forall effFn fn msg. MkEventHandler (Maybe msg) fn effFn => MkJsCallback effFn => DispatchMsgFn msg -> fn -> JsCallback effFn
A version of handle
(see comments there) with a possibility of not
producing a message.
#MkEventHandler Source
class MkEventHandler msg fn effFn | fn -> effFn, effFn -> fn where
This type class and its instances are implementation of handle
and
handleMaybe
. The base case is when fn ~ msg
- that is when the
message-producing function is not a function at all, but the message
itself. The recursive instance prepends an argument a
, thus allowing for
curried functions with arbitrary number of parameters.
Members
mkEventHandler :: fn -> (msg -> Effect Unit) -> effFn
Instances
(MkEventHandler msg fn0 effFn0) => MkEventHandler msg (a -> fn0) (a -> effFn0)
MkEventHandler msg msg (Effect Unit)