Skip to content

Commit

Permalink
Add Batman.Property.withoutTracking for preventing gets from creating…
Browse files Browse the repository at this point in the history
… sources on demand.
  • Loading branch information
airhorns committed Dec 9, 2011
1 parent 47d3baa commit a1cc5b5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
34 changes: 21 additions & 13 deletions lib/batman.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions src/batman.coffee
Expand Up @@ -494,11 +494,20 @@ class Batman.Property
base.property(key)
else
new Batman.Keypath(base, key)

@withoutTracking: (block) ->
@pushDummySourceTracker()
try
block()
finally
@popSourceTracker()
@registerSource: (obj) ->
return unless obj.isEventEmitter
@sourceTracker()?.add(obj)

@pushSourceTracker: -> Batman.Property._sourceTrackerStack.push(new Batman.SimpleSet)
@pushDummySourceTracker: -> Batman.Property._sourceTrackerStack.push(null)
@popSourceTracker: -> Batman.Property._sourceTrackerStack.pop()

constructor: (@base, @key) ->
developer.do =>
keyType = $typeOf(@key)
Expand Down Expand Up @@ -546,11 +555,8 @@ class Batman.Property
results
hasObservers: -> @observers().length > 0

pushSourceTracker: -> Batman.Property._sourceTrackerStack.push(new Batman.SimpleSet)
pushDummySourceTracker: -> Batman.Property._sourceTrackerStack.push(null)
popSourceTracker: -> Batman.Property._sourceTrackerStack.pop()
updateSourcesFromTracker: ->
newSources = @popSourceTracker()
newSources = @constructor.popSourceTracker()
handler = @sourceChangeHandler()
@_eachSourceChangeEvent (e) -> e.removeHandler(handler)
@sources = newSources
Expand All @@ -563,7 +569,7 @@ class Batman.Property
getValue: ->
@registerAsMutableSource()
unless @isCached()
@pushSourceTracker()
@constructor.pushSourceTracker()
try
@value = @valueFromAccessor()
@cached = yes
Expand Down Expand Up @@ -612,12 +618,12 @@ class Batman.Property

_changeValue: (block) ->
@cached = no
@pushDummySourceTracker()
@constructor.pushDummySourceTracker()
try
result = block.apply(this)
@refresh()
finally
@popSourceTracker()
@constructor.popSourceTracker()
@die() unless @isCached() or @hasObservers()
result

Expand Down Expand Up @@ -669,7 +675,6 @@ class Batman.Property
@_isolationCount--
isIsolated: -> @_isolationCount > 0


# Keypaths
# --------

Expand Down
18 changes: 18 additions & 0 deletions tests/batman/observable/observable_test.coffee
Expand Up @@ -79,6 +79,24 @@ test "get(key) works with cacheable properties with more than one level of acces
@obj.get('indirectProperty')
equal @fooPropertyAccessor.get.callCount, 2

test "get(key) works with Batman.Property.withoutTracking", ->
callCount = 0
@obj.accessor 'withoutTrackingProperty', ->
callCount++
@get('foo')
Batman.Property.withoutTracking =>
@get('foo.bar')
true

@obj.get('withoutTrackingProperty')
equal callCount, 1
@obj.set('foo.bar', true)
@obj.get('withoutTrackingProperty')
equal callCount, 1
@obj.set('foo')
@obj.get('withoutTrackingProperty')
equal callCount, 2

###
# set(key)
###
Expand Down
10 changes: 4 additions & 6 deletions tests/batman/property/keypath_test.coffee
Expand Up @@ -136,10 +136,10 @@ test "working with Hashes", ->
hash: new Batman.Hash
foo: new Batman.Object(bar: 'nested value'),
"foo.bar": 'flat value'

equal obj.get('hash.foo.bar'), 'nested value'
equal obj.hash.get('foo.bar'), 'flat value'

property = obj.property('hash.foo.bar')
ok property instanceof Batman.Keypath
equal property.getValue(), 'nested value'
Expand All @@ -163,10 +163,10 @@ test "working with SimpleHashes", ->
hash: new Batman.SimpleHash
foo: new Batman.Object(bar: 'nested value'),
"foo.bar": 'flat value'

equal obj.get('hash.foo.bar'), 'nested value'
equal obj.hash.get('foo.bar'), 'flat value'

property = obj.property('hash.foo.bar')
ok property instanceof Batman.Keypath
equal property.getValue(), 'nested value'
Expand All @@ -179,5 +179,3 @@ test "working with SimpleHashes", ->
obj.set 'hash.foo.bar', 'new value'
equal hashFooBarSpy.callCount, 1
deepEqual hashFooBarSpy.lastCallArguments, ['new value', 'nested value']


0 comments on commit a1cc5b5

Please sign in to comment.