objstore

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GenericStore

type GenericStore[SharedObject any, ObjID comparable, InitParams any] struct {
	// contains filtered or unexported fields
}

func NewGenericStore

func NewGenericStore[SharedObject any, ObjID comparable, InitParams any](
	getID func(obj SharedObject) ObjID,
	idLess func(a, b ObjID) bool,
	gatherRequirements ObjRequirementsFunc[SharedObject, ObjID, InitParams],
	initObj ObjInitFunc[SharedObject, InitParams],
	startObj ObjStartFunc[SharedObject, InitParams],
	stopObj ObjStopFunc[SharedObject],
	closeObj ObjCloseFunc[SharedObject],
	l utils.Logger,
) *GenericStore[SharedObject, ObjID, InitParams]

This is a generic store for shared objects. It can be used to support another interface instead of SharedObject. All lyfecycle functions are optional except Init.

func NewStore

func NewStore[CustomSharedObject SharedObject[CustomSharedObject, InitParams], InitParams any](getID func(obj CustomSharedObject) string, l utils.Logger) *GenericStore[CustomSharedObject, string, InitParams]

func (*GenericStore[SharedObject, ObjID, InitParams]) Close

func (s *GenericStore[SharedObject, ObjID, InitParams]) Close()

Close must be called after Stop. It is used to finalize objects. Can be used to free resources and ensure they are not used anywhere else. The only thing it does is calls Close() on all objects in the store.

func (*GenericStore[SharedObject, ObjID, InitParams]) Get

func (s *GenericStore[SharedObject, ObjID, InitParams]) Get(objID ObjID) SharedObject

Returns object by its ID.

func (*GenericStore[SharedObject, ObjID, InitParams]) Init

func (s *GenericStore[SharedObject, ObjID, InitParams]) Init(initParams InitParams) error

Init must be called first of all lifecycle methods. It is intended for gathering objects requirements and then setting their initial state. After Init has finished, object must be able to receive calls from other objects.

func (*GenericStore[SharedObject, ObjID, InitParams]) RecentlyRegisteredSharedObjects

func (s *GenericStore[SharedObject, ObjID, InitParams]) RecentlyRegisteredSharedObjects() []ObjID

Returns objects, which were registered from last call of this method. It includes registration even if registered object was already in the store. This might be useful to retrieve requrements of the object without knowing what it does.

func (*GenericStore[SharedObject, ObjID, InitParams]) Register

func (s *GenericStore[SharedObject, ObjID, InitParams]) Register(obj interface{})

Register object to be shared with other users. Expects pointer to pointer.

func (*GenericStore[SharedObject, ObjID, InitParams]) Start

func (s *GenericStore[SharedObject, ObjID, InitParams]) Start() error

Start must be called after Init. It is used as PostInit hook. It is intended for starting background processes, timers, etc. The onlt thing it does is calls Start() on all objects in the store.

func (*GenericStore[SharedObject, ObjID, InitParams]) Stop

func (s *GenericStore[SharedObject, ObjID, InitParams]) Stop()

Stop must be called after Start. It is used as PreClose hook. It is intended for stopping background processes, timers, etc. The only thing it does is calls Stop() on all objects in the store.

func (*GenericStore[SharedObject, ObjID, InitParams]) TopLevelDependencies

func (s *GenericStore[SharedObject, ObjID, InitParams]) TopLevelDependencies() []ObjID

Returns all objects, which were registered in the store before Init() was called.

type ObjCloseFunc

type ObjCloseFunc[SharedObject any] func(obj SharedObject)

type ObjInitFunc

type ObjInitFunc[SharedObject any, InitParams any] func(obj SharedObject, params InitParams) error

type ObjRequirementsFunc

type ObjRequirementsFunc[SharedObject any, ObjID comparable, InitParams any] func(obj SharedObject, s *GenericStore[SharedObject, ObjID, InitParams])

type ObjStartFunc

type ObjStartFunc[SharedObject any, InitParams any] func(obj SharedObject, params InitParams) error

type ObjStopFunc

type ObjStopFunc[SharedObject any] func(obj SharedObject)

type SharedObject

type SharedObject[CustomSharedObject any, InitParams any] interface {
	// RegisterDependencies is the first lifecycle method called by store. It is used to
	// gather requirements of the object. You must call store.Register(obj) for each
	// object you depend on.
	RegisterDependencies(s SharedStore[CustomSharedObject, InitParams])

	// Init is called after all requirements are gathered. It is used to initialize object.
	// It is intended for setting up initial state of the object.
	// When Init returns, object is expected to be able to receive calls from other objects.
	Init(p InitParams) error

	// Start is called after Init. It is used as PostInit hook.
	// It is intended for starting background processes, timers, etc.
	Start(p InitParams) error

	// Stop is called before Close. It is used as PreClose hook.
	// It is intended for stopping background processes, timers, etc.
	Stop()

	// Close is called last. It is used to finalize objects.
	// Can be used to free resources and ensure they are not used anywhere else.
	Close()
}

type SharedRegistry

type SharedRegistry[ObjType any] interface {
	// Register object to be shared with other users.
	// Expects pointer to pointer.
	Register(obj interface{})
}

type SharedStore

type SharedStore[CustomSharedObject any, InitParams any] interface {
	SharedRegistry[CustomSharedObject]

	// Init must be called first of all lifecycle methods.
	// It gathers objects requirements and then calls Init() on all objects.
	Init(params InitParams) error

	// Start must be called after Init. It is used as PostInit hook.
	// It is intended for starting background processes, timers, etc.
	// The onlt thing it does is calls Start() on all objects in the store.
	Start() error

	// Stop must be called after Start. It is used as PreClose hook.
	// It is intended for stopping background processes, timers, etc.
	// The only thing it does is calls Stop() on all objects in the store.
	Stop()

	// Close must be called after Stop. It is used to finalize objects.
	// Can be used to free resources and ensure they are not used anywhere else.
	// The only thing it does is calls Close() on all objects in the store.
	Close()

	// Returns object by its ID.
	Get(objID string) CustomSharedObject

	// Returns all objects, which were registered in the store before Init() was called.
	TopLevelDependencies() []string

	// Returns objects, which were registered from last call of this method.
	// It includes registration even if registered object was already in the store.
	// This might be useful to retrieve requrements of the object without knowing what it does.
	RecentlyRegisteredSharedObjects() []string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL