]> git.ktnx.net Git - mpd-feeder.git/commitdiff
fixup idle rework: add WorkQueue
authorDamyan Ivanov <dmn@debian.org>
Sun, 21 Nov 2021 08:47:55 +0000 (08:47 +0000)
committerDamyan Ivanov <dmn@debian.org>
Sun, 21 Nov 2021 08:47:55 +0000 (08:47 +0000)
lib/App/MPD/Feeder/WorkQueue.pm [new file with mode: 0644]

diff --git a/lib/App/MPD/Feeder/WorkQueue.pm b/lib/App/MPD/Feeder/WorkQueue.pm
new file mode 100644 (file)
index 0000000..aa21fb0
--- /dev/null
@@ -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;