statemachine: don't force users to see reflect

This commit is contained in:
Łukasz Magiera 2020-01-15 01:08:03 +01:00
parent 05edf50931
commit 23472aa4f2
3 changed files with 7 additions and 9 deletions

View File

@ -25,12 +25,12 @@ type StateGroup struct {
sms map[datastore.Key]*StateMachine
}
// stateType: T - (reflect.TypeOf(MyStateStruct{}))
func New(ds datastore.Datastore, hnd StateHandler, stateType reflect.Type) *StateGroup {
// stateType: T - (MyStateStruct{})
func New(ds datastore.Datastore, hnd StateHandler, stateType interface{}) *StateGroup {
return &StateGroup{
sts: statestore.New(ds),
hnd: hnd,
stateType: stateType,
stateType: reflect.TypeOf(stateType),
sms: map[datastore.Key]*StateMachine{},
}

View File

@ -2,7 +2,6 @@ package statemachine
import (
"context"
"reflect"
"testing"
"github.com/ipfs/go-datastore"
@ -67,7 +66,7 @@ func TestBasic(t *testing.T) {
th := &testHandler{t: t, done: make(chan struct{}), proceed: make(chan struct{})}
close(th.proceed)
smm := New(ds, th, reflect.TypeOf(TestState{}))
smm := New(ds, th, TestState{})
if err := smm.Send(uint64(2), &TestEvent{A: "start"}); err != nil {
t.Fatalf("%+v", err)
@ -82,7 +81,7 @@ func TestPersist(t *testing.T) {
ds := datastore.NewMapDatastore()
th := &testHandler{t: t, done: make(chan struct{}), proceed: make(chan struct{})}
smm := New(ds, th, reflect.TypeOf(TestState{}))
smm := New(ds, th, TestState{})
if err := smm.Send(uint64(2), &TestEvent{A: "start"}); err != nil {
t.Fatalf("%+v", err)
@ -93,7 +92,7 @@ func TestPersist(t *testing.T) {
return
}
smm = New(ds, th, reflect.TypeOf(TestState{}))
smm = New(ds, th, TestState{})
if err := smm.Send(uint64(2), &TestEvent{A: "restart"}); err != nil {
t.Fatalf("%+v", err)
}

View File

@ -3,7 +3,6 @@ package storage
import (
"context"
"errors"
"reflect"
"time"
"github.com/ipfs/go-cid"
@ -86,7 +85,7 @@ func NewMiner(api storageMinerApi, addr address.Address, h host.Host, ds datasto
}
// TODO: separate sector stuff from miner struct
m.sectors = statemachine.New(namespace.Wrap(ds, datastore.NewKey(SectorStorePrefix)), m, reflect.TypeOf(SectorInfo{}))
m.sectors = statemachine.New(namespace.Wrap(ds, datastore.NewKey(SectorStorePrefix)), m, SectorInfo{})
return m, nil
}