Permalink
Browse files

README: add "best practices" for callback section

Closes #4.
  • Loading branch information...
1 parent a6152bb commit abedf3c885002f64e120b213f05101e56c98f0c5 @TooTallNate committed Apr 14, 2012
Showing with 32 additions and 0 deletions.
  1. +32 −0 README.md
View
@@ -74,6 +74,38 @@ typeof ref.foo === 'undefined'
```
+Weak Callback Function "Best Practices"
+---------------------------------------
+
+It's important to be careful when using the "callbacks" feature of `node-weak`,
+otherwise you can end up in a situation where the watched object will never
+be garbage collected.
+
+You _should **not**_ define the callback function in the same scope as the
+object that is being watched. It's often best to define the callback function
+at the highest scope possible (top-level being the best). Named function work
+really well for this:
+
+``` js
+var http = require('http')
+ , weak = require('weak')
+
+http.createServer(function (req, res) {
+ weak(req, gcReq)
+ weak(res, gcRes)
+ res.end('Hello World\n')
+}).listen(3000)
+
+function gcReq () {
+ console.log('GC\'d `req` object')
+}
+
+function gcRes () {
+ console.log('GC\'d `res` object')
+}
+```
+
+
API
---

0 comments on commit abedf3c

Please sign in to comment.