applyHooks.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. 'use strict';
  2. var PromiseProvider = require('../../promise_provider');
  3. var VersionError = require('../../error').VersionError;
  4. module.exports = applyHooks;
  5. /*!
  6. * Register hooks for this model
  7. *
  8. * @param {Model} model
  9. * @param {Schema} schema
  10. */
  11. function applyHooks(model, schema) {
  12. var q = schema && schema.callQueue;
  13. var toWrapEl;
  14. var len;
  15. var i;
  16. var j;
  17. var pointCut;
  18. var keys;
  19. var newName;
  20. model.$appliedHooks = true;
  21. for (i = 0; i < schema.childSchemas.length; ++i) {
  22. var childModel = schema.childSchemas[i].model;
  23. if (childModel.$appliedHooks) {
  24. continue;
  25. }
  26. applyHooks(childModel, schema.childSchemas[i].schema);
  27. if (childModel.discriminators != null) {
  28. keys = Object.keys(childModel.discriminators);
  29. for (j = 0; j < keys.length; ++j) {
  30. applyHooks(childModel.discriminators[keys[j]],
  31. childModel.discriminators[keys[j]].schema);
  32. }
  33. }
  34. }
  35. if (!q.length) {
  36. return;
  37. }
  38. // we are only interested in 'pre' hooks, and group by point-cut
  39. var toWrap = { post: [] };
  40. var pair;
  41. for (i = 0; i < q.length; ++i) {
  42. pair = q[i];
  43. if (pair[0] !== 'pre' && pair[0] !== 'post' && pair[0] !== 'on') {
  44. continue;
  45. }
  46. var args = [].slice.call(pair[1]);
  47. pointCut = pair[0] === 'on' ? 'post' : args[0];
  48. if (!(pointCut in toWrap)) {
  49. toWrap[pointCut] = {post: [], pre: []};
  50. }
  51. if (pair[0] === 'post') {
  52. toWrap[pointCut].post.push(args);
  53. } else if (pair[0] === 'on') {
  54. toWrap[pointCut].push(args);
  55. } else {
  56. toWrap[pointCut].pre.push(args);
  57. }
  58. }
  59. // 'post' hooks are simpler
  60. len = toWrap.post.length;
  61. toWrap.post.forEach(function(args) {
  62. model.on.apply(model, args);
  63. });
  64. delete toWrap.post;
  65. // 'init' should be synchronous on subdocuments
  66. if (toWrap.init && (model.$isSingleNested || model.$isArraySubdocument)) {
  67. if (toWrap.init.pre) {
  68. toWrap.init.pre.forEach(function(args) {
  69. model.prototype.$pre.apply(model.prototype, args);
  70. });
  71. }
  72. if (toWrap.init.post) {
  73. toWrap.init.post.forEach(function(args) {
  74. model.prototype.$post.apply(model.prototype, args);
  75. });
  76. }
  77. delete toWrap.init;
  78. }
  79. if (toWrap.set) {
  80. // Set hooks also need to be sync re: gh-3479
  81. newName = '$__original_set';
  82. model.prototype[newName] = model.prototype.set;
  83. if (toWrap.set.pre) {
  84. toWrap.set.pre.forEach(function(args) {
  85. model.prototype.$pre.apply(model.prototype, args);
  86. });
  87. }
  88. if (toWrap.set.post) {
  89. toWrap.set.post.forEach(function(args) {
  90. model.prototype.$post.apply(model.prototype, args);
  91. });
  92. }
  93. delete toWrap.set;
  94. }
  95. toWrap.validate = toWrap.validate || { pre: [], post: [] };
  96. keys = Object.keys(toWrap);
  97. len = keys.length;
  98. for (i = 0; i < len; ++i) {
  99. pointCut = keys[i];
  100. // this is so we can wrap everything into a promise;
  101. newName = ('$__original_' + pointCut);
  102. if (!model.prototype[pointCut]) {
  103. continue;
  104. }
  105. if (model.prototype[pointCut].$isWrapped) {
  106. continue;
  107. }
  108. if (!model.prototype[pointCut].$originalFunction) {
  109. model.prototype[newName] = model.prototype[pointCut];
  110. }
  111. model.prototype[pointCut] = (function(_newName) {
  112. return function wrappedPointCut() {
  113. var Promise = PromiseProvider.get();
  114. var _this = this;
  115. var args = [].slice.call(arguments);
  116. var lastArg = args.pop();
  117. var fn;
  118. var originalError = new Error();
  119. var $results;
  120. if (lastArg && typeof lastArg !== 'function') {
  121. args.push(lastArg);
  122. } else {
  123. fn = lastArg;
  124. }
  125. var promise = new Promise.ES6(function(resolve, reject) {
  126. args.push(function(error) {
  127. if (error) {
  128. // gh-2633: since VersionError is very generic, take the
  129. // stack trace of the original save() function call rather
  130. // than the async trace
  131. if (error instanceof VersionError) {
  132. error.stack = originalError.stack;
  133. }
  134. if (!fn) {
  135. _this.$__handleReject(error);
  136. }
  137. reject(error);
  138. return;
  139. }
  140. // There may be multiple results and promise libs other than
  141. // mpromise don't support passing multiple values to `resolve()`
  142. $results = Array.prototype.slice.call(arguments, 1);
  143. resolve.apply(promise, $results);
  144. });
  145. _this[_newName].apply(_this, args);
  146. });
  147. if (fn) {
  148. if (this.constructor.$wrapCallback) {
  149. fn = this.constructor.$wrapCallback(fn);
  150. }
  151. promise.then(
  152. function() {
  153. process.nextTick(function() {
  154. fn.apply(null, [null].concat($results));
  155. });
  156. },
  157. function(error) {
  158. process.nextTick(function() {
  159. fn(error);
  160. });
  161. });
  162. }
  163. return promise;
  164. };
  165. })(newName);
  166. model.prototype[pointCut].$originalFunction = newName;
  167. model.prototype[pointCut].$isWrapped = true;
  168. toWrapEl = toWrap[pointCut];
  169. var _len = toWrapEl.pre.length;
  170. for (j = 0; j < _len; ++j) {
  171. args = toWrapEl.pre[j];
  172. args[0] = newName;
  173. model.prototype.$pre.apply(model.prototype, args);
  174. }
  175. _len = toWrapEl.post.length;
  176. for (j = 0; j < _len; ++j) {
  177. args = toWrapEl.post[j];
  178. args[0] = newName;
  179. model.prototype.$post.apply(model.prototype, args);
  180. }
  181. }
  182. }