mm_watcher 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/example/demo.js +2 -2
- package/index.js +12 -1
- package/lib/watcher.js +7 -8
- package/package.json +4 -1
package/example/demo.js
CHANGED
|
@@ -25,7 +25,7 @@ async function demoDirWatch() {
|
|
|
25
25
|
fs.mkdirSync(path.join(dir, 'node_modules'), { recursive: true });
|
|
26
26
|
|
|
27
27
|
const changes = [];
|
|
28
|
-
const watcher = new Watcher(dir,
|
|
28
|
+
const watcher = new Watcher({ target: dir, ignore: ['node_modules', '*.tmp'] });
|
|
29
29
|
const ok = watcher.start((list) => changes.push(...list));
|
|
30
30
|
console.log('目录监听:', ok, '| 目标:', dir);
|
|
31
31
|
|
|
@@ -59,7 +59,7 @@ async function demoFileWatch() {
|
|
|
59
59
|
fs.writeFileSync(file, '{}');
|
|
60
60
|
|
|
61
61
|
const changes = [];
|
|
62
|
-
const watcher = new Watcher(file);
|
|
62
|
+
const watcher = new Watcher({ target: file });
|
|
63
63
|
const ok = watcher.start((list) => changes.push(...list));
|
|
64
64
|
console.log('单文件监听:', ok, '| 目标:', file);
|
|
65
65
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
const Watcher = require('./lib/watcher');
|
|
2
|
+
const Admin = require('mm_corejs').Admin;
|
|
3
|
+
|
|
4
|
+
const admin = new Admin();
|
|
5
|
+
admin.setClass(Watcher);
|
|
2
6
|
|
|
3
7
|
module.exports = {
|
|
4
|
-
Watcher
|
|
8
|
+
Watcher,
|
|
9
|
+
admin,
|
|
10
|
+
watcherAdmin(scope, config) {
|
|
11
|
+
if (!scope) {
|
|
12
|
+
scope = 'os';
|
|
13
|
+
}
|
|
14
|
+
return admin.get(scope, config);
|
|
15
|
+
}
|
|
5
16
|
};
|
package/lib/watcher.js
CHANGED
|
@@ -29,17 +29,16 @@ class Watcher {
|
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* 创建监听器
|
|
32
|
-
* @param {string} target
|
|
33
|
-
* @param {{ ignore?: Array<string>, debounce?: number, onError?: () => void }} [options] 监听选项
|
|
32
|
+
* @param {{ target: string, ignore?: Array<string>, debounce?: number, onError?: () => void }} [config] 监听配置,target 为目录或文件绝对路径
|
|
34
33
|
*/
|
|
35
|
-
constructor(
|
|
36
|
-
this.#target = String(target).replace(/\\/g, '/');
|
|
37
|
-
this.#ignore = Array.isArray(
|
|
34
|
+
constructor(config = {}) {
|
|
35
|
+
this.#target = String(config.target).replace(/\\/g, '/');
|
|
36
|
+
this.#ignore = Array.isArray(config.ignore) ? [...config.ignore] : [];
|
|
38
37
|
this.#debounce =
|
|
39
|
-
Number.isFinite(
|
|
40
|
-
?
|
|
38
|
+
Number.isFinite(config.debounce) && config.debounce >= 0
|
|
39
|
+
? config.debounce
|
|
41
40
|
: DEFAULT_DEBOUNCE;
|
|
42
|
-
this.#onError = typeof
|
|
41
|
+
this.#onError = typeof config.onError === 'function' ? config.onError : null;
|
|
43
42
|
this.#key = [this.#target, this.#ignore.join(',')].join('\u0001');
|
|
44
43
|
}
|
|
45
44
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_watcher",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "文件监听器,支持监听目录(递归)与单个文件,事件防抖合并、忽略目录与文件(精确名/扩展名/通配符),同目标多监听者共享底层句柄。",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -36,5 +36,8 @@
|
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"jest": "^30.5.2"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"mm_corejs": "^1.1.5"
|
|
39
42
|
}
|
|
40
43
|
}
|