reddot

package
v0.7.6 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package reddot 提供小红点 / 未读聚合树:把散落各处的未读数按树形层级聚合, 父节点的未读 = 所有子节点未读之和,叶子清零自动向上传播。纯内存、并发安全。

解决的问题:App"我的"页那个红点,往往由 N 个来源汇总而来——未读消息 + 好友申请 + 活动红点 + 系统通知……手写"某处清零后重算所有祖先"极易漏更或算错。reddot 用 一棵树建模:业务只在叶子上 Set/Clear/Incr,任意节点的聚合未读实时可查,清零沿 父链自动更新。

模型:节点用路径式 ID 组织成树(如 "me/msg/chat")。分两类:

  • 叶子:直接持有未读计数(业务设置);
  • 内部节点:计数 = 所有后代叶子之和(自动聚合,不可直接设置)。

两种未读语义:Count(精确数字,"99+")与 Dot(仅有无红点,布尔)。父节点 Count = 子叶之和,Dot = 任一后代有未读。

树结构在首次访问路径时惰性创建。并发安全(单锁,红点树规模通常很小)。 零值不可用,用 New 构造。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Name  string
	Count int64
}

Entry 一个子节点的名字与聚合未读。

type Option

type Option func(*Tree)

Option 配置 Tree。

func WithSeparator

func WithSeparator(sep string) Option

WithSeparator 设置路径分隔符(默认 "/")。如 "me/msg/chat"。

type Tree

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

Tree 小红点聚合树。零值不可用,用 New 构造。并发安全。

func New

func New(opts ...Option) *Tree

New 创建一棵空的小红点树。

func (*Tree) Children

func (t *Tree) Children(path string) []Entry

Children 返回 path 直接子节点的名字及各自聚合未读(按名字排序),用于渲染分类列表。 路径不存在返回 nil。

func (*Tree) Clear

func (t *Tree) Clear(path string)

Clear 清零 path 子树下所有叶子的未读(等价"已读该分类")。

func (*Tree) Count

func (t *Tree) Count(path string) int64

Count 返回 path 节点的聚合未读数:叶子返回自身计数,内部节点返回所有后代叶子之和。 路径不存在返回 0。

func (*Tree) Dot

func (t *Tree) Dot(path string) bool

Dot 返回 path 是否应显示红点(聚合未读 > 0)。

func (*Tree) Incr

func (t *Tree) Incr(path string, delta int64) int64

Incr 给叶子 path 的未读数增加 delta(结果夹在 >=0),返回增加后的值。

func (*Tree) Set

func (t *Tree) Set(path string, n int64)

Set 把叶子 path 的未读数设为 n(n<0 视为 0)。path 被标记为叶子。

func (*Tree) Total

func (t *Tree) Total() int64

Total 返回整棵树的未读总数(根节点聚合)。

Jump to

Keyboard shortcuts

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