Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved WatchManager.js from WatchKeeper #283

Merged
merged 9 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.