OSDN Git Service

発言詳細欄の更新間隔をThrottlingTimerで制御する, TweenMain._colorize を廃止
authorKimura Youichi <kim.upsilon@bucyou.net>
Thu, 25 Apr 2019 21:45:13 +0000 (06:45 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Thu, 15 Aug 2019 20:29:23 +0000 (05:29 +0900)
OpenTween/ThrottlingTimer.cs
OpenTween/Tween.cs

index 3bc0e1b..33edf18 100644 (file)
@@ -41,11 +41,13 @@ namespace OpenTween
         private int refreshTimerEnabled = 0;
 
         public TimeSpan Interval { get; }
+        public TimeSpan MaxWait { get; }
 
-        public ThrottlingTimer(Func<Task> timerCallback, TimeSpan interval)
+        public ThrottlingTimer(Func<Task> timerCallback, TimeSpan interval, TimeSpan maxWait)
         {
             this.timerCallback = timerCallback;
             this.Interval = interval;
+            this.MaxWait = maxWait;
             this.throttlingTimer = new Timer(this.Execute);
         }
 
@@ -73,8 +75,12 @@ namespace OpenTween
             }
             else
             {
-                this.lastInvoked = DateTimeUtc.Now;
-                await this.timerCallback().ConfigureAwait(false);
+                var now = DateTimeUtc.Now;
+                if ((now - this.lastCalled) >= this.Interval || (now - this.lastInvoked) >= this.MaxWait)
+                {
+                    this.lastInvoked = now;
+                    await this.timerCallback().ConfigureAwait(false);
+                }
 
                 // dueTime は Execute が呼ばれる度に再設定する (period は使用しない)
                 // これにより timerCallback の実行に Interval 以上の時間が掛かっても重複して実行されることはなくなる
@@ -86,8 +92,11 @@ namespace OpenTween
         public void Dispose()
             => this.throttlingTimer.Dispose();
 
-        // lodash.js の _.throttle 的な処理をしたかったメソッド
+        // lodash.js の _.throttle, _.debounce 的な処理をしたかったメソッド群
         public static ThrottlingTimer Throttle(Func<Task> callback, TimeSpan wait)
-            => new ThrottlingTimer(callback, wait);
+            => new ThrottlingTimer(callback, wait, maxWait: wait);
+
+        public static ThrottlingTimer Debounce(Func<Task> callback, TimeSpan wait)
+            => new ThrottlingTimer(callback, wait, maxWait: TimeSpan.MaxValue);
     }
 }
index d6b4107..ac50534 100644 (file)
@@ -267,10 +267,10 @@ namespace OpenTween
         private bool osResumed = false;
 
         //////////////////////////////////////////////////////////////////////////////////////////////////////////
-        private bool _colorize = false;
 
         private System.Timers.Timer TimerTimeline = new System.Timers.Timer();
         private ThrottlingTimer RefreshThrottlingTimer;
+        private ThrottlingTimer selectionDebouncer;
 
         private string recommendedStatusFooter;
         private bool urlMultibyteSplit = false;
@@ -1115,6 +1115,7 @@ namespace OpenTween
 
             var streamingRefreshInterval = TimeSpan.FromSeconds(SettingManager.Common.UserstreamPeriod);
             this.RefreshThrottlingTimer = ThrottlingTimer.Throttle(() => this.InvokeAsync(() => this.RefreshTimeline()), streamingRefreshInterval);
+            this.selectionDebouncer = ThrottlingTimer.Debounce(() => this.InvokeAsync(() => this.UpdateSelectedPost()), TimeSpan.FromMilliseconds(100));
 
             TimerTimeline.AutoReset = true;
             TimerTimeline.SynchronizingObject = this;
@@ -1934,9 +1935,9 @@ namespace OpenTween
             this._statuses.SetReadAllTab(post.StatusId, read: true);
             //キャッシュの書き換え
             ChangeCacheStyleRead(true, index); // 既読へ(フォント、文字色)
-
             ColorizeList();
-            _colorize = true;
+
+            this.selectionDebouncer.Call();
         }
 
         private void ChangeCacheStyleRead(bool Read, int Index)
@@ -5583,10 +5584,8 @@ namespace OpenTween
             }
         }
 
-        private async Task Colorize()
+        private async Task UpdateSelectedPost()
         {
-            _colorize = false;
-            await this.DispSelectedPost();
             //件数関連の場合、タイトル即時書き換え
             if (SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.None &&
                SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.Post &&
@@ -5598,11 +5597,11 @@ namespace OpenTween
             if (!StatusLabelUrl.Text.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                 SetStatusLabelUrl();
 
-            foreach (var (tab, index) in this._statuses.Tabs.WithIndex())
+            if (SettingManager.Common.TabIconDisp)
             {
-                if (tab.UnreadCount == 0)
+                foreach (var (tab, index) in this._statuses.Tabs.WithIndex())
                 {
-                    if (SettingManager.Common.TabIconDisp)
+                    if (tab.UnreadCount == 0)
                     {
                         var tabPage = this.ListTab.TabPages[index];
                         if (tabPage.ImageIndex == 0)
@@ -5610,7 +5609,12 @@ namespace OpenTween
                     }
                 }
             }
-            if (!SettingManager.Common.TabIconDisp) ListTab.Refresh();
+            else
+            {
+                this.ListTab.Refresh();
+            }
+
+            await this.DispSelectedPost();
         }
 
         public string createDetailHtml(string orgdata)
@@ -5869,7 +5873,7 @@ namespace OpenTween
 
                 ShortcutCommand.Create(Keys.Control | Keys.Home, Keys.Control | Keys.End)
                     .FocusedOn(FocusedControl.ListTab)
-                    .Do(() => this._colorize = true, preventDefault: false),
+                    .Do(() => this.selectionDebouncer.Call(), preventDefault: false),
 
                 ShortcutCommand.Create(Keys.Control | Keys.N)
                     .FocusedOn(FocusedControl.ListTab)
@@ -7744,11 +7748,8 @@ namespace OpenTween
         private static bool blink = false;
         private static bool idle = false;
 
-        private async Task RefreshTasktrayIcon()
+        private void RefreshTasktrayIcon()
         {
-            if (_colorize)
-                await this.Colorize();
-
             if (!TimerRefreshIcon.Enabled) return;
             //Static usCheckCnt As int = 0
 
@@ -7832,8 +7833,8 @@ namespace OpenTween
             }
         }
 
-        private async void TimerRefreshIcon_Tick(object sender, EventArgs e)
-            => await this.RefreshTasktrayIcon(); // 200ms
+        private void TimerRefreshIcon_Tick(object sender, EventArgs e)
+            => this.RefreshTasktrayIcon(); // 200ms
 
         private void ContextMenuTabProperty_Opening(object sender, CancelEventArgs e)
         {