@foxpage/foxpage-manager
Version:
foxpage resource manager
48 lines (47 loc) • 1.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Schedule = void 0;
const common_1 = require("../common");
/**
* schedule
*
* @export
* @class Schedule
* @extends {FPEventEmitterInstance<ScheduleEvents<T>>}
*/
class Schedule extends common_1.FPEventEmitterInstance {
constructor(action, opt = {}) {
super();
this.action = action;
this.appId = opt.appId;
this.interval = opt.interval;
this.addListener('timeout', this.dispatch);
this.logger = (0, common_1.createLogger)(`app@${this.appId} schedule`);
}
start() {
if (!this.handler) {
this.handler = setInterval(() => this.emit('timeout'), this.interval);
}
}
stop() {
if (this.handler) {
clearInterval(this.handler);
this.handler = undefined;
this.data = null;
}
}
async dispatch() {
try {
const result = await this.action(this.data);
// update nextData
this.data = result;
// dispatch receive data
this.emit('DATA_RECEIVE', result);
}
catch (e) {
this.emit('ERROR', e);
this.logger.error('dispatch action error:', e);
}
}
}
exports.Schedule = Schedule;