-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
localpromise.go
42 lines (36 loc) · 1.02 KB
/
localpromise.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
37
38
39
40
41
42
package capnp
// ClientHook for a promise that will be resolved to some other capability
// at some point. Buffers calls in a queue until the promsie is fulfilled,
// then forwards them.
type localPromise struct {
aq *AnswerQueue
}
// NewLocalPromise returns a client that will eventually resolve to a capability,
// supplied via the resolver.
func NewLocalPromise[C ~ClientKind]() (C, Resolver[C]) {
aq := NewAnswerQueue(Method{})
f := NewPromise(Method{}, aq, aq)
p := f.Answer().Client().AddRef()
c := C(p)
r := localResolver[C]{
p: f,
c: c,
}
return c, r
}
type localResolver[C ~ClientKind] struct {
p *Promise
c C
}
func (lf localResolver[C]) Fulfill(c C) {
msg, seg := NewSingleSegmentMessage(nil)
Client(lf.c).AttachReleaser(lf.p.ReleaseClients)
Client(lf.c).AttachReleaser(msg.Release)
capID := msg.CapTable().Add(Client(c))
iface := NewInterface(seg, capID)
lf.p.Fulfill(iface.ToPtr())
}
func (lf localResolver[C]) Reject(err error) {
Client(lf.c).AttachReleaser(lf.p.ReleaseClients)
lf.p.Reject(err)
}