123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- /**
- * Expose `Receiver`.
- */
- module.exports = Receiver
- /**
- * Initialize a `Receiver`.
- *
- * @param {Mixed} val
- * @api private
- */
- function Receiver(val) {
- this.val = val
- this.isAdded = false
- this.err = null
- this.cb = null
- this.isDone = false
- }
- /**
- * Call the callback if the pending add is complete.
- *
- * @api private
- */
- Receiver.prototype.attemptNotify = function () {
- if ((this.isAdded || this.err) && this.cb && !this.isDone) {
- this.isDone = true
- setImmediate(function () { this.cb(this.err) }.bind(this))
- }
- }
- /**
- * Reject the pending add with an error.
- *
- * @param {Error} err
- * @api private
- */
- Receiver.prototype.error = function (err) {
- this.err = err
- this.attemptNotify()
- }
- /**
- * Get the `val` and set the state of the value to added
- *
- * @return {Mixed} val
- * @api private
- */
- Receiver.prototype.add = function () {
- this.isAdded = true
- this.attemptNotify()
- return this.val
- }
- /**
- * Register the callback.
- *
- * @api private
- */
- Receiver.prototype.callback = function (cb) {
- this.cb = cb
- this.attemptNotify()
- }
|