Settings Service
Location: src/main/persistence/service/settings-service.ts
The SettingsService is a main process service responsible for managing global application settings. These settings are not specific to any single collection but rather pertain to the overall state and configuration of the Trufos application itself.
Responsibilities
- Loading and Saving Settings:
- Manages application settings stored in a JSON file:
settings.json. - The location of this file is within the application’s user data directory (e.g.,
app.getPath('userData')/settings.json).
- Manages application settings stored in a JSON file:
- Providing Access to Settings:
- Offers read-only access to the current settings.
- Provides a mechanism to update settings and persist them.
- Initialization:
- On application startup (
init()method, called bysrc/main/main.ts), it checks ifsettings.jsonexists. - If the file exists, it reads and loads the settings.
- If the file does not exist, it creates a default
settings.jsonfile with initial values.
- On application startup (
Settings Data Structure
The settings are defined by the SettingsObject type:
export type SettingsObject = {
/** The index of the currently opened collection inside the collections array */
currentCollectionIndex: number;
/** A list of all the collection directories that have been opened */
collections: string[];
};
When persisted to settings.json, a version field (currently 1.0.0 based on SemVer) is added to the SettingsInfoFile type to allow for future schema migrations if needed, although no migration logic for settings is currently implemented.
type SettingsInfoFile = SettingsObject & { version: typeof VERSION.string };
Key Properties and Methods
DEFAULT_COLLECTION_DIR(static readonly):- Defines the path to the default collection directory, typically
USER_DATA_DIR/default-collection.
- Defines the path to the default collection directory, typically
SETTINGS_FILE(static readonly):- Defines the full path to the
settings.jsonfile.
- Defines the full path to the
instance(static readonly):- Provides a singleton instance of the
SettingsService.
- Provides a singleton instance of the
init(): Promise<void>:- Asynchronous initialization method. Reads existing settings or writes default ones. Implements the
Initializableinterface.
- Asynchronous initialization method. Reads existing settings or writes default ones. Implements the
settings(getter):Readonly<SettingsObject>:- Returns a read-only version of the current settings. This prevents accidental modification of the in-memory settings without going through the designated update mechanism.
setSettings(settings: Readonly<SettingsObject>): Promise<void>:- Updates the in-memory settings with a deep clone of the provided
settingsobject. - Persists the new settings (including the schema version) to
settings.json.
- Updates the in-memory settings with a deep clone of the provided
modifiableSettings(getter):SettingsObject:- Returns a deep clone of the current settings. This allows other services or parts of the application to safely modify a copy of the settings before deciding to persist them using
setSettings.
- Returns a deep clone of the current settings. This allows other services or parts of the application to safely modify a copy of the settings before deciding to persist them using
Interaction with Other Services
EnvironmentService:- Reads
settings.collectionsandsettings.currentCollectionIndexfromSettingsServiceto determine which collection to load on startup or when listing available collections. - Updates
SettingsService(viasetSettings) when the user changes the active collection or closes a collection, modifying thecollectionsarray andcurrentCollectionIndex.
- Reads
PersistenceService:- Uses
SettingsService.DEFAULT_COLLECTION_DIRto know where to create the default collection if it’s missing.
- Uses
The SettingsService ensures that user preferences regarding which collections are open and which one is active are remembered across application sessions.