Skip to content

Commit

Permalink
Added removeCallback() and removeCallbacks()
Browse files Browse the repository at this point in the history
  • Loading branch information
James Hartig authored and TooTallNate committed Aug 17, 2015
1 parent 2c35074 commit eccaf3f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,24 @@ Checks to see if `obj` is "weak reference" instance. Returns `true` if the
passed in object is a "weak reference", `false` otherwise.


### undefined weak.addCallback(Weakref ref, Function callback)
### EventEmitter weak.addCallback(Weakref ref, Function callback)

Adds `callback` to the Array of callback functions that will be invoked before the
Object gets garbage collected. The callbacks get executed in the order that they
are added.

### EventEmitter weak.removeCallback(Weakref ref, Function callback)

Removes `callback` from the Array of callback functions that will be invoked before
the Object gets garbage collected.

### EventEmitter weak.removeCallbacks(Weakref ref)

Empties the Array of callback functions that will be invoked before the Object gets
garbage collected.

### Array weak.callbacks(Weakref ref)

Returns the internal `Array` that `ref` iterates through to invoke the GC
callbacks. The array can be `push()`ed or `unshift()`ed onto, to have more control
over the execution order of the callbacks. This is similar in concept to node's
`EventEmitter#listeners()` function.
Returns an Array that `ref` iterates through to invoke the GC callbacks. This
utilizes node's `EventEmitter#listeners()` function and therefore returns a copy
in node 0.10 and newer.
17 changes: 15 additions & 2 deletions lib/weak.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ bindings._setCallback(callback);

exports = module.exports = create;
exports.addCallback = exports.addListener = addCallback;
exports.removeCallback = exports.removeListener = addCallback;
exports.removeCallback = exports.removeListener = removeCallback;
exports.removeCallbacks = exports.removeListeners = removeCallbacks;
exports.callbacks = exports.listeners = callbacks;

// backwards-compat with node-weakref
Expand Down Expand Up @@ -75,7 +76,7 @@ function removeCallback (weakref, fn) {
}

/**
* Removes a weak callback function from the Weakref instance.
* Returns a copy of the listeners on the Weakref instance.
*
* @api public
*/
Expand All @@ -85,6 +86,18 @@ function callbacks (weakref) {
return emitter.listeners(CB);
}


/**
* Removes all callbacks on the Weakref instance.
*
* @api public
*/

function removeCallbacks (weakref) {
var emitter = bindings._getEmitter(weakref);
return emitter.removeAllListeners(CB);
}

/**
* Common weak callback function.
*
Expand Down
32 changes: 29 additions & 3 deletions test/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,37 @@ describe('weak()', function () {

describe('callbacks()', function () {

it('should return the Weakref\'s internal "callback" Array', function () {
var r = weak({})
it('should return the Weakref\'s "callback" Array', function () {
var r = weak({}, function() {})
, callbacks = weak.callbacks(r)
assert(Array.isArray(callbacks))
assert.equal(0, callbacks.length)
assert.equal(1, callbacks.length)
})

})

describe('removeCallback()', function() {

it('removed callbacks should not be called', function() {
var called = false
, fn = function() { called = true }
, r = weak({}, fn)
weak.removeCallback(r, fn)
gc()
assert(!called)
})

})

describe('removeCallbacks()', function() {

it('removed callbacks should not be called', function() {
var called = false
, fn = function() { called = true }
, r = weak({}, fn)
weak.removeCallbacks(r)
gc()
assert(!called)
})

})
2 changes: 2 additions & 0 deletions test/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe('exports', function () {
checkFunction('isDead')
checkFunction('callbacks')
checkFunction('addCallback')
checkFunction('removeCallback')
checkFunction('removeCallbacks')

it('should be a circular reference to "create"', function () {
assert(weak === weak.create);
Expand Down

0 comments on commit eccaf3f

Please sign in to comment.