8000 feat(UserStore): implement store() · cAttte/pastebin.js@873038e · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 873038e

Browse files
committed
feat(UserStore): implement store()
1 parent 8ad6f66 commit 873038e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/struct/stores/UserStore.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ export default clas 8000 s UserStore extends Map<string, User> {
2424
for (const [k, v] of entries) this.set(k, v)
2525
}
2626

27+
/**
28+
* Store and get a user by their username.
29+
* @param data The data obtained from the API
30+
* @param username The user's username.
31+
*/
32+
store(
33+
data: any,
34+
username: string = data.username,
35+
Type: typeof User | typeof ClientUser = User
36+
): User {
37+
let existing = this.get(username)
38+
if (existing) {
39+
existing._apply(data)
40+
} else {
41+
existing = new Type(this.client, data)
42+
this.set(username, existing)
43+
}
44+
return existing
45+
}
46+
2747
/**
2848
* Fetch a user by their username, and store them in the cache.
2949
* @param username The user's username
@@ -57,8 +77,7 @@ export default class UserStore extends Map<string, User> {
5777
UserConstructor = ClientUser
5878
}
5979

60-
const user = new UserConstructor(this.client, data)
61-
this.set(username, user)
80+
const user = this.store(data, data.username, UserConstructor)
6281
return user
6382
}
6483
}

0 commit comments

Comments
 (0)
0