curds: Split out the body of the playlist __next__() function

This lets us have peek() advance a counter without actually changing the
state of the playlist and avoids a potential deadlock from calling
next() with the lock held.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2019-12-13 14:49:15 -05:00
parent 01a7cc3c74
commit c2034b16a2
1 changed files with 19 additions and 21 deletions

View File

@ -47,6 +47,19 @@ class Playlist(node.PlaylistNode):
with self.node_lock:
return len(self.list)
def __next__(self, cur):
if (max := len(self.list) - 1) == -1:
return None
elif max == 0:
return None if (self.loop == False and cur == 0) else 0
elif self.random == True:
return (cur + random.randint(1, max)) % max
elif cur < max:
return cur + 1
elif self.loop == True:
return 0
return None
def __setstate__(self, state):
node.PlaylistNode.__setstate__(self, state)
self.list = [ trak.lookup(path) for path in state["list"] ]
@ -105,22 +118,10 @@ class Playlist(node.PlaylistNode):
def next(self):
with self.node_lock:
max = len(self) - 1
if max == -1:
return None
elif max == 0:
if self.loop == False and self.current == 0:
return None
self.current = 0
elif self.random == True:
self.current = (self.current + random.randint(1, max)) % max
elif self.current < max:
self.current += 1
elif self.loop == True:
self.current = 0
else:
return None
return self.list[self.current]
if (n := self.__next__(self.current)) != None:
self.current = n
return self.list[self.current]
return None
def peek(self, n):
with self.node_lock:
@ -128,12 +129,9 @@ class Playlist(node.PlaylistNode):
res = [ ]
for i in range(n):
track = self.next()
if track == None:
if (cur := self.__next__(cur)) == None:
break
res.append(track)
self.current = cur
res.append(self.list[cur])
return res
def remove(self, track):