application

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 9 Imported by: 130

Documentation

Overview

Package application 是starter的核心包之一,里面包含了大部分用来管理应用程序生命周期的组件。

Index

Constants

View Source
const ExitCodeGeneratorClassName = "exit-code-generator"
View Source
const (
	LifeClassName = "life" // 	统一使用'class:"life"'注入
)

定义app生命周期组件的类名称

Variables

This section is empty.

Functions

func Exit

func Exit(context Context) (int, error)

Exit 函数用于退出应用

func Loop

func Loop(context Context) error

Loop 函数用于执行应用的主循环

func RunAndLoop added in v0.0.33

func RunAndLoop(config Configuration) (int, error)

RunAndLoop 函数依次调用 Run(), Loop(), Exit()

func Shutdown added in v0.1.2

func Shutdown(ctx Context) error

Shutdown 关闭应用

Types

type ComponentAfterService added in v0.0.41

type ComponentAfterService interface {
	Inject(instance ComponentInstance, context InstanceContext) error
	Init(instance ComponentInstance) error
	Destroy(instance ComponentInstance) error
}

ComponentAfterService 是组件出厂后的服务

type ComponentFactory

type ComponentFactory interface {
	GetPrototype() lang.Object
	NewInstance() ComponentInstance
	AfterService() ComponentAfterService
}

ComponentFactory 一个组件的工厂

type ComponentGroup added in v0.0.41

type ComponentGroup interface {
	Size() int
	ListAll() []ComponentHolder
	ListWithFilter(f ComponentHolderFilter) []ComponentHolder
	Name() string
}

ComponentGroup 是holder的分组

type ComponentGroupManager added in v0.0.41

type ComponentGroupManager interface {
	GetGroup(selector string) ComponentGroup
	Reload() error
}

ComponentGroupManager 用于管理holder的分组

type ComponentHolder

type ComponentHolder interface {
	GetInstance() ComponentInstance
	IsOriginalName(name string) bool
	GetInfo() ComponentInfo
	GetPrototype() lang.Object
	GetContext() Context
	MakeChild(context Context) ComponentHolder
}

ComponentHolder 一个具体的组件的代理

type ComponentHolderFilter

type ComponentHolderFilter func(name string, holder ComponentHolder) bool

ComponentHolderFilter 是组件过滤器的函数签名

type ComponentInfo

type ComponentInfo interface {
	GetID() string
	GetAliases() []string
	GetClasses() []string
	GetScope() ComponentScope
	GetFactory() ComponentFactory
	GetPrototype() lang.Object

	IsTypeOf(typeName string) bool
	IsNameOf(alias string) bool
}

ComponentInfo 一个组件的配置

type ComponentInstance

type ComponentInstance interface {
	Factory() ComponentFactory
	Get() lang.Object
	State() ComponentState
	Inject(context InstanceContext) error
	Init() error
	Destroy() error
}

ComponentInstance 一个具体的组件的实例的引用

type ComponentScope

type ComponentScope uint32

ComponentScope 枚举表示组件的作用域

const (
	ScopeMin       ComponentScope = 0 // ScopeMin 是作用域的最小值
	ScopeSingleton ComponentScope = 1 // ScopeSingleton 表示单例模式
	ScopeContext   ComponentScope = 2 // ScopeContext 表示上下文模式
	ScopePrototype ComponentScope = 3 // ScopePrototype 表示原型模式
	ScopeMax       ComponentScope = 4 // ScopeMax 是作用域的最大值
)

type ComponentState added in v0.0.41

type ComponentState uint32

ComponentState 枚举表示组件的状态

const (
	StateZero       ComponentState = 0 // 新建
	StateInjected   ComponentState = 1 // 已执行 injectMethod
	StateInitialled ComponentState = 2 // 已执行 initMethod
	StateReady      ComponentState = 3 // 正常可用
	StateDestroyed  ComponentState = 4 // 已执行 destroyMethod
)

type Components

type Components interface {
	// ids
	GetComponentNameList(includeAliases bool) []string

	// finders
	FindComponent(selector string) (ComponentHolder, error)
	FindComponents(selector string) []ComponentHolder
	FindComponentsWithFilter(selector string, f ComponentHolderFilter) []ComponentHolder

	GroupManager() ComponentGroupManager

	// export & import
	Export(map[string]ComponentHolder) map[string]ComponentHolder
	Import(map[string]ComponentHolder)
}

Components 接口表示一个组件的集合

type ConfigBuilder

type ConfigBuilder interface {
	AddComponent(info ComponentInfo)
	AddResources(res collection.Resources)
	AddProperties(p collection.Properties)
	AddProperties2(theDefault, theFinal collection.Properties)

	SetModules(mods []Module)

	SetResources(res collection.Resources)
	SetAttribute(name string, value interface{})

	SetErrorHandler(h lang.ErrorHandler)

	SetEnableLoadPropertiesFromArguments(enable bool)
	IsEnableLoadPropertiesFromArguments() bool

	DefaultProperties() collection.Properties
	FinalProperties() collection.Properties

	Create() Configuration
}

ConfigBuilder 表示应用程序配置

type Configuration

type Configuration interface {
	GetLoader() ContextLoader
	GetModules() []Module
	GetComponents() []ComponentInfo
	GetResources() collection.Resources
	GetAttributes() collection.Attributes
	GetEnvironment() collection.Environment
	GetDefaultProperties() collection.Properties
	GetFinalProperties() collection.Properties
	GetErrorHandler() lang.ErrorHandler
	IsEnableLoadPropertiesFromArguments() bool
}

Configuration 表示应用程序配置

type Context

type Context interface {
	context.Context
	ContextCollections
	ContextInfo
	io.Closer

	// helper
	SetErrorHandler(h lang.ErrorHandler)
	GetErrorHandler() lang.ErrorHandler
	NewChild() Context

	GetComponent(selector string) (lang.Object, error)
	GetComponentList(selector string) ([]lang.Object, error)
}

Context 表示一个通用的上下文对象

func Run

func Run(config Configuration, args []string) (Context, error)

Run 函数启动一个应用实例,返回应用上下文

type ContextCollections

type ContextCollections interface {

	// GetReleasePool 取context的生命周期管理池
	GetReleasePool() lang.ReleasePool

	// GetComponents 取context组件管理器
	GetComponents() Components

	// GetResources 取context的资源管理器
	GetResources() collection.Resources

	GetArguments() collection.Arguments
	GetAttributes() collection.Attributes
	GetEnvironment() collection.Environment
	GetProperties() collection.Properties
	GetParameters() collection.Parameters
}

ContextCollections 提供一组getter,来获取context的各种集合

type ContextGetter

type ContextGetter interface {

	// for property
	GetProperty(name string) (string, error)
	GetPropertySafely(name string, _default string) string
	GetPropertyString(name string, _default string) string
	GetPropertyInt(name string, _default int) int
}

ContextGetter 接口向 Context 的使用者提供简易的 getter 方法

type ContextInfo

type ContextInfo interface {

	// info
	GetURI() string
	GetApplicationName() string
	GetApplicationVersion() string
	GetStartupTimestamp() int64
	GetShutdownTimestamp() int64
}

ContextInfo 包含上下文的基本信息

type ContextLoader

type ContextLoader interface {
	Load(config Configuration, args []string) (Context, error)
}

ContextLoader 用于加载进程上下文

type DefineModule added in v0.0.33

type DefineModule struct {
	Name         string
	Version      string
	Revision     int
	Resources    collection.Resources
	Dependencies []Module
	OnMount      OnMountFunc
}

DefineModule 定义一个模块

func (*DefineModule) AddDependency added in v0.0.33

func (inst *DefineModule) AddDependency(mod Module)

AddDependency 添加一个依赖

func (*DefineModule) Apply added in v0.0.33

func (inst *DefineModule) Apply(cb ConfigBuilder) error

Apply 向 ConfigBuilder 注册本模块中包含的组件,并注入默认配置

func (*DefineModule) GetDependencies added in v0.0.33

func (inst *DefineModule) GetDependencies() []Module

GetDependencies 返回依赖的其它模块

func (*DefineModule) GetName added in v0.0.33

func (inst *DefineModule) GetName() string

GetName 取模块名称

func (*DefineModule) GetResources added in v0.0.33

func (inst *DefineModule) GetResources() collection.Resources

GetResources 取模块的资源

func (*DefineModule) GetRevision added in v0.0.33

func (inst *DefineModule) GetRevision() int

GetRevision 取模块修订编号

func (*DefineModule) GetVersion added in v0.0.33

func (inst *DefineModule) GetVersion() string

GetVersion 取模块版本

type ExitCodeGenerator

type ExitCodeGenerator interface {
	ExitCode() int
}

type FunctionInjectionTarget added in v0.0.9

type FunctionInjectionTarget struct {
	// contains filtered or unexported fields
}

func (*FunctionInjectionTarget) Close added in v0.0.9

func (inst *FunctionInjectionTarget) Close() error

func (*FunctionInjectionTarget) Init added in v0.0.9

func (*FunctionInjectionTarget) Write added in v0.0.9

func (inst *FunctionInjectionTarget) Write(o lang.Object) error

type FunctionInjectionTargetFn added in v0.0.9

type FunctionInjectionTargetFn func(lang.Object) error

type Initializer added in v0.0.33

type Initializer interface {
	Run()
	RunEx() (Runtime, error)

	SetErrorHandler(h lang.ErrorHandler) Initializer
	SetAttribute(name string, value interface{}) Initializer
	SetExitEnabled(enabled bool) Initializer
	SetPanicEnabled(enabled bool) Initializer
	SetArguments(args []string) Initializer

	UseResources(res collection.Resources) Initializer
	UseProperties(res collection.Properties) Initializer
	Use(module Module) Initializer
	UseMain(module Module) Initializer
	UsePanic() Initializer
}

Initializer 是应用程序的启动器

type Injection

type Injection interface {
	io.Closer
	lang.ErrorHandler

	Context() Context
	Pool() lang.ReleasePool

	// 类选择器: ".class"
	// ID选择器: "#id"
	// 属性选择器: "${prop.name}"
	// value选择器: "foo"
	// context选择器: "context"
	Select(selector string) InjectionSource
}

type InjectionSource added in v0.0.9

type InjectionSource interface {
	io.Closer
	Count() int
	Selector() string
	HasMore() bool

	Read() (lang.Object, error)
	ReadString() (string, error)
	ReadInt() (int, error)
	ReadInt32() (int32, error)
	ReadInt64() (int64, error)
	ReadFloat32() (float32, error)
	ReadFloat64() (float64, error)
	ReadBool() (bool, error)
}

type InjectionTarget added in v0.0.9

type InjectionTarget interface {
	io.Closer
	Write(lang.Object) error
}

type Injector

type Injector interface {
	OpenInjection(context Context) (Injection, error)
}

type InstanceContext added in v0.0.41

type InstanceContext interface {
	Context() Context
	Pool() lang.ReleasePool

	HandleError(err error)
	LastError() error

	GetComponent(selector string) (lang.Object, error)
	GetComponents(selector string) ([]lang.Object, error)
	GetComponentsByFilter(selector string, f ComponentHolderFilter) ([]lang.Object, error)

	GetInt(selector string) (int, error)
	GetInt8(selector string) (int8, error)
	GetInt16(selector string) (int16, error)
	GetInt32(selector string) (int32, error)
	GetInt64(selector string) (int64, error)
	GetFloat32(selector string) (float32, error)
	GetFloat64(selector string) (float64, error)
	GetBool(selector string) (bool, error)
	GetString(selector string) (string, error)
}

InstanceContext 组件实例的上下文

type Killer added in v0.1.2

type Killer interface {
	Shutdown() error
}

Killer 接口用于通知应用程序关闭, 【inject:".killer"】

type Life added in v0.1.0

type Life LifeRegistration

Life 是 LifeRegistration 的别名

type LifeRegistration added in v0.1.0

type LifeRegistration struct {
	Priority int // 数值越大,优先级越高,优先执行OnInit & OnStart

	OnInit    OnLifeFunc
	OnStart   OnLifeFunc
	Looper    Looper
	OnStop    OnLifeFunc
	OnDestroy OnLifeFunc
}

LifeRegistration 表示 Life 的注册项

type LifeRegistrationSorter added in v0.1.0

type LifeRegistrationSorter struct {
	List []*LifeRegistration
}

LifeRegistrationSorter 是 LifeRegistration 的排序器

func (*LifeRegistrationSorter) Len added in v0.1.0

func (inst *LifeRegistrationSorter) Len() int

func (*LifeRegistrationSorter) Less added in v0.1.0

func (inst *LifeRegistrationSorter) Less(a, b int) bool

func (*LifeRegistrationSorter) Swap added in v0.1.0

func (inst *LifeRegistrationSorter) Swap(a, b int)

type LifeRegistry added in v0.1.0

type LifeRegistry interface {
	GetLifeRegistration() *LifeRegistration
}

LifeRegistry 表示 Life 的注册者 【inject:".life"】

type Looper

type Looper interface {
	Loop() error
}

Looper 【inject:"#main-looper"】

type MainLooper added in v0.0.82

type MainLooper interface {
	Killer
	RunMain() error
}

MainLooper 【inject:"#main-looper"】

func GetMainLooper added in v0.1.2

func GetMainLooper(c Context) MainLooper

GetMainLooper 取主循环对象

type Module added in v0.0.33

type Module interface {
	GetName() string
	GetVersion() string
	GetRevision() int
	GetResources() collection.Resources
	GetDependencies() []Module
	Apply(cb ConfigBuilder) error
}

Module 表示一个可导入的模块

type ModuleBuilder added in v0.0.33

type ModuleBuilder struct {
	// contains filtered or unexported fields
}

ModuleBuilder 用于创建Module对象

func (*ModuleBuilder) Create added in v0.0.33

func (inst *ModuleBuilder) Create() Module

Create 创建模块

func (*ModuleBuilder) Dependencies added in v0.0.33

func (inst *ModuleBuilder) Dependencies(mods []Module) *ModuleBuilder

Dependencies 添加一组依赖

func (*ModuleBuilder) Dependency added in v0.0.33

func (inst *ModuleBuilder) Dependency(mod Module) *ModuleBuilder

Dependency 添加一个依赖

func (*ModuleBuilder) Name added in v0.0.33

func (inst *ModuleBuilder) Name(name string) *ModuleBuilder

Name 设置模块的名称

func (*ModuleBuilder) OnMount added in v0.0.33

func (inst *ModuleBuilder) OnMount(fn OnMountFunc) *ModuleBuilder

OnMount 设置配置模块的入口函数

func (*ModuleBuilder) Resources added in v0.0.33

func (inst *ModuleBuilder) Resources(resources collection.Resources) *ModuleBuilder

Resources 设置跟模块绑定的资源

func (*ModuleBuilder) Revision added in v0.0.33

func (inst *ModuleBuilder) Revision(revision int) *ModuleBuilder

Revision 设置模块的修订编号

func (*ModuleBuilder) Version added in v0.0.33

func (inst *ModuleBuilder) Version(version string) *ModuleBuilder

Version 设置模块的版本

type ModuleFactory added in v0.0.78

type ModuleFactory interface {

	// 以简单的方式获取一个模块
	ModuleProvider

	// 按照参数定制模块
	CreateModule(params *DefineModule) Module
}

ModuleFactory 提供简单的,或者定制的模块

type ModuleProvider added in v0.0.78

type ModuleProvider interface {
	GetModule() Module
}

ModuleProvider 以简单的方式提供一个模块

type OnLifeFunc added in v0.1.0

type OnLifeFunc func() error

OnLifeFunc 定义生命周期处理函数

type OnMountFunc added in v0.0.33

type OnMountFunc func(cb ConfigBuilder) error

OnMountFunc 是模块挂载函数的签名

type Runnable

type Runnable interface {
	Run(context Context) error
}

type Runtime added in v0.0.48

type Runtime interface {
	Context() Context
	Loop() error
	Exit() error
}

Runtime 提供运行应用程序的更多选项

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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