Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Nov 10, 2021
0 parents commit 76d4100
Show file tree
Hide file tree
Showing 78 changed files with 8,131 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"

- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
18 changes: 18 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: test

on: push

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.17'
- run: make cover
- uses: codecov/codecov-action@v2
with:
file: ./coverage.txt
fail_ci_if_error: true
31 changes: 31 additions & 0 deletions .github/workflows/deploy-gh.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: deploy

on: push

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: actions/setup-node@v2
with:
node-version: "^16"

- uses: actions/setup-go@v2
with:
go-version: '^1.17'

- uses: pnpm/action-setup@v2
with:
version: "^6"

- run: make build.gh-pages

- uses: JamesIves/github-pages-deploy-action@4.1.5
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: gh-pages
FOLDER: ./cmd/webapp/dist
CLEAN: true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 morlay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BASE_PATH=/

dev:
BASE_PATH=$(BASE_PATH) pnpx vite --host --config=vite.config.ts

build: pnpm.i
BASE_PATH=$(BASE_PATH) pnpx vite --config=vite.config.ts build

build.gh-pages:
$(MAKE) build -e BASE_PATH=/gox/

preview: build
pnpx vite preview --host --port 8080 ./cmd/webapp

pnpm.i:
pnpm i

test: tidy
go test -race ./pkg/...

cover:
go test -race -coverprofile=coverage.txt -covermode=atomic ./pkg/...

tidy: fmt
go mod tidy

fmt:
goimports -l -w ./pkg

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# GoX

[Experimental] React-like library for Golang in WASM

## Features

* Virtual DOM and HTML DSL with function calls.
* CSS in Go like [Emotion JS](https://github.com/emotion-js/emotion) did.
* `Fragment` && `Portal` supports.
* Component support as `interface { Render(ctx context.Context, childen ...interface{}) interface{}}`.
* Basic hooks support `UseState`, `UseEffect`, `UseMemo`, `UseRef`
* `UseContext` not needed in Go, the `context.Context` will pass into Component
* Request HTTP in web worker by XHR

## Known Issues

* Compiled to wasm
* bundle size is so big, but could be compressed by `gzip`/`brotli` (brotli is better)

Binary file added cmd/webapp/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions cmd/webapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
dist/
12 changes: 12 additions & 0 deletions cmd/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Webapp</title>
</head>
<body>
<div id="root"></div>
<div id="portal"></div>
</body>
<script type="module" src="./main.ts"></script>
</html>
175 changes: 175 additions & 0 deletions cmd/webapp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package main

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"

. "github.com/go-courier/gox/pkg/css"
. "github.com/go-courier/gox/pkg/dom"
. "github.com/go-courier/gox/pkg/gox"
"github.com/go-courier/gox/pkg/gox/renderer"
"github.com/go-courier/gox/pkg/httputil"
)

func withCSSCache(c *CSSCache) func(ctx context.Context) context.Context {
return func(ctx context.Context) context.Context {
return ContextWithCSSCache(ctx, c)
}
}

var css = NewCSSCache("app", Document.QuerySelector("head"))

var spanList = make([]interface{}, 0)

func init() {
for i := 0; i < 10000; i++ {
spanList = append(spanList, Span(fmt.Sprintf(" %d ", i)))
}
}

func UseOn(ctx context.Context, eventName string, handle func(event Event)) *Ref {
elRef := UseRef(ctx, nil)
handleRef := UseRef(ctx, nil)
handleRef.Current = handle

UseEffect(ctx, func() func() {
if n, ok := elRef.Current.(Node); ok {
h := func(event Event) {
handleRef.Current.(func(event Event))(event)
}

n.AddEventListener(eventName, h)
return func() {
n.RemoveEventListener(eventName, h)
}
}
return nil
}, []interface{}{elRef.Current})

return elRef
}

type SubComp struct {
}

func (a SubComp) Render(ctx context.Context, children ...interface{}) interface{} {
refInput := UseOn(ctx, "click", func(event Event) {
fmt.Println("click")
})
return Button(refInput, "button")
}

type App struct {
}

func (a App) Render(ctx context.Context, children ...interface{}) interface{} {
s := time.Now()
defer func() {
fmt.Println("render app cost", time.Since(s))
}()

UseEffect(ctx, func() func() {
fmt.Println("render app full cost", time.Since(s))
return nil
}, nil)

value, setValue := UseState(ctx, "")
v, setValue2 := UseState(ctx, &[]string{})
emojis := *v.(*[]string)

hello := UseMemo(ctx, func() interface{} {
return "Hello"
}, []interface{}{}).(string)

UseEffect(ctx, func() func() {
go func() {
c := httputil.GetShortConnClientContext(context.Background(), 5*time.Second)
req, _ := http.NewRequest("GET", "https://dog.ceo/api/breeds/list/all", nil)
resp, _ := c.Do(req)
m := struct {
Message map[string]interface{} `json:"message"`
}{}

_ = json.NewDecoder(resp.Body).Decode(&m)

keys := make([]string, 0, len(m.Message))

for i := range m.Message {
keys = append(keys, i)
}

setValue2(&keys)
}()

return nil
}, []interface{}{})

refInput := UseOn(ctx, "input", func(event Event) {
setValue(func(v interface{}) interface{} {
return event.Target().(Element).Get("value")
})
})

return Provider(withCSSCache(css))(
Main(
CSS{
"backgroundColor": "#ddd",
},
Div(
Div(hello),
),
Div(
CSS{
"overflow": "auto",
},
Span("Input:"),
Span(value),
),
Div(
Input(
refInput,
Attr("value", value),
),
),
H(SubComp{})(),
Div(
CSS{
"overflow": "scroll",
"height": "2em",
},
RangeSlice(emojis, func(i int) interface{} {
return Div(
Key(emojis[i]),
emojis[i],
)
}),
),
Div(
CSS{
"overflow": "scroll",
"height": "20em",
"fontSize": "8px",
},
Fragment(spanList),
),
),
renderer.Portal(Document.QuerySelector("#portal"))(
Div(
CSS{
"overflow": "auto",
},
"in portal", value,
),
),
)
}

func main() {
r := renderer.CreateRoot(Document.QuerySelector("#root"))
_ = r.Render(context.Background(), H(App{})())
fmt.Println("App ready hello")
<-make(chan struct{})
}
11 changes: 11 additions & 0 deletions cmd/webapp/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-ignore
import ("main.go")
.then(({main}) => main());

// @ts-ignore
import {registerSW} from "virtual:pwa-register"

const updateSW = registerSW({
onOfflineReady() {
},
})
19 changes: 19 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/go-courier/gox

go 1.17

require (
github.com/davecgh/go-spew v1.1.1
github.com/onsi/gomega v1.17.0
golang.org/x/net v0.0.0-20211108170745-6635138e15ea
)

require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/rogpeppe/go-internal v1.8.1-0.20210923151022-86f73c517451 // indirect
golang.org/x/sys v0.0.0-20210925032602-92d5a993a665 // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 76d4100

Please sign in to comment.