Skip to content

Commit

Permalink
feat: Fix the event problem of ffcreatorcenter
Browse files Browse the repository at this point in the history
  • Loading branch information
drawcall committed Oct 22, 2021
1 parent 7f30d38 commit 923c480
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
9 changes: 6 additions & 3 deletions lib/center/center.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,16 @@ const FFCreatorCenter = {

creator.on('complete', () => {
try {
this.progress.add(taskObj.id);
const { id } = taskObj;
const file = creator.getFile();
const result = { id: taskObj.id, file, taskObj };
taskObj.state = 'complete';
taskObj.file = file;
this.progress.add(id);
this.taskQueue.store(id);

const result = { id, file, taskObj };
this.event.emit('single-complete', result);
FFLogger.info(`Creator production completed. id:${result.id} file: ${file}`);
FFLogger.info(`Creator production completed. id:${id} file: ${file}`);
} catch (error) {
FFLogger.error(`Creator production error. ${util.inspect(error)}`);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/center/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
*/

class Progress {
constructor(max) {
constructor(max = 60) {
this.id = -1;
this.ids = [];
this.percent = 0;
this.max = max || 20;
this.max = max;
}

add(id) {
Expand Down
53 changes: 40 additions & 13 deletions lib/center/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
* @class
*/

const clone = require('lodash/clone');
const forEach = require('lodash/forEach');
const Utils = require('../utils/utils');

class TaskQueue {
constructor() {
this.queue = [];
this.storeData = {};
}

/**
Expand All @@ -24,7 +26,7 @@ class TaskQueue {
push({ task, params = {} }) {
const id = Utils.genUuid();
const paramsWithId = { ...params, taskId: id };
this.queue.push({ id, task, params: paramsWithId, state: 'waiting' });
this.queue.push({ id, task, params: paramsWithId, state: 'waiting', file: null });
return id;
}

Expand All @@ -48,14 +50,27 @@ class TaskQueue {
*/
clear() {
// clear object after 15s
forEach(this.queue, taskObj => {
setTimeout(() => {
Utils.destroyObj(taskObj);
}, 15 * 1000);
});
forEach(this.queue, obj => Utils.destroyObj(obj));
this.queue.length = 0;
}

/**
* Store all make complete data
* @public
*/
store(id) {
const taskObj = this.getTaskById(id);
if (!taskObj) return;

const cloneTaskObj = clone(taskObj);
this.storeData[id] = cloneTaskObj;

// 15 min after delete
setTimeout(() => {
delete this.storeData[id];
}, 15 * 60 * 1000);
}

getLength() {
return this.queue.length;
}
Expand All @@ -65,20 +80,32 @@ class TaskQueue {
* @public
*/
getTaskState(id) {
for (let taskObj of this.queue) {
if (id === taskObj.id) return taskObj.state;
}

return 'unknown';
const taskObj = this.getTaskById(id);
return taskObj ? taskObj.state : 'unknown';
}

/**
* Get the result file by id
* @public
*/
getResultFile(id) {
for (let taskObj of this.queue) {
if (id === taskObj.id) return taskObj.file;
const taskObj = this.getTaskById(id);
return taskObj ? taskObj.file : null;
}

/**
* Get the task from queue
* @public
*/
getTaskById(id) {
for (let i = 0; i < this.queue.length; i++) {
const taskObj = this.queue[i];
if (id === taskObj.id) return taskObj;
}

for (let key in this.storeData) {
const cloneTaskObj = this.storeData[key];
if (id === cloneTaskObj.id) return cloneTaskObj;
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ffcreator",
"version": "6.2.8",
"version": "6.3.1",
"description": "FFCreator is a lightweight and flexible short video production library",
"main": "lib/index.js",
"types": "types/index.d.ts",
Expand Down

0 comments on commit 923c480

Please sign in to comment.