Skip to content

Commit 923c480

Browse files
committed
feat: Fix the event problem of ffcreatorcenter
1 parent 7f30d38 commit 923c480

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

lib/center/center.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,16 @@ const FFCreatorCenter = {
201201

202202
creator.on('complete', () => {
203203
try {
204-
this.progress.add(taskObj.id);
204+
const { id } = taskObj;
205205
const file = creator.getFile();
206-
const result = { id: taskObj.id, file, taskObj };
207206
taskObj.state = 'complete';
208207
taskObj.file = file;
208+
this.progress.add(id);
209+
this.taskQueue.store(id);
210+
211+
const result = { id, file, taskObj };
209212
this.event.emit('single-complete', result);
210-
FFLogger.info(`Creator production completed. id:${result.id} file: ${file}`);
213+
FFLogger.info(`Creator production completed. id:${id} file: ${file}`);
211214
} catch (error) {
212215
FFLogger.error(`Creator production error. ${util.inspect(error)}`);
213216
}

lib/center/progress.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
*/
99

1010
class Progress {
11-
constructor(max) {
11+
constructor(max = 60) {
1212
this.id = -1;
1313
this.ids = [];
1414
this.percent = 0;
15-
this.max = max || 20;
15+
this.max = max;
1616
}
1717

1818
add(id) {

lib/center/task.js

+40-13
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
* @class
88
*/
99

10+
const clone = require('lodash/clone');
1011
const forEach = require('lodash/forEach');
1112
const Utils = require('../utils/utils');
1213

1314
class TaskQueue {
1415
constructor() {
1516
this.queue = [];
17+
this.storeData = {};
1618
}
1719

1820
/**
@@ -24,7 +26,7 @@ class TaskQueue {
2426
push({ task, params = {} }) {
2527
const id = Utils.genUuid();
2628
const paramsWithId = { ...params, taskId: id };
27-
this.queue.push({ id, task, params: paramsWithId, state: 'waiting' });
29+
this.queue.push({ id, task, params: paramsWithId, state: 'waiting', file: null });
2830
return id;
2931
}
3032

@@ -48,14 +50,27 @@ class TaskQueue {
4850
*/
4951
clear() {
5052
// clear object after 15s
51-
forEach(this.queue, taskObj => {
52-
setTimeout(() => {
53-
Utils.destroyObj(taskObj);
54-
}, 15 * 1000);
55-
});
53+
forEach(this.queue, obj => Utils.destroyObj(obj));
5654
this.queue.length = 0;
5755
}
5856

57+
/**
58+
* Store all make complete data
59+
* @public
60+
*/
61+
store(id) {
62+
const taskObj = this.getTaskById(id);
63+
if (!taskObj) return;
64+
65+
const cloneTaskObj = clone(taskObj);
66+
this.storeData[id] = cloneTaskObj;
67+
68+
// 15 min after delete
69+
setTimeout(() => {
70+
delete this.storeData[id];
71+
}, 15 * 60 * 1000);
72+
}
73+
5974
getLength() {
6075
return this.queue.length;
6176
}
@@ -65,20 +80,32 @@ class TaskQueue {
6580
* @public
6681
*/
6782
getTaskState(id) {
68-
for (let taskObj of this.queue) {
69-
if (id === taskObj.id) return taskObj.state;
70-
}
71-
72-
return 'unknown';
83+
const taskObj = this.getTaskById(id);
84+
return taskObj ? taskObj.state : 'unknown';
7385
}
7486

7587
/**
7688
* Get the result file by id
7789
* @public
7890
*/
7991
getResultFile(id) {
80-
for (let taskObj of this.queue) {
81-
if (id === taskObj.id) return taskObj.file;
92+
const taskObj = this.getTaskById(id);
93+
return taskObj ? taskObj.file : null;
94+
}
95+
96+
/**
97+
* Get the task from queue
98+
* @public
99+
*/
100+
getTaskById(id) {
101+
for (let i = 0; i < this.queue.length; i++) {
102+
const taskObj = this.queue[i];
103+
if (id === taskObj.id) return taskObj;
104+
}
105+
106+
for (let key in this.storeData) {
107+
const cloneTaskObj = this.storeData[key];
108+
if (id === cloneTaskObj.id) return cloneTaskObj;
82109
}
83110

84111
return null;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ffcreator",
3-
"version": "6.2.8",
3+
"version": "6.3.1",
44
"description": "FFCreator is a lightweight and flexible short video production library",
55
"main": "lib/index.js",
66
"types": "types/index.d.ts",

0 commit comments

Comments
 (0)