Skip to content

Commit 78c6a36

Browse files
Moved WatchManager.js from WatchKeeper (#283)
* Moved WatchManager.js from WatchKeeper * add WatchManager to module exports * npm audit fix * Revert "npm audit fix" This reverts commit c445e25. * npm audit fix take 2 * Apply suggestions from code review Co-authored-by: Adam King <rak@us.ibm.com> * update index.js copywrite note * replace lets/vars with const * changed missed let Co-authored-by: Adam King <rak@us.ibm.com>
1 parent bbf02fc commit 78c6a36

File tree

3 files changed

+108
-8
lines changed

3 files changed

+108
-8
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 IBM Corp. All Rights Reserved.
2+
* Copyright 2019, 2022 IBM Corp. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,10 +24,13 @@ const Watchman = require('./lib/Watchman');
2424

2525
const EventHandler = require('./lib/EventHandler');
2626

27+
const WatchManager = require('./lib/WatchManager');
28+
2729
module.exports = {
2830
KubeClass,
2931
KubeApiConfig,
3032
KubeResourceMeta,
3133
Watchman,
32-
EventHandler
34+
EventHandler,
35+
WatchManager
3336
};

lib/WatchManager.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Copyright 2019, 2022 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const Watchman = require('./Watchman');
18+
const log = require('./bunyan-api').createLogger('WatchManager');
19+
const objectPath = require('object-path');
20+
const hash = require('object-hash');
21+
22+
const _watchObjects = {};
23+
24+
module.exports = function WatchManager() {
25+
// private
26+
const _saveWatch = function (wm, querySelector, startWatch = true) {
27+
const selfLink = wm.selfLink;
28+
_removeWatch(selfLink);
29+
_watchObjects[selfLink] = { selfLink: wm.selfLink, watchman: wm, querySelectorHash: hash(querySelector) };
30+
if (startWatch) {
31+
wm.watch();
32+
}
33+
log.info(`Watch added: ${selfLink} ${JSON.stringify(querySelector)}`);
34+
return _watchObjects[selfLink];
35+
};
36+
37+
const _ensureWatch = function (options, objectHandler, globalWatch = false, startWatch = true) {
38+
const querySelector = objectPath.get(options, 'requestOptions.qs', {});
39+
const selfLink = options?.requestOptions?.uri;
40+
const w = _getWatch(selfLink);
41+
if (w && (!globalWatch || (globalWatch && w.querySelectorHash == hash(querySelector)))) {
42+
return w;
43+
}
44+
const wm = new Watchman(options, objectHandler);
45+
return _saveWatch(wm, querySelector, startWatch);
46+
};
47+
48+
const _startWatch = function (selfLink) {
49+
return _reWatch(selfLink);
50+
};
51+
52+
const _removeWatch = function (selfLink) {
53+
const w = objectPath.get(_getWatch(selfLink), 'watchman');
54+
if (w) {
55+
w.end();
56+
delete _watchObjects[selfLink];
57+
log.info(`Watch removed: ${selfLink}`);
58+
}
59+
return _watchObjects[selfLink];
60+
};
61+
62+
const _getWatch = function (selfLink) {
63+
return _watchObjects[selfLink];
64+
};
65+
66+
const _reWatch = function (selfLink) {
67+
const w = objectPath.get(_getWatch(selfLink), 'watchman');
68+
if (w) {
69+
w.watch();
70+
}
71+
return w;
72+
};
73+
74+
// public
75+
return {
76+
saveWatch: _saveWatch,
77+
ensureWatch: _ensureWatch,
78+
startWatch: _startWatch,
79+
removeWatch: _removeWatch,
80+
removeAllWatches: function () {
81+
const watches = Object.keys(_watchObjects);
82+
watches.forEach(w => _removeWatch(w));
83+
},
84+
getWatch: _getWatch,
85+
getAllWatches: function () {
86+
return _watchObjects;
87+
},
88+
reWatch: _reWatch,
89+
reWatchAll: function () {
90+
const watches = Object.keys(_watchObjects);
91+
watches.forEach(w => _reWatch(w));
92+
}
93+
};
94+
};

package-lock.json

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)