On the AT Protocol

Good code,
shared generously.

A community index of reusable code snippets — signed as open records on ATProto, searchable by language, and discoverable by anyone.

Task.async_stream with timeout Elixir
items
|> Task.async_stream(
  &process/1,
  timeout: 5_000,
  on_timeout: :kill_task,
  max_concurrency: 8
)
|> Stream.filter(&match?({:ok,_}, &1))
|> Enum.map(fn {:ok, v} -> v end)
concurrencystreams
@aliceatp
Retry with exponential backoff Rust
for attempt in 0..max_retries {
  match try_operation().await {
    Ok(result) => return Ok(result),
    Err(e) if attempt < max_retries-1 => {
      let d = base * 2u32.pow(attempt);
      tokio::time::sleep(d).await;
    }
    Err(e) => return Err(e),
  }
}
asyncerror-handling
@bsk.io
Zod discriminated union TypeScript
const Event = z.discriminatedUnion("type", [
  z.object({ type: z.literal("click"),
    x: z.number(), y: z.number() }),
  z.object({ type: z.literal("keydown"),
    key: z.string(), mod: z.boolean() }),
]);
type Event = z.infer<typeof Event>;
typesvalidation
@carrie.dev