chore: remove nested gojs module (preparing for standalone jsmod repo)
This commit is contained in:
parent
dba62d1b48
commit
5d115260d2
@ -1,3 +0,0 @@
|
|||||||
module apigo.cc/go/js/gojs
|
|
||||||
|
|
||||||
go 1.25.0
|
|
||||||
37
gojs/gojs.go
37
gojs/gojs.go
@ -1,37 +0,0 @@
|
|||||||
package gojs
|
|
||||||
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
var (
|
|
||||||
modules = make(map[string]map[string]any)
|
|
||||||
mu sync.RWMutex
|
|
||||||
)
|
|
||||||
|
|
||||||
// Register registers a Go module with its exported functions and properties.
|
|
||||||
// It is thread-safe and designed to be called during init() functions.
|
|
||||||
// If the same module name is registered multiple times, the exports are merged.
|
|
||||||
func Register(name string, exports map[string]any) {
|
|
||||||
mu.Lock()
|
|
||||||
defer mu.Unlock()
|
|
||||||
|
|
||||||
if modules[name] == nil {
|
|
||||||
modules[name] = make(map[string]any, len(exports))
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range exports {
|
|
||||||
modules[name][k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetModules returns all registered modules.
|
|
||||||
// It is thread-safe and safe to iterate during runtime.
|
|
||||||
func GetModules() map[string]map[string]any {
|
|
||||||
mu.RLock()
|
|
||||||
defer mu.RUnlock()
|
|
||||||
|
|
||||||
res := make(map[string]map[string]any, len(modules))
|
|
||||||
for name, exports := range modules {
|
|
||||||
res[name] = exports // exports map is treated as read-only after initialization
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
@ -1,59 +0,0 @@
|
|||||||
package gojs_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"apigo.cc/go/js/gojs"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestRegisterAndGet(t *testing.T) {
|
|
||||||
gojs.Register("db", map[string]any{
|
|
||||||
"query": func() string { return "ok" },
|
|
||||||
})
|
|
||||||
|
|
||||||
// Test merging
|
|
||||||
gojs.Register("db", map[string]any{
|
|
||||||
"exec": func() int { return 1 },
|
|
||||||
})
|
|
||||||
|
|
||||||
mods := gojs.GetModules()
|
|
||||||
dbMod, ok := mods["db"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatal("expected module 'db' to be registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := dbMod["query"]; !ok {
|
|
||||||
t.Error("expected 'query' in 'db' module")
|
|
||||||
}
|
|
||||||
if _, ok := dbMod["exec"]; !ok {
|
|
||||||
t.Error("expected 'exec' in 'db' module")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConcurrentRegister(t *testing.T) {
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(i int) {
|
|
||||||
defer wg.Done()
|
|
||||||
gojs.Register("concurrent_mod", map[string]any{
|
|
||||||
"func": i,
|
|
||||||
})
|
|
||||||
}(i)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
mods := gojs.GetModules()
|
|
||||||
if _, ok := mods["concurrent_mod"]; !ok {
|
|
||||||
t.Fatal("expected 'concurrent_mod' to be registered")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkGetModules(b *testing.B) {
|
|
||||||
gojs.Register("bench_mod", map[string]any{"a": 1, "b": 2})
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
_ = gojs.GetModules()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user