From 82ce66554c30c3a571eb65475c4e9f079c974ec0 Mon Sep 17 00:00:00 2001 From: ZomoXYZ Date: Thu, 26 May 2022 21:35:24 -0500 Subject: [PATCH] defaultValue = structuredClone(defaultValue) --- .gitignore | 3 ++- buildexample.js | 15 +++++++++++++-- data/global.json | 1 - examples/test.ts | 33 +++++++++++++++++++++++++++++++++ package.json | 1 + pnpm-lock.yaml | 12 +++++++----- src/storage.ts | 2 ++ src/storageclass.ts | 1 + src/util.ts | 2 ++ 9 files changed, 61 insertions(+), 9 deletions(-) delete mode 100644 data/global.json create mode 100644 examples/test.ts diff --git a/.gitignore b/.gitignore index 888997e..6a5cd09 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules lib -examples/*.js \ No newline at end of file +examples/*.js +data \ No newline at end of file diff --git a/buildexample.js b/buildexample.js index 8897b74..c1762f3 100644 --- a/buildexample.js +++ b/buildexample.js @@ -5,8 +5,19 @@ const define = { } build({ - entryPoints: ['examples/main.ts'], - outfile: 'examples/main.js', + entryPoints: ['examples/basic.ts'], + outfile: 'examples/basic.js', + + target: 'es6', + platform: 'node', + format: 'cjs', + + define, +}) + +build({ + entryPoints: ['examples/test.ts'], + outfile: 'examples/test.js', target: 'es6', platform: 'node', diff --git a/data/global.json b/data/global.json deleted file mode 100644 index 20d3729..0000000 --- a/data/global.json +++ /dev/null @@ -1 +0,0 @@ -{ "a": "aaaaa", "b": "bbbbb", "c": "ccccc" } diff --git a/examples/test.ts b/examples/test.ts new file mode 100644 index 0000000..e2e39ac --- /dev/null +++ b/examples/test.ts @@ -0,0 +1,33 @@ +import { Guild } from 'discord.js' +import { setFlag, globalStorage, guildStorage } from '..' +import { GuildStorage } from '../lib/storageclass' + +const DefaultStorage = { + pingCount: 0, +} + +function main() { + //change the storage directory + setFlag('dir', './data/test') + + let globalStor = globalStorage() + let guildStor = guildStorage({ id: '000' } as Guild) as + | GuildStorage + | undefined + + let globalJson = globalStor.getJson(DefaultStorage) + let guildJson = guildStor?.getJson(DefaultStorage) + + globalJson.pingCount++ + console.log(globalJson, guildJson) + + if (guildJson) guildJson.pingCount++ + console.log(globalJson, guildJson) + + globalStor.set(globalJson) + if (guildStor && guildJson) guildStor.set(guildJson) +} + +main() +// setTimeout(() => main(), 1000) +// setTimeout(() => main(), 2000) diff --git a/package.json b/package.json index a2a4c84..2798221 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "license": "ISC", "devDependencies": { "@types/fs-extra": "^9.0.13", + "@types/node": "^17.0.35", "discord.js": "^13.6.0", "prettier": "^2.6.2", "typescript": "^4.6.3" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 09aa4cb..7df8de2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2,6 +2,7 @@ lockfileVersion: 5.3 specifiers: '@types/fs-extra': ^9.0.13 + '@types/node': ^17.0.35 discord.js: ^13.6.0 esbuild: ^0.14.28 fs-extra: ^10.1.0 @@ -14,6 +15,7 @@ dependencies: devDependencies: '@types/fs-extra': 9.0.13 + '@types/node': 17.0.35 discord.js: 13.6.0 prettier: 2.6.2 typescript: 4.6.3 @@ -63,7 +65,7 @@ packages: integrity: sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==, } dependencies: - '@types/node': 17.0.23 + '@types/node': 17.0.35 dev: true /@types/node-fetch/2.6.1: @@ -72,14 +74,14 @@ packages: integrity: sha512-oMqjURCaxoSIsHSr1E47QHzbmzNR5rK8McHuNb11BOM9cHcIK3Avy0s/b2JlXHoQGTYS3NsvWzV1M0iK7l0wbA==, } dependencies: - '@types/node': 17.0.23 + '@types/node': 17.0.35 form-data: 3.0.1 dev: true - /@types/node/17.0.23: + /@types/node/17.0.35: resolution: { - integrity: sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==, + integrity: sha512-vu1SrqBjbbZ3J6vwY17jBs8Sr/BKA+/a/WtjRG+whKg1iuLFOosq872EXS0eXWILdO36DHQQeku/ZcL6hz2fpg==, } dev: true @@ -89,7 +91,7 @@ packages: integrity: sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==, } dependencies: - '@types/node': 17.0.23 + '@types/node': 17.0.35 dev: true /asynckit/0.4.0: diff --git a/src/storage.ts b/src/storage.ts index 0aa863f..850fa30 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -8,6 +8,7 @@ var GlobalCache = '' const GuildCache = new Map() export function initGlobalCache(defaultValue?: string | JSONObject) { + defaultValue = structuredClone(defaultValue) if (GlobalCache.length === 0) { let data = readFile('global') GlobalCache = defaultJsonString(data, defaultValue) @@ -18,6 +19,7 @@ export function initGuildCache( guild: Guild, defaultValue?: string | JSONObject ) { + defaultValue = structuredClone(defaultValue) if (!GuildCache.has(guild.id)) { let data = readFile(guild) GuildCache.set(guild.id, defaultJsonString(data, defaultValue)) diff --git a/src/storageclass.ts b/src/storageclass.ts index 91344cf..6c9c4dd 100644 --- a/src/storageclass.ts +++ b/src/storageclass.ts @@ -24,6 +24,7 @@ export class StorageBase { } getJson(defaultValue: T): T { + defaultValue = structuredClone(defaultValue) try { let val = JSON.parse(this.val.get()) if (defaultValue) { diff --git a/src/util.ts b/src/util.ts index 43d014b..b9b9dc9 100644 --- a/src/util.ts +++ b/src/util.ts @@ -4,6 +4,7 @@ export type JSONObject = { } export function defaultJson(data: any, defaultValue: T): T { + defaultValue = structuredClone(defaultValue) if (typeof data !== 'object') { return defaultValue } @@ -22,6 +23,7 @@ export function defaultJsonString( data: string, defaultValue: string | JSONObject = '' ): string { + defaultValue = structuredClone(defaultValue) if (data.length === 0) { //no existing data, give the default value instead