82 lines
3.1 KiB
C#
82 lines
3.1 KiB
C#
using YKanBan.Storage.AppData;
|
|
using YKanBan.Storage.Workspace;
|
|
|
|
namespace YKanBan.Services;
|
|
|
|
/// <summary>
|
|
/// Composition root for the whole process: owns the loaded configuration and
|
|
/// the single open workspace session. The configuration is persisted once at
|
|
/// shutdown; the session (lock plus connection) lives until disposal.
|
|
/// </summary>
|
|
public sealed class AppServices : IDisposable {
|
|
/// <summary>
|
|
/// Loads the configuration and prepares the service graph.
|
|
/// </summary>
|
|
public AppServices() {
|
|
ConfigStore = new AppConfigStore(AppDataPaths.AppConfigJsonPath);
|
|
Config = ConfigStore.Load();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the mutable application configuration, persisted at shutdown.
|
|
/// </summary>
|
|
public AppConfig Config { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the store backing app.json.
|
|
/// </summary>
|
|
public AppConfigStore ConfigStore { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the open workspace session, or <see langword="null"/> while no
|
|
/// workspace is open (for example on an error page).
|
|
/// </summary>
|
|
public WorkspaceSession? Session { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Opens a workspace, acquiring its exclusive lock and connection.
|
|
/// </summary>
|
|
/// <param name="folderPath">The workspace folder to open.</param>
|
|
/// <returns>The open session.</returns>
|
|
/// <exception cref="YKanBan.Storage.WorkspaceNotInitializedException">The folder has no .ykanban structure.</exception>
|
|
/// <exception cref="YKanBan.Storage.WorkspaceLockException">Another instance already holds the lock.</exception>
|
|
public WorkspaceSession OpenWorkspace(string folderPath) {
|
|
Session = WorkspaceSession.Open(folderPath);
|
|
return Session;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a fresh workspace and opens it, holding the lock throughout.
|
|
/// </summary>
|
|
/// <param name="folderPath">The folder to initialize.</param>
|
|
/// <returns>The open session.</returns>
|
|
/// <exception cref="YKanBan.Storage.WorkspaceDirectoryMissingException">The folder does not exist on disk.</exception>
|
|
/// <exception cref="YKanBan.Storage.WorkspaceLockException">Another instance already holds the lock.</exception>
|
|
public WorkspaceSession InitializeWorkspace(string folderPath) {
|
|
Session = WorkspaceSession.Initialize(folderPath);
|
|
return Session;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Releases the workspace session, if any, and forgets it.
|
|
/// </summary>
|
|
public void CloseWorkspace() {
|
|
Session?.Dispose();
|
|
Session = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Persists the configuration; called once at shutdown.
|
|
/// </summary>
|
|
// YYC MARK: the whole app.json is rewritten unconditionally at shutdown.
|
|
// Running several instances would make the last one to exit overwrite every
|
|
// field with its own in-memory state. Multi-instance use is not supported,
|
|
// so this last-writer-wins behavior is accepted.
|
|
public void SaveConfiguration() => ConfigStore.Save(Config);
|
|
|
|
/// <summary>
|
|
/// Releases the workspace session, if any.
|
|
/// </summary>
|
|
public void Dispose() => Session?.Dispose();
|
|
}
|