Skip to content

Commit

Permalink
Merge pull request #769 from TokenScript/feat/event-arg-generics
Browse files Browse the repository at this point in the history
feat: Add generic argument types to "on" and "eventSender" functions
  • Loading branch information
nicktaras authored Jul 26, 2023
2 parents e401cb0 + c9097ae commit 19dddf3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 48 deletions.
43 changes: 9 additions & 34 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,16 @@ import {
createIssuerHashArray,
} from '../utils'
import { getNftCollection, getNftTokens } from '../utils/token/nftProvider'
import { Authenticator } from '@tokenscript/attestation'
import { TokenStore } from './tokenStore'
import {
OffChainTokenConfig,
OnChainTokenConfig,
AuthenticateInterface,
NegotiationInterface,
TokenNegotiatorEvents,
EventSenderTokensSelected,
EventSenderConnectedWallet,
EventSenderDisconnectedWallet,
EventSenderError,
EventSenderViewChanged,
OnChainIssuer,
EventSenderViewLoaded,
EventSenderOpenedOverlay,
EventSenderClosedOverlay,
EventSenderTokensRefreshed,
EventSenderTokensLoaded,
OutletTokenResult,
MultiTokenInterface,
TokenNegotiatorEventsArgs,
} from './interface'
import { SignedUNChallenge } from './auth/signedUNChallenge'
import { TicketZKProof } from './auth/ticketZKProof'
Expand All @@ -49,10 +38,6 @@ import { AttestationIdClient } from '../outlet/attestationIdClient'

if (typeof window !== 'undefined') window.tn = { VERSION }

interface EventSenderTokens {
data: any[]
}

declare global {
interface Window {
tokenToggleSelection: any
Expand Down Expand Up @@ -117,7 +102,7 @@ export class Client {
private web3WalletProvider: Web3WalletProvider
private messaging: Messaging
protected ui: UiInterface
private clientCallBackEvents: {} = {}
private clientCallBackEvents: { [key: string]: (data: any) => Promise<void> | void } = {}
protected tokenStore: TokenStore
private uiUpdateCallbacks: { [type in UIUpdateEventType] } = {
[UIUpdateEventType.ISSUERS_LOADING]: undefined,
Expand Down Expand Up @@ -1025,22 +1010,8 @@ export class Client {
this.eventSender('token-proof', { issuer, error: err, data: null })
}

async eventSender(eventName: 'loaded', data: EventSenderViewLoaded)
async eventSender(eventName: 'tokens-refreshed', data: EventSenderTokensRefreshed)
async eventSender(eventName: 'closed-overlay', data: EventSenderClosedOverlay)
async eventSender(eventName: 'opened-overlay', data: EventSenderOpenedOverlay)
async eventSender(eventName: 'view-changed', data: EventSenderViewChanged)
async eventSender(eventName: 'tokens', data: EventSenderTokens)
async eventSender(eventName: 'token-proof', data: any)
async eventSender(eventName: 'tokens-selected', data: EventSenderTokensSelected)
async eventSender(eventName: 'tokens-loaded', data: EventSenderTokensLoaded)
async eventSender(eventName: 'connected-wallet', data: EventSenderConnectedWallet)
async eventSender(eventName: 'disconnected-wallet', data: EventSenderDisconnectedWallet)
async eventSender(eventName: 'network-change', data: string)
async eventSender(eventName: 'error', data: EventSenderError)

async eventSender(eventName: TokenNegotiatorEvents, data: any) {
await Promise.resolve(this.on(eventName, null, data))
async eventSender<T extends keyof TokenNegotiatorEventsArgs, R extends TokenNegotiatorEventsArgs[T]>(eventName: T, data: R) {
await Promise.resolve(this.on(eventName, undefined, data))
}

getOutletConfigForCurrentOrigin(origin: string = window.location.origin) {
Expand Down Expand Up @@ -1106,7 +1077,11 @@ export class Client {
}
}

on(type: TokenNegotiatorEvents, callback?: any, data?: any) {
on<T extends keyof TokenNegotiatorEventsArgs, R extends (data: TokenNegotiatorEventsArgs[T]) => Promise<void> | void>(
type: T,
callback?: R,
data?: TokenNegotiatorEventsArgs[T],
) {
requiredParams(type, 'Event type is not defined')

if ((type === 'tokens' || type === 'tokens-selected') && callback) {
Expand Down
33 changes: 19 additions & 14 deletions src/client/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,21 @@ export interface AuthenticateInterface {
blockchain?: string
}

export type TokenNegotiatorEvents =
| 'view-changed'
| 'tokens-refreshed'
| 'opened-overlay'
| 'closed-overlay'
| 'loaded'
| 'token-proof'
| 'connected-wallet'
| 'disconnected-wallet'
| 'tokens-selected'
| 'tokens'
| 'tokens-loaded'
| 'network-change'
| 'error'
export interface TokenNegotiatorEventsArgs {
'view-changed': EventSenderViewChanged
'tokens-refreshed': EventSenderTokensRefreshed
'opened-overlay': EventSenderOpenedOverlay
'closed-overlay': EventSenderClosedOverlay
loaded: EventSenderViewLoaded
'token-proof': any // EventSenderTokenProof // TODO: Update this
'connected-wallet': EventSenderConnectedWallet
'disconnected-wallet': EventSenderDisconnectedWallet
'tokens-selected': EventSenderTokensSelected
tokens: EventSenderTokens
'tokens-loaded': EventSenderTokensLoaded
'network-change': string
error: EventSenderError
}

export interface EventSenderViewLoaded {
data: any
Expand Down Expand Up @@ -139,6 +140,10 @@ export interface EventSenderTokensSelected {
selectedTokens: Object
}

export interface EventSenderTokens {
data: any[]
}

export interface EventSenderTokensLoaded {
loadedCollections: number
}
Expand Down

0 comments on commit 19dddf3

Please sign in to comment.