|
| 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 | +}; |
0 commit comments