1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 17:44:59 +09:00

GO-1084: Refactor

This commit is contained in:
Sergey 2023-04-25 18:17:59 +02:00 committed by Mikhail Iudin
parent cb0da7b34b
commit 247554c376
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
5 changed files with 72 additions and 78 deletions

View file

@ -43,7 +43,7 @@ type ObjectFactory struct {
templateCloner templateCloner
fileService files.Service
smartblockFactory smartblockFactory
subObjectFactory subObjectFactory
}
func NewObjectFactory(
@ -71,13 +71,18 @@ func (f *ObjectFactory) Init(a *app.App) (err error) {
f.templateCloner = app.MustComponent[templateCloner](a)
f.fileService = app.MustComponent[files.Service](a)
f.smartblockFactory = smartblockFactory{
anytype: f.anytype,
f.subObjectFactory = subObjectFactory{
coreService: f.anytype,
fileBlockService: f.fileBlockService,
fileService: f.fileService,
indexer: app.MustComponent[smartblock.Indexer](a),
layoutConverter: f.layoutConverter,
objectStore: f.objectStore,
relationService: f.relationService,
restrictionService: app.MustComponent[restriction.Service](a),
sbtProvider: f.sbtProvider,
sourceService: f.sourceService,
tempDirProvider: f.tempDirProvider,
}
return nil
@ -146,7 +151,7 @@ func (f *ObjectFactory) InitObject(id string, initCtx *smartblock.InitContext) (
}
func (f *ObjectFactory) New(sbType model.SmartBlockType) (smartblock.SmartBlock, error) {
sb := f.smartblockFactory.Produce()
sb := f.subObjectFactory.produceSmartblock()
switch sbType {
case model.SmartBlockType_Page, model.SmartBlockType_Date, model.SmartBlockType_BundledRelation, model.SmartBlockType_BundledObjectType:
return NewPage(
@ -228,11 +233,9 @@ func (f *ObjectFactory) New(sbType model.SmartBlockType) (smartblock.SmartBlock,
f.relationService,
f.sourceService,
f.detailsModifier,
f.fileBlockService,
f.tempDirProvider,
f.sbtProvider,
f.layoutConverter,
f.smartblockFactory,
f.subObjectFactory,
f.templateCloner,
), nil
case model.SmartBlockType_MissingObject:

View file

@ -1,30 +0,0 @@
package editor
import (
"github.com/anytypeio/go-anytype-middleware/core/block/editor/smartblock"
"github.com/anytypeio/go-anytype-middleware/core/block/restriction"
"github.com/anytypeio/go-anytype-middleware/core/files"
relation2 "github.com/anytypeio/go-anytype-middleware/core/relation"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/core"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/objectstore"
)
type smartblockFactory struct {
anytype core.Service
fileService files.Service
indexer smartblock.Indexer
objectStore objectstore.ObjectStore
relationService relation2.Service
restrictionService restriction.Service
}
func (f smartblockFactory) Produce() smartblock.SmartBlock {
return smartblock.New(
f.anytype,
f.fileService,
f.restrictionService,
f.objectStore,
f.relationService,
f.indexer,
)
}

View file

@ -0,0 +1,55 @@
package editor
import (
"fmt"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/converter"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/file"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/smartblock"
"github.com/anytypeio/go-anytype-middleware/core/block/restriction"
"github.com/anytypeio/go-anytype-middleware/core/block/source"
"github.com/anytypeio/go-anytype-middleware/core/files"
relation2 "github.com/anytypeio/go-anytype-middleware/core/relation"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/core"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/objectstore"
"github.com/anytypeio/go-anytype-middleware/space/typeprovider"
)
type subObjectFactory struct {
coreService core.Service
fileBlockService file.BlockService
fileService files.Service
indexer smartblock.Indexer
layoutConverter converter.LayoutConverter
objectStore objectstore.ObjectStore
relationService relation2.Service
restrictionService restriction.Service
sbtProvider typeprovider.SmartBlockTypeProvider
sourceService source.Service
tempDirProvider core.TempDirProvider
}
func (f subObjectFactory) produceSmartblock() smartblock.SmartBlock {
return smartblock.New(
f.coreService,
f.fileService,
f.restrictionService,
f.objectStore,
f.relationService,
f.indexer,
)
}
func (f subObjectFactory) produce(collection string) (SubObjectImpl, error) {
sb := f.produceSmartblock()
switch collection {
case collectionKeyObjectTypes:
return NewObjectType(sb, f.objectStore, f.fileBlockService, f.coreService, f.relationService, f.tempDirProvider, f.sbtProvider, f.layoutConverter, f.fileService), nil
case collectionKeyRelations:
return NewRelation(sb, f.objectStore, f.fileBlockService, f.coreService, f.relationService, f.tempDirProvider, f.sbtProvider, f.layoutConverter, f.fileService), nil
case collectionKeyRelationOptions:
return NewRelationOption(sb, f.objectStore, f.fileBlockService, f.coreService, f.relationService, f.tempDirProvider, f.sbtProvider, f.layoutConverter, f.fileService), nil
default:
return nil, fmt.Errorf("unknown collection: %s", collection)
}
}

View file

@ -11,7 +11,6 @@ import (
"github.com/anytypeio/go-anytype-middleware/core/block/editor/basic"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/converter"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/dataview"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/file"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/smartblock"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/state"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/stext"
@ -57,16 +56,10 @@ type SubObjectCollection struct {
defaultCollectionName string
collections map[string]map[string]SubObjectImpl
sourceService source.Service
objectStore objectstore.ObjectStore
anytype core.Service
relationService relation2.Service
fileBlockService file.BlockService
tempDirProvider core.TempDirProvider
sbtProvider typeprovider.SmartBlockTypeProvider
layoutConverter converter.LayoutConverter
sourceService source.Service
objectStore objectstore.ObjectStore
smartblockFactory smartblockFactory
subObjectFactory subObjectFactory
}
func NewSubObjectCollection(
@ -76,11 +69,9 @@ func NewSubObjectCollection(
anytype core.Service,
relationService relation2.Service,
sourceService source.Service,
fileBlockService file.BlockService,
tempDirProvider core.TempDirProvider,
sbtProvider typeprovider.SmartBlockTypeProvider,
layoutConverter converter.LayoutConverter,
smartblockFactory smartblockFactory,
subObjectFactory subObjectFactory,
) *SubObjectCollection {
return &SubObjectCollection{
SmartBlock: sb,
@ -100,14 +91,9 @@ func NewSubObjectCollection(
objectStore: objectStore,
sourceService: sourceService,
anytype: anytype,
relationService: relationService,
fileBlockService: fileBlockService,
tempDirProvider: tempDirProvider,
layoutConverter: layoutConverter,
defaultCollectionName: defaultCollectionName,
collections: map[string]map[string]SubObjectImpl{},
smartblockFactory: smartblockFactory,
subObjectFactory: subObjectFactory,
}
}
@ -379,7 +365,7 @@ func (c *SubObjectCollection) initSubObject(st *state.State, collection string,
// inject the internal flag to the state
}
subObj, err := c.newSubObject(collection)
subObj, err := c.subObjectFactory.produce(collection)
if err != nil {
return fmt.Errorf("new sub-object: %w", err)
}
@ -398,21 +384,6 @@ func (c *SubObjectCollection) initSubObject(st *state.State, collection string,
return
}
// TODO Extract to subobject factory
func (c *SubObjectCollection) newSubObject(collection string) (SubObjectImpl, error) {
sb := c.smartblockFactory.Produce()
switch collection {
case collectionKeyObjectTypes:
return NewObjectType(sb, c.objectStore, c.fileBlockService, c.anytype, c.relationService, c.tempDirProvider, c.sbtProvider, c.layoutConverter, c.smartblockFactory.fileService), nil
case collectionKeyRelations:
return NewRelation(sb, c.objectStore, c.fileBlockService, c.anytype, c.relationService, c.tempDirProvider, c.sbtProvider, c.layoutConverter, c.smartblockFactory.fileService), nil
case collectionKeyRelationOptions:
return NewRelationOption(sb, c.objectStore, c.fileBlockService, c.anytype, c.relationService, c.tempDirProvider, c.sbtProvider, c.layoutConverter, c.smartblockFactory.fileService), nil
default:
return nil, fmt.Errorf("unknown collection: %s", collection)
}
}
func SubState(st *state.State, collection string, fullId string, workspaceId string) (*state.State, error) {
subId := strings.TrimPrefix(fullId, collection+addr.SubObjectCollectionIdSeparator)
data := pbtypes.GetStruct(st.GetSubObjectCollection(collection), subId)

View file

@ -10,7 +10,6 @@ import (
"github.com/anytypeio/go-anytype-middleware/core/block/editor/converter"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/dataview"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/file"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/smartblock"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/state"
"github.com/anytypeio/go-anytype-middleware/core/block/editor/template"
@ -65,11 +64,9 @@ func NewWorkspace(
relationService relation.Service,
sourceService source.Service,
modifier DetailsModifier,
fileBlockService file.BlockService,
tempDirProvider core.TempDirProvider,
sbtProvider typeprovider.SmartBlockTypeProvider,
layoutConverter converter.LayoutConverter,
smartblockFactory smartblockFactory,
smartblockFactory subObjectFactory,
templateCloner templateCloner,
) *Workspaces {
return &Workspaces{
@ -80,8 +77,6 @@ func NewWorkspace(
anytype,
relationService,
sourceService,
fileBlockService,
tempDirProvider,
sbtProvider,
layoutConverter,
smartblockFactory,