Package

purescript-task

Repository
ursi/purescript-task
License
BSD-3-Clause
Uploaded by
ursi
Published on
2022-08-22T22:18:31Z

Pursuit

This is a library for performing asynchronous effects, heavily inspired by Elm's Task. In terms of functionality, it is a superset of the Elm implementation, with the following additions:

  • Tasks can be evaluated in parallel
  • Users can create their own tasks (this is technically possible in Elm via a hack)
  • An Apply instance*
  • An Alt instance (both non-parallel* and parallel)
  • A Monoid instance*

Example

Here is a contrived example where we use js-timers and node-fs to make wait and readFile tasks, then use them together to make a program that waits 1 second then logs itself to the console.

module Main where

import Prelude
import Data.Either (Either(..))
import Effect (Effect)
import Effect.Class.Console (log)
import Effect.Exception (Error)
import Effect.Timer (setTimeout, clearTimeout)
import Node.Buffer (toString)
import Node.Encoding (Encoding(..))
import Node.FS.Async as Fs
import Task (Task, makeTask)
import Task as Task

main :: Effect Unit
main =
  Task.run do
    wait 1000
    log =<< readFile "src/Main.purs"

wait ::  x. Int -> Task x Unit
wait ms =
  makeTask \aC _ -> do
    id <- setTimeout ms $ aC unit
    pure $ clearTimeout id

readFile :: String -> Task Error String
readFile path =
  makeTask \aC xC -> do
    Fs.readTextFile UTF8 path case _ of
      Right str -> aC str
      Left error -> xC error
    -- There is no canceler for this task
    pure $ pure unit

* something similar can be done in Elm, but it would be more clunky and it isn't provided out of the box

Modules
Task
Task.Class
Task.Common
Dependencies