Skip to content

Commit 5596397

Browse files
authored
Global threadsafe in-mem state storage (#97)
1 parent ad58670 commit 5596397

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

.github/ISSUE_TEMPLATE/feature_request.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ assignees: ''
2525
- Describe alternatives you've considered
2626
- A clear and concise description of any alternative solutions or features you've considered.
2727
-->
28-
## The proposed solution
28+
## The alternatives
2929

3030
<!-- THE ADDITIONA CONTEXT:
3131
- Add any other context or screenshots about the feature request here.

src/App/NetDaemon.App/Common/INetDaemon.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using System.Collections.Concurrent;
67

78
namespace JoySoftware.HomeAssistant.NetDaemon.Common
89
{
@@ -185,6 +186,11 @@ public interface INetDaemonApp : IDisposable, IEquatable<INetDaemonApp>
185186
/// </summary>
186187
/// <param name="entityIds">The unique id:s of the script</param>
187188
IScript RunScript(params string[] entityIds);
189+
190+
/// <summary>
191+
/// A thread safe key/value dictionary to safely share states within and between apps in memory
192+
/// </summary>
193+
ConcurrentDictionary<string, object> Global { get; }
188194
}
189195

190196
/// <summary>

src/App/NetDaemon.App/Common/NetDaemonApp.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.Logging;
22
using System;
3+
using System.Collections.Concurrent;
34
using System.Collections.Generic;
45
using System.Diagnostics.CodeAnalysis;
56
using System.Linq;
@@ -32,6 +33,9 @@ public abstract class NetDaemonApp : INetDaemonApp, INetDaemonBase
3233
private FluentExpandoObject? _storageObject;
3334
internal FluentExpandoObject? InternalStorageObject { get { return _storageObject; } set { _storageObject = value; } }
3435

36+
// This is declared as static since it will contain state shared globally
37+
static ConcurrentDictionary<string, object> _global = new ConcurrentDictionary<string, object>();
38+
3539
/// <summary>
3640
/// Dependencies on other applications that will be initialized before this app
3741
/// </summary>
@@ -57,6 +61,9 @@ public abstract class NetDaemonApp : INetDaemonApp, INetDaemonBase
5761
/// <inheritdoc/>
5862
public bool IsEnabled { get; set; }
5963

64+
/// <inheritdoc/>
65+
public ConcurrentDictionary<string, object> Global => _global;
66+
6067
/// <inheritdoc/>
6168
public Task CallService(string domain, string service, dynamic? data = null, bool waitForResponse = false)
6269
{

tests/NetDaemon.Daemon.Tests/NetDaemonApp/NetDaemonAppTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace NetDaemon.Daemon.Tests.NetDaemonApp
1010
{
1111
public class AppTestApp : JoySoftware.HomeAssistant.NetDaemon.Common.NetDaemonApp { }
12+
public class AppTestApp2 : JoySoftware.HomeAssistant.NetDaemon.Common.NetDaemonApp { }
1213

1314
public class NetDaemonApptests
1415
{
@@ -220,5 +221,22 @@ public void GetStateShouldCallCorrectDaemonGetState()
220221
// ASSERT
221222
_netDaemonMock.Verify(n => n.GetState("entityid"));
222223
}
224+
225+
[Theory]
226+
[InlineData("int", 10)]
227+
[InlineData("str", "hello")]
228+
public void GlobalShouldReturnCorrectData(string key, object value)
229+
{
230+
var _app_two = new AppTestApp2();
231+
_app_two.StartUpAsync(_netDaemonMock.Object);
232+
_app_two.Id = "app2";
233+
234+
// ARRANGE and ACT
235+
_app.Global[key] = value;
236+
237+
// ASSERT
238+
Assert.Equal(_app_two.Global[key], value);
239+
240+
}
223241
}
224242
}

0 commit comments

Comments
 (0)