|
| 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 | + |
1 | 18 | package getty |
2 | 19 |
|
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 |
5 | 22 | 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 |
10 | 27 | } |
11 | 28 |
|
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 |
14 | 31 | 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 |
17 | 34 | } |
18 | 35 |
|
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 |
24 | 41 | func (t *callbacks) Add(handler, key interface{}, callback func()) { |
25 | | - // 防止添加空回调函数 |
| 42 | + // Prevent adding empty callback function |
26 | 43 | if callback == nil { |
27 | 44 | return |
28 | 45 | } |
29 | 46 |
|
30 | | - // 创建新的回调节点 |
| 47 | + // Create new callback node |
31 | 48 | newItem := &callbackCommon{handler, key, callback, nil} |
32 | 49 |
|
33 | 50 | if t.first == nil { |
34 | | - // 如果链表为空,新节点成为第一个节点 |
| 51 | + // If linked list is empty, new node becomes the first node |
35 | 52 | t.first = newItem |
36 | 53 | } else { |
37 | | - // 否则将新节点添加到链表末尾 |
| 54 | + // Otherwise add new node to the end of linked list |
38 | 55 | t.last.next = newItem |
39 | 56 | } |
40 | | - // 更新最后一个节点的指针 |
| 57 | + // Update pointer to last node |
41 | 58 | t.last = newItem |
42 | 59 | } |
43 | 60 |
|
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 |
49 | 66 | func (t *callbacks) Remove(handler, key interface{}) { |
50 | 67 | var prev *callbackCommon |
51 | 68 |
|
52 | | - // 遍历链表查找要移除的节点 |
| 69 | + // Traverse linked list to find the node to be removed |
53 | 70 | for callback := t.first; callback != nil; prev, callback = callback, callback.next { |
54 | | - // 找到匹配的节点 |
| 71 | + // Found matching node |
55 | 72 | if callback.handler == handler && callback.key == key { |
56 | 73 | if t.first == callback { |
57 | | - // 如果是第一个节点,更新 first 指针 |
| 74 | + // If it's the first node, update first pointer |
58 | 75 | t.first = callback.next |
59 | 76 | } else if prev != nil { |
60 | | - // 如果是中间节点,更新前一个节点的 next 指针 |
| 77 | + // If it's a middle node, update the next pointer of the previous node |
61 | 78 | prev.next = callback.next |
62 | 79 | } |
63 | 80 |
|
64 | 81 | if t.last == callback { |
65 | | - // 如果是最后一个节点,更新 last 指针 |
| 82 | + // If it's the last node, update last pointer |
66 | 83 | t.last = prev |
67 | 84 | } |
68 | 85 |
|
69 | | - // 找到并移除后立即返回 |
| 86 | + // Return immediately after finding and removing |
70 | 87 | return |
71 | 88 | } |
72 | 89 | } |
73 | 90 | } |
74 | 91 |
|
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 |
78 | 95 | func (t *callbacks) Invoke() { |
79 | | - // 从头节点开始遍历整个链表 |
| 96 | + // Traverse the entire linked list starting from the head node |
80 | 97 | for callback := t.first; callback != nil; callback = callback.next { |
81 | | - // 确保回调函数不为 nil 再执行 |
| 98 | + // Ensure callback function is not nil before executing |
82 | 99 | if callback.call != nil { |
83 | 100 | callback.call() |
84 | 101 | } |
85 | 102 | } |
86 | 103 | } |
87 | 104 |
|
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 |
90 | 107 | func (t *callbacks) Count() int { |
91 | 108 | var count int |
92 | 109 |
|
93 | | - // 遍历链表计数 |
| 110 | + // Traverse linked list to count |
94 | 111 | for callback := t.first; callback != nil; callback = callback.next { |
95 | 112 | count++ |
96 | 113 | } |
|
0 commit comments