2022-09-23 13:46:34 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Home = iota + 1
|
|
|
|
Archive
|
|
|
|
Category
|
2023-01-26 16:52:59 +00:00
|
|
|
Tag
|
2022-09-23 13:46:34 +00:00
|
|
|
Search
|
|
|
|
Detail
|
2023-01-21 13:13:33 +00:00
|
|
|
|
|
|
|
Ok
|
2023-01-17 15:18:31 +00:00
|
|
|
Empty404
|
2023-01-21 13:13:33 +00:00
|
|
|
Error
|
|
|
|
InternalErr
|
2022-09-23 13:46:34 +00:00
|
|
|
)
|
|
|
|
|
2023-01-17 15:18:31 +00:00
|
|
|
var IndexSceneMap = map[int]struct{}{
|
|
|
|
Home: {},
|
|
|
|
Archive: {},
|
|
|
|
Category: {},
|
2023-01-26 16:52:59 +00:00
|
|
|
Tag: {},
|
2023-01-17 15:18:31 +00:00
|
|
|
Search: {},
|
|
|
|
}
|
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
type Func[T any] func(*Plugin[T], *gin.Context, *T, int)
|
2022-09-23 13:46:34 +00:00
|
|
|
|
|
|
|
type Plugin[T any] struct {
|
2023-01-12 12:42:16 +00:00
|
|
|
calls []Func[T]
|
2022-09-23 13:46:34 +00:00
|
|
|
index int
|
|
|
|
post *T
|
2023-01-21 13:13:33 +00:00
|
|
|
scene int
|
2022-09-23 13:46:34 +00:00
|
|
|
c *gin.Context
|
|
|
|
}
|
|
|
|
|
2023-01-21 13:13:33 +00:00
|
|
|
func NewPlugin[T any](calls []Func[T], index int, post *T, scene int, c *gin.Context) *Plugin[T] {
|
2022-09-23 13:46:34 +00:00
|
|
|
return &Plugin[T]{calls: calls, index: index, post: post, scene: scene, c: c}
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:42:16 +00:00
|
|
|
func (p *Plugin[T]) Push(call ...Func[T]) {
|
2022-09-23 13:46:34 +00:00
|
|
|
p.calls = append(p.calls, call...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin[T]) Next() {
|
|
|
|
p.index++
|
|
|
|
for ; p.index < len(p.calls); p.index++ {
|
|
|
|
p.calls[p.index](p, p.c, p.post, p.scene)
|
|
|
|
}
|
|
|
|
}
|