2019-05-08 15:45:37 +00:00
|
|
|
// Copyright IBM Corp. 2015,2018. All Rights Reserved.
|
2016-04-01 22:25:16 +00:00
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
2019-05-08 15:45:37 +00:00
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2016-04-01 22:25:16 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const async = require('async');
|
|
|
|
const utils = require('./utils');
|
2019-02-23 10:18:18 +00:00
|
|
|
const debug = require('debug')('loopback:observer');
|
2015-05-14 18:51:52 +00:00
|
|
|
|
|
|
|
module.exports = ObserverMixin;
|
|
|
|
|
2015-05-18 16:00:49 +00:00
|
|
|
/**
|
2017-06-01 15:00:44 +00:00
|
|
|
* ObserverMixin class. Use to add observe/notifyObserversOf APIs to other
|
2015-05-18 16:00:49 +00:00
|
|
|
* classes.
|
|
|
|
*
|
|
|
|
* @class ObserverMixin
|
|
|
|
*/
|
2015-05-14 18:51:52 +00:00
|
|
|
function ObserverMixin() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register an asynchronous observer for the given operation (event).
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* Registers a `before save` observer for a given model.
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* MyModel.observe('before save', function filterProperties(ctx, next) {
|
|
|
|
if (ctx.options && ctx.options.skipPropertyFilter) return next();
|
|
|
|
if (ctx.instance) {
|
|
|
|
FILTERED_PROPERTIES.forEach(function(p) {
|
|
|
|
ctx.instance.unsetAttribute(p);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
FILTERED_PROPERTIES.forEach(function(p) {
|
|
|
|
delete ctx.data[p];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
* ```
|
|
|
|
*
|
2015-05-14 18:51:52 +00:00
|
|
|
* @param {String} operation The operation name.
|
|
|
|
* @callback {function} listener The listener function. It will be invoked with
|
|
|
|
* `this` set to the model constructor, e.g. `User`.
|
|
|
|
* @end
|
|
|
|
*/
|
|
|
|
ObserverMixin.observe = function(operation, listener) {
|
|
|
|
this._observers = this._observers || {};
|
|
|
|
if (!this._observers[operation]) {
|
|
|
|
this._observers[operation] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this._observers[operation].push(listener);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister an asynchronous observer for the given operation (event).
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* MyModel.removeObserver('before save', function removedObserver(ctx, next) {
|
|
|
|
// some logic user want to apply to the removed observer...
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
* ```
|
|
|
|
*
|
2015-05-14 18:51:52 +00:00
|
|
|
* @param {String} operation The operation name.
|
|
|
|
* @callback {function} listener The listener function.
|
|
|
|
* @end
|
|
|
|
*/
|
|
|
|
ObserverMixin.removeObserver = function(operation, listener) {
|
|
|
|
if (!(this._observers && this._observers[operation])) return;
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const index = this._observers[operation].indexOf(listener);
|
2015-05-14 18:51:52 +00:00
|
|
|
if (index !== -1) {
|
|
|
|
return this._observers[operation].splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister all asynchronous observers for the given operation (event).
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* Remove all observers connected to the `before save` operation.
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* MyModel.clearObservers('before save');
|
|
|
|
* ```
|
|
|
|
*
|
2015-05-14 18:51:52 +00:00
|
|
|
* @param {String} operation The operation name.
|
|
|
|
* @end
|
|
|
|
*/
|
|
|
|
ObserverMixin.clearObservers = function(operation) {
|
|
|
|
if (!(this._observers && this._observers[operation])) return;
|
|
|
|
|
|
|
|
this._observers[operation].length = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-05-26 17:15:39 +00:00
|
|
|
* Invoke all async observers for the given operation(s).
|
2017-06-01 15:00:44 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* Notify all async observers for the `before save` operation.
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var context = {
|
|
|
|
Model: Model,
|
|
|
|
instance: obj,
|
|
|
|
isNewInstance: true,
|
|
|
|
hookState: hookState,
|
|
|
|
options: options,
|
|
|
|
};
|
|
|
|
* Model.notifyObserversOf('before save', context, function(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
// user can specify the logic after the observers have been notified
|
|
|
|
});
|
|
|
|
* ```
|
|
|
|
*
|
2015-05-26 17:15:39 +00:00
|
|
|
* @param {String|String[]} operation The operation name(s).
|
2015-05-14 18:51:52 +00:00
|
|
|
* @param {Object} context Operation-specific context.
|
2017-06-01 15:00:44 +00:00
|
|
|
* @callback {function(Error=)} callback The callback to call when all observers
|
|
|
|
* have finished.
|
2015-05-14 18:51:52 +00:00
|
|
|
*/
|
|
|
|
ObserverMixin.notifyObserversOf = function(operation, context, callback) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
2015-05-14 18:51:52 +00:00
|
|
|
if (!callback) callback = utils.createPromiseCallback();
|
|
|
|
|
2015-05-26 17:15:39 +00:00
|
|
|
function createNotifier(op) {
|
|
|
|
return function(ctx, done) {
|
|
|
|
if (typeof ctx === 'function' && done === undefined) {
|
|
|
|
done = ctx;
|
|
|
|
ctx = context;
|
|
|
|
}
|
|
|
|
self.notifyObserversOf(op, context, done);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(operation)) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const tasks = [];
|
|
|
|
for (let i = 0, n = operation.length; i < n; i++) {
|
2015-05-26 17:15:39 +00:00
|
|
|
tasks.push(createNotifier(operation[i]));
|
|
|
|
}
|
|
|
|
return async.waterfall(tasks, callback);
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const observers = this._observers && this._observers[operation];
|
2015-05-26 17:15:39 +00:00
|
|
|
|
2015-05-14 18:51:52 +00:00
|
|
|
this._notifyBaseObservers(operation, context, function doNotify(err) {
|
|
|
|
if (err) return callback(err, context);
|
|
|
|
if (!observers || !observers.length) return callback(null, context);
|
|
|
|
|
|
|
|
async.eachSeries(
|
|
|
|
observers,
|
|
|
|
function notifySingleObserver(fn, next) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const retval = fn(context, next);
|
2015-05-14 18:51:52 +00:00
|
|
|
if (retval && typeof retval.then === 'function') {
|
|
|
|
retval.then(
|
2015-11-24 09:15:53 +00:00
|
|
|
function() { next(); return null; },
|
2019-12-03 09:09:16 +00:00
|
|
|
next, // error handler
|
2015-05-14 18:51:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2019-12-03 09:09:16 +00:00
|
|
|
function(err) { callback(err, context); },
|
2015-05-14 18:51:52 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
return callback.promise;
|
2015-05-20 22:02:44 +00:00
|
|
|
};
|
2015-05-14 18:51:52 +00:00
|
|
|
|
|
|
|
ObserverMixin._notifyBaseObservers = function(operation, context, callback) {
|
|
|
|
if (this.base && this.base.notifyObserversOf)
|
|
|
|
this.base.notifyObserversOf(operation, context, callback);
|
|
|
|
else
|
|
|
|
callback();
|
2015-05-20 22:02:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-06-01 15:00:44 +00:00
|
|
|
* Run the given function with before/after observers.
|
|
|
|
*
|
|
|
|
* It's done in three serial asynchronous steps:
|
2015-05-20 22:02:44 +00:00
|
|
|
*
|
|
|
|
* - Notify the registered observers under 'before ' + operation
|
|
|
|
* - Execute the function
|
|
|
|
* - Notify the registered observers under 'after ' + operation
|
|
|
|
*
|
2017-06-01 15:00:44 +00:00
|
|
|
* If an error happens, it fails first and calls the callback with err.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```javascript
|
|
|
|
* var context = {
|
|
|
|
Model: Model,
|
|
|
|
instance: obj,
|
|
|
|
isNewInstance: true,
|
|
|
|
hookState: hookState,
|
|
|
|
options: options,
|
|
|
|
};
|
|
|
|
* function work(done) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
done(null, 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
* Model.notifyObserversAround('execute', context, work, function(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
// user can specify the logic after the observers have been notified
|
|
|
|
});
|
|
|
|
* ```
|
2015-05-20 22:02:44 +00:00
|
|
|
*
|
|
|
|
* @param {String} operation The operation name
|
|
|
|
* @param {Context} context The context object
|
|
|
|
* @param {Function} fn The task to be invoked as fn(done) or fn(context, done)
|
2017-06-01 15:00:44 +00:00
|
|
|
* @callback {Function} callback The callback function
|
2015-05-20 22:02:44 +00:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
ObserverMixin.notifyObserversAround = function(operation, context, fn, callback) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const self = this;
|
2015-05-26 17:15:39 +00:00
|
|
|
context = context || {};
|
|
|
|
// Add callback to the context object so that an observer can skip other
|
|
|
|
// ones by calling the callback function directly and not calling next
|
|
|
|
if (context.end === undefined) {
|
|
|
|
context.end = callback;
|
|
|
|
}
|
|
|
|
// First notify before observers
|
2015-05-20 22:02:44 +00:00
|
|
|
return self.notifyObserversOf('before ' + operation, context,
|
|
|
|
function(err, context) {
|
2015-05-26 17:15:39 +00:00
|
|
|
if (err) return callback(err);
|
2015-05-20 22:02:44 +00:00
|
|
|
|
|
|
|
function cbForWork(err) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const args = [].slice.call(arguments, 0);
|
2019-02-23 10:18:18 +00:00
|
|
|
if (err) {
|
|
|
|
// call observer in case of error to hook response
|
|
|
|
context.error = err;
|
|
|
|
self.notifyObserversOf('after ' + operation + ' error', context,
|
|
|
|
function(_err, context) {
|
|
|
|
if (_err && err) {
|
|
|
|
debug(
|
|
|
|
'Operation %j failed and "after %s error" hook returned an error too. ' +
|
|
|
|
'Calling back with the hook error only.' +
|
|
|
|
'\nOriginal error: %s\nHook error: %s\n',
|
|
|
|
err.stack || err,
|
2019-12-03 09:09:16 +00:00
|
|
|
_err.stack || _err,
|
2019-02-23 10:18:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
callback.call(null, _err || err, context);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2015-05-26 17:15:39 +00:00
|
|
|
// Find the list of params from the callback in addition to err
|
2018-12-07 14:54:29 +00:00
|
|
|
const returnedArgs = args.slice(1);
|
2015-05-26 17:15:39 +00:00
|
|
|
// Set up the array of results
|
2015-05-20 22:02:44 +00:00
|
|
|
context.results = returnedArgs;
|
2015-05-26 17:15:39 +00:00
|
|
|
// Notify after observers
|
2015-05-20 22:02:44 +00:00
|
|
|
self.notifyObserversOf('after ' + operation, context,
|
|
|
|
function(err, context) {
|
|
|
|
if (err) return callback(err, context);
|
2018-12-07 14:54:29 +00:00
|
|
|
let results = returnedArgs;
|
2015-05-26 17:15:39 +00:00
|
|
|
if (context && Array.isArray(context.results)) {
|
|
|
|
// Pickup the results from context
|
2015-05-20 22:02:44 +00:00
|
|
|
results = context.results;
|
|
|
|
}
|
2015-05-26 17:15:39 +00:00
|
|
|
// Build the list of params for final callback
|
2018-12-07 14:54:29 +00:00
|
|
|
const args = [err].concat(results);
|
2015-05-20 22:02:44 +00:00
|
|
|
callback.apply(null, args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fn.length === 1) {
|
|
|
|
// fn(done)
|
|
|
|
fn(cbForWork);
|
|
|
|
} else {
|
|
|
|
// fn(context, done)
|
|
|
|
fn(context, cbForWork);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|