func buildRepo(srv *eventsource.Server) { repo := eventsource.NewSliceRepository() srv.Register("articles", repo) for i := range articles { repo.Add("articles", &articles[i]) srv.Publish([]string{"articles"}, &articles[i]) } }
func TimePublisher(srv *eventsource.Server) { start := time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC) ticker := time.NewTicker(time.Second) for i := 0; i < TICK_COUNT; i++ { <-ticker.C srv.Publish([]string{"time"}, TimeEvent(start)) start = start.Add(time.Second) } }
func (repo *articleRepository) Monitor(srv *eventsource.Server) { stream := make(chan *article) defer close(stream) var lastEventId string if err := repo.QueryRow("SELECT MAX(id) FROM article").Scan(&lastEventId); err != nil { glog.Fatal(err) } glog.Infof("Monitoring from Id: %s onwards ", lastEventId) go func() { for art := range stream { glog.Infof("Publishing Id: %s Channel: %s", art.id, art.channel) srv.Publish([]string{"articles", art.channel}, repo.Get(art.channel, art.id)) } }() for { glog.V(2).Infoln("Polling...") if last := repo.streamArticles("articles", lastEventId, stream); last.id != "" { lastEventId = last.id } time.Sleep(*interval) } }