|
| 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 | + |
| 18 | +package getty |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "reflect" |
| 23 | +) |
| 24 | + |
| 25 | +import ( |
| 26 | + perrors "github.com/pkg/errors" |
| 27 | +) |
| 28 | + |
| 29 | +import ( |
| 30 | + log "github.com/AlexStocks/getty/util" |
| 31 | +) |
| 32 | + |
| 33 | +// callbackNode represents a node in the callback linked list |
| 34 | +// Each node contains handler identifier, key, callback function and pointer to next node |
| 35 | +type callbackNode struct { |
| 36 | + handler any // Handler identifier, used to identify the source or type of callback |
| 37 | + key any // Unique identifier key for callback, used in combination with handler |
| 38 | + call func() // Actual callback function to be executed |
| 39 | + next *callbackNode // Pointer to next node, forming linked list structure |
| 40 | +} |
| 41 | + |
| 42 | +// callbacks is a singly linked list structure for managing multiple callback functions |
| 43 | +// Supports dynamic addition, removal and execution of callbacks |
| 44 | +type callbacks struct { |
| 45 | + first *callbackNode // Pointer to the first node of the linked list |
| 46 | + last *callbackNode // Pointer to the last node of the linked list, used for quick addition of new nodes |
| 47 | + cbNum int // Number of callback functions in the linked list |
| 48 | +} |
| 49 | + |
| 50 | +// isComparable checks if a value is comparable using Go's == operator |
| 51 | +// Returns true if the value can be safely compared, false otherwise |
| 52 | +func isComparable(v any) bool { |
| 53 | + if v == nil { |
| 54 | + return true |
| 55 | + } |
| 56 | + return reflect.TypeOf(v).Comparable() |
| 57 | +} |
| 58 | + |
| 59 | +// Add adds a new callback function to the callback linked list |
| 60 | +// Parameters: |
| 61 | +// - handler: Handler identifier, can be any type |
| 62 | +// - key: Unique identifier key for callback, used in combination with handler |
| 63 | +// - callback: Callback function to be executed, ignored if nil |
| 64 | +// |
| 65 | +// Note: If a callback with the same handler and key already exists, it will be replaced |
| 66 | +func (t *callbacks) Add(handler, key any, callback func()) { |
| 67 | + // Prevent adding empty callback function |
| 68 | + if callback == nil { |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + // Guard: avoid runtime panic on non-comparable types |
| 73 | + if !isComparable(handler) || !isComparable(key) { |
| 74 | + log.Error(perrors.New(fmt.Sprintf("callbacks.Add: non-comparable handler/key: %T, %T; ignored", handler, key))) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + // Check if a callback with the same handler and key already exists |
| 79 | + for cb := t.first; cb != nil; cb = cb.next { |
| 80 | + if cb.handler == handler && cb.key == key { |
| 81 | + // Replace existing callback |
| 82 | + cb.call = callback |
| 83 | + return |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Create new callback node |
| 88 | + newItem := &callbackNode{handler, key, callback, nil} |
| 89 | + |
| 90 | + if t.first == nil { |
| 91 | + // If linked list is empty, new node becomes the first node |
| 92 | + t.first = newItem |
| 93 | + } else { |
| 94 | + // Otherwise add new node to the end of linked list |
| 95 | + t.last.next = newItem |
| 96 | + } |
| 97 | + // Update pointer to last node |
| 98 | + t.last = newItem |
| 99 | + // Increment callback count |
| 100 | + t.cbNum++ |
| 101 | +} |
| 102 | + |
| 103 | +// Remove removes the specified callback function from the callback linked list |
| 104 | +// Parameters: |
| 105 | +// - handler: Handler identifier of the callback to be removed |
| 106 | +// - key: Unique identifier key of the callback to be removed |
| 107 | +// |
| 108 | +// Note: If no matching callback is found, this method has no effect |
| 109 | +func (t *callbacks) Remove(handler, key any) { |
| 110 | + // Guard: avoid runtime panic on non-comparable types |
| 111 | + if !isComparable(handler) || !isComparable(key) { |
| 112 | + log.Error(perrors.New(fmt.Sprintf("callbacks.Remove: non-comparable handler/key: %T, %T; ignored", handler, key))) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + var prev *callbackNode |
| 117 | + |
| 118 | + // Traverse linked list to find the node to be removed |
| 119 | + for callback := t.first; callback != nil; prev, callback = callback, callback.next { |
| 120 | + // Found matching node |
| 121 | + if callback.handler == handler && callback.key == key { |
| 122 | + if t.first == callback { |
| 123 | + // If it's the first node, update first pointer |
| 124 | + t.first = callback.next |
| 125 | + } else if prev != nil { |
| 126 | + // If it's a middle node, update the next pointer of the previous node |
| 127 | + prev.next = callback.next |
| 128 | + } |
| 129 | + |
| 130 | + if t.last == callback { |
| 131 | + // If it's the last node, update last pointer |
| 132 | + t.last = prev |
| 133 | + } |
| 134 | + |
| 135 | + // Decrement callback count |
| 136 | + t.cbNum-- |
| 137 | + |
| 138 | + // Return immediately after finding and removing |
| 139 | + return |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// Invoke executes all registered callback functions in the linked list |
| 145 | +// Executes each callback in the order they were added |
| 146 | +// Note: If a callback function is nil, it will be skipped |
| 147 | +// If a callback panics, it will be handled by the outer caller's panic recovery |
| 148 | +func (t *callbacks) Invoke() { |
| 149 | + // Traverse the entire linked list starting from the head node |
| 150 | + for callback := t.first; callback != nil; callback = callback.next { |
| 151 | + // Ensure callback function is not nil before executing |
| 152 | + if callback.call != nil { |
| 153 | + callback.call() |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// Len returns the number of callback functions in the linked list |
| 159 | +// Return value: Total number of currently registered callback functions |
| 160 | +func (t *callbacks) Len() int { |
| 161 | + return t.cbNum |
| 162 | +} |
0 commit comments