wrappy

Callback wrapping utility

USAGE

  1. var wrappy = require("wrappy")
  2. // var wrapper = wrappy(wrapperFunction)
  3. // make sure a cb is called only once
  4. // See also: http://npm.im/once for this specific use case
  5. var once = wrappy(function (cb) {
  6. var called = false
  7. return function () {
  8. if (called) return
  9. called = true
  10. return cb.apply(this, arguments)
  11. }
  12. })
  13. function printBoo () {
  14. console.log('boo')
  15. }
  16. // has some rando property
  17. printBoo.iAmBooPrinter = true
  18. var onlyPrintOnce = once(printBoo)
  19. onlyPrintOnce() // prints 'boo'
  20. onlyPrintOnce() // does nothing
  21. // random property is retained!
  22. assert.equal(onlyPrintOnce.iAmBooPrinter, true)