Guest User

Untitled

a guest
Jan 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. import { Gauge } from './client'
  2.  
  3. const observers = new Map()
  4.  
  5. export default function <A extends Function> (fn: A, name: string, help: string = ''): A {
  6. if (!observers.has(name)) {
  7. observers.set(name, new Gauge({ name, help }))
  8. }
  9.  
  10. const gauge = observers.get(name)
  11.  
  12. return new Proxy(fn, {
  13. apply (target, context, args) {
  14. const end = gauge.startTimer()
  15. const result = Reflect.apply(target, context, args)
  16.  
  17. if (result instanceof Promise) {
  18. result.then(() => end())
  19. } else {
  20. end()
  21. }
  22.  
  23. return result
  24. }
  25. })
  26. }
Add Comment
Please, Sign In to add comment