Skip to content

Instantly share code, notes, and snippets.

@mltsy
Last active June 8, 2017 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mltsy/339713d76b083502be221e39c83e89b3 to your computer and use it in GitHub Desktop.
Save mltsy/339713d76b083502be221e39c83e89b3 to your computer and use it in GitHub Desktop.
First draft of a Savable package for elm to complement RemoteData
module Savable exposing (..)
type Savable a = Saved a | Unsaved a a | Saving a a
update : Savable a -> a -> Savable a
update savable new =
case savable of
Saved old -> Unsaved old new
Unsaved old _ -> Unsaved old new
Saving old _ -> Unsaved old new
saving : Savable a -> Savable a
saving savable =
case savable of
Unsaved old new -> Saving old new
_ -> savable
saved : a -> Savable a -> Savable a
saved savedValue savable =
let savedIfEqual newValue savedValue =
if savedValue == newValue then Saved newValue
else Unsaved savedValue newValue
in case savable of
Saved value -> Saved savedValue
Unsaved _ newValue -> savedIfEqual newValue savedValue
Saving _ newValue -> savedIfEqual newValue savedValue
failSaving : Savable a -> Savable a
failSaving savable =
case savable of
Saving old new -> Unsaved old new
_ -> savable
getNewValue : Savable a -> a
getNewValue savable =
case savable of
Saved value -> value
Unsaved old new -> new
Saving old new -> new
getOldValue : Savable a -> a
getOldValue savable =
case savable of
Saved value -> value
Unsaved old new -> old
Saving old new -> old
@mltsy
Copy link
Author

mltsy commented Jun 8, 2017

Thoughts:

  • xilnocas on elm-slack recommended a new Union type that includes All the flags from RemoteData as well as the ones here (which I like but it would require significant copypasta and refactoring of the RemoteData library to make it similarly robust)
  • I've considered rounding out the flags with SaveFailed a a err where err is a second type variable (similar to RemoteData)

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