@@ -16,6 +16,7 @@ import 'package:watcher/watcher.dart';
16
16
17
17
import '../exception.dart' ;
18
18
import '../js/chokidar.dart' ;
19
+ import '../js/parcel_watcher.dart' ;
19
20
20
21
@JS ('process' )
21
22
external final Process ? _nodeJsProcess; // process is null in the browser
@@ -248,39 +249,65 @@ int get exitCode => _process?.exitCode ?? 0;
248
249
249
250
set exitCode (int code) => _process? .exitCode = code;
250
251
251
- Future <Stream <WatchEvent >> watchDir (String path, {bool poll = false }) {
252
+ Future <Stream <WatchEvent >> watchDir (String path, {bool poll = false }) async {
252
253
if (! isNodeJs) {
253
254
throw UnsupportedError ("watchDir() is only supported on Node.js" );
254
255
}
255
- var watcher = chokidar.watch (path, ChokidarOptions (usePolling: poll));
256
256
257
257
// Don't assign the controller until after the ready event fires. Otherwise,
258
258
// Chokidar will give us a bunch of add events for files that already exist.
259
259
StreamController <WatchEvent >? controller;
260
- watcher
261
- ..on (
262
- 'add' ,
263
- allowInterop ((String path, [void _]) =>
264
- controller? .add (WatchEvent (ChangeType .ADD , path))))
265
- ..on (
266
- 'change' ,
267
- allowInterop ((String path, [void _]) =>
268
- controller? .add (WatchEvent (ChangeType .MODIFY , path))))
269
- ..on (
270
- 'unlink' ,
271
- allowInterop ((String path) =>
272
- controller? .add (WatchEvent (ChangeType .REMOVE , path))))
273
- ..on ('error' , allowInterop ((Object error) => controller? .addError (error)));
274
-
275
- var completer = Completer <Stream <WatchEvent >>();
276
- watcher.on ('ready' , allowInterop (() {
277
- // dart-lang/sdk#45348
278
- var stream = (controller = StreamController <WatchEvent >(onCancel: () {
279
- watcher.close ();
260
+ if (poll) {
261
+ var watcher = chokidar.watch (path, ChokidarOptions (usePolling: true ));
262
+ watcher
263
+ ..on (
264
+ 'add' ,
265
+ allowInterop ((String path, [void _]) =>
266
+ controller? .add (WatchEvent (ChangeType .ADD , path))))
267
+ ..on (
268
+ 'change' ,
269
+ allowInterop ((String path, [void _]) =>
270
+ controller? .add (WatchEvent (ChangeType .MODIFY , path))))
271
+ ..on (
272
+ 'unlink' ,
273
+ allowInterop ((String path) =>
274
+ controller? .add (WatchEvent (ChangeType .REMOVE , path))))
275
+ ..on (
276
+ 'error' , allowInterop ((Object error) => controller? .addError (error)));
277
+
278
+ var completer = Completer <Stream <WatchEvent >>();
279
+ watcher.on ('ready' , allowInterop (() {
280
+ // dart-lang/sdk#45348
281
+ var stream = (controller = StreamController <WatchEvent >(onCancel: () {
282
+ watcher.close ();
283
+ }))
284
+ .stream;
285
+ completer.complete (stream);
286
+ }));
287
+
288
+ return completer.future;
289
+ } else {
290
+ var subscription = await ParcelWatcher .subscribeFuture (path,
291
+ (Object ? error, List <ParcelWatcherEvent > events) {
292
+ if (error != null ) {
293
+ controller? .addError (error);
294
+ } else {
295
+ for (var event in events) {
296
+ switch (event.type) {
297
+ case 'create' :
298
+ controller? .add (WatchEvent (ChangeType .ADD , event.path));
299
+ case 'update' :
300
+ controller? .add (WatchEvent (ChangeType .MODIFY , event.path));
301
+ case 'delete' :
302
+ controller? .add (WatchEvent (ChangeType .REMOVE , event.path));
303
+ }
304
+ }
305
+ }
306
+ });
307
+
308
+ return (controller = StreamController <WatchEvent >(onCancel: () {
309
+ subscription.unsubscribe ();
280
310
}))
281
311
.stream;
282
- completer.complete (stream);
283
- }));
284
-
285
- return completer.future;
312
+ }
286
313
}
0 commit comments