From 6376d1ed6f4bbae46e1dc4aecec36bfe5de044e8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 15 Jul 2021 08:26:37 -0500 Subject: update PushbulletService for options changes, implement IDisposable --- .../Integrations/Pushbullet/IPushbulletService.cs | 3 +- .../Integrations/Pushbullet/PushbulletService.cs | 36 +++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/slskd/Integrations/Pushbullet/IPushbulletService.cs b/src/slskd/Integrations/Pushbullet/IPushbulletService.cs index 6da42326..bad33903 100644 --- a/src/slskd/Integrations/Pushbullet/IPushbulletService.cs +++ b/src/slskd/Integrations/Pushbullet/IPushbulletService.cs @@ -17,12 +17,13 @@ namespace slskd.Integrations.Pushbullet { + using System; using System.Threading.Tasks; /// /// Pushbullet integration service. /// - public interface IPushbulletService + public interface IPushbulletService : IDisposable { /// /// Sends a push notification to Pushbullet. diff --git a/src/slskd/Integrations/Pushbullet/PushbulletService.cs b/src/slskd/Integrations/Pushbullet/PushbulletService.cs index 04994d92..1a224321 100644 --- a/src/slskd/Integrations/Pushbullet/PushbulletService.cs +++ b/src/slskd/Integrations/Pushbullet/PushbulletService.cs @@ -47,18 +47,28 @@ namespace slskd.Integrations.Pushbullet ILogger log) { HttpClientFactory = httpClientFactory; - Options = optionsMonitor.CurrentValue; + OptionsMonitor = optionsMonitor; Log = log; RecentlySent = new MemoryCache(new MemoryCacheOptions()); } + private bool Disposed { get; set; } private IHttpClientFactory HttpClientFactory { get; } - private Options Options { get; } private ILogger Log { get; } - private PushbulletOptions PushbulletOptions => Options.Integration.Pushbullet; + private IOptionsMonitor OptionsMonitor { get; } + private PushbulletOptions PushbulletOptions => OptionsMonitor.CurrentValue.Integration.Pushbullet; private IMemoryCache RecentlySent { get; } + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + /// /// Sends a push notification to Pushbullet. /// @@ -68,6 +78,11 @@ namespace slskd.Integrations.Pushbullet /// The operation context. public Task PushAsync(string title, string cacheKey, string body) { + if (!PushbulletOptions.Enabled) + { + return Task.CompletedTask; + } + if (string.IsNullOrWhiteSpace(title)) { throw new ArgumentException("A notification title must be supplied", nameof(title)); @@ -93,6 +108,19 @@ namespace slskd.Integrations.Pushbullet return PushInternalAsync(title, body); } + protected virtual void Dispose(bool disposing) + { + if (!Disposed) + { + if (disposing) + { + RecentlySent.Dispose(); + } + + Disposed = true; + } + } + private async Task PushInternalAsync(string title, string body) { try @@ -135,4 +163,4 @@ namespace slskd.Integrations.Pushbullet } } } -} +} \ No newline at end of file -- cgit v1.2.3