This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
fix(cacheFactory): check key exists before decreasing size #12329
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,19 @@ describe('$cacheFactory', function() { | |
expect(cache.info().size).toBe(0); | ||
})); | ||
|
||
it('should only decrement size when an element is actually removed via remove', inject(function($cacheFactory) { | ||
cache.put('foo', 'bar'); | ||
expect(cache.info().size).toBe(1); | ||
|
||
cache.remove('undefined'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be possible to add a case that the key is |
||
expect(cache.info().size).toBe(1); | ||
|
||
cache.remove('hasOwnProperty'); | ||
expect(cache.info().size).toBe(1); | ||
|
||
cache.remove('foo'); | ||
expect(cache.info().size).toBe(0); | ||
})); | ||
|
||
it('should return cache id', inject(function($cacheFactory) { | ||
expect(cache.info().id).toBe('test'); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will still has unexpected results if
key
is prototypally inherited bydata
.You could use
!data.hasOwnProperty(key)
. It might also be possible to usedata = createMap()
and not have to deal with the prototype chain (but I haven't checked if more modifications are needed in other places).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the simplest and best of the two options would be to do
data = createMap()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that literally what this line should be? Sorry, this is my first time contributing here and I'm having trouble finding anything about a
createMap()
function. Is this just pseudo-code, or something internal and already available?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createMap
is defined in src/Angular.js. It just performsObject.create(null);
to build an object with a null prototypeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, thanks. Could I get a bit more guidance as to how I should make use of
createMap()
in this case, though? If I'm settingdata = createMap()
, wouldn't that effectively empty thedata
object?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change https://github.com/angular/angular.js/blob/master/src/ng/cacheFactory.js#L96 and https://github.com/angular/angular.js/blob/master/src/ng/cacheFactory.js#L240 to
data = createMap();