2020-12-19 08:10:21 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-floating-promises */
|
|
|
|
|
2020-12-18 06:18:52 +00:00
|
|
|
// SpookVooper API - modules/TransactionHub.ts
|
2021-06-15 15:36:22 +00:00
|
|
|
// Written by Quinn Lane - https://quinnlane.dev/
|
2020-12-18 06:18:52 +00:00
|
|
|
|
2020-12-19 08:10:21 +00:00
|
|
|
import { Observable } from 'rxjs'
|
2020-12-18 06:18:52 +00:00
|
|
|
import { HubConnectionBuilder, LogLevel } from '@microsoft/signalr'
|
|
|
|
import { ReturnedTransaction } from './interfaces/Interfaces'
|
|
|
|
|
|
|
|
const URI = 'https://spookvooper.com/transactionHub'
|
2020-12-19 08:10:21 +00:00
|
|
|
const retryTime = 5
|
|
|
|
const retryTimeMS = retryTime * 1000
|
|
|
|
|
2020-12-18 06:18:52 +00:00
|
|
|
class TransactionHub {
|
|
|
|
private readonly connection = new HubConnectionBuilder()
|
|
|
|
.withUrl(URI)
|
|
|
|
.withAutomaticReconnect()
|
|
|
|
.configureLogging(LogLevel.Information)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
private val: ReturnedTransaction
|
|
|
|
|
|
|
|
public fromAccount: string
|
|
|
|
public toAccount: string
|
|
|
|
|
2020-12-23 06:12:43 +00:00
|
|
|
public fromType: string
|
|
|
|
public toType: string
|
|
|
|
|
2020-12-19 08:10:21 +00:00
|
|
|
public event = new Observable((observer) => {
|
2020-12-18 06:18:52 +00:00
|
|
|
this.connection.on('NotifyTransaction', (recieved: string) => {
|
|
|
|
this.val = JSON.parse(recieved)
|
|
|
|
this.fromAccount = this.val.FromAccount
|
|
|
|
this.toAccount = this.val.ToAccount
|
2020-12-19 08:10:21 +00:00
|
|
|
|
2020-12-24 08:03:28 +00:00
|
|
|
if (this.fromAccount.startsWith('u-')) {
|
|
|
|
this.fromType = 'user'
|
|
|
|
} else if (this.fromAccount.startsWith('g-')) {
|
|
|
|
this.fromType = 'group'
|
|
|
|
} else {
|
|
|
|
this.fromType = 'Error'
|
|
|
|
}
|
|
|
|
if (this.toAccount.startsWith('u-')) {
|
|
|
|
this.toType = 'user'
|
|
|
|
} else if (this.toAccount.startsWith('g-')) {
|
|
|
|
this.toType = 'group'
|
|
|
|
} else {
|
|
|
|
this.toType = 'Error'
|
|
|
|
}
|
|
|
|
|
2020-12-19 08:10:21 +00:00
|
|
|
observer.next(this.val)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
constructor () {
|
2020-12-20 07:43:51 +00:00
|
|
|
this.connection.onclose((e) => {
|
2020-12-19 08:10:21 +00:00
|
|
|
this.onClosed(e)
|
2020-12-18 06:18:52 +00:00
|
|
|
})
|
2020-12-20 07:43:51 +00:00
|
|
|
|
|
|
|
this.start()
|
2020-12-18 06:18:52 +00:00
|
|
|
}
|
2020-12-19 08:10:21 +00:00
|
|
|
|
|
|
|
public async start (): Promise<void> {
|
2020-12-20 07:43:51 +00:00
|
|
|
console.log(`TransactionHub: Starting connection between local and ${URI}`)
|
2020-12-19 08:10:21 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.connection.start()
|
2020-12-20 07:43:51 +00:00
|
|
|
console.log('TransactionHub: Connected!')
|
2020-12-19 08:10:21 +00:00
|
|
|
} catch (e) {
|
2020-12-20 07:43:51 +00:00
|
|
|
console.error('TransactionHub Error: Connection failed while trying to establish a connection\n', e)
|
|
|
|
console.log(`TransactionHub: Retrying in ${retryTime} seconds`)
|
2020-12-19 08:10:21 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
this.start()
|
|
|
|
}, retryTimeMS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async onClosed (e: any): Promise<void> {
|
2020-12-20 07:43:51 +00:00
|
|
|
console.error('TransactionHub Error: Connection closed unexpectedly', e)
|
2020-12-19 08:10:21 +00:00
|
|
|
await this.start()
|
|
|
|
}
|
2020-12-18 06:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default TransactionHub
|