Last change
on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
685 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | # wrappy
|
---|
| 2 |
|
---|
| 3 | Callback wrapping utility
|
---|
| 4 |
|
---|
| 5 | ## USAGE
|
---|
| 6 |
|
---|
| 7 | ```javascript
|
---|
| 8 | var wrappy = require("wrappy")
|
---|
| 9 |
|
---|
| 10 | // var wrapper = wrappy(wrapperFunction)
|
---|
| 11 |
|
---|
| 12 | // make sure a cb is called only once
|
---|
| 13 | // See also: http://npm.im/once for this specific use case
|
---|
| 14 | var once = wrappy(function (cb) {
|
---|
| 15 | var called = false
|
---|
| 16 | return function () {
|
---|
| 17 | if (called) return
|
---|
| 18 | called = true
|
---|
| 19 | return cb.apply(this, arguments)
|
---|
| 20 | }
|
---|
| 21 | })
|
---|
| 22 |
|
---|
| 23 | function printBoo () {
|
---|
| 24 | console.log('boo')
|
---|
| 25 | }
|
---|
| 26 | // has some rando property
|
---|
| 27 | printBoo.iAmBooPrinter = true
|
---|
| 28 |
|
---|
| 29 | var onlyPrintOnce = once(printBoo)
|
---|
| 30 |
|
---|
| 31 | onlyPrintOnce() // prints 'boo'
|
---|
| 32 | onlyPrintOnce() // does nothing
|
---|
| 33 |
|
---|
| 34 | // random property is retained!
|
---|
| 35 | assert.equal(onlyPrintOnce.iAmBooPrinter, true)
|
---|
| 36 | ```
|
---|
Note:
See
TracBrowser
for help on using the repository browser.