@foxpage/foxpage-manager
Version:
foxpage resource manager
110 lines (109 loc) • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FunctionManagerImpl = void 0;
const common_1 = require("../common");
const data_service_1 = require("../data-service");
const function_1 = require("./function");
/**
* function manager
*
* @export
* @class FunctionManager
* @extends {ManagerBaseImpl}
*/
class FunctionManagerImpl extends common_1.ManagerBaseImpl {
constructor(app) {
super(app, { type: 'function', diskCache: { enable: true } });
}
/**
* add function content to manager
*
* @param {FPFunction} content
*/
addFunction(content) {
this.logger.info(`add function@${content.id}`);
this.logger.debug(`add function@${content.id}, detail:`, JSON.stringify(content));
const newFunc = this.newFunction(content);
this.addOne(content.id, content, newFunc);
return newFunc;
}
/**
* get function
*
* @param {string} functionId
* @return {*} {(Promise<FPFunction | undefined>)}
*/
async getFunction(functionId) {
return (await this.getFunctions([functionId]))[0];
}
/**
* get function item
*
* @param {string} functionId
* @param {string} functionItemName
* @return {*} {(Promise<FPFunctionItem | undefined)}
*/
async getFunctionItem(functionId, functionItemName) {
const result = await this.getFunction(functionId);
if (!result) {
this.logger.warn(`not exist this function@${functionId}`);
return null;
}
return result.getFunctionItem ? result.getFunctionItem(functionItemName) : null;
}
/**
* get functions
*
* @param {string[]} functionIds
* @return {*} {Promise<FPFunction[]>}
*/
async getFunctions(functionIds) {
return await this.find(functionIds);
}
/**
* fetch application functions
*/
async freshFunctions(functionIds) {
const functions = await data_service_1.foxpageDataService.fetchFunctions(this.appId, { functionIds });
return functions.map(item => {
return this.addFunction(item);
});
}
/**
* remove functions via functionIds
*
* @param {string[]} functionIds
*/
removeFunctions(functionIds) {
this.remove(functionIds);
}
async onFetch(list) {
return await this.freshFunctions(list);
}
async onPull(data) {
const { updates, removes } = data.function || {};
if (updates && updates.length > 0) {
const contentIds = await this.filterExists(updates);
if (contentIds.length > 0) {
this.markNeedUpdates(contentIds);
await this.freshFunctions(contentIds);
}
}
if (removes && removes.length > 0) {
this.removeFunctions(removes);
}
}
onStash(data) {
var _a;
(_a = data.functions) === null || _a === void 0 ? void 0 : _a.map(item => {
this.addFunction(item);
});
}
async createInstance(data) {
return this.newFunction(data);
}
newFunction(data) {
return new function_1.FPFunctionInstance(data);
}
}
exports.FunctionManagerImpl = FunctionManagerImpl;