Comments (9)
Perhaps this is what Cmd.ofEffect
is for
from elmish.
In my project I often add a similar helpers and more.
The code is the same as yours just using the same structure as Elmish code base
I call it Cmd.OfFunc.exec
:
module Elmish
open Elmish
module Cmd =
module OfFunc =
let exec
(task: 'a -> _)
(arg: 'a) : Cmd<'msg> =
let bind _dispatch =
try
task arg
with x ->
()
[ bind ]
// Other variants for Async, Promise, etc.
@et1975 We can probably add such variant to Elmish core what do you think?
from elmish.
if you don't care about either outcome, why bother involving elmish at all? Just invoke whatever you have yourself, no?
from elmish.
That wouldn't be executed after the model has been updated, and therefore the view etc.
from elmish.
To be clear - I'm working on Sutil's implementation of Elmish, and I don't want to introduce any new ideas into Sutil's Elmish that don't map back to ideas and concepts defined in this (master) repo. I'm saying this so you can judge the priority of the proposal accordingly.
from elmish.
I think I do it this way to have a similar looking code everywhere.
Like that all the "side effects" are triggered in the Cmd.XXX.YYY
portion of the code.
It also happens after the model has been updated.
from elmish.
Those use-cases seem perfectly valid to me and you absolutely should have them supported in your prelude (library or application). But I think from conceptual/elmish perspective the commands are a way to feed results back into update loop. Which is why I don't think we should enshrine commands as something like delayed execution mechanism.
@kspeakman what's your take on this?
from elmish.
@et1975 TLDR; I agree with you to leave it out as not encouraged usage. It can be added in user space as needed.
I agree with @MangelMaxime that these side effects (ones triggered from update
) belong as Cmds. In these rare cases, I typically dispatch a no-op return Msg. Logging for example. | Logged -> model, Cmd.none
. That way I can see in the Msg log that it happened and the timing relative to other Msgs.
It is worth noting: Elm's internal API does not permit the usage in question.. A Task must return a single Msg.
Please feel free to poke holes in my reasoning, provide counter examples where these strategies are untenable, etc. If I'm missing something I would love to learn about it.
from elmish.
I can't help thinking that when one allows perform, attempt and Cmd.ofEffect, you have already prompted for the inclusion of Cmd.OfFunc.exec - they all ignore certain outcomes (mostly exceptions).
You make some good points. For context. The Cmd API permits not returning a msg. Which may make it appear that this is a good option. However, the choice of using dispatch is more of a pragmatic one. It's much easier to integrate with multiple UI platforms. There is another issue discussing ways to remove dispatch from the Elmish API without breaking the world. No solutions as yet.
I believe the fns you mentioned were inspired by Elm's Task API. The difference being that F# doesn't tightly restrict side effects as Elm does. Anything can throw. A safe default is for failing side effects to not crash the app. So all these fns wrap the provided fn in a try catch just in case.
To your point, this does change the semantic meaning of perform
for example. In Elm it indicates "this can't fail" (which Elm enforces), but in F#/Elmish it effectively means "ignore failures". Which happens to make it suitable only for code that can't fail.
from elmish.
Related Issues (20)
- Ability to stop subscriptions HOT 9
- Cmd helpers for checking success by option HOT 4
- Double subscribe when dispatching during sub startup
- None of the duplicate subscriptions should be started HOT 12
- Subscription doc updates for v4
- Is there an expected time to get to a stable version of v4 HOT 2
- Elmish for WebSharper apps HOT 4
- Question about FSharp.Core version HOT 4
- Docs: String.replicate piece raises an exception HOT 1
- Remove redundant Subscription trace logs HOT 7
- Replacement with ofEffect for ofSub not working as in v3? HOT 3
- Proposal: port Elm's virtual dom to work with Feliz HOT 4
- [Question] Elmish way of disposing component dependencies HOT 1
- Suggestion: Cmd.OfTask.attempt with untyped Task HOT 1
- How to dispose of `Cmd.ofEffect` events handler? HOT 2
- FuncUI TextBox.onTextChange issues HOT 4
- Official website’s /elmish/cmd.html page 404 HOT 4
- Question about the Elmish loop HOT 4
- Racing condition in Elmish loop with Async and Task Cmd. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elmish.