Skip to content

Commit da52c4d

Browse files
committed
init
1 parent ef4e8b8 commit da52c4d

11 files changed

+197
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
npm=pnpm
2+
3+
.PHONY: dev
4+
dev:
5+
cd ./test/ && nodemon -e go --watch './**/*.go' --signal SIGTERM --exec 'go' run .

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/simbafs/wsync
2+
3+
go 1.21.0

handler/new.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package wsync
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
type Wsync struct{}
9+
10+
func (ws *Wsync) ServeHTTP(w http.ResponseWriter, r *http.Request) {
11+
// upgrade to websocket
12+
fmt.Fprintf(w, "<h1>hello world</h1>")
13+
}
14+
15+
func New() *Wsync {
16+
return new(Wsync)
17+
}

hook/index.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* init websocket to server with auto reconnecting and ping check
3+
* @param {string} url
4+
*/
5+
function init(url) {
6+
if (!window) return
7+
const ws = new WebSocket(url)
8+
9+
const onOpen = () => console.log(`open websocket connection to ${url}`)
10+
const onClose = () => {
11+
// TODO: reconnecting
12+
console.log(`close websocket connection to ${url}`)
13+
}
14+
const onError = e => console.error(e)
15+
16+
ws.addEventListener('open', onOpen)
17+
ws.addEventListener('close', onClose)
18+
ws.addEventListener('error', onError)
19+
20+
window.ws = ws
21+
}
22+
23+
/**
24+
* before setState, sync to websocket
25+
* @template T
26+
* @param {T} state
27+
* @param {(state: T) => void} setState
28+
*
29+
* @returns {[T, (state: T) => void]}
30+
*
31+
*/
32+
function sync(state, setState) {
33+
// TODO
34+
35+
return [state, setState]
36+
}

hook/useWsReducer.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useReducer } from "react";
2+
3+
/**
4+
* @template S
5+
* @template A
6+
*
7+
* @callback Reducer
8+
* @param {S} state
9+
* @param {A} action
10+
* @returns {S}
11+
*/
12+
13+
/**
14+
* @template S
15+
* @template A
16+
* @template I
17+
*
18+
* @param {WebSocket} ws
19+
* @param {string} key
20+
* @param {Reducer<S, A>} reducer
21+
* @param {S | I} initArgs
22+
* @param {(args: S | I) => S} [init]
23+
*
24+
* @returns {[S, React.Dispatch<A>]}
25+
*/
26+
export default function useWsReducer(ws, key, reducer, initArgs, init) {
27+
const [state, updateState] = useReducer(reducer, initArgs, init)
28+
29+
return [state, updateState]
30+
}

hook/useWsState.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useState } from "react";
2+
3+
/**
4+
* @template T
5+
* @param {WebSocket} ws
6+
* @param {string} key
7+
* @param {T} [initState]
8+
* @returns {[T, React.Dispatch<T>]}
9+
*/
10+
export default function useWsState(ws, key, initState) {
11+
const [state, setState] = useState(initState)
12+
13+
return [state, setState]
14+
}

package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "wsync",
3+
"version": "1.0.0",
4+
"description": "sync states from backend to frontend with websocket",
5+
"main": "hook/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "SimbaFs <[email protected]>",
11+
"license": "MIT",
12+
"dependencies": {
13+
"@types/react": "^18.2.21",
14+
"react": "^18.2.0"
15+
}
16+
}

pnpm-lock.yaml

+53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
wsync "github.com/simbafs/wsync/handler"
8+
)
9+
10+
func main() {
11+
http.Handle("/ws", wsync.New())
12+
13+
log.Fatal(http.ListenAndServe(":3000", nil))
14+
}

tsconfig.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"checkJs": false,
5+
"strict": false,
6+
"skipLibCheck": true
7+
}
8+
}

0 commit comments

Comments
 (0)