📝 Update More API Document

This commit is contained in:
Nanaloveyuki
2026-05-20 11:37:49 +08:00
parent 55af0b664f
commit 25a6a973d2
41 changed files with 2484 additions and 15 deletions
+67
View File
@@ -0,0 +1,67 @@
---
name: filter-sink
group: api
category: sink
update-time: 20260520
description: Create a sink that forwards only records matching a predicate.
key-word:
- sink
- filter
- predicate
- public
---
## Filter-sink
Create a sink that forwards only records matching a predicate. This helper is the sink-level counterpart to `Logger::with_filter(...)`.
### Interface
```moonbit
pub fn[S] filter_sink(sink : S, predicate : (Record) -> Bool) -> FilterSink[S] {
```
#### input
- `sink : S` - Wrapped sink receiving records that pass the predicate.
- `predicate : (Record) -> Bool` - Filtering function evaluated for each record.
#### output
- `FilterSink[S]` - Predicate-filtering sink.
### Explanation
Detailed rules explaining key parameters and behaviors
- Only records for which the predicate returns `true` are forwarded.
- This helper is useful for sink-first composition and explicit routing graphs.
- Use the logger-level helper when the filter should be attached after `Logger::new(...)` instead.
### How to Use
Here are some specific examples provided.
#### When Need Predicate Filtering At Sink Construction Time
When a sink graph should reject records before they reach the destination:
```moonbit
let sink = filter_sink(console_sink(), fn(rec) {
rec.target == "kept"
})
```
In this example, only matching records are forwarded to the wrapped sink.
### Error Case
e.g.:
- If record selection should be attached to an already-built logger, use `with_filter(...)` instead.
- Predicate logic is caller-defined, so accidental over-filtering comes from predicate choice.
### Notes
1. This helper is useful for sink-first composition graphs.
2. It works well with predicate builders such as `target_is(...)` and `all_of(...)`.