aboutsummaryrefslogblamecommitdiffstats
path: root/vendor/github.com/vektah/gqlgen/graphql/defer.go
blob: 79346a846810f0f3b5aef615111b25f8fd3657fb (plain) (tree)





























                                                                                                                             
package graphql

import (
	"io"
	"sync"
)

// Defer will begin executing the given function and immediately return a result that will block until the function completes
func Defer(f func() Marshaler) Marshaler {
	var deferred deferred
	deferred.mu.Lock()

	go func() {
		deferred.result = f()
		deferred.mu.Unlock()
	}()

	return &deferred
}

type deferred struct {
	result Marshaler
	mu     sync.Mutex
}

func (d *deferred) MarshalGQL(w io.Writer) {
	d.mu.Lock()
	d.result.MarshalGQL(w)
	d.mu.Unlock()
}