~repos /gromer

#golang#htmx#ssr

git clone https://pyrossh.dev/repos/gromer.git

gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.


f202d784 pyros2097

5 years ago
improve code structure
Files changed (9) hide show
  1. .gitattributes +0 -2
  2. .gitignore +0 -21
  3. app_nowasm.go +2 -15
  4. app_wasm.go +13 -16
  5. element.go +13 -1
  6. js/js_wasm.go +10 -12
  7. node.go +0 -17
  8. readme.md +2 -2
  9. storage_wasm.go +6 -10
.gitattributes DELETED
@@ -1,2 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
.gitignore DELETED
@@ -1,21 +0,0 @@
1
- # Binaries for programs and plugins
2
- *.exe
3
- *.exe~
4
- *.dll
5
- *.so
6
- *.dylib
7
- *.app
8
-
9
- # Test binary, build with `go test -c`
10
- *.test
11
- test
12
-
13
-
14
- # Output of the go coverage tool, specifically when used with LiteIDE
15
- *.out
16
-
17
- # Static generated files
18
- *.gz
19
- *.gcloudignore
20
- *.wasm
21
- main
app_nowasm.go CHANGED
@@ -2,23 +2,10 @@
2
2
 
3
3
  package app
4
4
 
5
- import (
6
- "net/url"
7
- "os"
8
- )
9
-
10
- func getenv(k string) string {
11
- return os.Getenv(k)
12
- }
13
-
14
- func navigate(u *url.URL, updateHistory bool) error {
15
- panic("wasm required")
16
- }
17
-
18
- func reload() {
5
+ func Reload() {
19
6
  panic("wasm required")
20
7
  }
21
8
 
22
- func run(r RenderFunc) {
9
+ func Run(r RenderFunc) {
23
10
  panic("wasm required")
24
11
  }
app_wasm.go CHANGED
@@ -1,8 +1,7 @@
1
+ // +build wasm
1
2
  package app
2
3
 
3
4
  import (
4
- "net/url"
5
-
6
5
  "github.com/pyros2097/wapp/js"
7
6
  )
8
7
 
@@ -59,17 +58,15 @@ func initContent() {
59
58
  body.body = append(body.body, content)
60
59
  }
61
60
 
62
- func onPopState(this Value, args []Value) interface{} {
61
+ // func onPopState(this js.Value, args []js.Value) interface{} {
63
- dispatch(func() {
62
+ // dispatch(func() {
64
- // navigate(Window().URL(), false)
63
+ // // navigate(Window().URL(), false)
65
- })
64
+ // })
66
- return nil
65
+ // return nil
67
- }
66
+ // }
68
-
69
- func isExternalNavigation(u *url.URL) bool {
67
+ // func isExternalNavigation(u *url.URL) bool {
70
- return u.Host != "" && u.Host != Window().URL().Host
68
+ // return u.Host != "" && u.Host != js.Window().URL().Host
71
- }
69
+ // }
72
-
73
- func isFragmentNavigation(u *url.URL) bool {
70
+ // func isFragmentNavigation(u *url.URL) bool {
74
- return u.Fragment != ""
71
+ // return u.Fragment != ""
75
- }
72
+ // }
element.go CHANGED
@@ -294,7 +294,19 @@ func (e *elem) setEventHandler(k string, h js.EventHandlerFunc) {
294
294
  }
295
295
 
296
296
  func (e *elem) setJsEventHandler(k string, h js.EventHandler) {
297
- jshandler := makeJsEventHandler(e.self(), h.Value)
297
+ jshandler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
298
+ dispatch(func() {
299
+ if !e.self().Mounted() {
300
+ return
301
+ }
302
+ e := js.Event{
303
+ Value: args[0],
304
+ }
305
+ trackMousePosition(e)
306
+ h.Value(e)
307
+ })
308
+ return nil
309
+ })
298
310
  h.JSvalue = jshandler
299
311
  e.events[k] = h
300
312
  e.JSValue().Call("addEventListener", k, jshandler)
js/js_wasm.go CHANGED
@@ -1,11 +1,10 @@
1
+ // +build wasm
1
2
  package js
2
3
 
3
4
  import (
4
5
  "net/url"
5
6
  "reflect"
6
7
  "syscall/js"
7
-
8
- "github.com/pyros2097/wapp/errors"
9
8
  )
10
9
 
11
10
  var Window = &browserWindow{value: value{Value: js.Global()}}
@@ -167,13 +166,14 @@ func (w *browserWindow) ScrollToID(id string) {
167
166
  }
168
167
 
169
168
  func (w *browserWindow) AddEventListener(event string, h EventHandler) func() {
169
+ panic("not implemented")
170
- callback := makeJsEventHandler(body, h)
170
+ // callback := MakeJsEventHandler(body, h)
171
- w.Call("addEventListener", event, callback)
171
+ // w.Call("addEventListener", event, callback)
172
172
 
173
- return func() {
173
+ // return func() {
174
- w.Call("removeEventListener", event, callback)
174
+ // w.Call("removeEventListener", event, callback)
175
- callback.Release()
175
+ // callback.Release()
176
- }
176
+ // }
177
177
  }
178
178
 
179
179
  func (w *browserWindow) Location() *Location {
@@ -181,7 +181,7 @@ func (w *browserWindow) Location() *Location {
181
181
  }
182
182
 
183
183
  type Location struct {
184
- value js.Value
184
+ value Value
185
185
  }
186
186
 
187
187
  func (l *Location) Reload() {
@@ -207,9 +207,7 @@ func jsval(v Value) js.Value {
207
207
  return jsval(v.Value)
208
208
 
209
209
  default:
210
- Log("%s", errors.New("syscall/js value conversion failed").
210
+ println("syscall/js value conversion failed type: " + reflect.TypeOf(v).String())
211
- Tag("type", reflect.TypeOf(v)),
212
- )
213
211
  return js.Undefined()
214
212
  }
215
213
  }
node.go CHANGED
@@ -61,23 +61,6 @@ func FilterUIElems(uis ...interface{}) []UI {
61
61
  return elems
62
62
  }
63
63
 
64
- func makeJsEventHandler(src UI, h js.EventHandlerFunc) js.Func {
65
- return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
66
- dispatch(func() {
67
- if !src.Mounted() {
68
- return
69
- }
70
- e := js.Event{
71
- Value: args[0],
72
- }
73
- trackMousePosition(e)
74
- h(e)
75
- })
76
-
77
- return nil
78
- })
79
- }
80
-
81
64
  func trackMousePosition(e js.Event) {
82
65
  x := e.Get("clientX")
83
66
  if !x.Truthy() {
readme.md CHANGED
@@ -48,8 +48,8 @@ import (
48
48
 
49
49
  func Counter(c *RenderContext) UI {
50
50
  count, setCount := c.UseInt(0)
51
- inc := func() { setCount(count() - 1) }
51
+ inc := func() { setCount(count() + 1) }
52
- dec := func() { setCount(count() + 1) }
52
+ dec := func() { setCount(count() - 1) }
53
53
  return Col(
54
54
  Row(
55
55
  Row(Css("text-6xl"),
storage_wasm.go CHANGED
@@ -3,9 +3,8 @@ package app
3
3
  import (
4
4
  "encoding/json"
5
5
  "sync"
6
- "syscall/js"
7
6
 
8
- "github.com/pyros2097/wapp/errors"
7
+ "github.com/pyros2097/wapp/js"
9
8
  )
10
9
 
11
10
  func init() {
@@ -26,10 +25,7 @@ func (s *jsStorage) Set(k string, v interface{}) (err error) {
26
25
  defer func() {
27
26
  r := recover()
28
27
  if r != nil {
29
- err = errors.New("setting storage value failed").
28
+ panic("setting storage value failed storage-type: " + s.name + "key " + k)
30
- Tag("storage-type", s.name).
31
- Tag("key", k).
32
- Wrap(r.(js.Error))
33
29
  }
34
30
  }()
35
31
 
@@ -41,7 +37,7 @@ func (s *jsStorage) Set(k string, v interface{}) (err error) {
41
37
  return err
42
38
  }
43
39
 
44
- Window().Get(s.name).Call("setItem", k, btos(b))
40
+ js.Window.Get(s.name).Call("setItem", k, btos(b))
45
41
  return nil
46
42
  }
47
43
 
@@ -49,7 +45,7 @@ func (s *jsStorage) Get(k string, v interface{}) error {
49
45
  s.mutex.RLock()
50
46
  defer s.mutex.RUnlock()
51
47
 
52
- item := Window().Get(s.name).Call("getItem", k)
48
+ item := js.Window.Get(s.name).Call("getItem", k)
53
49
  if !item.Truthy() {
54
50
  return nil
55
51
  }
@@ -61,12 +57,12 @@ func (s *jsStorage) Del(k string) {
61
57
  s.mutex.Lock()
62
58
  defer s.mutex.Unlock()
63
59
 
64
- Window().Get(s.name).Call("removeItem", k)
60
+ js.Window.Get(s.name).Call("removeItem", k)
65
61
  }
66
62
 
67
63
  func (s *jsStorage) Clear() {
68
64
  s.mutex.Lock()
69
65
  defer s.mutex.Unlock()
70
66
 
71
- Window().Get(s.name).Call("clear")
67
+ js.Window.Get(s.name).Call("clear")
72
68
  }