Merge pull request #46 from JalfResi/split-handlers

Separating singular handler type to ease reuse/chaining
This commit is contained in:
Jim Teeuwen 2014-10-14 20:28:18 +02:00
commit c7f7ccd37e
2 changed files with 26 additions and 53 deletions

View File

@ -16,8 +16,6 @@ func (d *databaseHandler) ProcessItems(f *Feed, ch *Channel, items []*Item) {
if len(newitems) > 0 && d.itemhandler != nil {
d.itemhandler.ProcessItems(f, ch, newitems)
}
// No items to process, may as well end here
}
func (d *databaseHandler) ProcessChannels(f *Feed, ch []*Channel) {
@ -30,28 +28,18 @@ func (d *databaseHandler) ProcessChannels(f *Feed, ch []*Channel) {
if len(newchannels) > 0 && d.chanhandler != nil {
d.chanhandler.ProcessChannels(f, newchannels)
}
// No channels to process, may as well end here
}
func NewDatabaseHandler(handler Handler) Handler {
func NewDatabaseItemHandler(db *database, itemhandler ItemHandler) ItemHandler {
database := new(databaseHandler)
database.db = NewDatabase()
database.itemhandler = handler
database.chanhandler = handler
return database
}
func NewDatabaseItemHandler(itemhandler ItemHandler) ItemHandler {
database := new(databaseHandler)
database.db = NewDatabase()
database.db = db
database.itemhandler = itemhandler
return database
}
func NewDatabaseChannelHandler(chanhandler ChannelHandler) ChannelHandler {
func NewDatabaseChannelHandler(db *database, chanhandler ChannelHandler) ChannelHandler {
database := new(databaseHandler)
database.db = NewDatabase()
database.db = db
database.chanhandler = chanhandler
return database
}

59
feed.go
View File

@ -59,11 +59,6 @@ func (h ItemHandlerFunc) ProcessItems(f *Feed, ch *Channel, newitems []*Item) {
}
}
type Handler interface {
ChannelHandler
ItemHandler
}
type ChannelHandler interface {
ProcessChannels(f *Feed, newchannels []*Channel)
}
@ -72,26 +67,6 @@ type ItemHandler interface {
ProcessItems(f *Feed, ch *Channel, newitems []*Item)
}
type HandlerBonder struct {
itemhandler ItemHandler
chanhandler ChannelHandler
}
func (hb *HandlerBonder) ProcessChannels(f *Feed, newchannels []*Channel) {
hb.chanhandler.ProcessChannels(f, newchannels)
}
func (hb *HandlerBonder) ProcessItems(f *Feed, ch *Channel, newitems []*Item) {
hb.itemhandler.ProcessItems(f, ch, newitems)
}
func NewHandlerBonder(chanhandler ChannelHandler, itemhandler ItemHandler) Handler {
return &HandlerBonder{
itemhandler: itemhandler,
chanhandler: chanhandler,
}
}
type Feed struct {
// Custom cache timeout in minutes.
CacheTimeout int
@ -112,8 +87,14 @@ type Feed struct {
// Url from which this feed was created.
Url string
// The channel and item handler
handler Handler
// Database
database *database
// The channel handler
channelHandler ChannelHandler
// The item handler
itemHandler ItemHandler
// Last time content was fetched. Used in conjunction with CacheTimeout
// to ensure we don't get content too often.
@ -121,21 +102,25 @@ type Feed struct {
}
// New is a helper function to stay semi-compatible with
// the old code. Includes the databse handler to ensure
// the old code. Includes the database handler to ensure
// that this approach is functionally identical to the
// old databse/handlers version
func New(cachetimeout int, enforcecachelimit bool, ch ChannelHandlerFunc, ih ItemHandlerFunc) *Feed {
return NewWithHandler(cachetimeout, enforcecachelimit, NewDatabaseHandler(NewHandlerBonder(ch, ih)))
db := NewDatabase()
f := NewWithHandlers(cachetimeout, enforcecachelimit, NewDatabaseChannelHandler(db, ch), NewDatabaseItemHandler(db, ih))
f.database = db
return f
}
// NewWithHandler creates a new feed with a handler
// People should use this appraoch from now on
func NewWithHandler(cachetimeout int, enforcecachelimit bool, h Handler) *Feed {
// NewWithHandler creates a new feed with handlers
// People should use this approach from now on
func NewWithHandlers(cachetimeout int, enforcecachelimit bool, ch ChannelHandler, ih ItemHandler) *Feed {
v := new(Feed)
v.CacheTimeout = cachetimeout
v.EnforceCacheLimit = enforcecachelimit
v.Type = "none"
v.handler = h
v.channelHandler = ch
v.itemHandler = ih
return v
}
@ -223,13 +208,13 @@ func (this *Feed) makeFeed(doc *xmlx.Document) (err error) {
func (this *Feed) notifyListeners() {
for _, channel := range this.Channels {
if len(channel.Items) > 0 && this.handler != nil {
this.handler.ProcessItems(this, channel, channel.Items)
if len(channel.Items) > 0 && this.itemHandler != nil {
this.itemHandler.ProcessItems(this, channel, channel.Items)
}
}
if len(this.Channels) > 0 && this.handler != nil {
this.handler.ProcessChannels(this, this.Channels)
if len(this.Channels) > 0 && this.channelHandler != nil {
this.channelHandler.ProcessChannels(this, this.Channels)
}
}