X-Git-Url: https://git.josue.xyz/?a=blobdiff_plain;f=.config%2Fcoc%2Fextensions%2Fnode_modules%2Fcoc-prettier%2Fnode_modules%2F%40mrmlnc%2Freaddir-enhanced%2Flib%2Fcall.js;fp=.config%2Fcoc%2Fextensions%2Fnode_modules%2Fcoc-prettier%2Fnode_modules%2F%40mrmlnc%2Freaddir-enhanced%2Flib%2Fcall.js;h=07e3d848f13af1526191284a7e426f7a3a0fb65b;hb=3c06164f15bd10aed7d66b6314764a2961a14762;hp=0000000000000000000000000000000000000000;hpb=0e9c3ceb40901f4d44981c1376cb9e23a248e006;p=dotfiles%2F.git diff --git a/.config/coc/extensions/node_modules/coc-prettier/node_modules/@mrmlnc/readdir-enhanced/lib/call.js b/.config/coc/extensions/node_modules/coc-prettier/node_modules/@mrmlnc/readdir-enhanced/lib/call.js new file mode 100644 index 00000000..07e3d848 --- /dev/null +++ b/.config/coc/extensions/node_modules/coc-prettier/node_modules/@mrmlnc/readdir-enhanced/lib/call.js @@ -0,0 +1,54 @@ +'use strict'; + +let call = module.exports = { + safe: safeCall, + once: callOnce, +}; + +/** + * Calls a function with the given arguments, and ensures that the error-first callback is _always_ + * invoked exactly once, even if the function throws an error. + * + * @param {function} fn - The function to invoke + * @param {...*} args - The arguments to pass to the function. The final argument must be a callback function. + */ +function safeCall (fn, args) { + // Get the function arguments as an array + args = Array.prototype.slice.call(arguments, 1); + + // Replace the callback function with a wrapper that ensures it will only be called once + let callback = call.once(args.pop()); + args.push(callback); + + try { + fn.apply(null, args); + } + catch (err) { + callback(err); + } +} + +/** + * Returns a wrapper function that ensures the given callback function is only called once. + * Subsequent calls are ignored, unless the first argument is an Error, in which case the + * error is thrown. + * + * @param {function} fn - The function that should only be called once + * @returns {function} + */ +function callOnce (fn) { + let fulfilled = false; + + return function onceWrapper (err) { + if (!fulfilled) { + fulfilled = true; + return fn.apply(this, arguments); + } + else if (err) { + // The callback has already been called, but now an error has occurred + // (most likely inside the callback function). So re-throw the error, + // so it gets handled further up the call stack + throw err; + } + }; +}