[blam] Get rid of more GLib.Timeout uses



commit d77bc74a89498d3640927e44c7315bc80984e85c
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Fri Jun 14 19:28:14 2013 +0200

    Get rid of more GLib.Timeout uses
    
    The runtime provides better ways of doing timers and delays than going
    over to glib.

 src/Application.cs       |   38 ++++++++++---------------
 src/ChannelCollection.cs |   70 +++++++++++++++++++--------------------------
 src/ItemStore.cs         |    1 -
 3 files changed, 45 insertions(+), 64 deletions(-)
---
diff --git a/src/Application.cs b/src/Application.cs
index 0f75b23..7e0b212 100644
--- a/src/Application.cs
+++ b/src/Application.cs
@@ -100,7 +100,7 @@ namespace Imendio.Blam {
 
         private ChannelCollection mCollection;
 
-        private uint              mAutoRefreshId;
+        Timer                     AutoRefreshTimer;
 
         public static TargetEntry[] DragEntries = new TargetEntry[] {
             new TargetEntry("STRING", 0, (uint)TargetType.String),
@@ -666,7 +666,7 @@ namespace Imendio.Blam {
             /* First move the refresh back */
             StartStopAutoRefresh();
             /* And pretend a timeout occurred */
-            TimeoutRefreshAll();
+            DoRefreshAll(null);
         }
 
         private void DragDataReceivedCb(object o, DragDataReceivedArgs args)
@@ -757,33 +757,25 @@ namespace Imendio.Blam {
             statusbar.Push(contextId, StatusString);
         }
 
-        private bool TimeoutRefreshAll ()
+               void DoRefreshAll(object obj)
         {
             ShowNextUpdateTime();
-            mCollection.RefreshAllAsync();
-
-            /* Continue until source is removed */
-            return true;
+            mCollection.RefreshAll();
         }
 
-        private void StartStopAutoRefresh ()
-        {
-            bool doAutoRefresh;
-
-            if (mAutoRefreshId != 0) {
-                GLib.Source.Remove (mAutoRefreshId);
-                mAutoRefreshId = 0;
-            }
+               private void StartStopAutoRefresh ()
+               {
+                       bool doAutoRefresh = Conf.Get (Preference.AUTO_REFRESH, false);
+                       if (!doAutoRefresh)
+                               return;
 
-            doAutoRefresh = Conf.Get (Preference.AUTO_REFRESH, false);
-            if (!doAutoRefresh) {
-                return;
-            }
+                       var rate = TimeSpan.FromMinutes(Conf.Get(Preference.AUTO_REFRESH_RATE, 15));
+                       if (AutoRefreshTimer != null) {
+                               AutoRefreshTimer.Change(rate, rate);
+                       }
 
-            // Check if any feeds require refresh every ten seconds
-            mAutoRefreshId = GLib.Timeout.Add ((uint) Conf.Get(Preference.AUTO_REFRESH_RATE, 15) * 60 * 1000,
-                                               new GLib.TimeoutHandler (TimeoutRefreshAll));
-        }
+                       AutoRefreshTimer = new Timer(DoRefreshAll, null, rate, rate);
+               }
 
         private void ConfNotifyHandler (object sender, NotifyEventArgs args)
         {
diff --git a/src/ChannelCollection.cs b/src/ChannelCollection.cs
index 5709bfa..3f72314 100644
--- a/src/ChannelCollection.cs
+++ b/src/ChannelCollection.cs
@@ -30,9 +30,10 @@ namespace Imendio.Blam {
        public string FileName;
 
        private Object clock = new Object();
+       Task DelayedWrite;
+       CancellationTokenSource DelayedWriteCancellation = new CancellationTokenSource();
+
 
-       private bool mDirty = false;
-       private uint mTimeoutId = 0;
        private static uint WRITE_TIMEOUT = 5 * 60 * 1000; // Every 5 minutes
        
        static XmlSerializer serializer = new XmlSerializer (typeof (ChannelCollection));
@@ -91,8 +92,10 @@ namespace Imendio.Blam {
            ChannelCollection collection;
 
            try {
+                               Console.WriteLine("trying to load from file");
                collection = RealLoadFromFile (file);
-           } catch {
+           } catch (Exception e) {
+                               Console.WriteLine("failed: {0}", e.InnerException.Message);
                try {
                    collection = RealLoadFromFile (Defines.APP_DATADIR + "/collection.xml");
                } catch {
@@ -133,6 +136,7 @@ namespace Imendio.Blam {
             
            reader.Close ();
 
+                       Console.WriteLine("channels is {0}", collection.Channels);
             if (collection.Channels == null) {
                 collection.mChannels = new ArrayList ();
             }
@@ -143,9 +147,6 @@ namespace Imendio.Blam {
        public void SaveToFile ()
        {
            lock (clock) {
-               if (!mDirty) {
-                   return;
-               }
 
                string tmpFile = GetTempFile (this.FileName);
 
@@ -166,9 +167,7 @@ namespace Imendio.Blam {
                    Console.Error.WriteLine ("File replace error: " + e.Message);
                    return;
                }
-               
-               MarkAsDirty (false);
-           }
+               }
        }
 
         public async void Add (IChannel channel)
@@ -184,7 +183,7 @@ namespace Imendio.Blam {
         mChannels.Add (channel);
                        bool res = await channel.RefreshAsync();
                        if (res)
-                               MarkAsDirty(true);
+                               MarkAsDirty();
                        if (ChannelAdded != null)
                                ChannelAdded (channel);
                }
@@ -199,12 +198,12 @@ namespace Imendio.Blam {
                                ChannelGroupAdded(group, channel);
                        }
 
-                       MarkAsDirty(true);
+                       MarkAsDirty();
         }
 
        public void Updated (IChannel channel)
        {
-           MarkAsDirty (true);
+           MarkAsDirty();
 
            if (ChannelUpdated != null) {
                ChannelUpdated (channel);
@@ -232,7 +231,7 @@ namespace Imendio.Blam {
                ChannelRemoved (channel);
            }
 
-        MarkAsDirty(true);
+        MarkAsDirty();
        }
 
         delegate void RefreshAllDelegate();
@@ -251,7 +250,7 @@ namespace Imendio.Blam {
 
             Parallel.ForEach(mChannels.Cast<Channel>(), c => {
                 if (c.Refresh())
-                                       MarkAsDirty(true);
+                                       MarkAsDirty();
                 new MainloopEmitter (this.ChannelRefreshFinished, c).Emit ();
             });
         }
@@ -262,35 +261,26 @@ namespace Imendio.Blam {
                ChannelRefreshStarted (channel);
            }
        }
-       
-       private bool WriteTimeoutHandler ()
-       {
-           SaveToFile ();
-            ItemStore.Save();
-           
-           return false;
-       }
 
-       private void MarkAsDirty (bool dirty)
-       {
-           lock (clock) {
-               if (dirty) {
-                   if (mTimeoutId != 0) {
-                       GLib.Source.Remove (mTimeoutId);
-                   } 
-               
-                   mTimeoutId = GLib.Timeout.Add (WRITE_TIMEOUT, new GLib.TimeoutHandler 
(WriteTimeoutHandler));
-               } else {
-                   if (mTimeoutId != 0) {
-                       GLib.Source.Remove (mTimeoutId);
-                       mTimeoutId = 0;
-                   }
+               // TODO: this keeps the old semantics of restarting the timer, but we
+               // might want to simply see if we should start one and not do anything
+               // if there's already a task enqueued.
+               private void MarkAsDirty()
+               {
+                       if (DelayedWrite != null && !DelayedWrite.IsCompleted)
+                               DelayedWriteCancellation.Cancel();
+
+                       DelayedWriteCancellation = new CancellationTokenSource();
+                       DelayedWrite = Task.Delay(TimeSpan.FromMilliseconds(WRITE_TIMEOUT), 
DelayedWriteCancellation.Token);
+                       DelayedWrite.ContinueWith(task => {
+                               if (task.IsCanceled)
+                                       return;
+
+                               SaveToFile();
+                               ItemStore.Save();
+                       });
                }
 
-               mDirty = dirty;
-           }
-       }
-
        private static string GetTempFile (string fileName) 
        {
            return fileName + ".tmp";
diff --git a/src/ItemStore.cs b/src/ItemStore.cs
index e46841c..2365e04 100644
--- a/src/ItemStore.cs
+++ b/src/ItemStore.cs
@@ -91,7 +91,6 @@ namespace Imendio.Blam
                 return;
             }
 
-            Console.WriteLine("about to format");
             SyndicationFeed feed = SyndicationFeed.Load(reader);
             foreach(SyndicationItem item in feed.Items){
                 instance.items.Add(item.Id, new Item(item));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]