-
-
Notifications
You must be signed in to change notification settings - Fork 984
Expand file tree
/
Copy pathdefaultclient.go
More file actions
110 lines (95 loc) · 3.57 KB
/
defaultclient.go
File metadata and controls
110 lines (95 loc) · 3.57 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package app
import (
"encoding/json"
"io/fs"
"net/http"
"github.com/wavetermdev/waveterm/tsunami/engine"
"github.com/wavetermdev/waveterm/tsunami/vdom"
)
func DefineComponent[P any](name string, renderFn func(props P) any) vdom.Component[P] {
return engine.DefineComponentEx(engine.GetDefaultClient(), name, renderFn)
}
func SetGlobalEventHandler(handler func(event vdom.VDomEvent)) {
engine.GetDefaultClient().SetGlobalEventHandler(handler)
}
// RegisterSetupFn registers a single setup function that is called before the app starts running.
// Only one setup function is allowed, so calling this will replace any previously registered
// setup function.
func RegisterSetupFn(fn func()) {
engine.GetDefaultClient().RegisterSetupFn(fn)
}
// SendAsyncInitiation notifies the frontend that the backend has updated state
// and requires a re-render. Normally the frontend calls the backend in response
// to events, but when the backend changes state independently (e.g., from a
// background process), this function gives the frontend a "nudge" to update.
func SendAsyncInitiation() error {
return engine.GetDefaultClient().SendAsyncInitiation()
}
func ConfigAtom[T any](name string, defaultValue T) Atom[T] {
fullName := "$config." + name
client := engine.GetDefaultClient()
atom := engine.MakeAtomImpl(defaultValue)
client.Root.RegisterAtom(fullName, atom)
return Atom[T]{name: fullName, client: client}
}
func DataAtom[T any](name string, defaultValue T) Atom[T] {
fullName := "$data." + name
client := engine.GetDefaultClient()
atom := engine.MakeAtomImpl(defaultValue)
client.Root.RegisterAtom(fullName, atom)
return Atom[T]{name: fullName, client: client}
}
func SharedAtom[T any](name string, defaultValue T) Atom[T] {
fullName := "$shared." + name
client := engine.GetDefaultClient()
atom := engine.MakeAtomImpl(defaultValue)
client.Root.RegisterAtom(fullName, atom)
return Atom[T]{name: fullName, client: client}
}
// HandleDynFunc registers a dynamic HTTP handler function with the internal http.ServeMux.
// The pattern MUST start with "/dyn/" to be valid. This allows registration of dynamic
// routes that can be handled at runtime.
func HandleDynFunc(pattern string, fn func(http.ResponseWriter, *http.Request)) {
engine.GetDefaultClient().HandleDynFunc(pattern, fn)
}
// RunMain is used internally by generated code and should not be called directly.
func RunMain() {
engine.GetDefaultClient().RunMain()
}
// RegisterEmbeds is used internally by generated code and should not be called directly.
func RegisterEmbeds(assetsFilesystem fs.FS, staticFilesystem fs.FS, manifest []byte) {
client := engine.GetDefaultClient()
client.AssetsFS = assetsFilesystem
client.StaticFS = staticFilesystem
client.ManifestFileBytes = manifest
}
// DeepCopy creates a deep copy of the input value using JSON marshal/unmarshal.
// Panics on JSON errors.
func DeepCopy[T any](v T) T {
data, err := json.Marshal(v)
if err != nil {
panic(err)
}
var result T
err = json.Unmarshal(data, &result)
if err != nil {
panic(err)
}
return result
}
// QueueRefOp queues a reference operation to be executed on the DOM element.
// Operations include actions like "focus", "scrollIntoView", etc.
// If the ref is nil or not current, the operation is ignored.
// This function must be called within a component context.
func QueueRefOp(ref *vdom.VDomRef, op vdom.VDomRefOperation) {
if ref == nil || !ref.HasCurrent {
return
}
if op.RefId == "" {
op.RefId = ref.RefId
}
client := engine.GetDefaultClient()
client.Root.QueueRefOp(op)
}