feat: Add typescript types to subscribe (#1576)

feat: Add typescript types to subscribe (#1576)
This commit is contained in:
Nathan Nichols
2021-09-07 10:11:25 -07:00
committed by Mayukha Vadari
parent a996fafe79
commit c8a2a6690b
15 changed files with 513 additions and 52 deletions

View File

@@ -58,7 +58,11 @@ export default class BroadcastClient extends Client {
...Object.getOwnPropertyNames(Object.getPrototypeOf(firstClient)),
)
for (const name of methods) {
if (typeof firstClient[name] === 'function' && name !== 'constructor') {
if (
typeof firstClient[name] === 'function' &&
name !== 'constructor' &&
name !== 'on'
) {
methodNames.push(name)
}
}

View File

@@ -94,6 +94,16 @@ import {
PingResponse,
RandomRequest,
RandomResponse,
LedgerStream,
ValidationStream,
TransactionStream,
PathFindStream,
PeerStatusStream,
ConsensusStream,
SubscribeRequest,
SubscribeResponse,
UnsubscribeRequest,
UnsubscribeResponse,
} from '../models/methods'
import { BaseRequest, BaseResponse } from '../models/methods/baseMethod'
import prepareCheckCancel from '../transaction/check-cancel'
@@ -211,6 +221,34 @@ class Client extends EventEmitter {
this.emit('error', errorCode, errorMessage, data)
})
this.connection.on('ledgerClosed', (ledger) => {
this.emit('ledgerClosed', ledger)
})
this.connection.on('transaction', (tx) => {
this.emit('transaction', tx)
})
this.connection.on('validationReceived', (validation) => {
this.emit('validationReceived', validation)
})
this.connection.on('manifestReceived', (manifest) => {
this.emit('manifestReceived', manifest)
})
this.connection.on('peerStatusChange', (status) => {
this.emit('peerStatusChange', status)
})
this.connection.on('consensusPhase', (consensus) => {
this.emit('consensusPhase', consensus)
})
this.connection.on('path_find', (path) => {
this.emit('path_find', path)
})
this.connection.on('connected', () => {
this.emit('connected')
})
@@ -282,6 +320,8 @@ class Client extends EventEmitter {
public async request(
r: SubmitMultisignedRequest,
): Promise<SubmitMultisignedResponse>
public request(r: SubscribeRequest): Promise<SubscribeResponse>
public request(r: UnsubscribeRequest): Promise<UnsubscribeResponse>
public async request(
r: TransactionEntryRequest,
): Promise<TransactionEntryResponse>
@@ -351,6 +391,23 @@ class Client extends EventEmitter {
return this.connection.request(nextPageRequest) as unknown as U
}
public on(event: 'ledgerClosed', listener: (ledger: LedgerStream) => void)
public on(
event: 'validationReceived',
listener: (validation: ValidationStream) => void,
)
public on(event: 'transaction', listener: (tx: TransactionStream) => void)
public on(
event: 'peerStatusChange',
listener: (status: PeerStatusStream) => void,
)
public on(event: 'consensusPhase', listener: (phase: ConsensusStream) => void)
public on(event: 'path_find', listener: (path: PathFindStream) => void)
public on(event: string, listener: (...args: any[]) => void)
public on(eventName: string, listener: (...args: any[]) => void) {
return super.on(eventName, listener)
}
/**
* Prepare a transaction.
*

View File

@@ -46,6 +46,7 @@ import {
ConsensusStream,
LedgerStream,
OrderBookStream,
PathFindStream,
PeerStatusStream,
Stream,
SubscribeRequest,
@@ -203,6 +204,7 @@ export {
LedgerStream,
ValidationStream,
TransactionStream,
PathFindStream,
PeerStatusStream,
OrderBookStream,
ConsensusStream,

View File

@@ -1,9 +1,9 @@
import { OfferCreateTransaction } from '../../common/types/objects'
import { Currency, StreamType } from '../common'
import type { Amount, Currency, Path, StreamType } from '../common'
import { Transaction } from '../transactions'
import TransactionMetadata from '../transactions/metadata'
import { BaseRequest, BaseResponse } from './baseMethod'
import type { BaseRequest, BaseResponse } from './baseMethod'
interface Book {
taker_gets: Currency
@@ -111,10 +111,25 @@ export interface ConsensusStream extends BaseStream {
consensus: 'open' | 'establish' | 'accepted'
}
export interface PathFindStream extends BaseStream {
type: 'path_find'
source_account: string
destination_account: string
destination_amount: Amount
full_reply: boolean
id: number | string
send_max?: Amount
alternatives: {
paths_computed: Path[]
source_amount: Amount
}
}
export type Stream =
| LedgerStream
| ValidationStream
| TransactionStream
| PathFindStream
| PeerStatusStream
| OrderBookStream
| ConsensusStream