Skip to content

Commit

Permalink
Session will now remember the last active script across between app l…
Browse files Browse the repository at this point in the history
…ifetimes
  • Loading branch information
tareqimbasher committed Dec 10, 2023
1 parent 068f327 commit 4f8f200
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/Core/NetPad.Application/Sessions/Session.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using NetPad.Data;
using NetPad.Events;
using NetPad.Exceptions;
using NetPad.Scripts;
Expand All @@ -7,18 +8,23 @@ namespace NetPad.Sessions;

public class Session : ISession
{
private const string CurrentActiveSaveKey = "session.active";

private readonly IScriptEnvironmentFactory _scriptEnvironmentFactory;
private readonly ITrivialDataStore _trivialDataStore;
private readonly IEventBus _eventBus;
private readonly ILogger<Session> _logger;
private readonly List<ScriptEnvironment> _environments;
private Guid? _lastActiveScriptId;

public Session(
IScriptEnvironmentFactory scriptEnvironmentFactory,
ITrivialDataStore trivialDataStore,
IEventBus eventBus,
ILogger<Session> logger)
{
_scriptEnvironmentFactory = scriptEnvironmentFactory;
_trivialDataStore = trivialDataStore;
_eventBus = eventBus;
_logger = logger;
_environments = new List<ScriptEnvironment>();
Expand Down Expand Up @@ -53,17 +59,21 @@ public async Task OpenAsync(Script script, bool activate = true)

public async Task OpenAsync(IEnumerable<Script> scripts)
{
Script? last = null;
scripts = scripts.ToArray();

foreach (var script in scripts)
{
await OpenAsync(script, activate: false);
last = script;
}

if (last != null)
if (Guid.TryParse(_trivialDataStore.Get<string>(CurrentActiveSaveKey), out var lastSavedScriptId) &&
scripts.Any(s => s.Id == lastSavedScriptId))
{
await ActivateAsync(lastSavedScriptId);
}
else
{
await ActivateAsync(last.Id);
await ActivateAsync(scripts.LastOrDefault()?.Id);
}
}

Expand Down Expand Up @@ -126,7 +136,9 @@ public Task ActivateAsync(Guid? scriptId)
}

if (scriptId == null)
{
newActive = null;
}
else
{
var environment = Get(scriptId.Value);
Expand All @@ -136,6 +148,8 @@ public Task ActivateAsync(Guid? scriptId)
Active = newActive;
_eventBus.PublishAsync(new ActiveEnvironmentChangedEvent(newActive?.Script.Id));

_trivialDataStore.Set(CurrentActiveSaveKey, Active?.Script.Id);

return Task.CompletedTask;
}

Expand All @@ -147,5 +161,6 @@ public Task ActivateLastActiveScriptAsync()
return Task.CompletedTask;
}

private bool CanActivateLastActiveScript() => _lastActiveScriptId != null && _environments.Any(e => e.Script.Id == _lastActiveScriptId);
private bool CanActivateLastActiveScript() =>
_lastActiveScriptId != null && _environments.Any(e => e.Script.Id == _lastActiveScriptId);
}
2 changes: 2 additions & 0 deletions src/Tests/NetPad.Tests/Helpers/SessionTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NetPad.Events;
using NetPad.Scripts;
using NetPad.Sessions;
using NetPad.Tests.Services;

namespace NetPad.Tests.Helpers;

Expand All @@ -13,6 +14,7 @@ public static Session CreateSession(IServiceProvider serviceProvider)
{
return new Session(
new DefaultScriptEnvironmentFactory(serviceProvider),
new NullTrivialDataStore(),
serviceProvider.GetRequiredService<IEventBus>(),
serviceProvider.GetRequiredService<ILogger<Session>>());
}
Expand Down
20 changes: 20 additions & 0 deletions src/Tests/NetPad.Tests/Services/NullTrivialDataStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NetPad.Data;

namespace NetPad.Tests.Services;

public class NullTrivialDataStore : ITrivialDataStore
{
public TValue? Get<TValue>(string key) where TValue : class
{
return null;
}

public void Set<TValue>(string key, TValue value)
{
}

public bool Contains(string key)
{
return false;
}
}

0 comments on commit 4f8f200

Please sign in to comment.