-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In this commit, we refactor store managed instance lookups to have its own class that takes the store's container. This moves a large portion of code out of the store and instead uses delegation. There's also a performance benefit: If you look up `adapter:post` and fallback to `adapter:application`, this lookup is cached. So, if you look up `adapter:post` again, that instance will actually be cached. Previously, we did the lookup for `adapter:post` every time. This also removes passing factories as the `adapter` property to DS.Store. closes #3050
- Loading branch information
Stanley Stuart
committed
Jun 4, 2015
1 parent
ae6355a
commit ee97760
Showing
8 changed files
with
294 additions
and
74 deletions.
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
86 changes: 86 additions & 0 deletions
86
packages/ember-data/lib/system/store/container-instance-cache.js
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import Ember from 'ember'; | ||
|
||
/** | ||
* The `ContainerInstanceCache` serves as a lazy cache for looking up | ||
* instances of serializers and adapters. It has some additional logic for | ||
* finding the 'fallback' adapter or serializer. | ||
* | ||
* The 'fallback' adapter or serializer is an adapter or serializer that is looked up | ||
* when the preferred lookup fails. For example, say you try to look up `adapter:post`, | ||
* but there is no entry (app/adapters/post.js in EmberCLI) for `adapter:post` in the registry. | ||
* | ||
* The `fallbacks` array passed will then be used; the first entry in the fallbacks array | ||
* that exists in the container will then be cached for `adapter:post`. So, the next time you | ||
* look up `adapter:post`, you'll get the `adapter:application` instance (or whatever the fallback | ||
* was if `adapter:application` doesn't exist). | ||
* | ||
* @private | ||
* @class ContainerInstanceCache | ||
* | ||
*/ | ||
function ContainerInstanceCache(container) { | ||
this._container = container; | ||
this._cache = Ember.create(null); | ||
} | ||
|
||
ContainerInstanceCache.prototype = Ember.create(null); | ||
|
||
Ember.merge(ContainerInstanceCache.prototype, { | ||
get: function(type, preferredKey, fallbacks) { | ||
let cache = this._cache; | ||
let preferredLookupKey = `${type}:${preferredKey}`; | ||
|
||
if (!(preferredLookupKey in cache)) { | ||
let instance = this.instanceFor(preferredLookupKey) || this._findInstance(type, fallbacks); | ||
if (instance) { | ||
cache[preferredLookupKey] = instance; | ||
} | ||
} | ||
return cache[preferredLookupKey]; | ||
}, | ||
|
||
_findInstance: function(type, fallbacks) { | ||
for (let i = 0, length = fallbacks.length; i < length; i++) { | ||
let fallback = fallbacks[i]; | ||
let lookupKey = `${type}:${fallback}`; | ||
let instance = this.instanceFor(lookupKey); | ||
|
||
if (instance) { | ||
return instance; | ||
} | ||
} | ||
}, | ||
|
||
instanceFor: function(key) { | ||
let cache = this._cache; | ||
if (!cache[key]) { | ||
let instance = this._container.lookup(key); | ||
if (instance) { | ||
cache[key] = instance; | ||
} | ||
} | ||
return cache[key]; | ||
}, | ||
|
||
destroy: function() { | ||
let cache = this._cache; | ||
let cacheEntries = Ember.keys(cache); | ||
|
||
for (let i = 0, length = cacheEntries.length; i < length; i++) { | ||
let cacheKey = cacheEntries[i]; | ||
let cacheEntry = cache[cacheKey]; | ||
if (cacheEntry) { | ||
cacheEntry.destroy(); | ||
} | ||
} | ||
this._container = null; | ||
}, | ||
|
||
constructor: ContainerInstanceCache, | ||
|
||
toString: function() { | ||
return 'ContainerInstanceCache'; | ||
} | ||
}); | ||
|
||
export default ContainerInstanceCache; |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
export default function(env, adapterDefinition) { | ||
var adapter = adapterDefinition; | ||
if (!DS.Adapter.detect(adapterDefinition)) { | ||
adapter = DS.Adapter.extend(adapterDefinition); | ||
} | ||
var store = env.store; | ||
env.registry.register('adapter:-custom', adapter); | ||
Ember.run(() => store.set('adapter', '-custom')); | ||
} |
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
Oops, something went wrong.