Skip to content

hayleigh-dot-dev/optimist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

optimist

Package Version Hex Docs

gleam add optimist@1

When building user interfaces, we are often faced with the question of what to do while waiting for a response from a server. One common pattern is to update the ui with an optimistic value - one we expect to get back if an operation is successful - and then update the ui again once we get an actual response.

This package, optimist, provides a simple way to manage this pattern through an Optimstic type and some functions to work with it. Let's take a quick look at what you can do...

1. Start with a value

First things first, we need some value to be optimistic about!

let wibble = optimist.from(1)

2. Update it

We have two ways to perform optimistic updates on an Optimistic value:

  • push a new value to replace the current one.
let wobble = wibble |> optimist.push(2)
  • update the current value with a function that takes the current value and returns a new one.
let wobble = wibble |> optimist.update(int.add(_, 1))

3. Do something with it

We've now performed an optimistic update and can render our value in the ui as if the operation has already succeeded.

html.p([], [
  html.text("Your value is: "),
  html.text(optimist.unwrap(wobble) |> int.to_string)
])

4. Resolve the update

We have a few different options for resolving an optimistic update depending on the situation:

  • resolve the optimistic update with a new value.
let response = Ok(2)
let resolved = wibble |> optimist.resolve(response)
  • try to resolve the optimistic update by applying a function to the response.
let response = Ok(1)
let resolved = wibble |> optimist.try(response, int.add)
  • revert the optimistic update to a previous value.
let resolved = wibble |> optimist.revert
  • force the optimistic update to resolve itself.
let resolved = wibble |> optimist.force

About

A simple package for modelling optimistic ui updates.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages