Add adds a new callback function to the callback linked list Parameters: - handler: Handler identifier, can be any type - key: Unique identifier key for callback, used in combination with handler - callback: Callback function to be executed, ignored if nil Note: If a callback with the same handler
(handler, key any, callback func())
| 24 | // |
| 25 | // Note: If a callback with the same handler and key already exists, it will be replaced |
| 26 | func (t *callbacks) Add(handler, key any, callback func()) { |
| 27 | // Prevent adding empty callback function |
| 28 | if callback == nil { |
| 29 | return |
| 30 | } |
| 31 | |
| 32 | // Check if a callback with the same handler and key already exists |
| 33 | for cb := t.first; cb != nil; cb = cb.next { |
| 34 | if cb.handler == handler && cb.key == key { |
| 35 | // Replace existing callback |
| 36 | cb.call = callback |
| 37 | return |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // Create new callback node |
| 42 | newItem := &callbackNode{handler, key, callback, nil} |
| 43 | |
| 44 | if t.first == nil { |
| 45 | // If linked list is empty, new node becomes the first node |
| 46 | t.first = newItem |
| 47 | } else { |
| 48 | // Otherwise add new node to the end of linked list |
| 49 | t.last.next = newItem |
| 50 | } |
| 51 | // Update pointer to last node |
| 52 | t.last = newItem |
| 53 | } |
| 54 | |
| 55 | // Remove removes the specified callback function from the callback linked list |
| 56 | // Parameters: |
no outgoing calls