Skip to content

Commit

Permalink
chore: change source to array (#97)
Browse files Browse the repository at this point in the history
* chore: change sources to array

* chore: revert commit
  • Loading branch information
Seidko authored Jul 13, 2023
1 parent a18b8f7 commit 5784222
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Dict, Element, Logger, Quester, Schema, Service, Session } from 'koishi'
import { Context, Element, Logger, Quester, Schema, Service, Session } from 'koishi'
import LanguageDetect from 'languagedetect'
import { ImageSource } from './source'
import { } from '@koishijs/assets'
Expand All @@ -14,32 +14,29 @@ declare module 'koishi' {
}

class ImageService extends Service {
private config: Config
private sources: Dict<ImageSource> = {}
private counter = 0

private sources: ImageSource[] = []
private languageDetect = new LanguageDetect()

constructor(ctx: Context, config: Config) {
constructor(ctx: Context, private config: Config) {
super(ctx, 'booru', true)
this.config = config
}

register(source: ImageSource) {
const id = ++this.counter
this.sources[id] = source
return this.caller.collect('booru', () => delete this.sources[id])
const index = this.sources.length
this.sources.push(source)
return this.caller.collect('booru', () => delete this.sources[index])
}

hasSource(name?: string): boolean {
if (name) {
return Object.values(this.sources).some((source) => source.config.label === name)
return this.sources.some((source) => source.config.label === name)
}
return Object.keys(this.sources).length > 0
return this.sources.some(Boolean)
}

async get(query: ImageService.Query): Promise<ImageArray> {
const sources = Object.values(this.sources)
const sources = this.sources
.filter((source) => {
if (query.labels.length && !query.labels.includes(source.config.label)) return false
if (this.config.detectLanguage) {
Expand All @@ -60,7 +57,7 @@ class ImageService extends Service {
// return the first non-empty result
for (const source of sources) {
const tags = source.tokenize(query.query)
const images = await source.get({ ...query, tags, raw: query.query }).catch((err) => {
const images = await source.get({ count: query.count, tags, raw: query.query }).catch((err) => {
if (Quester.isAxiosError(err)) {
logger.warn(`source ${source.config.label} request failed with code ${err.status} ${JSON.stringify(err.response?.data)}`)
} else {
Expand Down

0 comments on commit 5784222

Please sign in to comment.