UNPKG

@foxpage/foxpage-manager

Version:

foxpage resource manager

143 lines (142 loc) 4.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TagManagerImpl = void 0; const lodash_1 = __importDefault(require("lodash")); const foxpage_shared_1 = require("@foxpage/foxpage-shared"); const common_1 = require("../common"); const data_service_1 = require("../data-service"); /** * tag manager * * @export * @class TagManagerImpl */ class TagManagerImpl extends common_1.ManagerBaseImpl { constructor(app) { super(app, { type: 'tag', diskCache: { enable: true } }); /** * fileId & pageIds map * key: fileId, value: pageIds * * @private */ this.pageIdMap = new Map(); } /** * add tag * * @param {ContentTag} content */ addTag(content) { this.logger.info(`add tag content: ${content.id}`); this.logger.debug(`add tag content:`, JSON.stringify(content)); const { id: pageId, fileId } = content || {}; this.addOne(pageId, content, content); let pageIds = this.pageIdMap.get(fileId); if (!pageIds) { pageIds = [pageId]; } else { const exist = pageIds.indexOf(pageId) > -1; if (!exist) { pageIds.push(pageId); } } this.pageIdMap.set(fileId, pageIds); } /** * remove tags by pageIds * * @param {string[]} pageIds */ removeTags(pageIds = []) { // remove source this.remove(pageIds); this.pageIdMap.forEach((value, key) => { const result = lodash_1.default.remove(value, item => pageIds.indexOf(item) > -1); this.pageIdMap.set(key, result); }); } /** * get tag, contains content info * * @param {Tag[]} tags * @param {TagMatchOption} opt * @return {*} {(Promise<Content | null>)} */ async matchTag(tags, opt) { const { pathname = '', fileId = '' } = opt; if (fileId) { const pageIds = this.pageIdMap.get(fileId); let contentTags = []; let content = null; if (pageIds && pageIds.length > 0) { // only find from local contentTags = await this.find(pageIds, { autoFetch: false }); } if (contentTags.length > 0) { content = foxpage_shared_1.tag.matchContent(contentTags, tags); } if (content) { return content; } } this.logger.info(`local not exist the fileId "${fileId}" with tags: ${JSON.stringify(tags)} content, will fetch from server.`); let result = null; if (opt.withContentInfo) { result = await this.freshWithTags(fileId, pathname, tags); } else { result = await data_service_1.foxpageDataService.fetchContentByTags(this.appId, fileId, pathname, tags); } if (!result) { this.logger.warn(`not match the pathname "${pathname}", tags:`, tags); return null; } return result; } async onPull(data) { // updates & removes is content ids const { updates, removes } = data.tag || {}; if (updates === null || updates === void 0 ? void 0 : updates.length) { const contentIds = await this.filterExists(updates); if (contentIds.length > 0) { this.markNeedUpdates(contentIds); const contents = await data_service_1.foxpageDataService.fetchContents(this.appId, { contentIds }); contents.forEach(content => { this.addOne(content.id, content, content); }); } } if (removes === null || removes === void 0 ? void 0 : removes.length) { this.removeTags(removes); } } async createInstance(data) { return data; } async onFetch(_list) { return undefined; } async freshWithTags(fileId, pathname, tags = []) { const result = await data_service_1.foxpageDataService.fetchContentInfoByTags(this.appId, fileId, pathname, tags); if (result && result.content) { this.logger.info('fetched content info with tags:', JSON.stringify(tags)); if (result.contentInfo) { // emit event: cache user request data this.emit('DATA_PUSH', result.contentInfo); } this.addTag(result.content); return result.content; } return null; } destroy() { super.destroy(); this.pageIdMap.clear(); } } exports.TagManagerImpl = TagManagerImpl;