Skip to content

Commit

Permalink
Moved WatchManager.js from WatchKeeper (#283)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
nathanholzworth and adamkingit authored Oct 12, 2022
1 parent bbf02fc commit 78c6a36
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019 IBM Corp. All Rights Reserved.
* Copyright 2019, 2022 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,10 +24,13 @@ const Watchman = require('./lib/Watchman');

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

const WatchManager = require('./lib/WatchManager');

module.exports = {
KubeClass,
KubeApiConfig,
KubeResourceMeta,
Watchman,
EventHandler
EventHandler,
WatchManager
};
94 changes: 94 additions & 0 deletions lib/WatchManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright 2019, 2022 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const Watchman = require('./Watchman');
const log = require('./bunyan-api').createLogger('WatchManager');
const objectPath = require('object-path');
const hash = require('object-hash');

const _watchObjects = {};

module.exports = function WatchManager() {
// private
const _saveWatch = function (wm, querySelector, startWatch = true) {
const selfLink = wm.selfLink;
_removeWatch(selfLink);
_watchObjects[selfLink] = { selfLink: wm.selfLink, watchman: wm, querySelectorHash: hash(querySelector) };
if (startWatch) {
wm.watch();
}
log.info(`Watch added: ${selfLink} ${JSON.stringify(querySelector)}`);
return _watchObjects[selfLink];
};

const _ensureWatch = function (options, objectHandler, globalWatch = false, startWatch = true) {
const querySelector = objectPath.get(options, 'requestOptions.qs', {});
const selfLink = options?.requestOptions?.uri;
const w = _getWatch(selfLink);
if (w && (!globalWatch || (globalWatch && w.querySelectorHash == hash(querySelector)))) {
return w;
}
const wm = new Watchman(options, objectHandler);
return _saveWatch(wm, querySelector, startWatch);
};

const _startWatch = function (selfLink) {
return _reWatch(selfLink);
};

const _removeWatch = function (selfLink) {
const w = objectPath.get(_getWatch(selfLink), 'watchman');
if (w) {
w.end();
delete _watchObjects[selfLink];
log.info(`Watch removed: ${selfLink}`);
}
return _watchObjects[selfLink];
};

const _getWatch = function (selfLink) {
return _watchObjects[selfLink];
};

const _reWatch = function (selfLink) {
const w = objectPath.get(_getWatch(selfLink), 'watchman');
if (w) {
w.watch();
}
return w;
};

// public
return {
saveWatch: _saveWatch,
ensureWatch: _ensureWatch,
startWatch: _startWatch,
removeWatch: _removeWatch,
removeAllWatches: function () {
const watches = Object.keys(_watchObjects);
watches.forEach(w => _removeWatch(w));
},
getWatch: _getWatch,
getAllWatches: function () {
return _watchObjects;
},
reWatch: _reWatch,
reWatchAll: function () {
const watches = Object.keys(_watchObjects);
watches.forEach(w => _reWatch(w));
}
};
};
15 changes: 9 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 78c6a36

Please sign in to comment.