Skip to content

Commit 731aaf8

Browse files
committed
mutex for globalctx
1 parent a48c80b commit 731aaf8

2 files changed

Lines changed: 38 additions & 24 deletions

File tree

‎tsunami/comp/globalctx.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025, Command Line Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package comp
5+
6+
import (
7+
"sync"
8+
9+
"github.com/outrigdev/goid"
10+
)
11+
12+
// is set ONLY when we're in the render function of a component
13+
// used for hooks, and automatic dependency tracking
14+
var globalVC *VDomContextImpl
15+
var globalRenderGoId uint64
16+
var globalCtxMutex sync.Mutex
17+
18+
func withGlobalCtx[T any](vc *VDomContextImpl, fn func() T) T {
19+
globalCtxMutex.Lock()
20+
defer globalCtxMutex.Unlock()
21+
globalVC = vc
22+
globalRenderGoId = goid.Get()
23+
defer func() {
24+
globalVC = nil
25+
globalRenderGoId = 0
26+
}()
27+
return fn()
28+
}
29+
30+
func GetGlobalContext() *VDomContextImpl {
31+
globalCtxMutex.Lock()
32+
defer globalCtxMutex.Unlock()
33+
gid := goid.Get()
34+
if gid != globalRenderGoId {
35+
return nil
36+
}
37+
return globalVC
38+
}

‎tsunami/comp/rootelem.go‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"unicode"
1515

1616
"github.com/google/uuid"
17-
"github.com/outrigdev/goid"
1817
"github.com/wavetermdev/waveterm/tsunami/rpctypes"
1918
"github.com/wavetermdev/waveterm/tsunami/util"
2019
"github.com/wavetermdev/waveterm/tsunami/vdom"
@@ -23,11 +22,6 @@ import (
2322

2423
const ChildrenPropKey = "children"
2524

26-
// is set ONLY when we're in the render function of a component
27-
// used for hooks, and automatic dependency tracking
28-
var globalVC *VDomContextImpl
29-
var globalRenderGoId uint64
30-
3125
type RenderOpts struct {
3226
Resync bool
3327
}
@@ -443,24 +437,6 @@ func callCFunc(cfunc any, ctx context.Context, props map[string]any) any {
443437
return rtnVal[0].Interface()
444438
}
445439

446-
func withGlobalCtx[T any](vc *VDomContextImpl, fn func() T) T {
447-
globalVC = vc
448-
globalRenderGoId = goid.Get()
449-
defer func() {
450-
globalVC = nil
451-
globalRenderGoId = 0
452-
}()
453-
return fn()
454-
}
455-
456-
func GetGlobalContext() *VDomContextImpl {
457-
gid := goid.Get()
458-
if gid != globalRenderGoId {
459-
return nil
460-
}
461-
return globalVC
462-
}
463-
464440
func (r *RootElem) renderComponent(cfunc any, elem *vdom.VDomElem, comp **ComponentImpl, opts *RenderOpts) {
465441
if (*comp).Children != nil {
466442
for _, child := range (*comp).Children {

0 commit comments

Comments
 (0)