Skip to content

Commit

Permalink
Apply linting rules throughout the TypeScript TxMonitor client.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed May 21, 2022
1 parent c52916a commit 215a03b
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 194 deletions.
162 changes: 81 additions & 81 deletions clients/TypeScript/packages/client/src/TxMonitor/TxMonitorClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { InteractionContext } from '../Connection'
import { ensureSocketIsOpen, eventEmitterToGenerator, safeJSON } from '../util'
import { Ogmios, MempoolSizeAndCapacity, Slot, TxAlonzo, TxId } from "@cardano-ogmios/schema"
import { baseRequest, send } from "../Request"
import { handleAwaitAcquireResponse } from "./awaitAcquire"
import { handleHasTxResponse } from "./hasTx"
import { handleNextTxResponse } from "./nextTx"
import { handleReleaseResponse } from "./release"
import { handleSizeAndCapacityResponse } from "./sizeAndCapacity"
import { Ogmios, MempoolSizeAndCapacity, Slot, TxAlonzo, TxId } from '@cardano-ogmios/schema'
import { baseRequest, send } from '../Request'
import { handleAwaitAcquireResponse } from './awaitAcquire'
import { handleHasTxResponse } from './hasTx'
import { handleNextTxResponse } from './nextTx'
import { handleReleaseResponse } from './release'
import { handleSizeAndCapacityResponse } from './sizeAndCapacity'

/**
* See also {@link createTxMonitorClient} for creating a client.
Expand All @@ -17,7 +17,7 @@ export interface TxMonitorClient {
context: InteractionContext
awaitAcquire: (args?: {}) => Promise<Slot>
hasTx: (id: TxId) => Promise<boolean>
nextTx: (args?: { fields?: "all" }) => Promise<TxId | TxAlonzo | null>
nextTx: (args?: { fields?: 'all' }) => Promise<TxId | TxAlonzo | null>
sizeAndCapacity: (args?: {}) => Promise<MempoolSizeAndCapacity>
release: (args?: {}) => Promise<void>
shutdown: () => Promise<void>
Expand All @@ -29,20 +29,20 @@ export interface TxMonitorClient {
* @category Constructor
**/
export const createTxMonitorClient = async (
context: InteractionContext
context: InteractionContext
): Promise<TxMonitorClient> => {
const { socket } = context
const { socket } = context

const matchAny = (data: string) => {
const json = safeJSON.parse(data)
const methods = ['AwaitAcquire', 'HasTx', 'NextTx', 'SizeAndCapacity', 'ReleaseMempool']
if (json.type !== 'jsonwsp/fault' && !methods.includes(json.methodname)) {
return null
}
return json
const matchAny = (data: string) => {
const json = safeJSON.parse(data)
const methods = ['AwaitAcquire', 'HasTx', 'NextTx', 'SizeAndCapacity', 'ReleaseMempool']
if (json.type !== 'jsonwsp/fault' && !methods.includes(json.methodname)) {
return null
}
return json
}

const response = eventEmitterToGenerator(socket, 'message', matchAny)() as
const response = eventEmitterToGenerator(socket, 'message', matchAny)() as
AsyncGenerator<
| Ogmios['AwaitAcquireResponse']
| Ogmios['HasTxResponse']
Expand All @@ -51,72 +51,72 @@ export const createTxMonitorClient = async (
| Ogmios['ReleaseMempoolTxResponse']
>

return Promise.resolve({
context,
awaitAcquire: (args?: {}) => {
ensureSocketIsOpen(socket)
return send<Slot>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'AwaitAcquire',
args: args
} as unknown as Ogmios['AwaitAcquire']))
return Promise.resolve({
context,
awaitAcquire: (args?: {}) => {
ensureSocketIsOpen(socket)
return send<Slot>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'AwaitAcquire',
args: args
} as unknown as Ogmios['AwaitAcquire']))

return handleAwaitAcquireResponse((await response.next()).value)
}, context)
},
hasTx: (id: TxId) => {
ensureSocketIsOpen(socket)
return send<boolean>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'HasTx',
args: { id }
} as unknown as Ogmios['HasTx']))
return handleAwaitAcquireResponse((await response.next()).value)
}, context)
},
hasTx: (id: TxId) => {
ensureSocketIsOpen(socket)
return send<boolean>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'HasTx',
args: { id }
} as unknown as Ogmios['HasTx']))

return handleHasTxResponse((await response.next()).value)
}, context)
},
nextTx: (args?: {fields?: "all"}) => {
ensureSocketIsOpen(socket)
return send<TxId | TxAlonzo | null>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'NextTx',
args: args
} as unknown as Ogmios['NextTx']))
return handleHasTxResponse((await response.next()).value)
}, context)
},
nextTx: (args?: {fields?: 'all'}) => {
ensureSocketIsOpen(socket)
return send<TxId | TxAlonzo | null>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'NextTx',
args: args
} as unknown as Ogmios['NextTx']))

return handleNextTxResponse((await response.next()).value, args)
}, context)
},
sizeAndCapacity: (args?: {}) => {
ensureSocketIsOpen(socket)
return send<MempoolSizeAndCapacity>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'SizeAndCapacity',
args: args
} as unknown as Ogmios['SizeAndCapacity']))
return handleNextTxResponse((await response.next()).value, args)
}, context)
},
sizeAndCapacity: (args?: {}) => {
ensureSocketIsOpen(socket)
return send<MempoolSizeAndCapacity>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'SizeAndCapacity',
args: args
} as unknown as Ogmios['SizeAndCapacity']))

return handleSizeAndCapacityResponse((await response.next()).value)
}, context)
},
release: (args?: {}) => {
ensureSocketIsOpen(socket)
return send<void>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'ReleaseMempool',
args: args
} as unknown as Ogmios['ReleaseMempool']))
return handleSizeAndCapacityResponse((await response.next()).value)
}, context)
},
release: (args?: {}) => {
ensureSocketIsOpen(socket)
return send<void>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'ReleaseMempool',
args: args
} as unknown as Ogmios['ReleaseMempool']))

return handleReleaseResponse((await response.next()).value)
}, context)
},
shutdown: () => new Promise(resolve => {
ensureSocketIsOpen(socket)
socket.once('close', resolve)
socket.close()
})
} as TxMonitorClient)
return handleReleaseResponse((await response.next()).value)
}, context)
},
shutdown: () => new Promise(resolve => {
ensureSocketIsOpen(socket)
socket.once('close', resolve)
socket.close()
})
} as TxMonitorClient)
}
40 changes: 20 additions & 20 deletions clients/TypeScript/packages/client/src/TxMonitor/awaitAcquire.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ogmios, AwaitAcquired, Slot } from "@cardano-ogmios/schema"
import { UnknownResultError } from "../errors";
import { Ogmios, AwaitAcquired, Slot } from '@cardano-ogmios/schema'
import { UnknownResultError } from '../errors'
import { InteractionContext } from '../Connection'
import { Query } from '../StateQuery'

Expand All @@ -9,43 +9,43 @@ import { Query } from '../StateQuery'
* @category TxMonitor
*/
export const awaitAcquire = (context: InteractionContext, args?: {}) =>
Query<
Query<
Ogmios['AwaitAcquire'],
Ogmios['AwaitAcquireResponse'],
Slot
>({
methodName: 'AwaitAcquire',
args: args
methodName: 'AwaitAcquire',
args: args
}, {
handler: (response, resolve, reject) => {
try {
resolve(handleAwaitAcquireResponse(response))
} catch (e) {
reject(e)
}
handler: (response, resolve, reject) => {
try {
resolve(handleAwaitAcquireResponse(response))
} catch (e) {
reject(e)
}
}
}, context)

/**
* @internal
*/
export const isAwaitAcquiredResult = (result: any): result is AwaitAcquired => {
if (typeof result !== 'object' || result === null) {
return false
}
if (typeof result !== 'object' || result === null) {
return false
}

return ('AwaitAcquired' in (result as AwaitAcquired) && typeof result.AwaitAcquired === 'object')
return ('AwaitAcquired' in (result as AwaitAcquired) && typeof result.AwaitAcquired === 'object')
}

/**
* @internal
*/
export const handleAwaitAcquireResponse = (response: Ogmios['AwaitAcquireResponse']): Slot => {
const { result } = response
const { result } = response

if (isAwaitAcquiredResult(result)) {
return result.AwaitAcquired.slot
}
if (isAwaitAcquiredResult(result)) {
return result.AwaitAcquired.slot
}

throw new UnknownResultError(response)
throw new UnknownResultError(response)
}
24 changes: 12 additions & 12 deletions clients/TypeScript/packages/client/src/TxMonitor/hasTx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ogmios, TxId } from "@cardano-ogmios/schema"
import { UnknownResultError } from "../errors";
import { Ogmios, TxId } from '@cardano-ogmios/schema'
import { UnknownResultError } from '../errors'
import { InteractionContext } from '../Connection'
import { Query } from '../StateQuery'

Expand All @@ -9,28 +9,28 @@ import { Query } from '../StateQuery'
* @category TxMonitor
*/
export const hasTx = (context: InteractionContext, id: TxId) =>
Query<
Query<
Ogmios['HasTx'],
Ogmios['HasTxResponse'],
boolean
>({
methodName: 'HasTx',
args: { id }
methodName: 'HasTx',
args: { id }
}, {
handler: (response, resolve, reject) => {
try {
resolve(handleHasTxResponse(response))
} catch (e) {
reject(e)
}
handler: (response, resolve, reject) => {
try {
resolve(handleHasTxResponse(response))
} catch (e) {
reject(e)
}
}
}, context)

/**
* @internal
*/
export const isHasTxResult = (result: any): result is boolean =>
(typeof result === 'boolean')
(typeof result === 'boolean')

/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion clients/TypeScript/packages/client/src/TxMonitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export * from './nextTx'
export * from './sizeAndCapacity'
export * from './release'
export * from './TxMonitorClient'
export { UnknownResultError } from '../errors'
export { UnknownResultError } from '../errors'
Loading

0 comments on commit 215a03b

Please sign in to comment.