dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [REJECT] tracklist: add version
@ 2015-12-06  2:41 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-12-06  2:41 UTC (permalink / raw)
  To: spew

This will allow us to implement the versioning policy of
mpd.  But perhaps that's a bad idea.

While we're at it, use a Struct for track representation
to simplify our code.
---
 lib/dtas/track.rb     | 13 -------------
 lib/dtas/tracklist.rb | 15 +++++++++++++--
 2 files changed, 13 insertions(+), 15 deletions(-)
 delete mode 100644 lib/dtas/track.rb

diff --git a/lib/dtas/track.rb b/lib/dtas/track.rb
deleted file mode 100644
index 215ee52..0000000
--- a/lib/dtas/track.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-# Copyright (C) 2015 all contributors <dtas-all@nongnu.org>
-# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
-require_relative '../dtas'
-
-class DTAS::Track
-  attr_reader :track_id
-  attr_reader :to_path
-
-  def initialize(track_id, path)
-    @track_id = track_id
-    @to_path = path
-  end
-end
diff --git a/lib/dtas/tracklist.rb b/lib/dtas/tracklist.rb
index 1ae2719..40e0f56 100644
--- a/lib/dtas/tracklist.rb
+++ b/lib/dtas/tracklist.rb
@@ -2,7 +2,6 @@
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require_relative '../dtas'
 require_relative 'serialize'
-require_relative 'track'
 
 # the a tracklist object for -player
 # this is inspired by the MPRIS 2.0 TrackList spec
@@ -10,6 +9,8 @@ class DTAS::Tracklist # :nodoc:
   include DTAS::Serialize
   attr_accessor :repeat # true, false, 1
 
+  Track = Struct.new(:track_id, :to_path, :version)
+
   SIVS = %w(list pos repeat)
   TL_DEFAULTS = {
     "list" => [],
@@ -21,6 +22,7 @@ class DTAS::Tracklist # :nodoc:
     obj = new
     obj.instance_eval do
       list = hash["list"] and @list.replace(list.map! { |s| new_track(s) })
+      _inc_version
       @pos = hash["pos"] || -1
       @repeat = hash["repeat"] || false
     end
@@ -39,6 +41,7 @@ class DTAS::Tracklist # :nodoc:
     @list = []
     @goto_off = @goto_pos = nil
     @track_nr = 0
+    @version = 0
   end
 
   def new_track(path)
@@ -48,7 +51,7 @@ class DTAS::Tracklist # :nodoc:
     # avoid promoting to Bignum on 32-bit
     @track_nr = n = 1 if n >= 0x3fffffff
 
-    DTAS::Track.new(n, path)
+    Track.new(n, path, @version)
   end
 
   def reset
@@ -96,6 +99,7 @@ class DTAS::Tracklist # :nodoc:
     if after_track_id
       idx = _idx_of(after_track_id) or
         raise ArgumentError, "after_track_id invalid"
+      _inc_version
       @list[idx, 1] = [ @list[idx], track ]
       if set_as_current
         @pos = idx + 1
@@ -103,6 +107,7 @@ class DTAS::Tracklist # :nodoc:
         @pos += 1 if @pos > idx
       end
     else # nil = first_track
+      _inc_version
       @list.unshift(track)
       if set_as_current
         @pos = 0
@@ -119,6 +124,7 @@ class DTAS::Tracklist # :nodoc:
 
   def remove_track(track_id)
     idx = _idx_of(track_id) or return false
+    _inc_version
     track = @list.delete_at(idx)
     len = @list.size
     if @pos >= len
@@ -146,4 +152,9 @@ class DTAS::Tracklist # :nodoc:
     end
     @goto_pos = prev_idx
   end
+
+  def _inc_version
+    @version += 1
+    @version = 0 if @version > 0xffffffff
+  end
 end
-- 
EW


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2015-12-06  2:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-06  2:41 [REJECT] tracklist: add version Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).