Ensured that New() feed function is functionally equivalent still

This commit is contained in:
Ben Davies 2014-10-14 18:51:13 +01:00
parent c753ba0962
commit 5d9fc9c561
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 { if len(newitems) > 0 && d.itemhandler != nil {
d.itemhandler.ProcessItems(f, ch, newitems) d.itemhandler.ProcessItems(f, ch, newitems)
} }
// No items to process, may as well end here
} }
func (d *databaseHandler) ProcessChannels(f *Feed, ch []*Channel) { 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 { if len(newchannels) > 0 && d.chanhandler != nil {
d.chanhandler.ProcessChannels(f, newchannels) 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 := new(databaseHandler)
database.db = NewDatabase() database.db = db
database.itemhandler = handler
database.chanhandler = handler
return database
}
func NewDatabaseItemHandler(itemhandler ItemHandler) ItemHandler {
database := new(databaseHandler)
database.db = NewDatabase()
database.itemhandler = itemhandler database.itemhandler = itemhandler
return database return database
} }
func NewDatabaseChannelHandler(chanhandler ChannelHandler) ChannelHandler { func NewDatabaseChannelHandler(db *database, chanhandler ChannelHandler) ChannelHandler {
database := new(databaseHandler) database := new(databaseHandler)
database.db = NewDatabase() database.db = db
database.chanhandler = chanhandler database.chanhandler = chanhandler
return database return database
} }

59
feed.go
View File

@ -55,11 +55,6 @@ func (h ItemHandlerFunc) ProcessItems(f *Feed, ch *Channel, newitems []*Item) {
h(f, ch, newitems) h(f, ch, newitems)
} }
type Handler interface {
ChannelHandler
ItemHandler
}
type ChannelHandler interface { type ChannelHandler interface {
ProcessChannels(f *Feed, newchannels []*Channel) ProcessChannels(f *Feed, newchannels []*Channel)
} }
@ -68,26 +63,6 @@ type ItemHandler interface {
ProcessItems(f *Feed, ch *Channel, newitems []*Item) 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 { type Feed struct {
// Custom cache timeout in minutes. // Custom cache timeout in minutes.
CacheTimeout int CacheTimeout int
@ -108,8 +83,14 @@ type Feed struct {
// Url from which this feed was created. // Url from which this feed was created.
Url string Url string
// The channel and item handler // Database
handler Handler database *database
// The channel handler
channelHandler ChannelHandler
// The item handler
itemHandler ItemHandler
// Last time content was fetched. Used in conjunction with CacheTimeout // Last time content was fetched. Used in conjunction with CacheTimeout
// to ensure we don't get content too often. // to ensure we don't get content too often.
@ -117,21 +98,25 @@ type Feed struct {
} }
// New is a helper function to stay semi-compatible with // 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 handlera to ensure
// that this approach is functionally identical to the // that this approach is functionally identical to the
// old databse/handlers version // old databse/handlers version
func New(cachetimeout int, enforcecachelimit bool, ch ChannelHandlerFunc, ih ItemHandlerFunc) *Feed { 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 // NewWithHandler creates a new feed with handlers
// People should use this appraoch from now on // People should use this approach from now on
func NewWithHandler(cachetimeout int, enforcecachelimit bool, h Handler) *Feed { func NewWithHandlers(cachetimeout int, enforcecachelimit bool, ch ChannelHandler, ih ItemHandler) *Feed {
v := new(Feed) v := new(Feed)
v.CacheTimeout = cachetimeout v.CacheTimeout = cachetimeout
v.EnforceCacheLimit = enforcecachelimit v.EnforceCacheLimit = enforcecachelimit
v.Type = "none" v.Type = "none"
v.handler = h v.channelHandler = ch
v.itemHandler = ih
return v return v
} }
@ -219,13 +204,13 @@ func (this *Feed) makeFeed(doc *xmlx.Document) (err error) {
func (this *Feed) notifyListeners() { func (this *Feed) notifyListeners() {
for _, channel := range this.Channels { for _, channel := range this.Channels {
if len(channel.Items) > 0 && this.handler != nil { if len(channel.Items) > 0 && this.itemHandler != nil {
this.handler.ProcessItems(this, channel, channel.Items) this.itemHandler.ProcessItems(this, channel, channel.Items)
} }
} }
if len(this.Channels) > 0 && this.handler != nil { if len(this.Channels) > 0 && this.channelHandler != nil {
this.handler.ProcessChannels(this, this.Channels) this.channelHandler.ProcessChannels(this, this.Channels)
} }
} }