mm_scanner 1.0.2 → 1.1.0

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.
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ const { normalizeExt } = require('./path_util');
4
+
5
+ /* 默认忽略的常见目录(版本控制、依赖、IDE 配置),始终生效,用户配置在其后追加 */
6
+ const DEFAULT_IGNORE_DIRS = ['.git', '.svn', '.hg', 'node_modules', '.idea', '.vscode'];
7
+
8
+ /* 扫描器默认配置 */
9
+ const DEFAULT_CONFIG = {
10
+ /** 支持的扩展名 */
11
+ extensions: ['.json'],
12
+ /** 忽略的目录 */
13
+ ignore: DEFAULT_IGNORE_DIRS,
14
+ };
15
+
16
+ /**
17
+ * 合并默认配置与用户配置
18
+ * @description 扩展名为空时回退默认;忽略目录为默认列表与用户列表的并集(默认项始终生效)
19
+ * @param {{ extensions: string[], ignore: string[] }} default_config - 默认配置
20
+ * @param {{ extensions?: string[], ignore?: string[] }} config - 用户配置
21
+ * @returns {{ extensions: string[], ignore: string[] }} 规范化后的配置
22
+ */
23
+ function createConfig(default_config, config) {
24
+ const extensions =
25
+ Array.isArray(config.extensions) && config.extensions.length
26
+ ? config.extensions
27
+ : default_config.extensions;
28
+ const user_ignore = Array.isArray(config.ignore) ? config.ignore : [];
29
+
30
+ return {
31
+ extensions: extensions.map(normalizeExt),
32
+ ignore: [...new Set([...default_config.ignore, ...user_ignore])],
33
+ };
34
+ }
35
+
36
+ module.exports = { DEFAULT_CONFIG, createConfig };
package/lib/symbols.js ADDED
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ /* 内部同步方法名,仅供监听器调用 */
4
+ const SYNC = Symbol('mm_scanner.sync');
5
+ /* 内部回调方法名,仅供监听器调用 */
6
+ const NOTIFY = Symbol('mm_scanner.notify');
7
+
8
+ module.exports = { SYNC, NOTIFY };
@@ -0,0 +1,170 @@
1
+ 'use strict';
2
+
3
+ const { Watcher } = require('mm_watcher');
4
+ const cache_store = require('./cache_store');
5
+ const cache_updater = require('./cache_update');
6
+ const instance_registry = require('./instance_registry');
7
+
8
+ /* 文件事件防抖窗口,合并一次写入产生的多条事件 */
9
+ const DEBOUNCE = 100;
10
+
11
+ /**
12
+ * 目录监听管理器
13
+ * @description 每个缓存键持有一个 mm_watcher 监听器,订阅 add/set/del/reload 事件,
14
+ * 同一防抖批次内累积变更后增量维护缓存并同步全部监听实例
15
+ */
16
+ class WatchManager {
17
+ /** 缓存键 -> Watcher */
18
+ #watchers = new Map();
19
+ /** 缓存键 -> { changes, need_refresh, scheduled } */
20
+ #states = new Map();
21
+
22
+ /**
23
+ * 为缓存绑定目录监听器
24
+ * @param {string} key 缓存键
25
+ * @returns {boolean} 监听是否可用
26
+ */
27
+ bind(key) {
28
+ const cache = cache_store.readCache(key);
29
+ if (!cache) return false;
30
+
31
+ if (this.#watchers.has(key)) {
32
+ this._markWatched(cache);
33
+ return true;
34
+ }
35
+
36
+ const state = { changes: [], need_refresh: false, scheduled: false };
37
+ const watcher = new Watcher({
38
+ target: cache.dir,
39
+ ignore: cache.ignore,
40
+ debounce: DEBOUNCE,
41
+ onError: () => this.unbind(key),
42
+ });
43
+ this._subscribe(watcher, key, state);
44
+
45
+ if (!watcher.listen()) {
46
+ return false;
47
+ }
48
+
49
+ this._markWatched(cache);
50
+ this.#watchers.set(key, watcher);
51
+ this.#states.set(key, state);
52
+ return true;
53
+ }
54
+
55
+ /**
56
+ * 解除目录监听并恢复时间戳校验
57
+ * @param {string} key 缓存键
58
+ * @returns {void}
59
+ */
60
+ unbind(key) {
61
+ const watcher = this.#watchers.get(key);
62
+ if (watcher) {
63
+ watcher.dispose();
64
+ this.#watchers.delete(key);
65
+ }
66
+ this.#states.delete(key);
67
+
68
+ instance_registry.remove(key);
69
+
70
+ const cache = cache_store.readCache(key);
71
+ if (cache) {
72
+ cache.watched = false;
73
+ cache_store.captureMtimes(cache);
74
+ }
75
+ }
76
+
77
+ /**
78
+ * 解除全部目录监听并清空实例登记
79
+ * @returns {void}
80
+ */
81
+ closeAll() {
82
+ const keys = [...this.#watchers.keys()];
83
+ for (let i = 0; i < keys.length; i++) {
84
+ this.unbind(keys[i]);
85
+ }
86
+ instance_registry.clear();
87
+ }
88
+
89
+ /**
90
+ * 标记缓存进入监听状态并清空时间戳校验记录
91
+ * @param {object} cache 缓存项
92
+ * @returns {void}
93
+ * @private
94
+ */
95
+ _markWatched(cache) {
96
+ cache.watched = true;
97
+ cache.dir_mtimes = [];
98
+ cache.file_mtimes = [];
99
+ }
100
+
101
+ /**
102
+ * 订阅监听器的全部事件类型
103
+ * @param {object} watcher mm_watcher 监听器
104
+ * @param {string} key 缓存键
105
+ * @param {object} state 批次累积状态
106
+ * @returns {void}
107
+ * @private
108
+ */
109
+ _subscribe(watcher, key, state) {
110
+ watcher.on('add', (rel_path) => this._onEvent(key, 'add', rel_path, state));
111
+ watcher.on('set', (rel_path) => this._onEvent(key, 'set', rel_path, state));
112
+ watcher.on('del', (rel_path) => this._onEvent(key, 'del', rel_path, state));
113
+ watcher.on('reload', () => this._onEvent(key, 'reload', '', state));
114
+ }
115
+
116
+ /**
117
+ * 处理单个监听事件,累积后于微任务统一提交
118
+ * @param {string} key 缓存键
119
+ * @param {string} type 事件类型,add/set/del/reload
120
+ * @param {string} rel_path 相对路径
121
+ * @param {object} state 批次累积状态
122
+ * @returns {void}
123
+ * @private
124
+ */
125
+ _onEvent(key, type, rel_path, state) {
126
+ if (type === 'reload') {
127
+ state.need_refresh = true;
128
+ } else {
129
+ const cache = cache_store.readCache(key);
130
+ if (!cache_updater.applyChange(cache, { type, path: rel_path }, state.changes)) {
131
+ state.need_refresh = true;
132
+ }
133
+ }
134
+
135
+ if (!state.scheduled) {
136
+ state.scheduled = true;
137
+ queueMicrotask(() => this._process(key));
138
+ }
139
+ }
140
+
141
+ /**
142
+ * 提交批次内累积的变更:必要时全量刷新并同步全部监听实例
143
+ * @param {string} key 缓存键
144
+ * @returns {void}
145
+ * @private
146
+ */
147
+ _process(key) {
148
+ const state = this.#states.get(key);
149
+ if (!state) return;
150
+ state.scheduled = false;
151
+
152
+ const need_refresh = state.need_refresh;
153
+ const changes = state.changes;
154
+ state.need_refresh = false;
155
+ state.changes = [];
156
+
157
+ if (!changes.length && !need_refresh) return;
158
+
159
+ if (need_refresh) {
160
+ const cache = cache_store.readCache(key);
161
+ cache_updater.refreshCache(cache, changes);
162
+ }
163
+ instance_registry.sync(key, changes);
164
+ }
165
+ }
166
+
167
+ const watch_manager = new WatchManager();
168
+
169
+ /* 导出单例 */
170
+ module.exports = watch_manager;
package/lib/watcher.js ADDED
@@ -0,0 +1,119 @@
1
+ 'use strict';
2
+
3
+ const instance_registry = require('./instance_registry');
4
+ const watch_manager = require('./watch_manager');
5
+ const { SYNC, NOTIFY } = require('./symbols');
6
+
7
+ /**
8
+ * 目录监听器
9
+ * @description 代表 Scanner 实例的监听身份:登记到实例登记表、绑定目录监听、接收缓存同步并触发变更回调
10
+ */
11
+ class Watcher {
12
+ /** 本次监听对应的缓存键 */
13
+ #cache_key;
14
+ /** 回填扫描结果的回调,由 Scanner 注入 */
15
+ #fill;
16
+ /** 外部变更回调 */
17
+ #callback = null;
18
+
19
+ /**
20
+ * 创建监听器实例
21
+ * @param {string} cache_key 缓存键
22
+ * @param {(items: Array<object>, dirs: Array<string>) => void} fill 回填扫描结果的回调
23
+ */
24
+ constructor(cache_key, fill) {
25
+ this.#cache_key = cache_key;
26
+ this.#fill = fill;
27
+ }
28
+
29
+ /**
30
+ * 开启监听,重复调用只更新回调
31
+ * @description 绑定失败时自动注销登记并清空回调,静默降级为未监听
32
+ * @param {(changes: Array<{ type: string, key: string, item: object }>) => void} [callback] 变更回调,type 为 add/change/unlink
33
+ * @returns {boolean} 监听是否可用
34
+ */
35
+ start(callback) {
36
+ if (typeof callback === 'function') this.#callback = callback;
37
+ if (this.active) return true;
38
+
39
+ instance_registry.add(this.#cache_key, this);
40
+ if (!watch_manager.bind(this.#cache_key)) {
41
+ this.#stop();
42
+ this.#callback = null;
43
+ return false;
44
+ }
45
+ return true;
46
+ }
47
+
48
+ /**
49
+ * 关闭监听并清空变更回调
50
+ * @returns {void}
51
+ */
52
+ dispose() {
53
+ this.#callback = null;
54
+ this.#stop();
55
+ }
56
+
57
+ /**
58
+ * 判断当前监听器是否处于监听状态
59
+ * @returns {boolean} 已登记返回 true
60
+ */
61
+ get active() {
62
+ return instance_registry.has(this.#cache_key, this);
63
+ }
64
+
65
+ /**
66
+ * 注销本监听器登记,最后一个注销时解绑目录监听
67
+ * @returns {void}
68
+ */
69
+ #stop() {
70
+ if (instance_registry.del(this.#cache_key, this)) {
71
+ watch_manager.unbind(this.#cache_key);
72
+ }
73
+ }
74
+
75
+ /**
76
+ * 接收监听器同步的最新条目并回填
77
+ * @param {Array<object>} items 缓存中的条目
78
+ * @param {Array<string>} dirs 缓存中的目录列表
79
+ * @returns {void}
80
+ */
81
+ [SYNC](items, dirs) {
82
+ this.#fill(items, dirs);
83
+ }
84
+
85
+ /**
86
+ * 把变更列表交给外部回调
87
+ * @param {Array<{ type: string, key: string, item: object }>} changes 变更列表
88
+ * @returns {void}
89
+ */
90
+ [NOTIFY](changes) {
91
+ if (!this.#callback || !changes.length) return;
92
+ try {
93
+ this.#callback(changes.map((one) => ({ ...one, item: { ...one.item } })));
94
+ } catch (error) {
95
+ console.error('[scanner] watch 回调执行失败:', error);
96
+ }
97
+ }
98
+
99
+ /**
100
+ * 把缓存的最新条目与变更同步到指定缓存键下的全部监听器
101
+ * @param {string} cache_key 缓存键
102
+ * @param {Array<object>} changes 变更列表
103
+ * @returns {void}
104
+ */
105
+ static sync(cache_key, changes) {
106
+ instance_registry.sync(cache_key, changes);
107
+ }
108
+
109
+ /**
110
+ * 关闭全部目录监听并清空监听器登记
111
+ * @returns {void}
112
+ */
113
+ static closeAll() {
114
+ watch_manager.closeAll();
115
+ }
116
+ }
117
+
118
+ /* 导出类 */
119
+ module.exports = Watcher;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mm_scanner",
3
- "version": "1.0.2",
4
- "description": "目录扫描器,按扩展名递归扫描目录并缓存文件元信息,支持按 key、扩展名、目录检索文件路径。",
3
+ "version": "1.1.0",
4
+ "description": "目录扫描器,按扩展名递归扫描目录并缓存文件元信息,支持按 key、扩展名、目录检索文件路径,默认忽略 .git/node_modules 等常见目录,支持目录监听热更新。",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://gitee.com/qiuwenwu91/mm_scanner.git"
@@ -35,7 +35,9 @@
35
35
  "prepublishOnly": "npm test"
36
36
  },
37
37
  "dependencies": {
38
- "mm_corejs": "^1.1.5"
38
+ "chokidar": "^3.6.0",
39
+ "mm_corejs": "^1.1.5",
40
+ "mm_watcher": "^2.2.0"
39
41
  },
40
42
  "devDependencies": {
41
43
  "jest": "^30.5.2"