From 963b27fd71560194e1e1941a134fb5cbe6c04fcb Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 9 Apr 2019 17:01:55 -0400 Subject: [PATCH] state service --- src/Core/Abstractions/IStateService.cs | 11 ++++++++ src/Core/Services/StateService.cs | 38 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/Core/Abstractions/IStateService.cs create mode 100644 src/Core/Services/StateService.cs diff --git a/src/Core/Abstractions/IStateService.cs b/src/Core/Abstractions/IStateService.cs new file mode 100644 index 000000000..03fe074b0 --- /dev/null +++ b/src/Core/Abstractions/IStateService.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; + +namespace Bit.Core.Abstractions +{ + public interface IStateService + { + Task GetAsync(string key); + Task RemoveAsync(string key); + Task SaveAsync(string key, T obj); + } +} \ No newline at end of file diff --git a/src/Core/Services/StateService.cs b/src/Core/Services/StateService.cs new file mode 100644 index 000000000..d345b88dd --- /dev/null +++ b/src/Core/Services/StateService.cs @@ -0,0 +1,38 @@ +using Bit.Core.Abstractions; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Bit.Core.Services +{ + public class StateService : IStateService + { + private readonly Dictionary _state = new Dictionary(); + + public Task GetAsync(string key) + { + return Task.FromResult(_state.ContainsKey(key) ? (T)_state[key] : (T)(object)null); + } + + public Task SaveAsync(string key, T obj) + { + if(_state.ContainsKey(key)) + { + _state[key] = obj; + } + else + { + _state.Add(key, obj); + } + return Task.FromResult(0); + } + + public Task RemoveAsync(string key) + { + if(_state.ContainsKey(key)) + { + _state.Remove(key); + } + return Task.FromResult(0); + } + } +}