-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_reference.go
More file actions
165 lines (142 loc) · 4.58 KB
/
Copy pathfunction_reference.go
File metadata and controls
165 lines (142 loc) · 4.58 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package convex
import (
"context"
)
// QueryReference is a typed reference to a Convex query function. Generated Go
// API packages can expose values of this type while the string-based APIs stay
// available.
type QueryReference[Args, Result any] struct {
path string
}
// NewQueryReference creates a typed query reference for path.
func NewQueryReference[Args, Result any](path string) QueryReference[Args, Result] {
return QueryReference[Args, Result]{path: path}
}
// Kind returns QueryKind.
func (r QueryReference[Args, Result]) Kind() FunctionKind {
return QueryKind
}
// Path returns the Convex function path.
func (r QueryReference[Args, Result]) Path() string {
return r.path
}
// Query runs this typed query through any client that supports QueryValue.
func (r QueryReference[Args, Result]) Query(
ctx context.Context,
client interface {
QueryValue(context.Context, string, any) (Value, error)
},
args Args,
) (Result, error) {
value, err := client.QueryValue(ctx, r.path, args)
if err != nil {
var zero Result
return zero, err
}
return decodeTypedValue[Result](value)
}
// Subscribe starts a typed WebSocket query subscription.
func (r QueryReference[Args, Result]) Subscribe(ctx context.Context, client *WebSocketClient, args Args) (*TypedQuerySubscription[Result], error) {
subscription, err := client.Subscribe(ctx, r.path, args)
if err != nil {
return nil, err
}
return &TypedQuerySubscription[Result]{raw: subscription}, nil
}
// MutationReference is a typed reference to a Convex mutation function.
type MutationReference[Args, Result any] struct {
path string
}
// NewMutationReference creates a typed mutation reference for path.
func NewMutationReference[Args, Result any](path string) MutationReference[Args, Result] {
return MutationReference[Args, Result]{path: path}
}
// Kind returns MutationKind.
func (r MutationReference[Args, Result]) Kind() FunctionKind {
return MutationKind
}
// Path returns the Convex function path.
func (r MutationReference[Args, Result]) Path() string {
return r.path
}
// Mutation runs this typed mutation through the HTTP client.
func (r MutationReference[Args, Result]) Mutation(ctx context.Context, client *HTTPClient, args Args, opts ...MutationOption) (Result, error) {
value, err := client.MutationValue(ctx, r.path, args, opts...)
if err != nil {
var zero Result
return zero, err
}
return decodeTypedValue[Result](value)
}
// ActionReference is a typed reference to a Convex action function.
type ActionReference[Args, Result any] struct {
path string
}
// NewActionReference creates a typed action reference for path.
func NewActionReference[Args, Result any](path string) ActionReference[Args, Result] {
return ActionReference[Args, Result]{path: path}
}
// Kind returns ActionKind.
func (r ActionReference[Args, Result]) Kind() FunctionKind {
return ActionKind
}
// Path returns the Convex function path.
func (r ActionReference[Args, Result]) Path() string {
return r.path
}
// Action runs this typed action through the HTTP client.
func (r ActionReference[Args, Result]) Action(ctx context.Context, client *HTTPClient, args Args) (Result, error) {
value, err := client.ActionValue(ctx, r.path, args)
if err != nil {
var zero Result
return zero, err
}
return decodeTypedValue[Result](value)
}
// TypedQuerySubscription decodes query subscription updates into Result.
type TypedQuerySubscription[Result any] struct {
raw *QuerySubscription
}
// ID exposes the underlying subscription id.
func (s *TypedQuerySubscription[Result]) ID() SubscriberID {
return s.raw.ID()
}
// Next blocks until the next update and decodes it into Result.
func (s *TypedQuerySubscription[Result]) Next(ctx context.Context) (Result, error) {
result, err := s.raw.Next(ctx)
if err != nil {
var zero Result
return zero, err
}
if err := result.Err(); err != nil {
var zero Result
return zero, err
}
value, _ := result.Value()
return decodeTypedValue[Result](value)
}
// Unsubscribe removes the subscription from the remote query set.
func (s *TypedQuerySubscription[Result]) Unsubscribe(ctx context.Context) error {
return s.raw.Unsubscribe(ctx)
}
// Close closes the local subscription handle.
func (s *TypedQuerySubscription[Result]) Close() error {
return s.raw.Close()
}
func decodeTypedValue[Result any](value Value) (Result, error) {
var out Result
if target, ok := any(&out).(*Value); ok {
*target = value
return out, nil
}
goValue := value.GoValue()
if target, ok := any(&out).(*any); ok {
*target = goValue
return out, nil
}
if err := decodeInto(goValue, &out); err != nil {
var zero Result
return zero, err
}
return out, nil
}