BetterDiscordApp-rauenzi/renderer/src/modules/api/data.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-05-20 00:37:21 +02:00
import DataStore from "@modules/datastore";
2022-10-02 09:34:34 +02:00
/**
* `Data` is a simple utility class for the management of plugin data. An instance is available on {@link BdApi}.
* @type Data
* @summary {@link Data} is a simple utility class for the management of plugin data.
* @name Data
*/
class Data {
2022-10-08 23:00:18 +02:00
#callerName = "";
2022-10-02 09:34:34 +02:00
constructor(callerName) {
if (!callerName) return;
2022-10-08 23:00:18 +02:00
this.#callerName = callerName;
2022-10-02 09:34:34 +02:00
}
/**
* Saves JSON-serializable data.
*
* @param {string} pluginName Name of the plugin saving data
* @param {string} key Which piece of data to store
* @param {any} data The data to be saved
*/
save(pluginName, key, data) {
2022-10-08 23:00:18 +02:00
if (this.#callerName) {
data = key;
key = pluginName;
pluginName = this.#callerName;
}
2022-10-02 09:34:34 +02:00
return DataStore.setPluginData(pluginName, key, data);
}
/**
* Loads previously stored data.
*
* @param {string} pluginName Name of the plugin loading data
* @param {string} key Which piece of data to load
* @returns {any} The stored data
*/
2022-10-08 23:00:18 +02:00
load(pluginName, key) {
if (this.#callerName) {
key = pluginName;
pluginName = this.#callerName;
}
return DataStore.getPluginData(pluginName, key);
2022-10-02 09:34:34 +02:00
}
/**
* Deletes a piece of stored data. This is different than saving `null` or `undefined`.
2022-10-02 09:34:34 +02:00
*
* @param {string} pluginName Name of the plugin deleting data
* @param {string} key Which piece of data to delete.
2022-10-02 09:34:34 +02:00
*/
2022-10-08 23:00:18 +02:00
delete(pluginName, key) {
if (this.#callerName) {
key = pluginName;
pluginName = this.#callerName;
}
return DataStore.deletePluginData(pluginName, key);
2022-10-02 09:34:34 +02:00
}
}
Object.freeze(Data);
2022-10-08 23:00:18 +02:00
Object.freeze(Data.prototype);
2022-10-02 09:34:34 +02:00
export default Data;