Shopify/batman — Issue #187: Misc. Memory Leak Patches
Author: pvande
Created: 2011-10-19 02:45:37+00:00
State: open

I haven't yet had time to properly test or integrate these changes, but they have significantly reduced memory leakage in our application. I'm publishing these here in case it takes me a while to get proper pull requests put together, and to encourage faster feedback.

``` diff
---
lib/batman.coffee | 13 ++++++++-----
1 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/lib/batman.coffee b/lib/batman.coffee
index 1051e25..4b322ee 100644
--- a/lib/batman.coffee
+++ b/lib/batman.coffee
@@ -442,8 +442,8 @@ class Batman.Property
event
accessor: ->
keyAccessors = @base._batman?.get('keyAccessors')
- accessor = if keyAccessors && (val = keyAccessors.get(@key))
- val
+ accessor = if keyAccessors?.hasKey(@key)
+ val = keyAccessors.get(@key)
else
@base._batman?.getFirst('defaultAccessor') or Batman.Property.defaultAccessor
@accessor = -> accessor
@@ -900,6 +900,7 @@ class Batman.SimpleHash
if @equality(obj, key)
pair = pairs.splice(index,1)
@length--
+ delete @_storage[@hashKeyFor(key)] unless pairs.length
return pair[0][1]
getOrSet: Batman.Observable.getOrSet
hashKeyFor: (obj) -> obj?.hashKey?() or obj
@@ -944,7 +945,9 @@ class Batman.Hash extends Batman.Object
super

$extendsEnumerable(@::)
- propertyClass: Batman.Property
+
+ property: (key) ->
+ new Batman.Property(this, key)

@accessor
get: Batman.SimpleHash::get
@@ -1955,7 +1958,7 @@ class Batman.Model extends Batman.Object
# Add a universally accessible accessor for retrieving the primrary key, regardless of which key its stored under.
@accessor 'id',
get: ->
- pk = @constructor.get('primaryKey')
+ pk = @constructor.primaryKey
if pk == 'id'
@id
else
@@ -1965,7 +1968,7 @@ class Batman.Model extends Batman.Object
if typeof v is "string" and !isNaN(intId = parseInt(v, 10))
v = intId

- pk = @constructor.get('primaryKey')
+ pk = @constructor.primaryKey
if pk == 'id'
@id = v
else
--
```