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')
}
setObject()
setObject(key: string, value: object): void;
Stores a plain JSON object under the given key.
Empty keys and non-plain-object values are silently ignored. The object has to contain nested JSON-compatible primitives, arrays, and objects.
Nested arrays/objects are supported up to a maximum depth of 20 levels. Unsupported values (functions, symbols, class instances, cyclic references) cause the entire write to be silently skipped.
Example
import { prefs } from '@mobrowser/api';
prefs.setObject('ui.windowState', {
width: 1024, height: 768, x: 0, y: 0
})
hasObject()
hasObject(key: string): boolean;
Returns true if an object value is stored under key.
Example
import { prefs } from '@mobrowser/api';
if (prefs.hasObject('ui.windowState')) {
const state = prefs.getObject('ui.windowState')
}
setArray()
setArray(key: string, value: unknown[]): void;
Stores a JSON array under the given key.
Empty keys and non-array values are silently ignored. Array elements have to be JSON-compatible primitives, arrays, or plain objects.
Nested arrays/objects are supported up to a maximum depth of 20 levels. Unsupported values (functions, symbols, class instances, cyclic references) cause the entire write to be silently skipped.
Example
import { prefs } from '@mobrowser/api';
prefs.setArray('app.recentFiles', ['/home/user/doc.txt', '/home/user/img.png'])
hasArray()
hasArray(key: string): boolean;
Returns true if an array value is stored under key.
Example
import { prefs } from '@mobrowser/api';
if (prefs.hasArray('app.recentFiles')) {
const files = prefs.getArray<string>('app.recentFiles')
}
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')
}