Skip to content

Commit

Permalink
frontend, updater: Delete commented out code; clean up.
Browse files Browse the repository at this point in the history
This is a refactor that deletes commented out code and improves the
code style for better readability. No behavior changes.
  • Loading branch information
dmitshur committed Aug 17, 2018
1 parent 3bc880f commit 19e2d27
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 43 deletions.
73 changes: 30 additions & 43 deletions frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ func run() {
// Initial frontend render.
vecty.RenderBody(body)

// Start the scheduler loop.
go scheduler()

// Start streaming repo presentations from the backend.
err := stream()
if err != nil {
log.Println(err)
}
}

// stream streams the list of repo presentations from the backend,
// and appends them to the store as they arrive.
func stream() error {
started := time.Now()
defer func() { fmt.Println("stream:", time.Since(started)) }()
Expand All @@ -61,7 +65,7 @@ func stream() error {
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
for /*len(store.RPs()) < 10*/ {
for {
var rp model.RepoPresentation
err := dec.Decode(&rp)
if err == io.EOF {
Expand All @@ -77,64 +81,47 @@ func stream() error {
return nil
}

type actionAndResponse struct {
Action action.Action
RespCh chan<- action.Response
}

var (
actionCh = make(chan actionAndResponse) // TODO: Consider/try buffered channel of size 10.
renderCh <-chan time.Time
)

func apply(a action.Action) action.Response {
respCh := make(chan action.Response)
actionCh <- actionAndResponse{Action: a, RespCh: respCh}
resp := <-respCh
return resp
}

// scheduler runs a loop that is responsible for
// applying actions to the store as they're made available,
// and rendering the body after processing new actions.
//
// It coalesces temporally adjacent actions, processing
// them in batches without performing rendering in between.
func scheduler() {
//var renderOn = make(chan struct{})
//close(renderOn)

//forceRenderCh := time.Tick(5000 * time.Millisecond)
var renderCh <-chan time.Time

for {
select {
case a := <-actionCh:
resp := store.Apply(a.Action)
a.RespCh <- resp

// Don't render (needlessly) after *action.SetUpdating, etc.
// TODO: Move this elsewhere (into store.Apply somehow?).
// THINK: Can't do this, need to update heading after all.
//if _, ok := a.Action.(*action.SetUpdating); ok {
// break
//}

renderCh = time.After(10 * time.Millisecond)
case <-renderCh:
renderBody()
renderCh = nil
//runtime.Gosched()

// TODO: Add another case that forces a re-render to happen at least once every
// 500 milliseconds or so (in case there are never-ending actions that
// take a while to get through; we still want to display some progress).
//case <-forceRenderCh:
// if renderCh == nil {
// break
// }
// renderBody()
// renderCh = nil
}

//time.Sleep(time.Second)
//runtime.Gosched()
}
}

// TODO: Consider using time.NewTimer and Timer.Stop instead of time.After.

var actionCh = make(chan actionAndResponse) // TODO: Consider/try buffered channel of size 10.

type actionAndResponse struct {
Action action.Action
RespCh chan<- action.Response
}

// apply applies the given action to the store,
// and returns the response.
func apply(a action.Action) action.Response {
respCh := make(chan action.Response)
actionCh <- actionAndResponse{Action: a, RespCh: respCh}
resp := <-respCh
return resp
}

func renderBody() {
started := time.Now()
defer func() { fmt.Println("renderBody:", time.Since(started)) }()
Expand Down
2 changes: 2 additions & 0 deletions updater/dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Dep struct {
Dir string
}

// Update specified repository to latest version by calling
// "dep ensure -update <repo-root>" in d.Dir directory.
func (d Dep) Update(repo *gps.Repo) error {
cmd := exec.Command("dep", "ensure", "-update", repo.Root)
fmt.Println(strings.Join(cmd.Args, " "))
Expand Down

0 comments on commit 19e2d27

Please sign in to comment.