@eggjs/watcher 4.0.4 → 4.0.5-beta.2
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/README.md +1 -5
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +7 -0
- package/dist/app.d.ts +2 -0
- package/dist/app.js +7 -0
- package/dist/config/config.default.d.ts +13 -0
- package/dist/config/config.default.js +14 -0
- package/dist/config/config.local.d.ts +8 -0
- package/dist/config/config.local.js +5 -0
- package/dist/config/config.unittest.d.ts +8 -0
- package/dist/config/config.unittest.js +5 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +7 -0
- package/dist/lib/boot.d.ts +10 -0
- package/dist/lib/boot.js +19 -0
- package/dist/lib/event-sources/base.d.ts +9 -0
- package/dist/lib/event-sources/base.js +7 -0
- package/dist/lib/event-sources/default.d.ts +10 -0
- package/dist/lib/event-sources/default.js +19 -0
- package/dist/lib/event-sources/development.d.ts +11 -0
- package/dist/lib/event-sources/development.js +66 -0
- package/dist/lib/event-sources/index.d.ts +4 -0
- package/dist/lib/event-sources/index.js +5 -0
- package/{src/lib/types.ts → dist/lib/types.d.ts} +10 -12
- package/dist/lib/types.js +1 -0
- package/dist/lib/utils.d.ts +5 -0
- package/dist/lib/utils.js +15 -0
- package/dist/lib/watcher.d.ts +14 -0
- package/dist/lib/watcher.js +59 -0
- package/package.json +49 -59
- package/dist/commonjs/agent.d.ts +0 -2
- package/dist/commonjs/agent.js +0 -5
- package/dist/commonjs/app.d.ts +0 -2
- package/dist/commonjs/app.js +0 -5
- package/dist/commonjs/config/config.default.d.ts +0 -10
- package/dist/commonjs/config/config.default.js +0 -22
- package/dist/commonjs/config/config.local.d.ts +0 -5
- package/dist/commonjs/config/config.local.js +0 -8
- package/dist/commonjs/config/config.unittest.d.ts +0 -5
- package/dist/commonjs/config/config.unittest.js +0 -8
- package/dist/commonjs/index.d.ts +0 -3
- package/dist/commonjs/index.js +0 -20
- package/dist/commonjs/lib/boot.d.ts +0 -6
- package/dist/commonjs/lib/boot.js +0 -23
- package/dist/commonjs/lib/event-sources/base.d.ts +0 -5
- package/dist/commonjs/lib/event-sources/base.js +0 -8
- package/dist/commonjs/lib/event-sources/default.d.ts +0 -6
- package/dist/commonjs/lib/event-sources/default.js +0 -19
- package/dist/commonjs/lib/event-sources/development.d.ts +0 -7
- package/dist/commonjs/lib/event-sources/development.js +0 -103
- package/dist/commonjs/lib/event-sources/index.d.ts +0 -4
- package/dist/commonjs/lib/event-sources/index.js +0 -13
- package/dist/commonjs/lib/types.d.ts +0 -31
- package/dist/commonjs/lib/types.js +0 -3
- package/dist/commonjs/lib/utils.d.ts +0 -2
- package/dist/commonjs/lib/utils.js +0 -25
- package/dist/commonjs/lib/watcher.d.ts +0 -10
- package/dist/commonjs/lib/watcher.js +0 -113
- package/dist/commonjs/package.json +0 -3
- package/dist/esm/agent.d.ts +0 -2
- package/dist/esm/agent.js +0 -3
- package/dist/esm/app.d.ts +0 -2
- package/dist/esm/app.js +0 -3
- package/dist/esm/config/config.default.d.ts +0 -10
- package/dist/esm/config/config.default.js +0 -17
- package/dist/esm/config/config.local.d.ts +0 -5
- package/dist/esm/config/config.local.js +0 -6
- package/dist/esm/config/config.unittest.d.ts +0 -5
- package/dist/esm/config/config.unittest.js +0 -6
- package/dist/esm/index.d.ts +0 -3
- package/dist/esm/index.js +0 -4
- package/dist/esm/lib/boot.d.ts +0 -6
- package/dist/esm/lib/boot.js +0 -19
- package/dist/esm/lib/event-sources/base.d.ts +0 -5
- package/dist/esm/lib/event-sources/base.js +0 -4
- package/dist/esm/lib/event-sources/default.d.ts +0 -6
- package/dist/esm/lib/event-sources/default.js +0 -16
- package/dist/esm/lib/event-sources/development.d.ts +0 -7
- package/dist/esm/lib/event-sources/development.js +0 -97
- package/dist/esm/lib/event-sources/index.d.ts +0 -4
- package/dist/esm/lib/event-sources/index.js +0 -5
- package/dist/esm/lib/types.d.ts +0 -31
- package/dist/esm/lib/types.js +0 -2
- package/dist/esm/lib/utils.d.ts +0 -2
- package/dist/esm/lib/utils.js +0 -18
- package/dist/esm/lib/watcher.d.ts +0 -10
- package/dist/esm/lib/watcher.js +0 -106
- package/dist/esm/package.json +0 -3
- package/dist/package.json +0 -4
- package/src/agent.ts +0 -3
- package/src/app.ts +0 -3
- package/src/config/config.default.ts +0 -18
- package/src/config/config.local.ts +0 -7
- package/src/config/config.unittest.ts +0 -7
- package/src/index.ts +0 -3
- package/src/lib/boot.ts +0 -22
- package/src/lib/event-sources/base.ts +0 -6
- package/src/lib/event-sources/default.ts +0 -18
- package/src/lib/event-sources/development.ts +0 -101
- package/src/lib/event-sources/index.ts +0 -9
- package/src/lib/utils.ts +0 -20
- package/src/lib/watcher.ts +0 -120
package/src/lib/watcher.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import { Base } from 'sdk-base';
|
|
3
|
-
import camelcase from 'camelcase';
|
|
4
|
-
import { importModule } from '@eggjs/utils';
|
|
5
|
-
import type { EggAppConfig } from '@eggjs/core';
|
|
6
|
-
import { BaseEventSource } from './event-sources/base.js';
|
|
7
|
-
import { isEqualOrParentPath } from './utils.js';
|
|
8
|
-
import type { ChangeInfo } from './types.js';
|
|
9
|
-
|
|
10
|
-
const debug = debuglog('@eggjs/watcher/lib/watcher');
|
|
11
|
-
|
|
12
|
-
export type WatchListener = (info: ChangeInfo) => void;
|
|
13
|
-
|
|
14
|
-
export class Watcher extends Base {
|
|
15
|
-
#config: EggAppConfig;
|
|
16
|
-
#eventSource: BaseEventSource;
|
|
17
|
-
|
|
18
|
-
constructor(config: EggAppConfig) {
|
|
19
|
-
super({
|
|
20
|
-
initMethod: '_init',
|
|
21
|
-
});
|
|
22
|
-
this.#config = config;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
protected async _init() {
|
|
26
|
-
const watcherType = this.#config.watcher.type;
|
|
27
|
-
let EventSource = this.#config.watcher.eventSources[watcherType] as unknown as typeof BaseEventSource;
|
|
28
|
-
if (typeof EventSource === 'string') {
|
|
29
|
-
EventSource = await importModule(EventSource, {
|
|
30
|
-
importDefaultOnly: true,
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// chokidar => watcherChokidar
|
|
35
|
-
// custom => watcherCustom
|
|
36
|
-
//
|
|
37
|
-
// e.g:
|
|
38
|
-
// config => { watcher: { type: 'custom' }, watcherCustom: { ... } }
|
|
39
|
-
const key = camelcase([ 'watcher', watcherType ]);
|
|
40
|
-
const eventSourceOptions = this.#config[key] ?? {};
|
|
41
|
-
this.#eventSource = Reflect.construct(EventSource, [ eventSourceOptions ]);
|
|
42
|
-
this.#eventSource.on('change', this.#onChange.bind(this))
|
|
43
|
-
.on('fuzzy-change', this.#onFuzzyChange.bind(this))
|
|
44
|
-
.on('info', (...args) => this.emit('info', ...args))
|
|
45
|
-
.on('warn', (...args) => this.emit('warn', ...args))
|
|
46
|
-
.on('error', (...args) => this.emit('error', ...args));
|
|
47
|
-
await this.#eventSource.ready();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
watch(path: string | string[], listener: WatchListener) {
|
|
51
|
-
this.emit('info', '[@eggjs/watcher] Start watching: %j', path);
|
|
52
|
-
if (!path) return;
|
|
53
|
-
|
|
54
|
-
// support array
|
|
55
|
-
if (Array.isArray(path)) {
|
|
56
|
-
path.forEach(p => this.watch(p, listener));
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// one file only watch once
|
|
61
|
-
if (!this.listenerCount(path)) {
|
|
62
|
-
this.#eventSource.watch(path);
|
|
63
|
-
}
|
|
64
|
-
this.on(path, listener);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/*
|
|
68
|
-
// TODO wait unsubscribe implementation of cluster-client
|
|
69
|
-
unwatch(path, callback) {
|
|
70
|
-
if (!path) return;
|
|
71
|
-
|
|
72
|
-
// support array
|
|
73
|
-
if (Array.isArray(path)) {
|
|
74
|
-
path.forEach(p => this.unwatch(p, callback));
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (callback) {
|
|
79
|
-
this.removeListener(path, callback);
|
|
80
|
-
// stop watching when no listener bound to the path
|
|
81
|
-
if (this.listenerCount(path) === 0) {
|
|
82
|
-
this._eventSource.unwatch(path);
|
|
83
|
-
}
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
this.removeAllListeners(path);
|
|
88
|
-
this._eventSource.unwatch(path);
|
|
89
|
-
}
|
|
90
|
-
*/
|
|
91
|
-
|
|
92
|
-
#onChange(info: ChangeInfo) {
|
|
93
|
-
debug('onChange %o', info);
|
|
94
|
-
this.emit('info', '[@eggjs/watcher] Received a change event from eventSource: %j', info);
|
|
95
|
-
const path = info.path;
|
|
96
|
-
|
|
97
|
-
for (const p of this.eventNames()) {
|
|
98
|
-
if (typeof p !== 'string') continue;
|
|
99
|
-
// if it is a sub path, emit a `change` event
|
|
100
|
-
if (isEqualOrParentPath(p, path)) {
|
|
101
|
-
this.emit(p, info);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
#onFuzzyChange(info: ChangeInfo) {
|
|
107
|
-
debug('onFuzzyChange %o', info);
|
|
108
|
-
this.emit('info', '[@eggjs/watcher] Received a fuzzy-change event from eventSource: %j', info);
|
|
109
|
-
const path = info.path;
|
|
110
|
-
|
|
111
|
-
for (const p of this.eventNames()) {
|
|
112
|
-
if (typeof p !== 'string') continue;
|
|
113
|
-
// if it is a parent path, emit a `change` event
|
|
114
|
-
// just the opposite to `_onChange`
|
|
115
|
-
if (isEqualOrParentPath(path, p)) {
|
|
116
|
-
this.emit(p, info);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|