diff --git a/README.md b/README.md index 7b63b90..97f1c64 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/weak.js b/lib/weak.js index 12b568c..5502b0d 100644 --- a/lib/weak.js +++ b/lib/weak.js @@ -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 @@ -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 */ @@ -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. * diff --git a/test/callback.js b/test/callback.js index df20448..fbbcafb 100644 --- a/test/callback.js +++ b/test/callback.js @@ -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) }) }) diff --git a/test/exports.js b/test/exports.js index b3f1b0d..348cffd 100644 --- a/test/exports.js +++ b/test/exports.js @@ -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);