Skip to content

Add mapAsync#96

Merged
zyla merged 6 commits intomasterfrom
map-async
Apr 29, 2025
Merged

Add mapAsync#96
zyla merged 6 commits intomasterfrom
map-async

Conversation

@zyla
Copy link
Collaborator

@zyla zyla commented Dec 11, 2023

Asynchronous version of map, for easily integrating e.g. network requests into a FRP computation.

data AsyncComputation a = Sync a | Async (Aff a)

data AsyncState a
  = InProgress (Maybe (Either Error a))
  | Finished (Either Error a)

-- | Map a possibly-asynchronous function over a Dynamic.
-- |
-- | When the source dynamic changes, the mapping function is re-evaluated. If it returns `Sync`,
-- | this works like `map` - the change is propagated in the same cycle.
-- |
-- | If it returns `Async`, then the dynamic will first transition to `InProgress` and start the async computation.
-- | After it finished, it will transition to `Finished` (which might contain an error).
mapAsync :: forall a b. (a -> I.AsyncComputation b) -> Dynamic a -> Dynamic (I.AsyncState b)

@zyla zyla requested review from jborkowski and kozak April 23, 2025 16:15
Copy link
Contributor

@kozak kozak left a comment

Choose a reason for hiding this comment

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

LG.

pure unit
Running fiber ->
launchAff_ do
killFiber (error "Cancelled") fiber
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
killFiber (error "Cancelled") fiber
killFiber cancelledError fiber

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch. But I wonder why tests didn't catch it.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are no tests for cancel I think.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No direct test, but this one should cancel the task internally

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, mapAsync doesn't call cancel, only run which calls killFiber directly. So this function is unnecessary

@zyla zyla merged commit 4ac328f into master Apr 29, 2025
2 checks passed
zyla added a commit that referenced this pull request May 3, 2025
* Add mapAsync

* Doc comment

* Remove warning

* Remove unnecessary code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments