Skip to content

Commit

Permalink
fix: stable live reload
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed Apr 30, 2024
1 parent a15cc21 commit 3ffb933
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/yuin/goldmark v1.7.1
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
github.com/yuin/gopher-lua v1.1.0
golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/net v0.17.0
gopkg.in/yaml.v3 v3.0.1
layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf
)
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
96 changes: 71 additions & 25 deletions pkg/alvu/alvu.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,49 +53,78 @@ type AlvuConfig struct {
logger Logger
hookHandler *Hooks
watcher *Watcher
rebuildChan chan bool

// Internals - Websockets // can be separated
shouldRebuild bool
rebuildLock sync.Mutex
rebuildCond *sync.Cond
connections map[*websocket.Conn]struct{}
}

func (ac *AlvuConfig) Rebuild(path string) {
ac.rebuildLock.Lock()
ac.logger.Info(fmt.Sprintf("Changed: %v, Recompiling.", path))
err := ac.Build()
if err != nil {
ac.logger.Error(err.Error())
}
ac.rebuildChan <- true
ac.shouldRebuild = true
ac.rebuildLock.Unlock()
ac.rebuildCond.Broadcast()
}

func (ac *AlvuConfig) Run() error {
ac.rebuildChan = make(chan bool, 1)
ac.logger = Logger{
logPrefix: "[alvu]",
}

ac.rebuildCond = sync.NewCond(&ac.rebuildLock)
ac.connections = make(map[*websocket.Conn]struct{})

if ac.Serve {
ac.watcher = NewWatcher()
ac.watcher.logger = ac.logger

go func(ac *AlvuConfig) {
for path := range ac.watcher.recompile {
ac.Rebuild(path)
}
}(ac)

ac.watcher.Start()
ac.monitorRebuilds()
}

err := ac.Build()
if err != nil {
return err
}

if ac.Serve {
ac.watcher.Start()
}

return ac.StartServer()
}

func (ac *AlvuConfig) monitorRebuilds() {
go func() {
for {
ac.rebuildCond.L.Lock()
for !ac.shouldRebuild {
ac.rebuildCond.Wait()
}
for conn := range ac.connections {
err := websocket.Message.Send(conn, "reload")
if err != nil {
delete(ac.connections, conn)
}
}
ac.shouldRebuild = false
ac.rebuildCond.L.Unlock()
}
}()
}

func (ac *AlvuConfig) Build() error {
hooksHandler := Hooks{
ac: *ac,
ac: ac,
}
ac.hookHandler = &hooksHandler

Expand Down Expand Up @@ -421,10 +450,16 @@ func (ac *AlvuConfig) StartServer() error {
// _webSocketHandler Internal function to setup a listener loop
// for the live reload setup
func (ac *AlvuConfig) _webSocketHandler(ws *websocket.Conn) {
// collect connections
ac.connections[ws] = struct{}{}

defer ws.Close()
for range ac.rebuildChan {
err := websocket.Message.Send(ws, "reload")
// message loop, till connection breaks
for {
var msg string
err := websocket.Message.Receive(ws, &msg)
if err != nil {
delete(ac.connections, ws)
break
}
}
Expand Down Expand Up @@ -554,21 +589,32 @@ func injectInLegacySlot(htmlString string, replacement string) string {

func injectWebsocketConnection(htmlString string, port string) string {
return htmlString + fmt.Sprintf(`<script>
const socket = new WebSocket("ws://localhost:%v/ws");
// Connection opened
socket.addEventListener("open", (event) => {
socket.send("Hello Server!");
});
// Listen for messages
socket.addEventListener("message", (event) => {
if (event.data == "reload") {
socket.close();
window.location.reload();
}
});
</script>`, port)
connect();
function connect() {
let socket = new WebSocket("ws://localhost:%v/ws");
// Connection opened
socket.addEventListener("open", (event) => {
socket.send("Init");
});
socket.addEventListener("close", (event) => {
socket = null;
setTimeout(() => {
connect();
}, 5000);
});
// Listen for messages
socket.addEventListener("message", (event) => {
if (event.data == "reload") {
socket.close();
window.location.reload();
}
});
}
</script>`, port)
}

func hasKeys(i map[string]interface{}) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/alvu/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type HookSource struct {
}

type Hooks struct {
ac AlvuConfig
ac *AlvuConfig
collection []*HookSource
forSpecificFiles map[string][]*HookSource

Expand Down

0 comments on commit 3ffb933

Please sign in to comment.