Skip to content

Instantly share code, notes, and snippets.

@eduardoromero
Created January 19, 2019 00:55
Show Gist options
  • Save eduardoromero/20ee95b0b0a0938ef1aba00a16cc256d to your computer and use it in GitHub Desktop.
Save eduardoromero/20ee95b0b0a0938ef1aba00a16cc256d to your computer and use it in GitHub Desktop.
A Proxy Object that peeks into your Objects.
const Observe = (AnObject, Label = '') => {
const name = Label || generateLabel(AnObject)
if (DISABLE_TRACING) {
return AnObject
}
return new Proxy(AnObject, {
get(target, prop) {
let label = ''
if (name) {
iopipe.label(name)
label = `${name}-`
}
const method = target[prop]
const isPromise = method instanceof Promise
const isAsyncFunction = method[Symbol.toStringTag] === 'AsyncFunction'
const marker = `${label}${Inflector.dasherize(Inflector.underscore(prop))}`
if (isPromise) {
iopipe.mark.start(marker)
// return a new Promise that will stop the marker after the promise finished
return Promise.resolve(method).finally(() => { iopipe.mark.end(marker) })
} else if (isAsyncFunction) {
iopipe.mark.start(marker)
// return a new async function that will await for the call and stop the marker
return (async (...args) => {
const result = await method(...args)
iopipe.mark.end(marker)
return result
})
}
return target[prop]
},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment