-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
framework/view: experiment to extract out svelte into a swappable 'vi…
…ewer'
- Loading branch information
1 parent
5f5b616
commit a4794af
Showing
16 changed files
with
371 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package svelte | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
"net/http" | ||
|
||
"github.com/livebud/bud/package/js" | ||
"github.com/livebud/bud/package/router" | ||
) | ||
|
||
func New(fsys fs.FS, vm js.VM) *Viewer { | ||
return &Viewer{fsys, vm} | ||
} | ||
|
||
type Viewer struct { | ||
fsys fs.FS | ||
vm js.VM | ||
} | ||
|
||
func (v *Viewer) Handler(route string, props interface{}) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
v.Render(w, route, props) | ||
}) | ||
} | ||
|
||
func (v *Viewer) Render(w http.ResponseWriter, viewPath string, props interface{}) { | ||
script, err := fs.ReadFile(v.fsys, "bud/view/_ssr.js") | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
propBytes, err := json.Marshal(props) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
result, err := v.vm.Eval("_ssr.js", fmt.Sprintf(`%s; bud.render(%q, %s)`, script, viewPath, propBytes)) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
fmt.Println("got result", result) | ||
return | ||
} | ||
|
||
func (v *Viewer) Serve(r *router.Router) { | ||
// Serving files! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package viewrt | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/livebud/bud/package/router" | ||
) | ||
|
||
type Viewer interface { | ||
// TODO: remove handler | ||
Handler(route string, props interface{}) http.Handler | ||
Render(w http.ResponseWriter, viewPath string, props interface{}) | ||
Serve(router *router.Router) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package virtual | ||
|
||
import ( | ||
"io" | ||
"io/fs" | ||
"time" | ||
) | ||
|
||
// File struct | ||
type File struct { | ||
Name string | ||
Data []byte | ||
Mode fs.FileMode | ||
ModTime time.Time | ||
Sys interface{} | ||
offset int64 | ||
} | ||
|
||
var _ fs.File = (*File)(nil) | ||
var _ io.ReadSeeker = (*File)(nil) | ||
|
||
// Reset the read data offset to 0 | ||
func (f *File) Reset() { | ||
f.offset = 0 | ||
} | ||
|
||
func (f *File) Close() error { | ||
return nil | ||
} | ||
|
||
func (f *File) Read(b []byte) (int, error) { | ||
if f.offset >= int64(len(f.Data)) { | ||
return 0, io.EOF | ||
} | ||
if f.offset < 0 { | ||
return 0, &fs.PathError{Op: "read", Path: f.Name, Err: fs.ErrInvalid} | ||
} | ||
n := copy(b, f.Data[f.offset:]) | ||
f.offset += int64(n) | ||
return n, nil | ||
} | ||
|
||
func (f *File) Stat() (fs.FileInfo, error) { | ||
return &fileInfo{ | ||
name: f.Name, | ||
mode: f.Mode &^ fs.ModeDir, | ||
modTime: f.ModTime, | ||
size: int64(len(f.Data)), | ||
sys: f.Sys, | ||
}, nil | ||
} | ||
|
||
func (f *File) Seek(offset int64, whence int) (int64, error) { | ||
switch whence { | ||
case 0: | ||
// offset += 0 | ||
case 1: | ||
offset += f.offset | ||
case 2: | ||
offset += int64(len(f.Data)) | ||
} | ||
if offset < 0 || offset > int64(len(f.Data)) { | ||
return 0, &fs.PathError{Op: "seek", Path: f.Name, Err: fs.ErrInvalid} | ||
} | ||
f.offset = offset | ||
return offset, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package virtual | ||
|
||
import ( | ||
"io/fs" | ||
"time" | ||
) | ||
|
||
// A fileInfo implements fs.FileInfo and fs.DirEntry for a given map file. | ||
type fileInfo struct { | ||
name string | ||
size int64 | ||
mode fs.FileMode | ||
modTime time.Time | ||
sys interface{} | ||
} | ||
|
||
func (i *fileInfo) Name() string { return i.name } | ||
func (i *fileInfo) Mode() fs.FileMode { return i.mode } | ||
func (i *fileInfo) Type() fs.FileMode { return i.mode.Type() } | ||
func (i *fileInfo) ModTime() time.Time { return i.modTime } | ||
func (i *fileInfo) IsDir() bool { return i.mode&fs.ModeDir != 0 } | ||
func (i *fileInfo) Sys() interface{} { return i.sys } | ||
func (i *fileInfo) Info() (fs.FileInfo, error) { return i, nil } | ||
func (i *fileInfo) Size() int64 { return i.size } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.