loopback-datasource-juggler/test/helpers/hook-monitor.js

36 lines
1015 B
JavaScript
Raw Normal View History

2016-04-12 13:06:42 +00:00
// Copyright IBM Corp. 2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-08-22 19:55:22 +00:00
'use strict';
2016-04-12 13:06:42 +00:00
module.exports = HookMonitor;
function HookMonitor(opts) {
if (!(this instanceof HookMonitor)) {
return new HookMonitor();
}
this.options = opts || {};
this.names = [];
2018-12-07 16:13:48 +00:00
}
2016-04-12 13:06:42 +00:00
HookMonitor.prototype.install = function(ObservedModel, hookNames) {
2018-12-07 16:13:48 +00:00
const monitor = this;
2016-04-12 13:06:42 +00:00
this.names = [];
ObservedModel._notify = ObservedModel.notifyObserversOf;
ObservedModel.notifyObserversOf = function(operation, context, callback) {
if (!Array.isArray(hookNames) || hookNames.indexOf(operation) !== -1) {
2018-12-07 16:13:48 +00:00
const item = monitor.options.includeModelName ?
2016-04-12 13:06:42 +00:00
ObservedModel.modelName + ':' + operation :
operation;
monitor.names.push(item);
}
this._notify.apply(this, arguments);
};
};
HookMonitor.prototype.resetNames = function() {
this.names = [];
};