CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
once.js43 linesDownload Raw Back to once
1var wrappy = require('wrappy')2module.exports = wrappy(once)3module.exports.strict = wrappy(onceStrict)4 5once.proto = once(function () {6  Object.defineProperty(Function.prototype, 'once', {7    value: function () {8      return once(this)9    },10    configurable: true11  })12 13  Object.defineProperty(Function.prototype, 'onceStrict', {14    value: function () {15      return onceStrict(this)16    },17    configurable: true18  })19})20 21function once (fn) {22  var f = function () {23    if (f.called) return f.value24    f.called = true25    return f.value = fn.apply(this, arguments)26  }27  f.called = false28  return f29}30 31function onceStrict (fn) {32  var f = function () {33    if (f.called)34      throw new Error(f.onceError)35    f.called = true36    return f.value = fn.apply(this, arguments)37  }38  var name = fn.name || 'Function wrapped with `once`'39  f.onceError = name + " shouldn't be called more than once"40  f.called = false41  return f42}43