@@ -4,10 +4,18 @@ import { BehaviorSubject } from "rxjs";
4
4
import { IAccount , IAccountConstructor , SerializedAccount } from "./types.js" ;
5
5
6
6
export class AccountManager < Metadata extends unknown = any > {
7
- active = new BehaviorSubject < IAccount < any , any , Metadata > | null > ( null ) ;
8
- accounts = new BehaviorSubject < Record < string , IAccount < any , any , Metadata > > > ( { } ) ;
9
7
types = new Map < string , IAccountConstructor < any , any , Metadata > > ( ) ;
10
8
9
+ active$ = new BehaviorSubject < IAccount < any , any , Metadata > | null > ( null ) ;
10
+ get active ( ) {
11
+ return this . active$ . value ;
12
+ }
13
+
14
+ accounts$ = new BehaviorSubject < IAccount < any , any , Metadata > [ ] > ( [ ] ) ;
15
+ get accounts ( ) {
16
+ return this . accounts$ . value ;
17
+ }
18
+
11
19
// Account type CRUD
12
20
13
21
/** Add account type class */
@@ -28,37 +36,35 @@ export class AccountManager<Metadata extends unknown = any> {
28
36
getAccount < Signer extends Nip07Interface > (
29
37
id : string | IAccount < Signer , any , Metadata > ,
30
38
) : IAccount < Signer , any , Metadata > | undefined {
31
- if ( typeof id === "string" ) return this . accounts . value [ id ] ;
32
- else if ( this . accounts . value [ id . id ] ) return id ;
39
+ if ( typeof id === "string" ) return this . accounts$ . value . find ( ( a ) => a . id === id ) ;
40
+ else if ( this . accounts$ . value . includes ( id ) ) return id ;
33
41
else return undefined ;
34
42
}
35
43
36
44
/** Return the first account for a pubkey */
37
45
getAccountForPubkey ( pubkey : string ) : IAccount < any , any , Metadata > | undefined {
38
- return Object . values ( this . accounts . value ) . find ( ( account ) => account . pubkey === pubkey ) ;
46
+ return Object . values ( this . accounts$ . value ) . find ( ( account ) => account . pubkey === pubkey ) ;
39
47
}
40
48
41
49
/** Returns all accounts for a pubkey */
42
50
getAccountsForPubkey ( pubkey : string ) : IAccount < any , any , Metadata > [ ] {
43
- return Object . values ( this . accounts . value ) . filter ( ( account ) => account . pubkey === pubkey ) ;
51
+ return Object . values ( this . accounts$ . value ) . filter ( ( account ) => account . pubkey === pubkey ) ;
44
52
}
45
53
46
54
/** adds an account to the manager */
47
55
addAccount ( account : IAccount < any , any , Metadata > ) {
48
56
if ( this . getAccount ( account . id ) ) return ;
49
57
50
- this . accounts . next ( {
51
- ...this . accounts . value ,
58
+ this . accounts$ . next ( {
59
+ ...this . accounts$ . value ,
52
60
[ account . id ] : account ,
53
61
} ) ;
54
62
}
55
63
56
64
/** Removes an account from the manager */
57
65
removeAccount ( account : string | IAccount < any , any , Metadata > ) {
58
66
const id = typeof account === "string" ? account : account . id ;
59
- const next = { ...this . accounts . value } ;
60
- delete next [ id ] ;
61
- this . accounts . next ( next ) ;
67
+ this . accounts$ . next ( this . accounts$ . value . filter ( ( a ) => a . id !== id ) ) ;
62
68
}
63
69
64
70
/** Replaces an account with another */
@@ -67,7 +73,7 @@ export class AccountManager<Metadata extends unknown = any> {
67
73
68
74
// if the old account was active, switch to the new one
69
75
const id = typeof account === "string" ? account : account . id ;
70
- if ( this . active . value ?. id === id ) this . setActive ( account ) ;
76
+ if ( this . active$ . value ?. id === id ) this . setActive ( account ) ;
71
77
72
78
this . removeAccount ( old ) ;
73
79
}
@@ -76,20 +82,20 @@ export class AccountManager<Metadata extends unknown = any> {
76
82
77
83
/** Returns the currently active account */
78
84
getActive ( ) {
79
- return this . active . value ;
85
+ return this . active$ . value ;
80
86
}
81
87
/** Sets the currently active account */
82
88
setActive ( id : string | IAccount < any , any , Metadata > ) {
83
89
const account = this . getAccount ( id ) ;
84
90
if ( ! account ) throw new Error ( "Cant find account with that ID" ) ;
85
91
86
- if ( this . active . value ?. id !== account . id ) {
87
- this . active . next ( account ) ;
92
+ if ( this . active$ . value ?. id !== account . id ) {
93
+ this . active$ . next ( account ) ;
88
94
}
89
95
}
90
96
/** Clears the currently active account */
91
97
clearActive ( ) {
92
- this . active . next ( null ) ;
98
+ this . active$ . next ( null ) ;
93
99
}
94
100
95
101
// Metadata CRUD
@@ -112,7 +118,7 @@ export class AccountManager<Metadata extends unknown = any> {
112
118
113
119
/** Returns an array of serialized accounts */
114
120
toJSON ( ) : SerializedAccount < any , Metadata > [ ] {
115
- return Array . from ( Object . values ( this . accounts ) ) . map ( ( account ) => account . toJSON ( ) ) ;
121
+ return Array . from ( Object . values ( this . accounts$ ) ) . map ( ( account ) => account . toJSON ( ) ) ;
116
122
}
117
123
118
124
/**
0 commit comments