diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2024-02-06 16:42:47 +0100 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2024-02-19 14:15:07 +0100 |
| commit | 50ca13f2928e6598f9a41dd987e75f798bfe4536 (patch) | |
| tree | 75c9f06c36bf2661007aeb9f0e5a01a3554abf56 /types/cookie/cookie.go | |
| parent | a84050a5e93f5fb9f5bbb79ca21b37e8359cf289 (diff) | |
Cookie + Exports: Store and return the cookie from the underlying context
This has the ability so that we can easily get the same cookie back in
the client package by creating the one from the context
Diffstat (limited to 'types/cookie/cookie.go')
| -rw-r--r-- | types/cookie/cookie.go | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/types/cookie/cookie.go b/types/cookie/cookie.go index 55fe938..1582a9c 100644 --- a/types/cookie/cookie.go +++ b/types/cookie/cookie.go @@ -19,11 +19,21 @@ type Cookie struct { H cgo.Handle } +type contextt int8 + +const CONTEXTK contextt = 0 + // NewWithContext creates a new cookie with a context // It stores the cancel and channel inside of the struct -func NewWithContext(ctx context.Context) Cookie { +func NewWithContext(ctx context.Context) *Cookie { + // if the context already has a handle, return that cookie + if h, ok := ctx.Value(CONTEXTK).(cgo.Handle); ok { + if ck, ok := h.Value().(*Cookie); ok { + return ck + } + } ctx, cancel := context.WithCancel(ctx) - return Cookie{ + return &Cookie{ c: make(chan string), ctx: ctx, ctxCancel: cancel, @@ -77,5 +87,5 @@ func (c *Cookie) Send(data string) error { // Context gets the underlying context of the cookie func (c *Cookie) Context() context.Context { - return c.ctx + return context.WithValue(c.ctx, CONTEXTK, c.H) } |
