-
Notifications
You must be signed in to change notification settings - Fork 27.5k
feat($cacheFactory): Make Cache.put return the value #1583
Conversation
@taralx This looks good apart from the fact that the test could be simplified (let me know if I've missed anything). Could you re-work the commit message as described in http://docs.angularjs.org/misc/contribute ? |
Updated. |
@taralx Did you sign the CLA? |
Yes. |
@@ -46,13 +46,15 @@ function $CacheFactoryProvider() { | |||
|
|||
refresh(lruEntry); | |||
|
|||
if (isUndefined(value)) return; | |||
if (isUndefined(value)) return value; |
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 is useless because return undefined
is the same thing as return
.
I was inclined to say no to this change because it looked like only coffee script would benefit from it, but it turns out that even in js you can simplify the code thanks to the ability to use ternary operators. Before:
Before with inlined assignment:
After:
After w/ ternary operator:
|
|
||
it("should return value from put", inject(function($cacheFactory) { | ||
function DummyClass() {} | ||
var obj = new DummyClass(); |
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.
why not just var obj = {}
, we just need to be able to test object identity and for that object literal is good enough.
This allows common programming patterns to be expressed with more concise code. See #1583 for code examples.
landed as 168db33 thanks! |
This allows common programming patterns to be expressed with more concise code. See angular#1583 for code examples.
Using cache.put is slightly awkward because you can't just wrap an expression with it like you can a standard object.
Coffeescript with plain object:
cache = {}
(key) -> cache[key] ?= genValue(key)
Coffeescript with $cacheFactory cache:
cache = $cacheFactory.get('cache')
(key) -> cache.get(key) ? (r = genValue(key); cache.put(key, r); r)
With this change:
cache = $cacheFactory.get('cache')
(key) -> cache.get(key) ? cache.put(key, genValue(key))