Ensured that New() feed function is functionally equivalent still
This commit is contained in:
parent
c753ba0962
commit
5d9fc9c561
|
@ -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
59
feed.go
|
@ -55,11 +55,6 @@ func (h ItemHandlerFunc) ProcessItems(f *Feed, ch *Channel, newitems []*Item) {
|
|||
h(f, ch, newitems)
|
||||
}
|
||||
|
||||
type Handler interface {
|
||||
ChannelHandler
|
||||
ItemHandler
|
||||
}
|
||||
|
||||
type ChannelHandler interface {
|
||||
ProcessChannels(f *Feed, newchannels []*Channel)
|
||||
}
|
||||
|
@ -68,26 +63,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
|
||||
|
@ -108,8 +83,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.
|
||||
|
@ -117,21 +98,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 handlera 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
|
||||
}
|
||||
|
||||
|
@ -219,13 +204,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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue