defaultValue = structuredClone(defaultValue)
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
node_modules
|
||||
lib
|
||||
examples/*.js
|
||||
data
|
||||
@@ -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',
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
{ "a": "aaaaa", "b": "bbbbb", "c": "ccccc" }
|
||||
33
examples/test.ts
Normal file
33
examples/test.ts
Normal file
@@ -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)
|
||||
@@ -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"
|
||||
|
||||
12
pnpm-lock.yaml
generated
12
pnpm-lock.yaml
generated
@@ -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:
|
||||
|
||||
@@ -8,6 +8,7 @@ var GlobalCache = ''
|
||||
const GuildCache = new Map<string, string>()
|
||||
|
||||
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))
|
||||
|
||||
@@ -24,6 +24,7 @@ export class StorageBase {
|
||||
}
|
||||
|
||||
getJson<T = JSONObject>(defaultValue: T): T {
|
||||
defaultValue = structuredClone(defaultValue)
|
||||
try {
|
||||
let val = JSON.parse(this.val.get())
|
||||
if (defaultValue) {
|
||||
|
||||
@@ -4,6 +4,7 @@ export type JSONObject<Value = JSONDataTypes, Key extends string = string> = {
|
||||
}
|
||||
|
||||
export function defaultJson<T>(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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user