simple-watcher
Advanced tools
Comparing version 5.0.0 to 5.0.1
26
index.js
@@ -14,18 +14,2 @@ 'use strict' | ||
// AbortController falback. | ||
export class AbortController { | ||
constructor () { | ||
this.callbacks = [] | ||
this.aborted = false | ||
this.signal = { | ||
aborted: false, | ||
onabort: callback => this.callbacks.push(callback), | ||
abort: () => { | ||
this.callbacks.forEach(cb => cb()) | ||
this.signal.aborted = true | ||
} | ||
} | ||
} | ||
} | ||
// Graceful closing. | ||
@@ -36,3 +20,3 @@ function createWatchers (abortSignal) { | ||
// Close all watchers on abort. | ||
abortSignal && abortSignal.onabort(() => { | ||
abortSignal && abortSignal.addEventListener('abort', () => { | ||
for (const entityPath of Object.keys(watchers)) { | ||
@@ -50,3 +34,3 @@ close(entityPath) | ||
watchers[entityPath] = w | ||
w.on('error', err => watchers.close(pathToWatch)) | ||
w.on('error', () => watchers.close(entityPath)) | ||
} | ||
@@ -70,3 +54,3 @@ } | ||
abortSignal && abortSignal.onabort(() => { | ||
abortSignal && abortSignal.addEventListener('abort', () => { | ||
const nextPromise = promiseQueue.shift() | ||
@@ -171,3 +155,3 @@ nextPromise && nextPromise.resolve() | ||
export default async function* watch (pathsToWatch, options = {}) { | ||
export default async function * watch (pathsToWatch, options = {}) { | ||
// Normalize paths to array. | ||
@@ -181,3 +165,3 @@ pathsToWatch = pathsToWatch.constructor === Array ? pathsToWatch : [pathsToWatch] | ||
options.fallback = options.fallback ?? !PLATFORMS.includes(process.platform) | ||
const watchFunction = options.fallback ? watchFallback : watchNative | ||
const watchFunction = options.fallback ? watchFallback : watchNative | ||
@@ -184,0 +168,0 @@ // Create watchers registry. |
{ | ||
"name": "simple-watcher", | ||
"version": "5.0.0", | ||
"version": "5.0.1", | ||
"description": "\"A simple file s watcher.\"", | ||
@@ -15,3 +15,3 @@ "type": "module", | ||
"engines": { | ||
"node": ">=14.15.1" | ||
"node": ">=16.16.0" | ||
}, | ||
@@ -37,5 +37,5 @@ "repository": { | ||
"devDependencies": { | ||
"fs-extra": "^9.1.0", | ||
"triala": "^0.4.0" | ||
"fs-extra": "10.1.0", | ||
"triala": "0.4.0" | ||
} | ||
} |
@@ -28,11 +28,10 @@ # Simple Watcher | ||
```JavaScript | ||
import watch, { AbortController } from 'simple-watcher' | ||
import watch from 'simple-watcher' | ||
// The AbortController is available natively since 15.9.0. | ||
// Optional: abort the watcher after 10 seconds. | ||
const ac = new AbortController() | ||
const { signal } = ac | ||
setTimeout(() => ac.abort(), 10000) | ||
// Watch over file or directory. | ||
for await (const changedPath of watch('/path/to/foo'), { signal }) { | ||
for await (const changedPath of watch('/path/to/foo'), { signal: ac.signal }) { | ||
console.log(`Changed: ${filePath}`) | ||
@@ -42,4 +41,4 @@ } | ||
// Watch over multiple paths. | ||
for await (const changedPath of watch(['/path/to/bar', '/path/to/baz']), { signal }) { | ||
for await (const changedPath of watch(['/path/to/bar', '/path/to/baz']), { signal: ac.signal }) { | ||
console.log(`Changed: ${filePath}`) | ||
} |
@@ -7,3 +7,3 @@ 'use strict' | ||
import test from 'triala' | ||
import watch, { AbortController } from '../index.js' | ||
import watch from '../index.js' | ||
@@ -41,6 +41,5 @@ // Existing paths. | ||
async _pause (ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
return new Promise(resolve => setTimeout(resolve, ms)) | ||
} | ||
_touch (path) { | ||
@@ -52,8 +51,8 @@ const time = new Date() | ||
async _timeout (ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
return new Promise(resolve => setTimeout(resolve, ms)) | ||
} | ||
async 'Watch over directory' () { | ||
const signal = (new AbortController()).signal | ||
const changes = await this._watch(PATH_TO_WATCH, { signal }, async () => { | ||
const ac = new AbortController() | ||
const changes = await this._watch(PATH_TO_WATCH, { signal: ac.signal }, async () => { | ||
this._touch(LEVEL1_FILE) | ||
@@ -68,3 +67,3 @@ this._touch(LEVEL1_FILE) | ||
await this._pause(10) | ||
signal.abort() | ||
ac.abort() | ||
}) | ||
@@ -83,4 +82,4 @@ | ||
async 'Watch over directory (zero tolerance)' () { | ||
const signal = (new AbortController()).signal | ||
const changes = await this._watch(PATH_TO_WATCH, { signal, tolerance: 0 }, async () => { | ||
const ac = new AbortController() | ||
const changes = await this._watch(PATH_TO_WATCH, { signal: ac.signal, tolerance: 0 }, async () => { | ||
this._touch(LEVEL1_FILE) | ||
@@ -93,3 +92,3 @@ this._touch(LEVEL1_FILE) | ||
await this._pause(10) | ||
signal.abort() | ||
ac.abort() | ||
}) | ||
@@ -108,4 +107,4 @@ | ||
async 'Watch over directory (fallback)' () { | ||
const signal = (new AbortController()).signal | ||
const changes = await this._watch(PATH_TO_WATCH, { signal, fallback: true }, async () => { | ||
const ac = new AbortController() | ||
const changes = await this._watch(PATH_TO_WATCH, { signal: ac.signal, fallback: true }, async () => { | ||
await this._pause(10) // Allow for the watchers to apply recursively. | ||
@@ -129,3 +128,3 @@ | ||
await this._pause(10) | ||
signal.abort() | ||
ac.abort() | ||
}) | ||
@@ -145,8 +144,8 @@ | ||
async 'Watch over file' () { | ||
const signal = (new AbortController()).signal | ||
const changes = await this._watch(LEVEL1_FILE, { signal }, async () => { | ||
const ac = new AbortController() | ||
const changes = await this._watch(LEVEL1_FILE, { signal: ac.signal }, async () => { | ||
this._touch(LEVEL1_FILE) | ||
this._touch(LEVEL1_FILE) | ||
await this._pause(10) | ||
signal.abort() | ||
ac.abort() | ||
}) | ||
@@ -153,0 +152,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
12882
294
43