From 049b5674e73e50959b08cbf8878842041dca7743 Mon Sep 17 00:00:00 2001 From: Ch1ck3nNeedsRNG Date: Sat, 17 May 2025 16:03:35 -0400 Subject: [PATCH 1/2] typings --- storage/LocalStore.js | 87 +++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/storage/LocalStore.js b/storage/LocalStore.js index d28a12e..268944d 100644 --- a/storage/LocalStore.js +++ b/storage/LocalStore.js @@ -1,51 +1,66 @@ const cachedInstances = [] +const moduleNameSym = Symbol("moduleName") +const fileNameSym = Symbol("fileName") + /** * - This data is meant to be edited on the fly as well as gathered from a file * - Note: The data is backed up in a folder inside of `.minecraft/config/tska-backup` every 10minutes * and whenever the file gets corrupted it attempts to re-set it with the backup one + * @template T + * @param {string} moduleName + * @param {T} [defaultData={}] `{}` + * @param {string} [fileName=".data.json"] `".data.json"` + * @returns {T & LocalStoreImpl} */ -export class LocalStore { - constructor(moduleName, defaultData = {}, fileName = ".data.json") { - let cachedData = FileLib.read(moduleName, fileName) || FileLib.read(`./config/tska-backup/${moduleName}/${fileName}`) - if (!cachedData && FileLib.exists(moduleName, fileName)) - console.warn(`[TSKA] Seems like your data for module \"${moduleName}\" was corruputed therefore resetted.`) - - const parsed = JSON.parse(cachedData || "{}") - Object.assign(this, defaultData, parsed) - - /** - * - Function that returns the module's `moduleName` and `fileName` - * to store the data at - * @private - * @returns {[string, string]} - */ - this.getModuleData = () => { - return [ moduleName, fileName ] - } - - cachedInstances.push(this) - } +function LocalStoreImpl(moduleName, defaultData = {}, fileName = ".data.json") { + if (!(this instanceof LocalStoreImpl)) return new LocalStoreImpl(moduleName, defaultData, fileName) - /** - * - This function is mostly for internal use since the dev should not handle this - * but it is open to use if you feel like it is better. - * - This gets called every time the game unloads automatically - * @deprecated - */ - save() { - const [ moduleName, fileName ] = this.getModuleData() + let cachedData = FileLib.read(moduleName, fileName) || FileLib.read(`./config/tska-backup/${moduleName}/${fileName}`) + if (!cachedData && FileLib.exists(moduleName, fileName)) + console.warn(`[TSKA] Seems like your data for module \"${moduleName}\" was corruputed therefore resetted.`) - FileLib.write(moduleName, fileName, JSON.stringify(this, null, 4), true) - } + const parsed = JSON.parse(cachedData || "{}") + Object.assign(this, defaultData, parsed) - /** @private */ - _saveBackup() { - const [ moduleName, fileName ] = this.getModuleData() + this[moduleNameSym] = moduleName + this[fileNameSym] = fileName - FileLib.write(`./config/tska-backup/${moduleName}/${fileName}`, JSON.stringify(this, null, 4), true) - } + cachedInstances.push(this) + + return this +} + +/** + * - Function that returns the module's `moduleName` and `fileName` + * to store the data at + * @private + * @returns {[string, string]} + */ +LocalStoreImpl.prototype.getModuleData = () => { + return [this[moduleNameSym], this[fileNameSym]] } +/** + * - This function is mostly for internal use since the dev should not handle this + * but it is open to use if you feel like it is better. + * - This gets called every time the game unloads automatically + * @deprecated + */ +LocalStoreImpl.prototype.save = function save() { + const [moduleName, fileName] = this.getModuleData() + + FileLib.write(moduleName, fileName, JSON.stringify(this, null, 4), true) +} + +/** @private */ +LocalStoreImpl.prototype._saveBackup = function _saveBackup() { + const [moduleName, fileName] = this.getModuleData() + + FileLib.write(`./config/tska-backup/${moduleName}/${fileName}`, JSON.stringify(this, null, 4), true) +} + +/** @type {{ new(moduleName: string, defaultData: T, fileName: string): (T & LocalStoreImpl) } & typeof LocalStoreImpl} */ +export const LocalStore = LocalStoreImpl; const data = new LocalStore("tska", { LocalStore: { From 9d6d95b210881ef408d7164b8f8eec8eaa0ff4b3 Mon Sep 17 00:00:00 2001 From: Ch1ck3nNeedsRNG Date: Sat, 17 May 2025 16:08:11 -0400 Subject: [PATCH 2/2] forgot tree man is bad --- storage/LocalStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/LocalStore.js b/storage/LocalStore.js index 268944d..ac6fb6d 100644 --- a/storage/LocalStore.js +++ b/storage/LocalStore.js @@ -60,7 +60,7 @@ LocalStoreImpl.prototype._saveBackup = function _saveBackup() { } /** @type {{ new(moduleName: string, defaultData: T, fileName: string): (T & LocalStoreImpl) } & typeof LocalStoreImpl} */ -export const LocalStore = LocalStoreImpl; +export const LocalStore = LocalStoreImpl const data = new LocalStore("tska", { LocalStore: {