daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Jan 24th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Monad.Aff (Aff, Canceler(..), makeAff)
  2. import Control.Monad.Eff.Class (liftEff)
  3. import Control.Monad.Eff.Exception (error)
  4. import DOM (DOM)
  5. import DOM.Event.EventTarget (addEventListener, eventListener, removeEventListener)
  6. import DOM.File.FileReader as FR
  7. import DOM.File.Types (Blob, fileReaderToEventTarget)
  8. import DOM.HTML.Event.EventTypes as Events
  9. import Data.Foreign (readString)
  10.  
  11. -- | Reads the contents of a DOM API `File` from disk, with the assumption that
  12. -- | the contents is plain text.
  13. readAsText ∷ ∀ e. Blob → Aff String
  14. readAsText blob = makeAff \cb → do
  15.   reader ← liftEff FR.fileReader
  16.   let
  17.     et = fileReaderToEventTarget reader
  18.     listener = eventListener \_ → do
  19.       removeEventListener Events.load listener false et
  20.       r ← runExcept ∘ readString <$> FR.result reader
  21.       cb $ lmap (const (error "A problem occurred while reading a file as text")) r
  22.   addEventListener Events.load listener false et
  23.   FR.readAsText blob reader
  24. pure (Canceler \_ → liftEff (removeEventListener Events.load listener false et))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top