-
Notifications
You must be signed in to change notification settings - Fork 6
/
profile.ts
103 lines (90 loc) · 2.13 KB
/
profile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { $ } from '../$';
import { inspect } from '../inspect';
import { proto } from '../utils';
let init = false;
function _init() {
if (init) return;
addEventListener('unload', $.accountInitialize());
init = true;
}
export type ProfileUid = [bigint, bigint];
// The currently selected profile
let p: Profile | null | undefined;
/**
* Represents a user profile that exists on the system.
*/
export class Profile {
/**
* The unique ID of the profile, represented as an array of two `bigint` values.
*/
declare readonly uid: ProfileUid;
/**
* The human readable nickname of the profile.
*/
declare readonly nickname: string;
/**
* The raw JPEG data for the profile image. Can be decoded with the `Image` class.
*/
declare readonly image: ArrayBuffer;
/**
* Creates a new `Profile` instance from the given profile UID.
*
* @example
*
* ```typescript
* const profile = new Switch.Profile([
* 0x10005d4864d166b7n,
* 0x965b8cb028cd8a81n,
* ]);
* console.log(profile.nickname);
* ```
*/
constructor(uid: ProfileUid) {
_init();
return proto($.accountProfileNew(uid), Profile);
}
static get current(): Profile | null {
if (typeof p !== 'undefined') return p;
_init();
p = $.accountCurrentProfile();
return p;
}
static set current(v: Profile | null) {
p = v;
}
/**
* Shows the user selection interface and returns a {@link Profile}
* instance representing the user that was selected.
*
* @note This function blocks the event loop until the user has made their selection.
*/
static select() {
_init();
const p = $.accountSelectProfile();
if (p) proto(p, Profile);
return p;
}
/**
* Can be used as an iterator to retrieve the list of user profiles.
*
* @example
*
* ```typescript
* for (const profile of Switch.Profile) {
* console.log(profile.nickname);
* }
* ```
*/
static *[Symbol.iterator]() {
_init();
const pr = $.accountProfiles();
for (const p of pr) {
yield proto(p, Profile);
}
}
}
$.accountProfileInit(Profile);
Object.defineProperty(Profile.prototype, inspect.keys, {
enumerable: false,
value: () => ['uid', 'nickname', 'image'],
});