-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(stream): change sending into reusable functions
- Loading branch information
Showing
3 changed files
with
131 additions
and
82 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/lib-protocol-stream/src/stream/add-send-amount.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { isFailure } from "@dassie/lib-type-utils" | ||
|
||
import { assertConnectionCanSendMoney } from "../connection/assert-can-send" | ||
import type { ConnectionState } from "../connection/state" | ||
import type { StreamState } from "./state" | ||
|
||
interface AddSendAmountOptions { | ||
connectionState: ConnectionState | ||
state: StreamState | ||
amount: bigint | ||
} | ||
|
||
export function addSendAmount({ | ||
connectionState, | ||
state, | ||
amount, | ||
}: AddSendAmountOptions) { | ||
{ | ||
const result = assertConnectionCanSendMoney(connectionState) | ||
if (isFailure(result)) return result | ||
} | ||
|
||
state.sendMaximum += amount | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { createDeferred, createScope } from "@dassie/lib-reactive" | ||
import { isFailure } from "@dassie/lib-type-utils" | ||
|
||
import { NoExchangeRateFailure } from "../connection/failures/no-exchange-rate-failure" | ||
import { NoRemoteAddressFailure } from "../connection/failures/no-remote-address-failure" | ||
import { sendUntilDone } from "../connection/send-until-done" | ||
import type { ConnectionState } from "../connection/state" | ||
import { addSendAmount } from "./add-send-amount" | ||
import { | ||
SEND_INCOMPLETE_FAILURE, | ||
SEND_TIMEOUT_FAILURE, | ||
SendFailure, | ||
} from "./failures/send-failure" | ||
import type { RemoteMoneyEvent, StreamState } from "./state" | ||
|
||
const DEFAULT_TIMEOUT = 30_000 | ||
|
||
interface SendAndAwaitOptions { | ||
connectionState: ConnectionState | ||
state: StreamState | ||
amount: bigint | ||
timeout?: number | undefined | ||
} | ||
|
||
export function sendAndAwait({ | ||
connectionState, | ||
state, | ||
amount, | ||
timeout = DEFAULT_TIMEOUT, | ||
}: SendAndAwaitOptions): Promise< | ||
void | SendFailure | NoRemoteAddressFailure | NoExchangeRateFailure | ||
> { | ||
const scope = createScope("stream-send") | ||
|
||
{ | ||
const result = addSendAmount({ connectionState, state, amount }) | ||
if (isFailure(result)) return Promise.resolve(result) | ||
} | ||
|
||
const deferred = createDeferred<void | SendFailure>() | ||
const targetAmount = state.sendMaximum | ||
|
||
const handleSendCompleted = (result: void | SendFailure = undefined) => { | ||
scope.dispose().catch((error: unknown) => { | ||
connectionState.context.logger.error("error disposing send scope", { | ||
error, | ||
}) | ||
}) | ||
connectionState.context.clock.clearTimeout(timeoutId) | ||
deferred.resolve(result) | ||
} | ||
|
||
const timeoutId = connectionState.context.clock.setTimeout(() => { | ||
handleSendCompleted(SEND_TIMEOUT_FAILURE) | ||
}, timeout) | ||
|
||
const sentListener = () => { | ||
if (state.sentAmount >= targetAmount) { | ||
handleSendCompleted() | ||
} | ||
} | ||
state.topics.moneySent.on(scope, sentListener) | ||
|
||
const remoteListener = (event: RemoteMoneyEvent) => { | ||
if ( | ||
event.receiveMaximum - event.receivedAmount < | ||
connectionState.context.policy.deMinimisAmount | ||
) { | ||
handleSendCompleted() | ||
} | ||
} | ||
state.topics.remoteMoney.on(scope, remoteListener) | ||
|
||
sendUntilDone(connectionState) | ||
.catch((error: unknown) => { | ||
connectionState.context.logger.error( | ||
"unexpected error returned by send loop", | ||
{ | ||
error, | ||
}, | ||
) | ||
}) | ||
.finally(() => { | ||
handleSendCompleted(SEND_INCOMPLETE_FAILURE) | ||
}) | ||
|
||
return deferred | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters