mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-09 22:41:55 +08:00
feat: refactor jsParser to use ParserMethod constants and remove redundant locking in ParseWithContext
This commit is contained in:
@@ -15,18 +15,6 @@ import (
|
|||||||
"github.com/krau/SaveAny-Bot/pkg/parser"
|
"github.com/krau/SaveAny-Bot/pkg/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
LatestParserVersion = semver.MustParse("1.0.0")
|
|
||||||
MinimumParserVersion = semver.MustParse("1.0.0")
|
|
||||||
)
|
|
||||||
|
|
||||||
type PluginMeta struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Version string `json:"version"` // [TODO] 分版本解析, 但是我们现在只有 v1 所以先不写
|
|
||||||
Description string `json:"description"`
|
|
||||||
Author string `json:"author"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type jsParser struct {
|
type jsParser struct {
|
||||||
meta PluginMeta
|
meta PluginMeta
|
||||||
vm *goja.Runtime
|
vm *goja.Runtime
|
||||||
@@ -34,7 +22,7 @@ type jsParser struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type jsParserReq struct {
|
type jsParserReq struct {
|
||||||
method string
|
method ParserMethod
|
||||||
url string
|
url string
|
||||||
respCh chan jsParserResp
|
respCh chan jsParserResp
|
||||||
}
|
}
|
||||||
@@ -47,14 +35,14 @@ type jsParserResp struct {
|
|||||||
|
|
||||||
func (p *jsParser) CanHandle(url string) bool {
|
func (p *jsParser) CanHandle(url string) bool {
|
||||||
respCh := make(chan jsParserResp, 1)
|
respCh := make(chan jsParserResp, 1)
|
||||||
p.reqCh <- jsParserReq{method: "canHandle", url: url, respCh: respCh}
|
p.reqCh <- jsParserReq{method: ParserMethodCanHandle, url: url, respCh: respCh}
|
||||||
resp := <-respCh
|
resp := <-respCh
|
||||||
return resp.ok && resp.err == nil
|
return resp.ok && resp.err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *jsParser) Parse(ctx context.Context, url string) (*parser.Item, error) {
|
func (p *jsParser) Parse(ctx context.Context, url string) (*parser.Item, error) {
|
||||||
respCh := make(chan jsParserResp, 1)
|
respCh := make(chan jsParserResp, 1)
|
||||||
p.reqCh <- jsParserReq{method: "parse", url: url, respCh: respCh}
|
p.reqCh <- jsParserReq{method: ParserMethodParse, url: url, respCh: respCh}
|
||||||
select {
|
select {
|
||||||
case resp := <-respCh:
|
case resp := <-respCh:
|
||||||
return resp.item, resp.err
|
return resp.item, resp.err
|
||||||
@@ -73,7 +61,7 @@ func newJSParser(vm *goja.Runtime, canHandleFunc, parseFunc goja.Value, metadata
|
|||||||
go func() {
|
go func() {
|
||||||
for req := range p.reqCh {
|
for req := range p.reqCh {
|
||||||
switch req.method {
|
switch req.method {
|
||||||
case "canHandle":
|
case ParserMethodCanHandle:
|
||||||
fn, _ := goja.AssertFunction(canHandleFunc)
|
fn, _ := goja.AssertFunction(canHandleFunc)
|
||||||
res, err := fn(goja.Undefined(), p.vm.ToValue(req.url))
|
res, err := fn(goja.Undefined(), p.vm.ToValue(req.url))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -81,7 +69,7 @@ func newJSParser(vm *goja.Runtime, canHandleFunc, parseFunc goja.Value, metadata
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
req.respCh <- jsParserResp{ok: res.ToBoolean()}
|
req.respCh <- jsParserResp{ok: res.ToBoolean()}
|
||||||
case "parse":
|
case ParserMethodParse:
|
||||||
fn, _ := goja.AssertFunction(parseFunc)
|
fn, _ := goja.AssertFunction(parseFunc)
|
||||||
result, err := fn(goja.Undefined(), p.vm.ToValue(req.url))
|
result, err := fn(goja.Undefined(), p.vm.ToValue(req.url))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ var (
|
|||||||
|
|
||||||
func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
|
func ParseWithContext(ctx context.Context, url string) (*parser.Item, error) {
|
||||||
doConfig.Do(func() {
|
doConfig.Do(func() {
|
||||||
parsersMu.Lock()
|
|
||||||
defer parsersMu.Unlock()
|
|
||||||
if len(parsers) == 0 {
|
if len(parsers) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
23
parsers/plugin.go
Normal file
23
parsers/plugin.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package parsers
|
||||||
|
|
||||||
|
import "github.com/blang/semver"
|
||||||
|
|
||||||
|
var (
|
||||||
|
LatestParserVersion = semver.MustParse("1.0.0")
|
||||||
|
MinimumParserVersion = semver.MustParse("1.0.0")
|
||||||
|
)
|
||||||
|
|
||||||
|
type PluginMeta struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Version string `json:"version"` // [TODO] 分版本解析, 但是我们现在只有 v1 所以先不写
|
||||||
|
Description string `json:"description"`
|
||||||
|
Author string `json:"author"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ParserMethod uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
_ ParserMethod = iota
|
||||||
|
ParserMethodCanHandle
|
||||||
|
ParserMethodParse
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user