Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | 1x 1x 1x 32x 32x 32x 32x 36x 36x 36x 36x 36x 36x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 24x 24x 24x 24x 1x 24x 48x 48x 2x 48x 25x 25x 25x 25x 25x 23x 24x 24x 24x 24x 39x 39x 26x 26x 26x 26x 26x 13x 4x 13x 4x 4x 4x 4x 4x 2x 4x 3x 1x 4x 24x 24x 1x 24x 1x 24x 13x 11x 39x 16x 23x 7x 7x 7x 7x 5x 1x 5x 7x 16x 36x 36x 36x 36x 36x 1x 2x 35x 35x 35x 35x 42x 42x 2x 4x 40x 40x 40x 40x 75x 36x 1x 51x 1x | /* eslint-disable no-multi-assign */ 'use strict'; const is = require('is'); const arrify = require('arrify'); const ERR_KEY = Symbol('errors'); class HooksPromise { static wrap(scope) { scope.pre = this.pre; scope.post = this.post; scope.hook = this.hook; scope.__setupHooks = this.__setupHooks; } static hook(name, originalMethod) { const proto = this; const presHooks = proto.__pres = proto.__pres || {}; const postsHooks = proto.__posts = proto.__posts || {}; presHooks[name] = presHooks[name] || []; postsHooks[name] = postsHooks[name] || []; /** * Wrap the original "target" method with hooks */ proto[name] = function wrapper() { const self = this; /** * save the arguments passed to the target method */ let passedArgs = Array.prototype.slice.apply(arguments); /** * Array or pre hooks */ const pres = this.__pres[name]; /** * Array of post hooks */ const posts = this.__posts[name]; /** * Total pre hooks */ const totalPres = pres.length; /** * Total posts hooks */ const totalPost = posts.length; /** * Scope for the hooks */ let scope = self; /** * Current hook being processed */ let currentPre = -1; let resolveFn; let rejectFn; if (self.preHooksEnabled === false) { return done.apply(self, passedArgs); } return new Promise((resolve, reject) => { resolveFn = resolve; rejectFn = reject; /** * Error Handler */ const handleError = function handleError(err) { reject(err); }; /** * "pre" hook handler */ const nextPreHook = function nextPreHook() { /** * read the arguments from previous "hook" response */ const args = Array.prototype.slice.apply(arguments); /** * Check if the arguments contains an "__override" property * that would override the arguments sent originally to the * target method */ if (is.object(args[0]) && {}.hasOwnProperty.call(args[0], '__override')) { passedArgs = arrify(args[0].__override); } /** * Reference to current pre hook */ let currentHook; if (currentPre + 1 < totalPres) { currentPre += 1; currentHook = pres[currentPre]; const hookMethod = currentHook.displayName || currentHook.name; /** * If there is a __scopeHook function on the object * we call it to get the scope wanted for the hook */ scope = getScope(self, name, passedArgs, hookMethod, 'pre'); /** * Execute the hook and recursively call the next one */ return currentHook.apply(scope, passedArgs).then(nextPreHook, handleError); } /** * We are done with "pre" hooks */ return done.apply(scope, passedArgs); }; return nextPreHook.apply(this, passedArgs); }); function done() { /** * In case we "skipped" the "pre" hooks we define our resolve function */ resolveFn = resolveFn || function resolve(data) { return Promise.resolve(data); }; let response; let currentPost = -1; /** * "post" hook handler * @param {*} res the response coming from the target method * or the reponse from any "post" hook */ const nextPostHook = function nextPostHook(res) { response = checkResponse(res, response); if (currentPost + 1 < totalPost && self.__hooksEnabled !== false) { currentPost += 1; // Reference to current post hook const currPost = posts[currentPost]; const hookMethod = currPost.displayName || currPost.name; scope = getScope(self, name, res, hookMethod, 'post'); // Recursively call all the "post" hooks return currPost.call(scope, response).then(nextPostHook, postHookErrorHandler); } /** * All "post" hook process done. * If the response has an "__override" property it will be our response */ if (is.object(response) && {}.hasOwnProperty.call(response, '__override')) { response = response.__override; } return resolveFn(response); }; /** * "post" hook Error handling * @param {*} err the error returned by latest "post" hook */ function postHookErrorHandler(err) { /** * Helper to add an error to an Object "errors" Symbol */ const addError = (obj) => { obj[ERR_KEY] = obj[ERR_KEY] || []; obj[ERR_KEY].push(err); return obj; }; /** * For response type that are *not* objects (string, integers, functions...) * we convert the response to an object with a "result" property and set it as "override" */ if (typeof response !== 'object') { response = { __override: { result: response, }, }; } if ({}.hasOwnProperty.call(response, '__override')) { response.__override = addError(response.__override); } else { response = addError(response); } return nextPostHook(response); } /** * Get the target method response */ let responsePromised; try { responsePromised = originalMethod.apply(self, passedArgs); } catch (e) { responsePromised = Promise.reject(e); } /** * Convert it to a Promise if it is not to be able to chain it */ if (responsePromised.constructor.name !== 'Promise') { responsePromised = Promise.resolve(responsePromised); } /** * If there are post hooks, we chain the response with the hook */ if (totalPost > 0) { return responsePromised.then(nextPostHook, rejectFn); } // no "post" hook, we're done! return responsePromised.then(resolveFn, rejectFn); } function checkResponse(currentResponse, previousResponse) { if (!previousResponse) { return currentResponse; } if (is.object(currentResponse) && {}.hasOwnProperty.call(currentResponse, '__override')) { const isObject = is.object(previousResponse); const hasOverride = isObject && {}.hasOwnProperty.call(previousResponse, '__override'); const hasErrorsPostHooks = hasOverride && typeof previousResponse.__override[ERR_KEY] !== 'undefined'; if (isObject && hasOverride && hasErrorsPostHooks) { if (typeof currentResponse.__override !== 'object') { currentResponse.__override = { result: currentResponse.__override, }; } /** * We add the previous "post" hooks error to the * current response */ currentResponse.__override[ERR_KEY] = previousResponse.__override[ERR_KEY]; } return currentResponse; } return previousResponse; } }; proto[name].__numAsyncPres = 0; proto[name].__hooked = true; return this; } static pre(name, fn) { const proto = this; if (is.array(fn)) { return fn.forEach((middleware) => { proto.pre.call(proto, name, middleware); }); } const pres = proto.__pres = proto.__pres || {}; proto.__setupHooks(proto, name); pres[name].push(fn); return proto; } static post(name, fn) { const proto = this; if (is.array(fn)) { return fn.forEach((middleware) => { proto.post.call(proto, name, middleware); }); } const posts = proto.__posts = proto.__posts || {}; proto.__setupHooks(proto, name); posts[name].push(fn); return proto; } static __setupHooks(proto, methodName) { if (proto[methodName] && proto[methodName].__hooked !== true) { this.hook(methodName, proto[methodName]); } } } HooksPromise.ERRORS = ERR_KEY; // Helpers /** * Set the scope (this) for the hook callback at runtime. * The wrapped object/Class need to have a __scopeHook method declared that will return the scope * wanted for each hook. * * @param {*} self Scope of the wrapped object * @param {*} hookName Name of the hook (the target method on the object/class) * @param {*} args The arguments passed to the target method * @param {*} hookMethod The name of the callback function (if not an anonymous function of course) * @param {*} hookType The hook type: "pre" or "post" */ function getScope(self, hookName, args, hookMethod, hookType) { return self.__scopeHook && typeof self.__scopeHook === 'function' ? self.__scopeHook(hookName, args, hookMethod, hookType) : self; } module.exports = HooksPromise; |