@foxpage/foxpage-manager
Version:
foxpage resource manager
95 lines (94 loc) • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TemplateManagerImpl = void 0;
const common_1 = require("../common");
const data_service_1 = require("../data-service");
const template_1 = require("./template");
/**
* template manager
*
* @export
* @class TemplateManager
*/
class TemplateManagerImpl extends common_1.ManagerBaseImpl {
constructor(app) {
super(app, { type: 'template', diskCache: { enable: true } });
}
/**
* add template to manager
*
* @param {Template} template
*/
addTemplate(template) {
this.logger.info(`add template@${template.id}`);
this.logger.debug(`add template@${template.id}, detail:`, JSON.stringify(template));
const newTpl = this.newTemplate(template);
this.addOne(template.id, template, newTpl);
return newTpl;
}
/**
* get template
* if not exist local, will fetch from server then cache to local
* @param {string} templateId
*/
async getTemplate(templateId) {
return (await this.getTemplates([templateId]))[0];
}
/**
* get templates batch
*
* @param {string[]} templateIds
* @return {*} {Promise<Template[]>}
*/
async getTemplates(templateIds) {
return await this.find(templateIds);
}
/**
* remove template
*
* @param {string[]} templateIds
*/
removeTemplates(templateIds) {
this.remove(templateIds);
}
/**
* fetch templates from server
*
* @return {*} Promise<Template[]>
*/
async freshTemplates(templateIds) {
const templates = await data_service_1.foxpageDataService.fetchTemplates(this.appId, { templateIds });
return templates.map(template => {
return this.addTemplate(template);
});
}
async onFetch(templateIds) {
return this.freshTemplates(templateIds);
}
async onPull(data) {
const { updates, removes } = data.template || {};
if (updates && updates.length > 0) {
const contentIds = await this.filterExists(updates);
if (contentIds.length > 0) {
this.markNeedUpdates(contentIds);
await this.freshTemplates(contentIds);
}
}
if (removes && removes.length > 0) {
this.removeTemplates(removes);
}
}
onStash(data) {
var _a;
(_a = data.templates) === null || _a === void 0 ? void 0 : _a.map(template => {
this.addTemplate(template);
});
}
async createInstance(data) {
return this.newTemplate(data);
}
newTemplate(data) {
return new template_1.TemplateInstance(data);
}
}
exports.TemplateManagerImpl = TemplateManagerImpl;