import DoubanPlugin from "../../../main"; import {BasicConst, SubjectHandledStatus, SyncType} from "../../../constant/Constsant"; import {DoubanSyncHandler} from "./DoubanSyncHandler"; import {SyncConfig} from "../model/SyncConfig"; import HandleContext from "../../data/model/HandleContext"; import {SubjectListItem} from "../../data/model/SubjectListItem"; import {sleepRange} from "../../../utils/TimeUtil"; import DoubanSubjectLoadHandler from "../../data/handler/DoubanSubjectLoadHandler"; import {DoubanListHandler} from "./list/DoubanListHandler"; import DoubanSubject from "../../data/model/DoubanSubject"; import {log} from "../../../utils/Logutil"; import {i18nHelper} from "../../../lang/helper"; export abstract class DoubanAbstractSyncHandler implements DoubanSyncHandler{ private plugin: DoubanPlugin; private doubanSubjectLoadHandler:DoubanSubjectLoadHandler; private doubanListHandlers:DoubanListHandler[]; constructor(plugin: DoubanPlugin, doubanSubjectLoadHandler:DoubanSubjectLoadHandler, doubanListHandlers:DoubanListHandler[]) { this.plugin = plugin; this.doubanSubjectLoadHandler = doubanSubjectLoadHandler; this.doubanListHandlers = doubanListHandlers; } support(t: string): boolean { return this.getSyncType() == t; } abstract getSyncType(): SyncType; async sync(syncConfig: SyncConfig, context: HandleContext): Promise{ return Promise.resolve() .then(() => this.getItems(syncConfig, context)) .then(items => this.removeExists(items , syncConfig, context)) .then(items => this.handleItems(items, context)); } private async getItems(syncConfig:SyncConfig, context:HandleContext):Promise { const supportHandlers:DoubanListHandler[] = this.doubanListHandlers.filter((h) => h.support(syncConfig)); let items:SubjectListItem[] = []; for(const handler of supportHandlers) { if (!context.plugin.statusHolder.syncing()) { return []; } const item = await handler.getAllPageList(context); if (item) { items = items.concat(item); } } return items; } private async removeExists(items:SubjectListItem[], syncConfig: SyncConfig, context: HandleContext):Promise { if (!context.plugin.statusHolder.syncing()) { return []; } return items; } private async handleItems(items:SubjectListItem[], context:HandleContext):Promise { if (!items || items.length == 0) { return ; } const {syncStatus} = context.syncStatusHolder; syncStatus.totalNum(items.length); const needHandled:number = items.filter(item => syncStatus.shouldSync(item.id)).length; syncStatus.setNeedHandled(needHandled); for (const item of items) { if (!context.plugin.statusHolder.syncing()) { return; } try { if(syncStatus.shouldSync(item.id)) { let subject: DoubanSubject = await this.doubanSubjectLoadHandler.handle(item.id, context); await sleepRange(BasicConst.CALL_DOUBAN_DELAY, BasicConst.CALL_DOUBAN_DELAY + BasicConst.CALL_DOUBAN_DELAY_RANGE); }else { syncStatus.unHandle(item.id, item.title); } }catch (e) { log.notice(i18nHelper.getMessage('130120')) } } } }