kcontext

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 7 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContextNode

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

Node 代表树状结构中的一个节点,封装context、cancel函数、名称和唯一ID 提供显式的父子关系管理,防止内存泄露,支持并发安全操作和重名场景查询

func (*ContextNode) AddListener

func (that *ContextNode) AddListener(listener MessageCallback) (listenerID string)

AddListener 添加监听器,返回唯一监听器ID

func (*ContextNode) Cancel

func (that *ContextNode) Cancel()

Cancel 取消当前节点及其子节点的context 与原生context的cancel等价,子context自动收到Done信号

func (*ContextNode) Children

func (that *ContextNode) Children() []*ContextNode

GetChildren 返回当前节点的子节点列表(只读)

func (*ContextNode) Context

func (that *ContextNode) Context() context.Context

Context 返回节点的context

func (*ContextNode) FindAllNodesByName

func (that *ContextNode) FindAllNodesByName(name string) []struct {
	Node *ContextNode
	Path []string
}

FindAllNodesByName 使用DFS查找所有匹配名称的节点 返回节点及其路径的列表,若无匹配返回空列表

func (*ContextNode) FindNodeByID

func (that *ContextNode) FindNodeByID(id string) (*ContextNode, []string)

FindNodeByID 使用DFS查找匹配ID的节点 ID唯一,直接返回节点及其路径,若未找到返回nil

func (*ContextNode) FindNodeByName

func (that *ContextNode) FindNodeByName(name string, parentID string) (*ContextNode, []string)

FindNodeByName 使用DFS查找第一个匹配名称的节点 可选parentID约束查找范围,返回节点及其路径,若未找到返回nil

func (*ContextNode) GetPath

func (that *ContextNode) GetPath() []string

GetPath 返回节点从根到自身的路径(名称列表)

func (*ContextNode) GetPathWithIDs

func (that *ContextNode) GetPathWithIDs() []string

GetPathWithIDs 返回节点从根到自身的路径(ID列表)

func (*ContextNode) ID

func (that *ContextNode) ID() string

ID 返回节点的唯一ID

func (*ContextNode) Name

func (that *ContextNode) Name() string

Name 返回节点的名称

func (*ContextNode) NewChild

func (that *ContextNode) NewChild(name string) *ContextNode

AddChild 为父节点添加子节点 自动创建子context并维护父子关系,生成唯一ID,线程安全

func (*ContextNode) NewChildWithTag

func (that *ContextNode) NewChildWithTag(name string, tag any) *ContextNode

AddChild 为父节点添加子节点 自动创建子context并维护父子关系,生成唯一ID,线程安全

func (*ContextNode) Notify

func (that *ContextNode) Notify(source *ContextNode, msg any)

Notify 异步通知所有监听器

func (*ContextNode) Parent

func (that *ContextNode) Parent() *ContextNode

GetParent 返回当前节点的父节点 如果是根节点,返回nil,线程安全

func (*ContextNode) Remove

func (that *ContextNode) Remove()

Remove 从父节点中移除当前节点,清理引用以防止内存泄露

func (*ContextNode) RemoveListener

func (that *ContextNode) RemoveListener(listenerID string)

RemoveListener 移除指定监听器

func (*ContextNode) Root

func (that *ContextNode) Root() *ContextNode

GetRoot 返回树的根节点 通过递归向上遍历,线程安全

func (*ContextNode) SetTag

func (that *ContextNode) SetTag(tag any)

SetTag 设置节点的tag值 支持任意类型,线程安全

func (*ContextNode) String

func (that *ContextNode) String() string

String 返回以当前节点为根的子树的JSON表示 仅包含id、name和children字段,格式化输出,线程安全

func (*ContextNode) Tag

func (that *ContextNode) Tag() any

GetTag 获取节点的tag值 返回nil如果未设置,线程安全

type ContextTree

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

ContextManager 管理整个上下文树,提供统一入口

func NewContextTree

func NewContextTree(rootName string) *ContextTree

NewContextManager 创建新的上下文管理器 相比原生context,提供显式树结构、资源清理和名称/ID查询功能

func (*ContextTree) Close

func (that *ContextTree) Close()

Close 关闭整个上下文树,清理所有监听器和通道

func (*ContextTree) GetRoot

func (that *ContextTree) GetRoot() *ContextNode

GetRoot 返回根节点

type MessageCallback

type MessageCallback func(source *ContextNode, msg any)

Jump to

Keyboard shortcuts

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