Fosi Fossil Service
Small teams. Open source. Built on Fossil.

IaC / SwiftFlow

High-performance WMS with wave picking, smart routing, and real-time inventory visibility.

Public | Owner: IaC | Default branch: trunk | Last update: 2026-10-23 14:37

Artifact 9eb062209d93771c8e20ec61e420219f4b0e8ef3f46c83212fd6ffa68903dcc7:


import exception
import gleam/erlang/process.{type Subject}
import gleam/list
import gleam/otp/actor

pub type Message {
  TryBegin(entity: String, reply: Subject(Bool))
  Finish(entity: String)
}

pub fn start() -> Result(Subject(Message), actor.StartError) {
  case actor.new([]) |> actor.on_message(handle) |> actor.start() {
    Ok(started) -> Ok(started.data)
    Error(err) -> Error(err)
  }
}

pub fn with_lock(lock: Subject(Message), entity: String, f: fn() -> a) -> Result(a, Nil) {
  case actor.call(lock, 5000, TryBegin(entity, _)) {
    False -> Error(Nil)
    True -> {
      use <- exception.defer(fn() { process.send(lock, Finish(entity)) })
      Ok(f())
    }
  }
}

fn handle(state: List(String), message: Message) -> actor.Next(List(String), Message) {
  case message {
    TryBegin(entity, reply) ->
      case list.contains(state, entity) {
        True -> {
          process.send(reply, False)
          actor.continue(state)
        }
        False -> {
          process.send(reply, True)
          actor.continue([entity, ..state])
        }
      }
    Finish(entity) ->
      actor.continue(list.filter(state, fn(held) { held != entity }))
  }
}

Clone

You can clone this repository with the following command:

fossil clone https://fosi.dev/IaC/SwiftFlow

Contribute

The best way to contribute is through e-mail. See the project documentation for more details.