@@ -1245,31 +1245,19 @@ type WatchEventKindRemove =
1245
1245
| { kind : 'folder' }
1246
1246
| { kind : 'other' }
1247
1247
1248
+ // TODO: Remove this in v3, return `Watcher` instead
1248
1249
/**
1249
1250
* @since 2.0.0
1250
1251
*/
1251
1252
type UnwatchFn = ( ) => void
1252
1253
1253
- async function unwatch ( rid : number ) : Promise < void > {
1254
- await invoke ( 'plugin:fs|unwatch' , { rid } )
1255
- }
1254
+ class Watcher extends Resource { }
1256
1255
1257
- /**
1258
- * Watch changes (after a delay) on files or directories.
1259
- *
1260
- * @since 2.0.0
1261
- */
1262
- async function watch (
1256
+ async function watchInternal (
1263
1257
paths : string | string [ ] | URL | URL [ ] ,
1264
1258
cb : ( event : WatchEvent ) => void ,
1265
- options ? : DebouncedWatchOptions
1259
+ options : DebouncedWatchOptions
1266
1260
) : Promise < UnwatchFn > {
1267
- const opts = {
1268
- recursive : false ,
1269
- delayMs : 2000 ,
1270
- ...options
1271
- }
1272
-
1273
1261
const watchPaths = Array . isArray ( paths ) ? paths : [ paths ]
1274
1262
1275
1263
for ( const path of watchPaths ) {
@@ -1283,15 +1271,35 @@ async function watch(
1283
1271
1284
1272
const rid : number = await invoke ( 'plugin:fs|watch' , {
1285
1273
paths : watchPaths . map ( ( p ) => ( p instanceof URL ? p . toString ( ) : p ) ) ,
1286
- options : opts ,
1274
+ options,
1287
1275
onEvent
1288
1276
} )
1289
1277
1278
+ const watcher = new Watcher ( rid )
1279
+
1290
1280
return ( ) => {
1291
- void unwatch ( rid )
1281
+ void watcher . close ( )
1292
1282
}
1293
1283
}
1294
1284
1285
+ // TODO: Return `Watcher` instead in v3
1286
+ /**
1287
+ * Watch changes (after a delay) on files or directories.
1288
+ *
1289
+ * @since 2.0.0
1290
+ */
1291
+ async function watch (
1292
+ paths : string | string [ ] | URL | URL [ ] ,
1293
+ cb : ( event : WatchEvent ) => void ,
1294
+ options ?: DebouncedWatchOptions
1295
+ ) : Promise < UnwatchFn > {
1296
+ return await watchInternal ( paths , cb , {
1297
+ delayMs : 2000 ,
1298
+ ...options
1299
+ } )
1300
+ }
1301
+
1302
+ // TODO: Return `Watcher` instead in v3
1295
1303
/**
1296
1304
* Watch changes on files or directories.
1297
1305
*
@@ -1302,32 +1310,10 @@ async function watchImmediate(
1302
1310
cb : ( event : WatchEvent ) => void ,
1303
1311
options ?: WatchOptions
1304
1312
) : Promise < UnwatchFn > {
1305
- const opts = {
1306
- recursive : false ,
1313
+ return await watchInternal ( paths , cb , {
1307
1314
...options ,
1308
- delayMs : null
1309
- }
1310
-
1311
- const watchPaths = Array . isArray ( paths ) ? paths : [ paths ]
1312
-
1313
- for ( const path of watchPaths ) {
1314
- if ( path instanceof URL && path . protocol !== 'file:' ) {
1315
- throw new TypeError ( 'Must be a file URL.' )
1316
- }
1317
- }
1318
-
1319
- const onEvent = new Channel < WatchEvent > ( )
1320
- onEvent . onmessage = cb
1321
-
1322
- const rid : number = await invoke ( 'plugin:fs|watch' , {
1323
- paths : watchPaths . map ( ( p ) => ( p instanceof URL ? p . toString ( ) : p ) ) ,
1324
- options : opts ,
1325
- onEvent
1315
+ delayMs : undefined
1326
1316
} )
1327
-
1328
- return ( ) => {
1329
- void unwatch ( rid )
1330
- }
1331
1317
}
1332
1318
1333
1319
/**
0 commit comments