@@ -20,51 +20,66 @@ q.drain = () => {
20
20
totalJobs = 0
21
21
}
22
22
23
- exports . scheduleJob = async (
24
- job ,
25
- boundActionCreators ,
26
- pluginOptions ,
27
- reporter ,
28
- reportStatus = true
29
- ) => {
30
- const inputFileKey = job . inputPath . replace ( / \. / g, `%2E` )
31
- const outputFileKey = job . outputPath . replace ( / \. / g, `%2E` )
32
- const jobPath = `${ inputFileKey } .${ outputFileKey } `
23
+ const getFileKey = filePath => filePath . replace ( / \. / g, `%2E` )
24
+
25
+ const setJobToProcess = ( toProcess , job , deferred ) => {
26
+ const inputFileKey = getFileKey ( job . inputPath )
27
+ const outputFileKey = getFileKey ( job . outputPath )
28
+ const jobPath = `["${ inputFileKey } "].["${ outputFileKey } "]`
33
29
34
30
// Check if the job has already been queued. If it has, there's nothing
35
31
// to do, return.
36
32
if ( _ . has ( toProcess , jobPath ) ) {
37
- return _ . get ( toProcess , `${ jobPath } .deferred.promise` )
33
+ return { existingPromise : _ . get ( toProcess , `${ jobPath } .deferred.promise` ) }
38
34
}
39
35
40
36
// Check if the output file already exists so we don't redo work.
41
37
if ( existsSync ( job . outputPath ) ) {
42
- return Promise . resolve ( job )
38
+ return { existingPromise : Promise . resolve ( job ) }
43
39
}
44
40
45
41
let isQueued = false
46
42
if ( toProcess [ inputFileKey ] ) {
47
43
isQueued = true
48
44
}
49
45
46
+ _ . set ( toProcess , jobPath , {
47
+ job : job ,
48
+ deferred,
49
+ } )
50
+
51
+ return { isQueued }
52
+ }
53
+
54
+ const scheduleJob = async (
55
+ job ,
56
+ boundActionCreators ,
57
+ pluginOptions ,
58
+ reporter ,
59
+ reportStatus = true
60
+ ) => {
50
61
// deferred naming comes from https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred
51
62
let deferred = { }
52
63
deferred . promise = new Promise ( ( resolve , reject ) => {
53
64
deferred . resolve = resolve
54
65
deferred . reject = reject
55
66
} )
67
+
68
+ const { existingPromise, isQueued } = setJobToProcess (
69
+ toProcess ,
70
+ job ,
71
+ deferred
72
+ )
73
+ if ( existingPromise ) {
74
+ return existingPromise
75
+ }
76
+
56
77
if ( totalJobs === 0 ) {
57
78
bar = createProgress ( `Generating image thumbnails` , reporter )
58
79
bar . start ( )
59
80
}
60
-
61
81
totalJobs += 1
62
82
63
- _ . set ( toProcess , jobPath , {
64
- job : job ,
65
- deferred,
66
- } )
67
-
68
83
if ( ! isQueued ) {
69
84
// Create image job
70
85
const jobId = uuidv4 ( )
@@ -80,7 +95,7 @@ exports.scheduleJob = async (
80
95
q . push ( cb => {
81
96
runJobs (
82
97
jobId ,
83
- inputFileKey ,
98
+ getFileKey ( job . inputPath ) ,
84
99
boundActionCreators ,
85
100
pluginOptions ,
86
101
reportStatus ,
@@ -169,3 +184,5 @@ function runJobs(
169
184
} )
170
185
}
171
186
}
187
+
188
+ export { scheduleJob , setJobToProcess }
0 commit comments