@@ -1245,31 +1245,19 @@ type WatchEventKindRemove =
12451245 | { kind : 'folder' }
12461246 | { kind : 'other' }
12471247
1248+ // TODO: Remove this in v3, return `Watcher` instead
12481249/**
12491250 * @since 2.0.0
12501251 */
12511252type UnwatchFn = ( ) => void
12521253
1253- async function unwatch ( rid : number ) : Promise < void > {
1254- await invoke ( 'plugin:fs|unwatch' , { rid } )
1255- }
1254+ class Watcher extends Resource { }
12561255
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 (
12631257 paths : string | string [ ] | URL | URL [ ] ,
12641258 cb : ( event : WatchEvent ) => void ,
1265- options ? : DebouncedWatchOptions
1259+ options : DebouncedWatchOptions
12661260) : Promise < UnwatchFn > {
1267- const opts = {
1268- recursive : false ,
1269- delayMs : 2000 ,
1270- ...options
1271- }
1272-
12731261 const watchPaths = Array . isArray ( paths ) ? paths : [ paths ]
12741262
12751263 for ( const path of watchPaths ) {
@@ -1283,15 +1271,35 @@ async function watch(
12831271
12841272 const rid : number = await invoke ( 'plugin:fs|watch' , {
12851273 paths : watchPaths . map ( ( p ) => ( p instanceof URL ? p . toString ( ) : p ) ) ,
1286- options : opts ,
1274+ options,
12871275 onEvent
12881276 } )
12891277
1278+ const watcher = new Watcher ( rid )
1279+
12901280 return ( ) => {
1291- void unwatch ( rid )
1281+ void watcher . close ( )
12921282 }
12931283}
12941284
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
12951303/**
12961304 * Watch changes on files or directories.
12971305 *
@@ -1302,32 +1310,10 @@ async function watchImmediate(
13021310 cb : ( event : WatchEvent ) => void ,
13031311 options ?: WatchOptions
13041312) : Promise < UnwatchFn > {
1305- const opts = {
1306- recursive : false ,
1313+ return await watchInternal ( paths , cb , {
13071314 ...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
13261316 } )
1327-
1328- return ( ) => {
1329- void unwatch ( rid )
1330- }
13311317}
13321318
13331319/**
0 commit comments