Skip to content

Commit

Permalink
Merge c327947 into ad58670
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Apr 26, 2020
2 parents ad58670 + c327947 commit dc3e0c0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ assignees: ''
- Describe alternatives you've considered
- A clear and concise description of any alternative solutions or features you've considered.
-->
## The proposed solution
## The alternatives

<!-- THE ADDITIONA CONTEXT:
- Add any other context or screenshots about the feature request here.
Expand Down
6 changes: 6 additions & 0 deletions src/App/NetDaemon.App/Common/INetDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace JoySoftware.HomeAssistant.NetDaemon.Common
{
Expand Down Expand Up @@ -185,6 +186,11 @@ public interface INetDaemonApp : IDisposable, IEquatable<INetDaemonApp>
/// </summary>
/// <param name="entityIds">The unique id:s of the script</param>
IScript RunScript(params string[] entityIds);

/// <summary>
/// A thread safe key/value dictionary to safely share states within and between apps in memory
/// </summary>
ConcurrentDictionary<string, object> Global { get; }
}

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions src/App/NetDaemon.App/Common/NetDaemonApp.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand Down Expand Up @@ -32,6 +33,9 @@ public abstract class NetDaemonApp : INetDaemonApp, INetDaemonBase
private FluentExpandoObject? _storageObject;
internal FluentExpandoObject? InternalStorageObject { get { return _storageObject; } set { _storageObject = value; } }

// This is declared as static since it will contain state shared globally
static ConcurrentDictionary<string, object> _global = new ConcurrentDictionary<string, object>();

/// <summary>
/// Dependencies on other applications that will be initialized before this app
/// </summary>
Expand All @@ -57,6 +61,9 @@ public abstract class NetDaemonApp : INetDaemonApp, INetDaemonBase
/// <inheritdoc/>
public bool IsEnabled { get; set; }

/// <inheritdoc/>
public ConcurrentDictionary<string, object> Global => _global;

/// <inheritdoc/>
public Task CallService(string domain, string service, dynamic? data = null, bool waitForResponse = false)
{
Expand Down
18 changes: 18 additions & 0 deletions tests/NetDaemon.Daemon.Tests/NetDaemonApp/NetDaemonAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace NetDaemon.Daemon.Tests.NetDaemonApp
{
public class AppTestApp : JoySoftware.HomeAssistant.NetDaemon.Common.NetDaemonApp { }
public class AppTestApp2 : JoySoftware.HomeAssistant.NetDaemon.Common.NetDaemonApp { }

public class NetDaemonApptests
{
Expand Down Expand Up @@ -220,5 +221,22 @@ public void GetStateShouldCallCorrectDaemonGetState()
// ASSERT
_netDaemonMock.Verify(n => n.GetState("entityid"));
}

[Theory]
[InlineData("int", 10)]
[InlineData("str", "hello")]
public void GlobalShouldReturnCorrectData(string key, object value)
{
var _app_two = new AppTestApp2();
_app_two.StartUpAsync(_netDaemonMock.Object);
_app_two.Id = "app2";

// ARRANGE and ACT
_app.Global[key] = value;

// ASSERT
Assert.Equal(_app_two.Global[key], value);

}
}
}

0 comments on commit dc3e0c0

Please sign in to comment.