Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
Signed-off-by: Bulat Shakirzyanov <83289+avalanche123@users.noreply.github.com>
  • Loading branch information
avalanche123 committed Sep 6, 2023
1 parent 3c3d8ad commit 03d39b3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/clients/live_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func (c *liveQueryCache) trackObjectList(ctx context.Context, lq *runtime.LiveQu
return err
}

// Get implements cache.Cache. It wraps an underlying cache.Cache and sets up an Informer
// event handler that marks current live query as dirty if the current context has a live query.
func (c *liveQueryCache) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
if err := c.Cache.Get(ctx, key, obj, opts...); err != nil {
return err
Expand All @@ -152,6 +154,8 @@ func (c *liveQueryCache) Get(ctx context.Context, key client.ObjectKey, obj clie
return nil
}

// List implements cache.Cache. It wraps an underlying cache.Cache and sets up an Informer
// event handler that marks current live query as dirty if the current context has a live query.
func (c *liveQueryCache) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
if err := c.Cache.List(ctx, list, opts...); err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions internal/graph/extensions/live_query/json_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func parseJSON(in []byte) (out any) {
return out
}

// CreateJSONPatch creates a JSON patch between two json values.
func CreateJSONPatch(x, y []byte) ([]Operation, error) {
if !json.Valid(x) || !json.Valid(y) {
return nil, fmt.Errorf("invalid JSON")
Expand Down
16 changes: 16 additions & 0 deletions internal/graph/extensions/live_query/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,48 @@ import (
type QueryResolveFn func(ctx context.Context, sel ast.SelectionSet) graphql.Marshaler
type CancelFn func()

// LiveQuery is set in context in generated liveQuery resolver.
// Resolvers that need to refresh the query can use GetLiveQuery() to
// check if the current response is a live query.
// They can then use LiveQuery.IsDone() and LiveQuery.Notify() to
// check if the live query is still running and trigger a live query refresh
// accordingly.
type LiveQuery struct {
ctx context.Context
isDirty uint32
}

// IsDone returns true if live query finished. This is useful for stopping
// async listeners for discarded queries.
func (l *LiveQuery) IsDone() bool {
return l.ctx.Err() != nil
}

// Notify marks live query as dirty, triggering a live query refresh on next
// throttle interval.
func (l *LiveQuery) Notify() {
atomic.StoreUint32(&l.isDirty, 1)
}

// NeedsRefresh is a func that can be used to check if live query needs to be
// refreshed. It is used in generated live query resolver.
type NeedsRefresh func() bool

type liveQueryKey struct{}

var liveQueryCtxKey = liveQueryKey{}

// WithLiveQuery sets LiveQuery on derived context and returns a callable for
// checking if live query needs to be refreshed. This is used in generated
// live query resolver to set up periodic live query refresh if changes occured.
func WithLiveQuery(ctx context.Context) (context.Context, NeedsRefresh) {
lq := &LiveQuery{ctx: ctx}
return context.WithValue(ctx, liveQueryCtxKey, lq), func() bool {
return atomic.CompareAndSwapUint32(&lq.isDirty, 1, 0)
}
}

// GetLiveQuery returns live query from context or nil if isn't set.
func GetLiveQuery(ctx context.Context) *LiveQuery {
if lq, ok := ctx.Value(liveQueryCtxKey).(*LiveQuery); ok {
return lq
Expand Down

0 comments on commit 03d39b3

Please sign in to comment.