Optimist
A simple package for modelling optimistic ui updates.
Install / Use
/learn @hayleigh-dot-dev/OptimistREADME
optimist
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:
pusha new value to replace the current one.
let wobble = wibble |> optimist.push(2)
updatethe 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:
resolvethe optimistic update with a new value.
let response = Ok(2)
let resolved = wibble |> optimist.resolve(response)
tryto resolve the optimistic update by applying a function to the response.
let response = Ok(1)
let resolved = wibble |> optimist.try(response, int.add)
revertthe optimistic update to a previous value.
let resolved = wibble |> optimist.revert
forcethe optimistic update to resolve itself.
let resolved = wibble |> optimist.force
Related Skills
node-connect
343.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
92.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.3kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
