From c8b38fe24a09672aaf545f751bbdd3734cfa0b43 Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Mon, 29 Aug 2022 14:03:15 +0200 Subject: [PATCH] only send insert card error if not found in 150ms --- flow.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/flow.go b/flow.go index f5d9e3a..60e2564 100644 --- a/flow.go +++ b/flow.go @@ -2,6 +2,7 @@ package statuskeycardgo import ( "errors" + "time" "github.com/status-im/status-keycard-go/signal" ) @@ -186,21 +187,26 @@ func (f *KeycardFlow) connect() (*keycardContext, error) { return nil, errors.New(ErrorConnection) } - f.pause(InsertCard, ErrorConnection, FlowParams{}) - select { - case <-f.wakeUp: - if f.state != Cancelling { - panic("Resuming is not expected during connection") - } - return nil, giveupErr() - case <-kc.connected: - if kc.runErr != nil { - return nil, restartErr() - } + t := time.NewTimer(150 * time.Millisecond) - f.state = Running - signal.Send(CardInserted, FlowStatus{}) - return kc, nil + for { + select { + case <-f.wakeUp: + if f.state != Cancelling { + panic("Resuming is not expected during connection") + } + return nil, giveupErr() + case <-kc.connected: + if kc.runErr != nil { + return nil, restartErr() + } + t.Stop() + f.state = Running + signal.Send(CardInserted, FlowStatus{}) + return kc, nil + case <-t.C: + f.pause(InsertCard, ErrorConnection, FlowParams{}) + } } }