Skip to content

Commit

Permalink
refactor: rename ensureSocket to send, moving into Request module
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyslbw committed Jun 19, 2021
1 parent 8865062 commit 3829f93
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
31 changes: 1 addition & 30 deletions clients/TypeScript/packages/client/src/Connection.ts
Expand Up @@ -54,34 +54,5 @@ export const createInteractionContext = async (
})
}

const isContext = (config: ConnectionConfig | InteractionContext): config is InteractionContext =>
export const isContext = (config: ConnectionConfig | InteractionContext): config is InteractionContext =>
(config as InteractionContext).socket !== undefined

export const ensureSocket = async <T>(
send: (socket: WebSocket) => Promise<T>,
config?: ConnectionConfig | InteractionContext
): Promise<T> => {
const { socket } = isContext(config)
? config
: await createInteractionContext(
() => {},
{ connection: config }
)
const closeOnCompletion = !isContext(config)
const complete = (func: () => void) => {
if (closeOnCompletion) {
socket.once('close', func)
socket.close()
} else {
func()
}
}
return new Promise((resolve, reject) => {
if (!closeOnCompletion) {
return resolve(send(socket))
}
send(socket)
.then(result => complete(resolve.bind(this, result)))
.catch(error => complete(reject.bind(this, error)))
})
}
36 changes: 36 additions & 0 deletions clients/TypeScript/packages/client/src/Request.ts
@@ -1,6 +1,42 @@
import WebSocket from 'isomorphic-ws'
import {
ConnectionConfig,
createInteractionContext,
InteractionContext,
isContext
} from './Connection'

export const baseRequest = {
type: 'jsonwsp/request',
version: '1.0',
servicename: 'ogmios'
}

export const send = async <T>(
send: (socket: WebSocket) => Promise<T>,
config?: ConnectionConfig | InteractionContext
): Promise<T> => {
const { socket } = isContext(config)
? config
: await createInteractionContext(
() => {},
{ connection: config }
)
const closeOnCompletion = !isContext(config)
const complete = (func: () => void) => {
if (closeOnCompletion) {
socket.once('close', func)
socket.close()
} else {
func()
}
}
return new Promise((resolve, reject) => {
if (!closeOnCompletion) {
return resolve(send(socket))
}
send(socket)
.then(result => complete(resolve.bind(this, result)))
.catch(error => complete(reject.bind(this, error)))
})
}
6 changes: 3 additions & 3 deletions clients/TypeScript/packages/client/src/StateQuery/Query.ts
@@ -1,7 +1,7 @@
import { nanoid } from 'nanoid'
import { Data } from 'isomorphic-ws'
import { baseRequest } from '../Request'
import { ConnectionConfig, ensureSocket, InteractionContext } from '../Connection'
import { ConnectionConfig, InteractionContext } from '../Connection'
import { baseRequest, send } from '../Request'

export const Query = <
Request,
Expand All @@ -21,7 +21,7 @@ export const Query = <
},
config?: ConnectionConfig | InteractionContext
): Promise<Response> =>
ensureSocket<Response>((socket) =>
send<Response>((socket) =>
new Promise((resolve, reject) => {
const requestId = nanoid(5)

Expand Down

0 comments on commit 3829f93

Please sign in to comment.