forked from cshum/imagor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
51 lines (41 loc) · 1.04 KB
/
context.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
43
44
45
46
47
48
49
50
51
package vips
import (
"context"
)
type contextRefKey struct{}
type contextRef struct {
cbs []func()
Rotate90 bool
}
func (r *contextRef) Defer(cb func()) {
r.cbs = append(r.cbs, cb)
}
func (r *contextRef) Done() {
for _, cb := range r.cbs {
cb()
}
r.cbs = nil
}
// withContext with callback tracking
func withContext(ctx context.Context) context.Context {
return context.WithValue(ctx, contextRefKey{}, &contextRef{})
}
// contextDefer context add func for callback tracking for callback gc
func contextDefer(ctx context.Context, cb func()) {
ctx.Value(contextRefKey{}).(*contextRef).Defer(cb)
}
// contextDone closes all image refs that are being tracked through the context
func contextDone(ctx context.Context) {
ctx.Value(contextRefKey{}).(*contextRef).Done()
}
func setRotate90(ctx context.Context) {
if r, ok := ctx.Value(contextRefKey{}).(*contextRef); ok {
r.Rotate90 = !r.Rotate90
}
}
func isRotate90(ctx context.Context) bool {
if r, ok := ctx.Value(contextRefKey{}).(*contextRef); ok {
return r.Rotate90
}
return false
}