From: Damyan Ivanov Date: Sun, 21 Nov 2021 08:47:55 +0000 (+0000) Subject: fixup idle rework: add WorkQueue X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=64b30da5c255505591f9292034366d282f9a6ffc;p=mpd-feeder.git fixup idle rework: add WorkQueue --- diff --git a/lib/App/MPD/Feeder/WorkQueue.pm b/lib/App/MPD/Feeder/WorkQueue.pm new file mode 100644 index 0000000..aa21fb0 --- /dev/null +++ b/lib/App/MPD/Feeder/WorkQueue.pm @@ -0,0 +1,48 @@ +use v5.28; +use utf8; +use Object::Pad; +class App::MPD::Feeder::WorkQueue; + +use Log::Any qw($log); + +has @queue; +has %present; + +method add($key) { + if ( exists $present{$key} ) { + $log->trace("job '$key' already queued"); + } + else { + $present{$key} = undef; + push @queue, $key; + + $log->trace("job '$key' queued"); + } +} + +method next { + my $item = shift @queue; + + if ( defined($item) ) { + $log->trace("next job: '$item'"); + delete $present{$item}; + } + else { + $log->trace("job queue is empty"); + } + + return $item; +} + +method clear { + @queue = (); + %present = (); + $log->trace('work queue cleared'); +} + +method has_jobs { + $log->trace( "Queue jobs: " . scalar(@queue) ); + return scalar @queue; +} + +1;