Skip to content

Commit

Permalink
refactor(io): improved dir exists checking to reduce io
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Dec 23, 2024
1 parent aa24c1b commit 1c90baf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 44 deletions.
64 changes: 28 additions & 36 deletions viewengine_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,7 @@ func (ve *HtmlViewEngine) FileChanged(fsys fs.FS, app *App, event fsnotify.Event
}

func (ve *HtmlViewEngine) loadComponents() error {
_, err := fs.Stat(ve.fsys, "components")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}

return fs.WalkDir(ve.fsys, "components", func(path string, d fs.DirEntry, err error) error {
err := fs.WalkDir(ve.fsys, "components", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand All @@ -95,6 +87,12 @@ func (ve *HtmlViewEngine) loadComponents() error {
return err
})

if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}

return err

}

func (ve *HtmlViewEngine) loadTemplate(path string) (*HtmlTemplate, error) {
Expand All @@ -119,15 +117,7 @@ func (ve *HtmlViewEngine) loadTemplate(path string) (*HtmlTemplate, error) {
}

func (ve *HtmlViewEngine) loadLayouts() error {
_, err := fs.Stat(ve.fsys, "layouts")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}

return fs.WalkDir(ve.fsys, "layouts", func(path string, d fs.DirEntry, err error) error {
err := fs.WalkDir(ve.fsys, "layouts", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand All @@ -142,18 +132,16 @@ func (ve *HtmlViewEngine) loadLayouts() error {

return nil
})
}

func (ve *HtmlViewEngine) loadPages() error {
_, err := fs.Stat(ve.fsys, "pages")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}

return fs.WalkDir(ve.fsys, "pages", func(path string, d fs.DirEntry, err error) error {
return err
}

func (ve *HtmlViewEngine) loadPages() error {
err := fs.WalkDir(ve.fsys, "pages", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand All @@ -165,6 +153,12 @@ func (ve *HtmlViewEngine) loadPages() error {
return ve.loadPage(path)
})

if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}

return err

}

func (ve *HtmlViewEngine) loadPage(path string) error {
Expand All @@ -189,15 +183,7 @@ func (ve *HtmlViewEngine) loadPage(path string) error {
}

func (ve *HtmlViewEngine) loadViews() error {
_, err := fs.Stat(ve.fsys, "views")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}

return fs.WalkDir(ve.fsys, "views", func(path string, d fs.DirEntry, err error) error {
err := fs.WalkDir(ve.fsys, "views", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand All @@ -209,6 +195,12 @@ func (ve *HtmlViewEngine) loadViews() error {
return ve.loadView(path)
})

if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}

return err

}

func (ve *HtmlViewEngine) loadView(path string) error {
Expand Down
15 changes: 7 additions & 8 deletions viewengine_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ type StaticViewEngine struct {
}

func (ve *StaticViewEngine) Load(fsys fs.FS, app *App) error {
_, err := fs.Stat(fsys, "public")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}

return fs.WalkDir(fsys, "public", func(path string, d fs.DirEntry, err error) error {
err := fs.WalkDir(fsys, "public", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
Expand All @@ -31,6 +24,12 @@ func (ve *StaticViewEngine) Load(fsys fs.FS, app *App) error {

return nil
})

if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}

return err
}

func (ve *StaticViewEngine) FileChanged(fsys fs.FS, app *App, event fsnotify.Event) error {
Expand Down

0 comments on commit 1c90baf

Please sign in to comment.