1
- import { ResultAsync , err , errAsync , ok , okAsync } from 'neverthrow'
2
- import { Subscription , filter , switchMap , tap } from 'rxjs'
1
+ import { ResultAsync , err , ok } from 'neverthrow'
2
+ import { Subscription , filter , switchMap } from 'rxjs'
3
3
import { EncryptionClient } from '../../encryption'
4
4
import {
5
5
ActiveSession ,
6
6
PendingSession ,
7
+ Session ,
7
8
SessionClient ,
8
9
} from '../../session/session'
9
10
import type {
@@ -21,6 +22,8 @@ import { Curve25519 } from '../../crypto'
21
22
import { RadixConnectRelayApi } from './api'
22
23
import { RequestItem } from 'radix-connect-common'
23
24
25
+ type SessionChangeEvent = ( session : Session ) => void
26
+
24
27
export type RadixConnectRelayClient = ReturnType < typeof RadixConnectRelayClient >
25
28
export const RadixConnectRelayClient = ( input : {
26
29
baseUrl : string
@@ -41,6 +44,16 @@ export const RadixConnectRelayClient = (input: {
41
44
42
45
const encryptionClient = providers ?. encryptionClient ?? EncryptionClient ( )
43
46
47
+ const sessionChangeListeners : SessionChangeEvent [ ] = [ ]
48
+
49
+ const addSessionChangeListener = ( listener : SessionChangeEvent ) => {
50
+ sessionChangeListeners . push ( listener )
51
+ }
52
+
53
+ const emitChangesToListeners = ( session : Session ) => {
54
+ sessionChangeListeners . forEach ( ( listener ) => listener ( session ) )
55
+ }
56
+
44
57
const deepLinkClient =
45
58
providers ?. deepLinkClient ??
46
59
DeepLinkClient ( {
@@ -173,17 +186,18 @@ export const RadixConnectRelayClient = (input: {
173
186
. convertToActiveSession ( sessionId , walletPublicKey )
174
187
. mapErr ( ( ) => SdkError ( 'FailedToUpdateSession' , '' ) ) ,
175
188
)
176
- . andThen ( ( activeSession ) =>
177
- requestItemClient
189
+ . andThen ( ( activeSession ) => {
190
+ emitChangesToListeners ( activeSession )
191
+ return requestItemClient
178
192
. getPendingItems ( )
179
193
. mapErr ( ( ) => SdkError ( 'FailedToReadPendingItems' , '' ) )
180
194
. map ( ( items ) => {
181
195
const [ item ] = items . filter ( ( item ) => ! item . sentToWallet )
182
196
return item
183
197
} )
184
198
185
- . map ( ( item ) => ( { activeSession, pendingItem : item } ) ) ,
186
- )
199
+ . map ( ( item ) => ( { activeSession, pendingItem : item } ) )
200
+ } )
187
201
. andThen (
188
202
( {
189
203
activeSession,
@@ -327,8 +341,10 @@ export const RadixConnectRelayClient = (input: {
327
341
deepLinkClient . handleWalletCallback ( )
328
342
329
343
return {
344
+ id : 'radix-connect-relay' as const ,
330
345
isSupported : ( ) => isMobile ( ) ,
331
346
send : sendToWallet ,
347
+ addSessionChangeListener,
332
348
disconnect : ( ) => { } ,
333
349
destroy : ( ) => {
334
350
subscriptions . unsubscribe ( )
0 commit comments