Skip to content

Commit 558c626

Browse files
author
mazheng
committed
翻译注释,增加许可证
1 parent f7fe83d commit 558c626

File tree

1 file changed

+56
-39
lines changed

1 file changed

+56
-39
lines changed

transport/callback.go

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,113 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package getty
219

3-
// callbackCommon 表示回调链表中的一个节点
4-
// 每个节点包含处理器标识、键值、回调函数和指向下一个节点的指针
20+
// callbackCommon represents a node in the callback linked list
21+
// Each node contains handler identifier, key, callback function and pointer to next node
522
type callbackCommon struct {
6-
handler interface{} // 处理器标识,用于标识回调的来源或类型
7-
key interface{} // 回调的唯一标识键,与 handler 组合使用
8-
call func() // 实际要执行的回调函数
9-
next *callbackCommon // 指向下一个节点的指针,形成链表结构
23+
handler interface{} // Handler identifier, used to identify the source or type of callback
24+
key interface{} // Unique identifier key for callback, used in combination with handler
25+
call func() // Actual callback function to be executed
26+
next *callbackCommon // Pointer to next node, forming linked list structure
1027
}
1128

12-
// callbacks 是一个单向链表结构,用于管理多个回调函数
13-
// 支持动态添加、移除和执行回调
29+
// callbacks is a singly linked list structure for managing multiple callback functions
30+
// Supports dynamic addition, removal and execution of callbacks
1431
type callbacks struct {
15-
first *callbackCommon // 指向链表第一个节点的指针
16-
last *callbackCommon // 指向链表最后一个节点的指针,用于快速添加新节点
32+
first *callbackCommon // Pointer to the first node of the linked list
33+
last *callbackCommon // Pointer to the last node of the linked list, used for quick addition of new nodes
1734
}
1835

19-
// Add 向回调链表中添加一个新的回调函数
20-
// 参数说明:
21-
// - handler: 处理器标识,可以是任意类型
22-
// - key: 回调的唯一标识键,与 handler 组合使用
23-
// - callback: 要执行的回调函数,如果为 nil 则忽略
36+
// Add adds a new callback function to the callback linked list
37+
// Parameters:
38+
// - handler: Handler identifier, can be any type
39+
// - key: Unique identifier key for callback, used in combination with handler
40+
// - callback: Callback function to be executed, ignored if nil
2441
func (t *callbacks) Add(handler, key interface{}, callback func()) {
25-
// 防止添加空回调函数
42+
// Prevent adding empty callback function
2643
if callback == nil {
2744
return
2845
}
2946

30-
// 创建新的回调节点
47+
// Create new callback node
3148
newItem := &callbackCommon{handler, key, callback, nil}
3249

3350
if t.first == nil {
34-
// 如果链表为空,新节点成为第一个节点
51+
// If linked list is empty, new node becomes the first node
3552
t.first = newItem
3653
} else {
37-
// 否则将新节点添加到链表末尾
54+
// Otherwise add new node to the end of linked list
3855
t.last.next = newItem
3956
}
40-
// 更新最后一个节点的指针
57+
// Update pointer to last node
4158
t.last = newItem
4259
}
4360

44-
// Remove 从回调链表中移除指定的回调函数
45-
// 参数说明:
46-
// - handler: 要移除的回调的处理器标识
47-
// - key: 要移除的回调的唯一标识键
48-
// 注意: 如果找不到匹配的回调,此方法不会产生任何效果
61+
// Remove removes the specified callback function from the callback linked list
62+
// Parameters:
63+
// - handler: Handler identifier of the callback to be removed
64+
// - key: Unique identifier key of the callback to be removed
65+
// Note: If no matching callback is found, this method has no effect
4966
func (t *callbacks) Remove(handler, key interface{}) {
5067
var prev *callbackCommon
5168

52-
// 遍历链表查找要移除的节点
69+
// Traverse linked list to find the node to be removed
5370
for callback := t.first; callback != nil; prev, callback = callback, callback.next {
54-
// 找到匹配的节点
71+
// Found matching node
5572
if callback.handler == handler && callback.key == key {
5673
if t.first == callback {
57-
// 如果是第一个节点,更新 first 指针
74+
// If it's the first node, update first pointer
5875
t.first = callback.next
5976
} else if prev != nil {
60-
// 如果是中间节点,更新前一个节点的 next 指针
77+
// If it's a middle node, update the next pointer of the previous node
6178
prev.next = callback.next
6279
}
6380

6481
if t.last == callback {
65-
// 如果是最后一个节点,更新 last 指针
82+
// If it's the last node, update last pointer
6683
t.last = prev
6784
}
6885

69-
// 找到并移除后立即返回
86+
// Return immediately after finding and removing
7087
return
7188
}
7289
}
7390
}
7491

75-
// Invoke 执行链表中所有注册的回调函数
76-
// 按照添加的顺序依次执行每个回调
77-
// 注意: 如果某个回调函数为 nil,会被跳过
92+
// Invoke executes all registered callback functions in the linked list
93+
// Executes each callback in the order they were added
94+
// Note: If a callback function is nil, it will be skipped
7895
func (t *callbacks) Invoke() {
79-
// 从头节点开始遍历整个链表
96+
// Traverse the entire linked list starting from the head node
8097
for callback := t.first; callback != nil; callback = callback.next {
81-
// 确保回调函数不为 nil 再执行
98+
// Ensure callback function is not nil before executing
8299
if callback.call != nil {
83100
callback.call()
84101
}
85102
}
86103
}
87104

88-
// Count 返回链表中回调函数的数量
89-
// 返回值: 当前注册的回调函数总数
105+
// Count returns the number of callback functions in the linked list
106+
// Return value: Total number of currently registered callback functions
90107
func (t *callbacks) Count() int {
91108
var count int
92109

93-
// 遍历链表计数
110+
// Traverse linked list to count
94111
for callback := t.first; callback != nil; callback = callback.next {
95112
count++
96113
}

0 commit comments

Comments
 (0)