shithub: hell

ref: cf198b58e2b24938444437dbc7fc6f27f6590b67
dir: /dispatch.go/

View raw version
package main

import (
	"fmt"
	"time"

	"github.com/mattn/go-mastodon"
)

func (hc *Hellclient) queueManager() {

}

func (hc *Hellclient) clientDispatch() {
	var tootQueue []*mastodon.Toot
	var lastfire time.Time
	var lastreceive time.Time
	for {
		select {
		case statustoot := <-hc.dispatch:
			tootQueue = append(tootQueue, statustoot)
		default:
			if len(tootQueue) > 1 {
				fmt.Printf("Got %v lines fast!\n", len(tootQueue))
				tootQueue = nil
			}
			if 1 > len(tootQueue) {
				statustoot := <-hc.dispatch
				tootQueue = append(tootQueue, statustoot)
				break
			}
			if time.Since(lastfire) < hc.preferences.apidelay {
				time.Sleep(hc.preferences.apidelay - time.Since(lastfire))
				break
			}
			var status *mastodon.Status
			for _, toot := range tootQueue {
				lastfire = time.Now()
				var err error
				fmt.Printf("%v\n", toot.Status)
				hc.lock()
				hc.recentpost = status
				hc.unlock()
				if err != nil {
					fmt.Println(err)
				}
				tootQueue = tootQueue[1:]
				break
			}
		}
	}
}

func (hc *Hellclient) dispatchStatus(status string, visibility string) {
	hc.dispatch <- postStatus(status, visibility)
}

func (hc *Hellclient) dispatchReply(posttext string, replyto mastodon.ID, postItem *mastodon.Status) {
	hc.dispatch <- postReply(posttext, replyto, hc.currentuser.ID, postItem)
}