mirror of
https://github.com/status-im/status-keycard-go.git
synced 2025-01-18 18:31:56 +00:00
add flow execution
This commit is contained in:
parent
38d48b590f
commit
ffd7ca5f6c
94
flow.go
94
flow.go
@ -1,6 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/status-im/status-keycard-go/signal"
|
||||||
|
)
|
||||||
|
|
||||||
func NewFlow(storageDir string) (*keycardFlow, error) {
|
func NewFlow(storageDir string) (*keycardFlow, error) {
|
||||||
flow := &keycardFlow{
|
flow := &keycardFlow{
|
||||||
@ -12,20 +16,20 @@ func NewFlow(storageDir string) (*keycardFlow, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *keycardFlow) Start(flowType FlowType, params map[string]string) error {
|
func (f *keycardFlow) Start(flowType FlowType, params map[string]string) error {
|
||||||
if f.state != IDLE {
|
if f.state != Idle {
|
||||||
return errors.New("already running")
|
return errors.New("already running")
|
||||||
}
|
}
|
||||||
|
|
||||||
f.flowType = flowType
|
f.flowType = flowType
|
||||||
f.params = params
|
f.params = params
|
||||||
f.state = RUNNING
|
f.state = Running
|
||||||
go f.runFlow()
|
go f.runFlow()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *keycardFlow) Resume(params map[string]string) error {
|
func (f *keycardFlow) Resume(params map[string]string) error {
|
||||||
if f.state != PAUSED {
|
if f.state != Paused {
|
||||||
return errors.New("only paused flows can be resumed")
|
return errors.New("only paused flows can be resumed")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +37,7 @@ func (f *keycardFlow) Resume(params map[string]string) error {
|
|||||||
f.params[k] = v
|
f.params[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
f.state = RESUMING
|
f.state = Resuming
|
||||||
f.wakeUp <- struct{}{}
|
f.wakeUp <- struct{}{}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -42,12 +46,12 @@ func (f *keycardFlow) Resume(params map[string]string) error {
|
|||||||
func (f *keycardFlow) Cancel() error {
|
func (f *keycardFlow) Cancel() error {
|
||||||
prevState := f.state
|
prevState := f.state
|
||||||
|
|
||||||
if prevState != IDLE {
|
if prevState != Idle {
|
||||||
return errors.New("cannot cancel idle flow")
|
return errors.New("cannot cancel idle flow")
|
||||||
}
|
}
|
||||||
|
|
||||||
f.state = CANCELLING
|
f.state = Cancelling
|
||||||
if prevState == PAUSED {
|
if prevState == Paused {
|
||||||
f.wakeUp <- struct{}{}
|
f.wakeUp <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,5 +59,79 @@ func (f *keycardFlow) Cancel() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *keycardFlow) runFlow() {
|
func (f *keycardFlow) runFlow() {
|
||||||
|
repeat := true
|
||||||
|
var result map[string]string
|
||||||
|
|
||||||
|
for repeat {
|
||||||
|
switch f.flowType {
|
||||||
|
case GetStatus:
|
||||||
|
repeat, result = f.switchFlow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.state != Cancelling {
|
||||||
|
signal.SendEvent(FlowResult, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.state = Idle
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *keycardFlow) switchFlow() (bool, map[string]string) {
|
||||||
|
kc := f.connect()
|
||||||
|
defer f.closeKeycard(kc)
|
||||||
|
|
||||||
|
if kc == nil {
|
||||||
|
return false, map[string]string{"Error": "Couldn't connect to the card"}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch f.flowType {
|
||||||
|
case GetStatus:
|
||||||
|
return f.getStatusFlow(kc)
|
||||||
|
default:
|
||||||
|
return false, map[string]string{"Error": "Unknown flow"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *keycardFlow) pause(action string, status map[string]string) {
|
||||||
|
signal.SendEvent(action, status)
|
||||||
|
f.state = Paused
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *keycardFlow) pauseAndWait(action string, status map[string]string) {
|
||||||
|
f.pause(action, status)
|
||||||
|
<-f.wakeUp
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *keycardFlow) closeKeycard(kc *keycardContext) {
|
||||||
|
if kc != nil {
|
||||||
|
kc.stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *keycardFlow) connect() *keycardContext {
|
||||||
|
kc, err := startKeycardContext()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
f.pause(InsertCard, map[string]string{})
|
||||||
|
select {
|
||||||
|
case <-f.wakeUp:
|
||||||
|
if f.state != Cancelling {
|
||||||
|
panic("Resuming is not expected during connection")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case <-kc.connected:
|
||||||
|
if kc.runErr != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return kc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *keycardFlow) getStatusFlow(kc *keycardContext) (bool, map[string]string) {
|
||||||
|
|
||||||
|
return false, nil
|
||||||
}
|
}
|
||||||
|
@ -1,31 +1,37 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
type FlowType int
|
type FlowType int
|
||||||
type RunState int
|
type runState int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GET_STATUS FlowType = iota
|
GetStatus FlowType = iota
|
||||||
RECOVER_ACCOUNT
|
RecoverAccount
|
||||||
LOAD_ACCOUNT
|
LoadAccount
|
||||||
LOGIN
|
Login
|
||||||
SIGN
|
Sign
|
||||||
CHANGE_CREDENTIALS
|
ChangeCredentials
|
||||||
UNPAIR
|
UnpairThis
|
||||||
UNPAIR_OTHERS
|
UnpairOthers
|
||||||
DELETE_ACCOUNT_AND_UNPAIR
|
DeleteAccountAndUnpair
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
IDLE RunState = iota
|
Idle runState = iota
|
||||||
RUNNING
|
Running
|
||||||
PAUSED
|
Paused
|
||||||
RESUMING
|
Resuming
|
||||||
CANCELLING
|
Cancelling
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
FlowResult = "keycard.flow-result"
|
||||||
|
InsertCard = "keycard.action.insert-card"
|
||||||
|
CardInserted = "keycard.action.card-inserted"
|
||||||
)
|
)
|
||||||
|
|
||||||
type keycardFlow struct {
|
type keycardFlow struct {
|
||||||
flowType FlowType
|
flowType FlowType
|
||||||
state RunState
|
state runState
|
||||||
wakeUp chan (struct{})
|
wakeUp chan (struct{})
|
||||||
storage string
|
storage string
|
||||||
params map[string]string
|
params map[string]string
|
||||||
|
@ -7,3 +7,7 @@ const (
|
|||||||
func SendKeycardConnected(event interface{}) {
|
func SendKeycardConnected(event interface{}) {
|
||||||
send(EventKeycardConnected, event)
|
send(EventKeycardConnected, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SendEvent(typ string, event interface{}) {
|
||||||
|
send(typ, event)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user