Skip to content

Commit b192261

Browse files
authored
QuickDB (#376)
* QuickDB * QuickDB fixed some bug * QuickDB fixed some bug
1 parent 16cad09 commit b192261

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

scripts/quick-db/index.js

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ class QuickDB {
2424
}
2525

2626
has(key) {
27-
return !!GET.call(world, `${this.#identifier}${key}`);
27+
return !!(
28+
GET.call(world, `${this.#identifier}${key}`) &&
29+
GET.call(world, `${this.#identifier}${key}`) !== undefined
30+
);
2831
}
2932

3033
get(key) {
@@ -46,38 +49,55 @@ class QuickDB {
4649
}
4750

4851
keys() {
49-
return this.#UIDX("keys");
52+
return Array.from(this.#UIDX("keys"));
5053
}
5154

5255
values() {
53-
return this.#UIDX("values");
56+
return Array.from(this.#UIDX("values"));
5457
}
5558

5659
entries() {
57-
return this.#UIDX("entries");
60+
return Array.from(this.#UIDX("entries"));
5861
}
5962

6063
#UIDX(type) {
61-
const ids = IDS.call(world);
62-
const result = [];
63-
64-
for (const id of ids) {
65-
if (!id.startsWith(this.#identifier)) continue;
66-
67-
const key = id.replace(this.#identifier, "");
68-
if (type === "keys") {
69-
result.push(key);
70-
} else if (type === "values") {
71-
const value = JSON.parse(this.get(key));
72-
result.push(value);
73-
} else if (type === "entries") {
74-
const value = JSON.parse(this.get(key));
75-
result.push([key, value]);
64+
const ids = this.getIds();
65+
let u_idx = 0;
66+
const len = ids.length;
67+
68+
return function* () {
69+
while (u_idx < len) {
70+
const id = ids[u_idx];
71+
const key = id.split(this.#identifier)[1];
72+
const value = this.get(key);
73+
switch (type) {
74+
case "key":
75+
yield key;
76+
break;
77+
case "value":
78+
yield this.has(key) ? JSON.parse(value) : undefined;
79+
break;
80+
case "entries":
81+
yield [key, JSON.parse(value)];
82+
break;
83+
}
84+
u_idx++;
7685
}
77-
}
86+
}.bind(this)();
87+
}
7888

79-
return result;
89+
getIds() {
90+
return world
91+
.getDynamicPropertyIds()
92+
.filter((id) => id.startsWith(this.#identifier));
93+
}
94+
95+
clear() {
96+
for (const id of this.getIds()) {
97+
this.delete(id.replace(this.#identifier,""));
98+
}
8099
}
81100
}
82101

83102
export default QuickDB;
103+
export { QuickDB };

0 commit comments

Comments
 (0)