Skip to content

Commit 51218e7

Browse files
committed
fix(runtime): sync on sse reconnect
1 parent 36c0fb9 commit 51218e7

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

‎context.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ func (c *Context) sendPatch(p patch) {
244244
select {
245245
case patchChan <- p:
246246
default: // closed or buffer full - drop patch without blocking
247-
c.app.logWarn(c, "view out of sync: sse stream closed or queue is full")
248247
}
249248
}
250249

@@ -320,11 +319,8 @@ func (c *Context) ExecScript(s string) {
320319
func (c *Context) stopAllRoutines() {
321320
select {
322321
case c.ctxDisposedChan <- struct{}{}:
323-
c.app.logDebug(c, "stopped all routines")
324322
default:
325-
c.app.logDebug(c, "did not stop all routines")
326323
}
327-
328324
}
329325

330326
func newContext(id string, route string, v *V) *Context {
@@ -339,6 +335,7 @@ func newContext(id string, route string, v *V) *Context {
339335
componentRegistry: make(map[string]*Context),
340336
actionRegistry: make(map[string]func()),
341337
signals: new(sync.Map),
338+
patchChan: make(chan patch, 100),
342339
ctxDisposedChan: make(chan struct{}, 1),
343340
}
344341
}

‎via.go‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ func (v *V) Page(route string, initContextFn func(c *Context)) {
154154
}
155155
v.mux.HandleFunc("GET "+route, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
156156
v.logDebug(nil, "GET %s", r.URL.String())
157-
if strings.Contains(r.URL.Path, "favicon") {
157+
if strings.Contains(r.URL.Path, "favicon") ||
158+
strings.Contains(r.URL.Path, ".well-known") ||
159+
strings.Contains(r.URL.Path, "js.map") {
158160
return
159161
}
160162
id := fmt.Sprintf("%s_/%s", route, genRandID())
@@ -235,9 +237,6 @@ func (v *V) HandleFunc(pattern string, f http.HandlerFunc) {
235237

236238
// Start starts the Via HTTP server on the given address.
237239
func (v *V) Start() {
238-
if v.cfg.DevMode {
239-
v.devModeRestore()
240-
}
241240
v.logInfo(nil, "via started at [%s]", v.cfg.ServerAddress)
242241
log.Fatalf("[fatal] %v", http.ListenAndServe(v.cfg.ServerAddress, v.mux))
243242
}
@@ -307,7 +306,7 @@ func (v *V) devModeRemovePersisted(c *Context) {
307306
v.logDebug(c, "devmode removed persisted ctx from file")
308307
}
309308

310-
func (v *V) devModeRestore() {
309+
func (v *V) devModeRestore(cID string) {
311310
p := filepath.Join(".via", "devmode", "ctx.json")
312311
file, err := os.Open(p)
313312
if err != nil {
@@ -324,17 +323,18 @@ func (v *V) devModeRestore() {
324323
return
325324
}
326325
for ctxID, pageRoute := range ctxRegMap {
327-
pageInitFn, ok := v.devModePageInitFnMap[pageRoute]
328-
if !ok {
329-
v.logWarn(nil, "devmode could not restore ctx from file: page init fn for route '%s' not found", pageRoute)
330-
continue
326+
if ctxID == cID {
327+
pageInitFn, ok := v.devModePageInitFnMap[pageRoute]
328+
if !ok {
329+
v.logWarn(nil, "devmode could not restore ctx from file: page init fn for route '%s' not found", pageRoute)
330+
continue
331+
}
332+
c := newContext(ctxID, pageRoute, v)
333+
pageInitFn(c)
334+
v.registerCtx(c)
335+
v.logDebug(c, "devmode restored ctx")
331336
}
332-
333-
c := newContext(ctxID, pageRoute, v)
334-
pageInitFn(c)
335-
v.registerCtx(c)
336337
}
337-
v.logDebug(nil, "devmode restored ctx registry")
338338
}
339339

340340
type patchType int
@@ -375,6 +375,12 @@ func New() *V {
375375
var sigs map[string]any
376376
_ = datastar.ReadSignals(r, &sigs)
377377
cID, _ := sigs["via-ctx"].(string)
378+
379+
if v.cfg.DevMode {
380+
if _, err := v.getCtx(cID); err != nil {
381+
v.devModeRestore(cID)
382+
}
383+
}
378384
c, err := v.getCtx(cID)
379385
if err != nil {
380386
v.logErr(nil, "sse stream failed to start: %v", err)
@@ -385,9 +391,6 @@ func New() *V {
385391

386392
v.logDebug(c, "SSE connection established")
387393

388-
c.patchChan = make(chan patch, 1000)
389-
defer close(c.patchChan)
390-
391394
go func() {
392395
c.Sync()
393396
}()

0 commit comments

Comments
 (0)