OSDN Git Service

ThrottlingTimerで使用する変数名等をlodash.jsでの用語に合わせる
authorKimura Youichi <kim.upsilon@bucyou.net>
Thu, 25 Apr 2019 21:54:15 +0000 (06:54 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Thu, 15 Aug 2019 20:29:22 +0000 (05:29 +0900)
OpenTween/ThrottlingTimer.cs
OpenTween/Tween.cs

index 8891dfe..3bc0e1b 100644 (file)
@@ -36,22 +36,22 @@ namespace OpenTween
         private readonly Timer throttlingTimer;
         private readonly Func<Task> timerCallback;
 
+        private DateTimeUtc lastCalled = DateTimeUtc.MinValue;
         private DateTimeUtc lastInvoked = DateTimeUtc.MinValue;
-        private DateTimeUtc lastExecuted = DateTimeUtc.MinValue;
         private int refreshTimerEnabled = 0;
 
         public TimeSpan Interval { get; }
 
-        public ThrottlingTimer(TimeSpan interval, Func<Task> timerCallback)
+        public ThrottlingTimer(Func<Task> timerCallback, TimeSpan interval)
         {
-            this.Interval = interval;
             this.timerCallback = timerCallback;
+            this.Interval = interval;
             this.throttlingTimer = new Timer(this.Execute);
         }
 
-        public void Invoke()
+        public void Call()
         {
-            this.lastInvoked = DateTimeUtc.Now;
+            this.lastCalled = DateTimeUtc.Now;
 
             if (this.refreshTimerEnabled == TIMER_DISABLED)
             {
@@ -65,7 +65,7 @@ namespace OpenTween
 
         private async void Execute(object _)
         {
-            var timerExpired = this.lastInvoked < this.lastExecuted;
+            var timerExpired = this.lastCalled < this.lastInvoked;
             if (timerExpired)
             {
                 // 前回実行時より後に lastInvoked が更新されていなければタイマーを止める
@@ -73,8 +73,7 @@ namespace OpenTween
             }
             else
             {
-                this.lastExecuted = DateTimeUtc.Now;
-
+                this.lastInvoked = DateTimeUtc.Now;
                 await this.timerCallback().ConfigureAwait(false);
 
                 // dueTime は Execute が呼ばれる度に再設定する (period は使用しない)
@@ -86,5 +85,9 @@ namespace OpenTween
 
         public void Dispose()
             => this.throttlingTimer.Dispose();
+
+        // lodash.js の _.throttle 的な処理をしたかったメソッド
+        public static ThrottlingTimer Throttle(Func<Task> callback, TimeSpan wait)
+            => new ThrottlingTimer(callback, wait);
     }
 }
index 459ddf7..d6b4107 100644 (file)
@@ -1114,8 +1114,7 @@ namespace OpenTween
             //タイマー設定
 
             var streamingRefreshInterval = TimeSpan.FromSeconds(SettingManager.Common.UserstreamPeriod);
-            this.RefreshThrottlingTimer = new ThrottlingTimer(streamingRefreshInterval,
-                () => this.InvokeAsync(() => this.RefreshTimeline()));
+            this.RefreshThrottlingTimer = ThrottlingTimer.Throttle(() => this.InvokeAsync(() => this.RefreshTimeline()), streamingRefreshInterval);
 
             TimerTimeline.AutoReset = true;
             TimerTimeline.SynchronizingObject = this;
@@ -1282,7 +1281,7 @@ namespace OpenTween
             if (e.UserStream)
             {
                 var interval = TimeSpan.FromSeconds(SettingManager.Common.UserstreamPeriod);
-                var newTimer = new ThrottlingTimer(interval, () => this.InvokeAsync(() => this.RefreshTimeline()));
+                var newTimer = ThrottlingTimer.Throttle(() => this.InvokeAsync(() => this.RefreshTimeline()), interval);
                 var oldTimer = Interlocked.Exchange(ref this.RefreshThrottlingTimer, newTimer);
                 oldTimer.Dispose();
             }
@@ -11325,7 +11324,7 @@ namespace OpenTween
 
             this._statuses.DistributePosts();
 
-            this.RefreshThrottlingTimer.Invoke();
+            this.RefreshThrottlingTimer.Call();
         }
 
         private async void tw_UserStreamStarted(object sender, EventArgs e)