@foxpage/foxpage-manager
Version:
foxpage resource manager
142 lines (141 loc) • 4.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlockManagerImpl = void 0;
const lodash_1 = require("lodash");
const common_1 = require("../common");
const data_service_1 = require("../data-service");
const block_1 = require("./block");
const CHUNK_SIZE = 5;
/**
* block manager
*
* @export
* @class BlockManager
*/
class BlockManagerImpl extends common_1.ManagerBaseImpl {
constructor(app) {
super(app, { type: 'block', diskCache: { enable: true } });
}
/**
* add block to manager
*
* @param {Block} block
*/
addBlock(block) {
this.logger.info(`add block@${block.id}`);
this.logger.debug(`add block@${block.id}, detail:`, JSON.stringify(block));
const newBlock = this.newBlock(block);
this.addOne(block.id, block, newBlock);
return newBlock;
}
/**
* remove block from manager
*
* @param {string[]} blockIds
*/
removeBlocks(blockIds) {
this.remove(blockIds);
}
/**
* get block from local first
* no exist will fetch from server
*
* @param {string} blockId
* @return {*} {(Block | undefined)}
*/
async getBlock(blockId) {
return (await this.getBlocks([blockId]))[0];
}
/**
* get blocks
*
* @param {string[]} blockIds
* @return {*} {Promise<Block[]>}
*/
async getBlocks(blockIds) {
return await this.find(blockIds);
}
/**
* fetch draft blocks
*
* @param {string[]} blockIds
* @return {*} {Promise<ContentRelationInfo[]>}
*/
async getDraftBlocks(blockIds) {
return await data_service_1.foxpageDataService.fetchDraftBlockRelationInfos(this.appId, { contentIds: blockIds });
}
/**
* fetch draft blocks
*
* @param {string[]} blockId
* @param {number} version
* @return {*} {Promise<ContentRelationInfo>}
*/
async getPreviewBlocks(blockId, version) {
return await data_service_1.foxpageDataService.fetchPreviewBlockRelationInfos(this.appId, { contentId: blockId, version });
}
/**
* fetch blocks from server
*
* @return {*} {Promise<Block[]>}
*/
async freshBlocks(blockIds = []) {
const chunks = (0, lodash_1.chunk)(blockIds, CHUNK_SIZE);
let blocks = [];
const fetcher = async (list) => {
for (const _blockIds of list) {
const result = await data_service_1.foxpageDataService.fetchBlocks(this.appId, { blockIds: _blockIds });
blocks = blocks.concat(result);
}
};
await fetcher(chunks);
// add & update
return blocks.map(block => {
return this.addBlock(block);
});
}
/**
* first request block will return the all relations
*
* @protected
* @param {string[]} blockIds
*/
async onFetch(blockIds) {
const results = await data_service_1.foxpageDataService.fetchBlockRelationInfos(this.appId, { contentIds: blockIds });
this.logger.info('fetched content');
this.logger.debug('fetched content infos:', JSON.stringify(results));
return results.map(item => {
// emit event: cache user request data
this.emit('DATA_PUSH', item.relations);
return this.addBlock(item.content);
});
}
async onPull(data) {
this.logger.info('get pull');
this.logger.debug('get pull, detail:', data);
const { updates, removes } = data.block || {};
if (updates && updates.length > 0) {
const contentIds = await this.filterExists(updates);
if (contentIds.length > 0) {
this.markNeedUpdates(contentIds);
await this.freshBlocks(contentIds);
}
}
if (removes && removes.length > 0) {
this.removeBlocks(removes);
}
}
onStash(data) {
var _a;
(_a = data.blocks) === null || _a === void 0 ? void 0 : _a.map(item => {
this.addBlock(item);
});
}
async createInstance(data) {
return this.newBlock(data);
}
newBlock(data) {
return new block_1.BlockInstance(data);
}
}
exports.BlockManagerImpl = BlockManagerImpl;