notify_common.go (777B)
1 package nativenotify 2 3 import ( 4 "encoding/hex" 5 "sync" 6 "sync/atomic" 7 ) 8 9 var ( 10 nextID atomic.Int64 11 callbacks sync.Map 12 ) 13 14 func callbacksTake(m *sync.Map, id string) (Callback, bool) { 15 v, ok := m.LoadAndDelete(id) 16 if !ok { 17 return nil, false 18 } 19 cb, ok := v.(Callback) 20 if !ok { 21 return nil, false 22 } 23 return cb, true 24 } 25 26 func callbacksPut(m *sync.Map, id string, fn Callback) { 27 m.Store(id, fn) 28 } 29 30 // take returns the first element. 31 func take[S ~[]E, E any](s *S) (e E, ok bool) { 32 if len(*s) == 0 { 33 return e, false 34 } 35 defer func() { *s = (*s)[1:] }() 36 return (*s)[0], true 37 } 38 39 // decode from hex. 40 func decode(s string) string { 41 b, _ := hex.DecodeString(s) 42 return string(b) 43 } 44 45 // encode to hex. 46 func encode(s string) string { 47 return hex.EncodeToString([]byte(s)) 48 }