@@ -12,16 +12,6 @@ const createFSMachine = (
12
12
{ actions : { createNode, deleteNode } , getNode, createNodeId, reporter } ,
13
13
pluginOptions
14
14
) => {
15
- // For every path that is reported before the 'ready' event, we throw them
16
- // into a queue and then flush the queue when 'ready' event arrives.
17
- // After 'ready', we handle the 'add' event without putting it into a queue.
18
- let pathQueue = [ ]
19
- const flushPathQueue = ( ) => {
20
- let queue = pathQueue . slice ( )
21
- pathQueue = null
22
- return Promise . all ( queue . map ( createAndProcessNode ) )
23
- }
24
-
25
15
const createAndProcessNode = path => {
26
16
const fileNodePromise = createFileNode (
27
17
path ,
@@ -34,6 +24,35 @@ const createFSMachine = (
34
24
return fileNodePromise
35
25
}
36
26
27
+ const deletePathNode = path => {
28
+ const node = getNode ( createNodeId ( path ) )
29
+ // It's possible the node was never created as sometimes tools will
30
+ // write and then immediately delete temporary files to the file system.
31
+ if ( node ) {
32
+ deleteNode ( { node } )
33
+ }
34
+ }
35
+
36
+ // For every path that is reported before the 'ready' event, we throw them
37
+ // into a queue and then flush the queue when 'ready' event arrives.
38
+ // After 'ready', we handle the 'add' event without putting it into a queue.
39
+ let pathQueue = [ ]
40
+ const flushPathQueue = ( ) => {
41
+ let queue = pathQueue . slice ( )
42
+ pathQueue = null
43
+ return Promise . all (
44
+ // eslint-disable-next-line consistent-return
45
+ queue . map ( ( { op, path } ) => {
46
+ switch ( op ) {
47
+ case `delete` :
48
+ return deletePathNode ( path )
49
+ case `upsert` :
50
+ return createAndProcessNode ( path )
51
+ }
52
+ } )
53
+ )
54
+ }
55
+
37
56
const fsMachine = Machine (
38
57
{
39
58
id : `fs` ,
@@ -59,20 +78,19 @@ const createFSMachine = (
59
78
on : {
60
79
CHOKIDAR_READY : `READY` ,
61
80
CHOKIDAR_ADD : { actions : `queueNodeProcessing` } ,
81
+ CHOKIDAR_CHANGE : { actions : `queueNodeProcessing` } ,
82
+ CHOKIDAR_UNLINK : { actions : `queueNodeDeleting` } ,
62
83
} ,
63
84
exit : `flushPathQueue` ,
64
85
} ,
65
86
READY : {
66
87
on : {
67
88
CHOKIDAR_ADD : { actions : `createAndProcessNode` } ,
89
+ CHOKIDAR_CHANGE : { actions : `createAndProcessNode` } ,
90
+ CHOKIDAR_UNLINK : { actions : `deletePathNode` } ,
68
91
} ,
69
92
} ,
70
93
} ,
71
- // TODO: those two were not restricted to READY state, but maybe they should? or even maybe it should queue in NOT_READY?
72
- on : {
73
- CHOKIDAR_CHANGE : { actions : `createAndProcessNode` } ,
74
- CHOKIDAR_UNLINK : { actions : `deleteNode` } ,
75
- } ,
76
94
} ,
77
95
} ,
78
96
} ,
@@ -84,22 +102,20 @@ const createFSMachine = (
84
102
}
85
103
createAndProcessNode ( path ) . catch ( err => reporter . error ( err ) )
86
104
} ,
87
- deleteNode ( _ , { pathType, path } , { state } ) {
105
+ deletePathNode ( _ , { pathType, path } , { state } ) {
88
106
if ( state . matches ( `BOOTSTRAP.BOOTSTRAPPED` ) ) {
89
107
reporter . info ( `${ pathType } deleted at ${ path } ` )
90
108
}
91
- const node = getNode ( createNodeId ( path ) )
92
- // It's possible the node was never created as sometimes tools will
93
- // write and then immediately delete temporary files to the file system.
94
- if ( node ) {
95
- deleteNode ( { node } )
96
- }
109
+ deletePathNode ( path )
97
110
} ,
98
111
flushPathQueue ( _ , { resolve, reject } ) {
99
112
flushPathQueue ( ) . then ( resolve , reject )
100
113
} ,
114
+ queueNodeDeleting ( _ , { path } ) {
115
+ pathQueue . push ( { op : `delete` , path } )
116
+ } ,
101
117
queueNodeProcessing ( _ , { path } ) {
102
- pathQueue . push ( path )
118
+ pathQueue . push ( { op : `upsert` , path } )
103
119
} ,
104
120
} ,
105
121
}
0 commit comments