vibe_limiter 0.1.0
vibe_limiter: ^0.1.0 copied to clipboard
Weighted LRU cache, bounded priority job queue with de-duplication and visible drops, and an adaptive background-worker governor.
example/vibe_limiter_example.dart
import 'package:vibe_limiter/vibe_limiter.dart';
void main() {
// A cache that keeps at most 2 entries.
final cache = LruCache<String, String>(maxEntries: 2);
cache.put('a', 'one');
cache.put('b', 'two');
cache.get('a'); // 'a' is now the most recently used
cache.put('c', 'three'); // evicts 'b'
print(cache.keys.toList()); // [a, c]
// A queue that holds 2 jobs. User work (priority 2) pushes out background work.
final queue = BoundedQueue<String>(
maxPending: 2,
onDrop: (key, item) => print('dropped $key'),
);
queue.add('scan-1', 'scan folder 1', pri: 0);
queue.add('scan-2', 'scan folder 2', pri: 0);
queue.add('open', 'open the file the user clicked', pri: 2); // drops scan-2
print(queue.removeFirst()); // open the file the user clicked
// How many background workers may run right now?
final governor = ResourceGovernor()..foregroundBusy = () => false;
print('workers: ${governor.heavyWorkers}, pause: ${governor.pace}');
}