-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathcallback.go
36 lines (31 loc) · 798 Bytes
/
callback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package core
import (
"fmt"
"sync"
"github.com/progrium/macdriver/objc"
)
var (
callbacks sync.Map
cbCounter uint64
)
type callbackEntry struct {
cb interface{}
class objc.Class
instance objc.Object
}
func Callback(fn interface{}) (objc.Object, objc.Selector) {
// TODO: better way to identify the same function thats already been registered
cbCounter++
delegateName := fmt.Sprintf("CallbackDelegate%d", cbCounter)
c := objc.NewClass(delegateName, "NSObject")
c.AddMethod("callback:", fn)
objc.RegisterClass(c)
cb := callbackEntry{
cb: fn,
class: c,
instance: objc.Get(delegateName).Alloc().InitObject(),
}
//fmt.Fprintf(os.Stderr, "%#v %d\n", delegateName, &fn)
//callbacks.Store(rfn.Type().String(), cb)
return cb.instance, objc.Sel("callback:")
}