Skip to content

Commit

Permalink
Fix reload and reset settings buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnStarich committed Jul 28, 2021
1 parent 345209c commit b541931
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/editor/ide/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func newSettingsDropdown(attachTo *dom.Element) *dropdown {
return promise.From(global.Get("destroyMount").Invoke(path))
}
listenButton("reset", "Erase all data and reload?", func() {
mounts := interop.Keys(global.Get("getMounts").Invoke())
mounts := interop.StringsFromJSValue(global.Get("getMounts").Invoke())
var promises []promise.Promise
for _, mount := range mounts {
promises = append(promises, destroyMount(mount))
Expand Down
28 changes: 28 additions & 0 deletions internal/fs/clearfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fs

import (
"context"

"github.com/hack-pad/hackpadfs"
)

type clearFS interface {
hackpadfs.FS
Clear(ctx context.Context) error
}

type clearUnderlyingFS struct {
hackpadfs.FS
underlyingFS clearFS
}

func newClearUnderlyingFS(fs hackpadfs.FS, underlyingFS clearFS) clearFS {
return &clearUnderlyingFS{
FS: fs,
underlyingFS: underlyingFS,
}
}

func (c *clearUnderlyingFS) Clear(ctx context.Context) error {
return c.underlyingFS.Clear(ctx)
}
24 changes: 15 additions & 9 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func Mounts() []mount.Point {

func DestroyMount(path string) error {
mount, _ := filesystem.Mount(path)
if clearFs, ok := mount.(interface{ Clear() error }); ok {
return clearFs.Clear()
if clearFs, ok := mount.(clearFS); ok {
return clearFs.Clear(context.Background())
}
return &hackpadfs.PathError{Op: "clear", Path: path, Err: hackpadfs.ErrNotImplemented}
}
Expand Down Expand Up @@ -75,24 +75,30 @@ func OverlayTarGzip(mountPath string, r io.ReadCloser, persist bool, shouldCache
return err
}

memFS, err := mem.NewFS()
if err != nil {
return err
}

cacheOptions := cache.ReadOnlyOptions{
RetainData: func(name string, info hackpadfs.FileInfo) bool {
return shouldCache(path.Join(mountPath, name), info)
},
}
newCacheFS := func(underlyingFS clearFS) (clearFS, error) {
memFS, err := mem.NewFS()
if err != nil {
return nil, err
}
fs, err := cache.NewReadOnlyFS(underlyingFS, memFS, cacheOptions)
if err != nil {
return nil, err
}
return newClearUnderlyingFS(fs, underlyingFS), nil
}

_, err = hackpadfs.Stat(underlyingFS, tarfsDoneMarker)
if err == nil {
// tarfs already completed successfully and is persisted,
// so close tarfs reader and mount the existing files
r.Close()

cacheFS, err := cache.NewReadOnlyFS(underlyingFS, memFS, cacheOptions)
cacheFS, err := newCacheFS(underlyingFS)
if err != nil {
return err
}
Expand All @@ -110,7 +116,7 @@ func OverlayTarGzip(mountPath string, r io.ReadCloser, persist bool, shouldCache
if err != nil {
return err
}
cacheFS, err := cache.NewReadOnlyFS(tarFS, memFS, cacheOptions)
cacheFS, err := newCacheFS(tarFS)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions internal/tarfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,15 +270,15 @@ func (fs *FS) InitErr() error {
return fs.initErr
}

type clearerFs interface {
Clear() error
type clearFS interface {
Clear(ctx context.Context) error
}

func (fs *FS) Clear() (err error) {
if clearer, ok := fs.underlyingFS.(clearerFs); ok {
func (fs *FS) Clear(ctx context.Context) (err error) {
if clearFS, ok := fs.underlyingFS.(clearFS); ok {
fs.initErr = context.Canceled
fs.cancel()
return clearer.Clear()
return clearFS.Clear(ctx)
}
return errors.New("Unsupported operation for base FS")
}

0 comments on commit b541931

Please sign in to comment.