Prefs
Manages persistent application preferences.
All reads and writes operate in-memory. Call persist() to save
changes to disk. Preferences are stored in prefs.json under the application
user data directory.
Example
import { prefs } from '@mobrowser/api';
prefs.setNumber('editor.zoom', 1.5)
prefs.persist()
Methods
getNumber()
getNumber(key: string, defaultValue?: number): number;
Returns the number stored under key, or defaultValue if the key is
missing or holds a non-number value.
Defaults to 0 when no defaultValue is provided and the key is missing
or has the wrong type.
Example
import { prefs } from '@mobrowser/api';
const zoom = prefs.getNumber('editor.zoom', 1.0)
setNumber()
setNumber(key: string, value: number): void;
Stores a number under the given key.
Empty keys and non-finite values (NaN, Infinity) are silently ignored.
Example
import { prefs } from '@mobrowser/api';
prefs.setNumber('editor.fontSize', 14)
hasNumber()
hasNumber(key: string): boolean;
Returns true if a number value is stored under key.
Example
import { prefs } from '@mobrowser/api';
if (prefs.hasNumber('editor.zoom')) {
const zoom = prefs.getNumber('editor.zoom')
}
getBoolean()
getBoolean(key: string, defaultValue?: boolean): boolean;
Returns the boolean stored under key, or defaultValue if the key is
missing or holds a non-boolean value.
Defaults to false when no defaultValue is provided and the key is
missing or has the wrong type.
Example
import { prefs } from '@mobrowser/api';
const wordWrap = prefs.getBoolean('editor.wordWrap', false)
setBoolean()
setBoolean(key: string, value: boolean): void;
Stores a boolean under the given key.
Empty keys are silently ignored.
Example
import { prefs } from '@mobrowser/api';
prefs.setBoolean('ui.sidebar.visible', false)
hasBoolean()
hasBoolean(key: string): boolean;
Returns true if a boolean value is stored under key.
Example
import { prefs } from '@mobrowser/api';
if (prefs.hasBoolean('editor.wordWrap')) {
const wordWrap = prefs.getBoolean('editor.wordWrap')
}
getString()
getString(key: string, defaultValue?: string): string;
Returns the string stored under key, or defaultValue if the key is
missing or holds a non-string value.
Defaults to '' (empty string) when no defaultValue is provided and
the key is missing or has the wrong type.
Example
import { prefs } from '@mobrowser/api';
const theme = prefs.getString('ui.theme', 'light')
setString()
setString(key: string, value: string): void;
Stores a string under the given key.
Empty keys are silently ignored.
Example
import { prefs } from '@mobrowser/api';
prefs.setString('ui.theme', 'dark')
hasString()
hasString(key: string): boolean;
Returns true if a string value is stored under key.
Example
import { prefs } from '@mobrowser/api';
if (prefs.hasString('ui.theme')) {
const theme = prefs.getString('ui.theme')
}
remove()
remove(key: string): void;
Removes a single key from the in-memory cache.
Empty keys are silently ignored.
Example
import { prefs } from '@mobrowser/api';
if (prefs.getString('ui.theme')) {
prefs.remove('ui.theme')
}
clear()
clear(): void;
Removes all keys from the in-memory cache.
Example
import { prefs } from '@mobrowser/api';
prefs.clear()
persist()
persist(): boolean;
Saves the preferences to disk under the application user data directory.
Until this method is called, changes are in-memory only and will be lost if the application exits.
Return value
true if the preferences are saved successfully, false if the
save fails for any reason.
Example
import { prefs } from '@mobrowser/api';
prefs.setString('ui.theme', 'dark')
prefs.setNumber('editor.fontSize', 14)
if (!prefs.persist()) {
console.error('Failed to save preferences')
}