Giter Site home page Giter Site logo

andrewmacmurray / elm-concurrent-task Goto Github PK

View Code? Open in Web Editor NEW
35.0 35.0 5.0 679 KB

Run a tree of Tasks concurrently, call JS functions as Tasks (Task Ports).

Home Page: https://package.elm-lang.org/packages/andrewMacmurray/elm-concurrent-task/latest/

License: MIT License

Elm 87.97% TypeScript 10.70% JavaScript 1.21% Shell 0.13%
concurrency elm task-ports

elm-concurrent-task's Issues

Copy API from `Time`

How do you feel about the idea of copying the API from elm/time inside ConcurrentTask.Time?

The idea is that then you can import ConcurrentTask.Time as Time and just use it as if it were the normal Time module.

Multiple pools per ports

Currently it's assumed that you'd use 1 task pool per pair of ports.

However, this is not the case with something like a nested TEA single page app, where it makes sense to have a task pool per page. It's unsafe currently to do this:

  • A task is in-flight from one pool (say internally it has an attempt id of 0).
  • The user switches pages and immediately starts a task.
  • The attempt ids would overlap and there would likely be a weird race condition.

A possible solution would be to add ConcurrentTask.namedPool where you could give a pool a unique name / id - this would prevent race conditions as the runner would know which task pool the responses are associated with.

There may be a way for the pool to initially generate a random id internally before it does any work, but would have to experiment with that and hopefully not break the types

[feature] finallyDo

import ConcurrentTask as CT exposing (ConcurrentTask)

finallyDo : ConcurrentTask e a -> ConcurrentTask e b -> ConcurrentTask e a
finallyDo secondTask firstTask =
    firstTask
        |> CT.onError (\e -> secondTask |> CT.andThenDo (CT.fail e))
        |> CT.andThenDo secondTask

Similar to Javascripts try-catch-finally statement. For example, I'm using finallyDo to make sure unlockResource gets called in both, the success and failure case:

callInsideLock : ConcurrentTask e a -> ConcurrentTask e b
callInsideLock task =
    lockResource
        |> CT.andThenDo task
        |> CT.finallyDo unlockResource

instead of

callInsideLock : ConcurrentTask e a -> ConcurrentTask e b
callInsideLock task =
     lockResource
        |> CT.andThenDo (task |> CT.onError (\e -> CT.andThenDo (CT.fail e) unlockResource))
        |> CT.andThenDo unlockResource

Are you interested in adding this to elm-concurrent-task? I would open a PR if so.

andThenDoOnlyIf

I have two versions of the same function:

import ConcurrentTask as CT exposing (ConcurrentTask)

andThenDoOnlyIf : Bool -> ConcurrentTask Error () -> ConcurrentTask Error () -> ConcurrentTask Error ()
andThenDoOnlyIf shouldProcessTask secondTask firstTask =
    firstTask
        |> CT.andThenDo
            (if shouldProcessTask then
                secondTask

             else
                CT.succeed ()
            )

andThenDoOnlyIf : Bool -> ConcurrentTask Error () -> ConcurrentTask Error () -> ConcurrentTask Error ()
andThenDoOnlyIf shouldProcessTask secondTask firstTask =
    if shouldProcessTask then
        firstTask |> CT.andThenDo secondTask

    else
        CT.succeed ()

The first one does work, the second doesn't execute its second task. Why does the first one work when the if-else is inside the CT.andThenDo function? Any Idea why that is?

Http Bytes compatibility

It would be nice to make the ConcurrentTask.Http module compatible with elm/bytes.

A popular way seems to be encoding / decoding a Base64 string with a package like this. This however loses most of performance gains of using Bytes.

I'd be curious if anyone has any opinions / alternatives to this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.