Skip to content

Commit

Permalink
prevent calling discoverReaders if not completed (#351)
Browse files Browse the repository at this point in the history
* prevent calling discoverReaders if not completed

* add busy message func
  • Loading branch information
arekkubaczkowski authored Jun 17, 2022
1 parent 5f49b65 commit 387cd0e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions android/src/main/java/com/stripeterminalreactnative/Errors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ internal fun <T> requireCancelable(cancelable: T?, lazyMessage: () -> String): T
)
}

@Throws(TerminalException::class)
internal fun <T> throwIfBusy(command: T?, lazyMessage: () -> String): Unit? {
return command?.run {
throw TerminalException(
TerminalErrorCode.READER_BUSY,
lazyMessage()
)
}
}

@Throws(TerminalException::class)
internal fun <T> requireParam(input: T?, lazyMessage: () -> String): T {
return input ?: throw TerminalException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :

val listener = RNDiscoveryListener(context) { discoveredReadersList = it }

throwIfBusy(discoverCancelable) {
busyMessage("discoverReaders", "discoverReaders")
}

discoverCancelable = terminal.discoverReaders(
DiscoveryConfiguration(0, discoveryMethod, getBoolean(params, "simulated")),
listener,
Expand All @@ -160,7 +164,9 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
@ReactMethod
@Suppress("unused")
fun cancelDiscovering(promise: Promise) {
cancelOperation(promise, discoverCancelable, "discoverReaders")
cancelOperation(promise, discoverCancelable, "discoverReaders") {
discoverCancelable = null
}
}

private fun connectReader(
Expand Down Expand Up @@ -561,10 +567,16 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
promise: Promise,
cancelable: Cancelable?,
operationName: String,
block: (() -> Unit)? = null
) = withExceptionResolver(promise) {
val toCancel = requireCancelable(cancelable) {
"$operationName could not be canceled because it has already been canceled or has completed."
}
toCancel.cancel(NoOpCallback(promise))
block?.invoke()
}

private fun busyMessage(command: String, busyBy: String): String {
return "Could not execute $command because the SDK is busy with another command: $busyBy."
}
}
4 changes: 4 additions & 0 deletions ios/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Errors {
}
}

func busyMessage(command: String, by busyCommand: String) -> String {
return "Could not execute \(command) because the SDK is busy with another command: \(busyCommand)."
}

extension ErrorCode.Code {
var stringValue: String {
switch self {
Expand Down
6 changes: 6 additions & 0 deletions ios/StripeTerminalReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
discoveryMethod: Mappers.mapToDiscoveryMethod(discoveryMethod),
simulated: simulated ?? false
)

guard discoverCancelable == nil else {
let message = busyMessage(command: "discoverReaders", by: "discoverReaders")
resolve(Errors.createError(code: ErrorCode.busy, message: message))
return
}

self.discoverCancelable = Terminal.shared.discoverReaders(config, delegate: self) { error in
if let error = error as NSError? {
Expand Down

0 comments on commit 387cd0e

Please sign in to comment.