Add `overwrite` option to channel send to enable ring-buffer like behavior
In some cases it's useful to have a channel which acts as a ring-buffer that overwrites the oldest data.
For example in UIs you often want the last mouse input. The alternatives like calling tryRecv and then send require taking the lock multiple times and adds odd edge-cases.
Alternatively this could be made into a new proc sendOverwrite or forceSend as send(ch, val, overwrite=true) is no longer blocking in a sense.
forceSend or maybe push is the better API than a flag that changes behavior in a severe way.
forceSendor maybepushis the better API than a flag that changes behavior in a severe way.
I sorta like push. The forceSend would be good too. I updated it with push to see how it feels.
Claude gives me:
Based on the naming convention "push", I'd expect this method to have the following behavior:
- It would be non-blocking (like trySend).
- It would follow a "newest is most important" philosophy, which aligns with your need to prioritize new values.
In essence, push suggests a stack-like or priority operation where new items have precedence over old ones. This fits well with your requirement to overwrite older items instead of blocking. So if I saw channel.push(val) in code, I'd assume it's implementing precisely the behavior you're looking for - a non-blocking send that overwrites older data when the channel is full.