Skip to content

PluginDataHandler

A handler for loading and saving data to the plugin’s data file, refusing a data.json larger than a byte limit.

Import:

import { PluginDataHandler } from '@mnaoumov/obsidian-lib/data-handler';

Signature:

export class PluginDataHandler implements DataHandler

Implements: DataHandler

Constructor

new PluginDataHandler(plugin: Plugin, options?: PluginDataHandlerConstructorOptions)

Creates a new plugin data handler.

Properties

PropertyTypeDescription
loadData() => Promise<unknown>A function to load data from the plugin's data file.
(Inherited from DataHandler)
maxSizeInBytesnumberThe maximum size of data.json, in bytes.
recoveryStorenull | SettingsRecoveryStoreWhere recovery copies of data.json are kept, or null when none are.
recoveryStore?null | SettingsRecoveryStoreWhere a copy of the data file is kept before anything could lose what it held, or null (or absent) when the handler keeps none. A settings component keeps a copy here before a first save that would drop or change a stored value, and skips that save when the copy cannot be kept.
(Inherited from DataHandler)
saveData(data: unknown) => Promise<void>A function to save data to the plugin's data file.
(Inherited from DataHandler)

Methods

MethodReturnsDescription
getDataFilePath()null | stringGets the path of the plugin's data.json, as Obsidian's Plugin.loadData reads it.
loadData()Promise<unknown>Loads data from the plugin's data file.

The file's size is read through the adapter BEFORE Obsidian reads and parses it, so an oversized file is never loaded into memory. When Obsidian answers undefined or null, the file is read raw and parsed here, because Obsidian answers undefined for a file that exists but does not parse too.
reportParseError(error)voidTells the user that data.json could not be parsed: a notice naming the plugin and the path, and the error in the console.
saveData(data)Promise<void>Saves data to the plugin's data file.

The data is serialized exactly as Obsidian writes it, and its UTF-8 byte length is checked before anything is written.

Links to this page: