Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import moize from 'moize'
import keepFuncProps from 'keep-func-props'
import { mapValues } from './utils.js'
const kMoize = keepFuncProps(moize)
// Moize a function, or an object containing functions
export const moizeFuncs = function(value) {
if (typeof value === 'function') {
return kMoize(value, { isDeepEqual: true })
}
return mapValues(value, moizeFuncs)
}
// eslint-disable-next-line default-param-last
label = func.name,
category,
measuresIndex = 0,
) {
return function monitoredFunc(...args) {
const labelA = result(label, ...args)
const categoryA = result(category, ...args)
const perf = startPerf(labelA, categoryA)
const response = func(...args)
const { measures } = args[measuresIndex]
return promiseThen(response, recordPerf.bind(null, measures, perf))
}
}
export const monitor = keepFuncProps(kMonitor)
const recordPerf = function(measures, perf, response) {
const perfA = stopPerf(perf)
// We directly mutate the passed argument, because it greatly simplifies
// the code
// eslint-disable-next-line fp/no-mutating-methods
measures.push(perfA)
return response
}
// Combine monitor() and reduceAsync()
export const monitoredReduce = function({
funcs,
initialInput,
mapInput = identity,
mapResponse = identity,
import keepFuncProps from 'keep-func-props'
import { result } from '../utils/functional/result.js'
import { throwError, normalizeError, isError } from './main.js'
import { throwPb } from './props.js'
// Wrap a function with a error handler
// Allow passing an empty error handler, i.e. ignoring any error thrown
const kAddErrorHandler = function(func, errorHandler = () => undefined) {
return errorHandledFunc.bind(null, func, errorHandler)
}
export const addErrorHandler = keepFuncProps(kAddErrorHandler)
const errorHandledFunc = function(func, errorHandler, ...args) {
try {
const retVal = func(...args)
// eslint-disable-next-line promise/prefer-await-to-then
return retVal && typeof retVal.then === 'function'
? retVal.catch(error => errorHandler(error, ...args))
: retVal
} catch (error) {
return errorHandler(error, ...args)
}
}
// Use `addErrorHandler()` with a generic error handler that rethrows
export const addGenErrorHandler = function(func, { message, reason, extra }) {
'use strict'
const { keepFuncProps } = require('keep-func-props')
const { result } = require('../utils')
const { throwError, normalizeError, isError } = require('./main')
const { throwPb } = require('./props')
// Wrap a function with a error handler
// Allow passing an empty error handler, i.e. ignoring any error thrown
const addErrorHandler = function(func, errorHandler = () => undefined) {
return errorHandledFunc.bind(null, func, errorHandler)
}
const kAddErrorHandler = keepFuncProps(addErrorHandler)
const errorHandledFunc = function(func, errorHandler, ...args) {
try {
const retVal = func(...args)
// eslint-disable-next-line promise/prefer-await-to-then
return retVal && typeof retVal.then === 'function'
? retVal.catch(error => errorHandler(error, ...args))
: retVal
} catch (error) {
return errorHandler(error, ...args)
}
}
// Use `addErrorHandler()` with a generic error handler that rethrows
const addGenErrorHandler = function(func, { message, reason, extra }) {