aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/vektah/gqlgen/graphql/defer.go
blob: 79346a846810f0f3b5aef615111b25f8fd3657fb (plain) (blame)
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
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()
}