Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat: add extension status event messaging RDT-158
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Aug 2, 2023
1 parent 811a00d commit c8bd2c8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
23 changes: 22 additions & 1 deletion lib/IO/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,35 @@ export const WalletInteractionResponse = union([
WalletInteractionFailureResponse,
])

export const ExtensionInteraction = object({
interactionId: string(),
discriminator: literal('extensionStatus'),
})

export type ExtensionInteraction = z.infer<typeof ExtensionInteraction>

export const messageLifeCycleEventType = {
extensionStatus: 'extensionStatus',
receivedByExtension: 'receivedByExtension',
receivedByWallet: 'receivedByWallet',
requestCancelSuccess: 'requestCancelSuccess',
requestCancelFail: 'requestCancelFail',
} as const

export type MessageLifeCycleEvent = z.infer<typeof MessageLifeCycleEvent>
export const MessageLifeCycleExtensionStatusEvent = object({
eventType: literal(messageLifeCycleEventType.extensionStatus),
interactionId: string(),
isWalletLinked: boolean(),
isExtensionAvailable: boolean(),
})

export type MessageLifeCycleExtensionStatusEvent = z.infer<
typeof MessageLifeCycleExtensionStatusEvent
>

export const MessageLifeCycleEvent = object({
eventType: union([
literal(messageLifeCycleEventType.extensionStatus),
literal(messageLifeCycleEventType.receivedByExtension),
literal(messageLifeCycleEventType.receivedByWallet),
literal(messageLifeCycleEventType.requestCancelSuccess),
Expand All @@ -336,6 +355,8 @@ export const MessageLifeCycleEvent = object({
interactionId: string(),
})

export type MessageLifeCycleEvent = z.infer<typeof MessageLifeCycleEvent>

export type IncomingMessage = z.infer<typeof IncomingMessage>
const IncomingMessage = union([
MessageLifeCycleEvent,
Expand Down
36 changes: 36 additions & 0 deletions lib/connector-extension/connector-extension-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppLogger } from '../helpers/logger'
import {
CallbackFns,
IncomingMessage,
MessageLifeCycleExtensionStatusEvent,
WalletInteraction,
WalletInteractionSuccessResponse,
eventType,
Expand All @@ -18,7 +19,9 @@ import {
map,
merge,
of,
race,
share,
switchMap,
takeUntil,
tap,
timer,
Expand Down Expand Up @@ -191,11 +194,44 @@ export const ConnectorExtensionClient = (
)
}

const extensionStatusEvent$ = subjects.messageLifeCycleEventSubject.pipe(
filter(
(event): event is MessageLifeCycleExtensionStatusEvent =>
event.eventType === 'extensionStatus'
)
)

return {
send: sendWalletInteraction,
destroy: () => {
subscription.unsubscribe()
removeEventListener(eventType.incomingMessage, handleIncomingMessage)
},
extensionStatus$: of(true).pipe(
tap(() => {
subjects.outgoingMessageSubject.next({
interactionId: crypto.randomUUID(),
discriminator: 'extensionStatus',
})
}),
switchMap(() =>
race(
extensionStatusEvent$,
merge(
extensionStatusEvent$,
timer(config.extensionDetectionTime).pipe(
map(
() =>
({
eventType: 'extensionStatus',
isWalletLinked: false,
isExtensionAvailable: false,
} as MessageLifeCycleExtensionStatusEvent)
)
)
)
)
)
),
}
}
10 changes: 8 additions & 2 deletions lib/connector-extension/subjects.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Subject } from 'rxjs'
import {
ExtensionInteraction,
MessageLifeCycleEvent,
MessageLifeCycleExtensionStatusEvent,
WalletInteraction,
WalletInteractionResponse,
} from '../IO/schemas'

export type Subjects = ReturnType<typeof Subjects>

export const Subjects = () => ({
outgoingMessageSubject: new Subject<WalletInteraction>(),
outgoingMessageSubject: new Subject<
WalletInteraction | ExtensionInteraction
>(),
incomingMessageSubject: new Subject<
MessageLifeCycleEvent | WalletInteractionResponse
| MessageLifeCycleEvent
| MessageLifeCycleExtensionStatusEvent
| WalletInteractionResponse
>(),
responseSubject: new Subject<WalletInteractionResponse>(),
messageLifeCycleEventSubject: new Subject<MessageLifeCycleEvent>(),
Expand Down
1 change: 1 addition & 0 deletions lib/create-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ export const createMethods = (
return {
request,
sendTransaction,
extensionStatus$: connectorExtensionClient.extensionStatus$,
}
}

0 comments on commit c8bd2c8

Please sign in to comment.