Skip to content

Commit

Permalink
PlaylistObserver: support single handler for multiple types (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketMan committed Sep 9, 2022
1 parent f49a784 commit 1623d95
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
6 changes: 1 addition & 5 deletions controllers/PushServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ protected function loadOnNow() {
$spin = $entry->asArray();
$spin['artist'] = PlaylistEntry::swapNames($spin['artist']);
$event = $spin;
})->onComment(function($entry) use(&$event) {
$event = null;
})->onLogEvent(function($entry) use(&$event) {
$event = null;
})->onSetSeparator(function($entry) use(&$event) {
})->on('comment logEvent setSeparator', function($entry) use(&$event) {
$event = null;
}), 0, OnNowFilter::class);
}
Expand Down
59 changes: 40 additions & 19 deletions engine/PlaylistObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zookeeper Online
*
* @author Jim Mason <jmason@ibinx.com>
* @copyright Copyright (C) 1997-2019 Jim Mason <jmason@ibinx.com>
* @copyright Copyright (C) 1997-2022 Jim Mason <jmason@ibinx.com>
* @link https://zookeeper.ibinx.com/
* @license GPL-3.0
*
Expand Down Expand Up @@ -45,6 +45,19 @@
* onSpin
*
* One or more onXXX functions may be chained to the PlaylistObserver instance.
*
* Alternatively, you may use the form:
*
* on(types, function)
*
* where 'types' is a space-separated string of entry types; e.g.,
*
* on('comment logEvent', function($entry) {...})...
*
* In this case, the function will be installed for all the specified types.
*
* If the lambda function returns a trueish value, the observer will
* stop iteration.
*
*/
class PlaylistObserver {
Expand All @@ -58,76 +71,84 @@ public function __call($method, $args) {
if(isset($this->$method) && $this->$method instanceof \Closure)
return call_user_func_array($this->$method, $args);
}

/**
* install lambda function to handle comment entries
*/
public function onComment($comment) {
public function onComment(\Closure $comment) {
$this->comment = $comment;
return $this;
}

/**
* install lambda function to handle log event entries
*/
public function onLogEvent($logEvent) {
public function onLogEvent(\Closure $logEvent) {
$this->logEvent = $logEvent;
return $this;
}

/**
* install lambda function to handle set separator entries
*/
public function onSetSeparator($setSeparator) {
public function onSetSeparator(\Closure $setSeparator) {
$this->setSeparator = $setSeparator;
return $this;
}

/**
* install lambda function to handle spin entries
*/
public function onSpin($spin) {
public function onSpin(\Closure $spin) {
$this->spin = $spin;
return $this;
}

/**
* install lambda function to handle one or more entry types
*/
public function on(string $types, \Closure $fn) {
foreach(explode(' ', $types) as $type)
$this->$type = $fn;

return $this;
}

private function observeComment($entry) {
if($this->comment)
$this->comment($entry);
return $this->comment ? $this->comment($entry) : null;
}

private function observeLogEvent($entry) {
if($this->logEvent)
$this->logEvent($entry);
return $this->logEvent ? $this->logEvent($entry) : null;
}

private function observeSetSeparator($entry) {
if($this->setSeparator)
$this->setSeparator($entry);
return $this->setSeparator ? $this->setSeparator($entry) : null;
}

private function observeSpin($entry) {
if($this->spin)
$this->spin($entry);
return $this->spin ? $this->spin($entry) : null;
}

/**
* observe the specified PlaylistEntry
*/
public function observe(PlaylistEntry $entry) {
$retVal = null;
switch($entry->getType()) {
case PlaylistEntry::TYPE_SPIN:
$this->observeSpin($entry);
$retVal = $this->observeSpin($entry);
break;
case PlaylistEntry::TYPE_LOG_EVENT:
$this->observeLogEvent($entry);
$retVal = $this->observeLogEvent($entry);
break;
case PlaylistEntry::TYPE_COMMENT:
$this->observeComment($entry);
$retVal = $this->observeComment($entry);
break;
case PlaylistEntry::TYPE_SET_SEPARATOR:
$this->observeSetSeparator($entry);
$retVal = $this->observeSetSeparator($entry);
break;
}
return $retVal;
}
}
4 changes: 2 additions & 2 deletions engine/impl/Playlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ public function getTracksWithObserver($playlist, PlaylistObserver $observer, $de
$tracks = $this->getTracks($playlist, $desc);
if($tracks && $filter)
$tracks = new $filter($tracks);
while($tracks && ($track = $tracks->fetch()))
$observer->observe(new PlaylistEntry($track));
while($tracks && ($track = $tracks->fetch()) &&
!$observer->observe(new PlaylistEntry($track)));
return $tracks;
}

Expand Down
21 changes: 2 additions & 19 deletions ui/Playlists.php
Original file line number Diff line number Diff line change
Expand Up @@ -892,29 +892,12 @@ private function emitTrackAdder($playlistId, $playlist, $editTrack = false) {
$window = $api->getTimestampWindow($playlistId);
$time = [
"created" => null,
"break" => false,
"id" => $editTrack
];
$observer = (new PlaylistObserver())->onComment(function($entry) use(&$time) {
if($time['break']) return;
$time['break'] = $time['id'] === $entry->getId();
$created = $entry->getCreatedTime();
if($created) $time['created'] = $created;
})->onLogEvent(function($entry) use(&$time) {
if($time['break']) return;
$time['break'] = $time['id'] === $entry->getId();
$created = $entry->getCreatedTime();
if($created) $time['created'] = $created;
})->onSetSeparator(function($entry) use(&$time) {
if($time['break']) return;
$time['break'] = $time['id'] === $entry->getId();
$created = $entry->getCreatedTime();
if($created) $time['created'] = $created;
})->onSpin(function($entry) use(&$time) {
if($time['break']) return;
$time['break'] = $time['id'] === $entry->getId();
$observer = (new PlaylistObserver())->on('comment logEvent setSeparator spin', function($entry) use(&$time) {
$created = $entry->getCreatedTime();
if($created) $time['created'] = $created;
return $time['id'] === $entry->getId();
});
$api->getTracksWithObserver($playlistId, $observer);
if($time['created'])
Expand Down

0 comments on commit 1623d95

Please sign in to comment.