Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Fix caching in web platform #1304

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions lib/core_dom/web_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,49 +58,50 @@ class PlatformViewCache implements ViewCache {
final ViewCache cache;
final String selector;
final WebPlatform platform;
bool _shimNeeded;
String _cacheKeyPrefix;

get viewFactoryCache => cache.viewFactoryCache;
Cache<String, ViewFactory> get viewFactoryCache => cache.viewFactoryCache;
Http get http => cache.http;
TemplateCache get templateCache => cache.templateCache;
Compiler get compiler => cache.compiler;
dom.NodeTreeSanitizer get treeSanitizer => cache.treeSanitizer;

PlatformViewCache(this.cache, this.selector, this.platform);
PlatformViewCache(this.cache, this.selector, this.platform) {
_shimNeeded = selector != null && selector != "" && platform.shadowDomShimRequired;
// By adding a comment with the tag name we ensure the template html is unique per selector
// name when used as a key in the view factory cache.
_cacheKeyPrefix = _shimNeeded ? '<!-- Shimmed template for: <$selector> -->' : '';
}

ViewFactory fromHtml(String html, DirectiveMap directives) {
ViewFactory viewFactory;

if (selector != null && selector != "" && platform.shadowDomShimRequired) {
// By adding a comment with the tag name we ensure the template html is unique per selector
// name when used as a key in the view factory cache.
viewFactory = viewFactoryCache.get("<!-- Shimmed template for: <$selector> -->$html");
} else {
viewFactory = viewFactoryCache.get(html);
}
String cacheKey = _cacheKeyPrefix + html;
viewFactory = viewFactoryCache.get(cacheKey);

if (viewFactory == null) {
var div = new dom.DivElement();
div.setInnerHtml(html, treeSanitizer: treeSanitizer);

if (selector != null && selector != "" && platform.shadowDomShimRequired) {
// This MUST happen before the compiler is called so that every dom element gets touched
// before the compiler removes them for transcluding directives like `ng-if`
platform.shimShadowDom(div, selector);
}
// This MUST happen before the compiler is called so that every dom element gets touched
// before the compiler removes them for transcluding directives like `ng-if`
if (_shimNeeded) platform.shimShadowDom(div, selector);

viewFactory = compiler(div.nodes, directives);
viewFactoryCache.put(html, viewFactory);
viewFactoryCache.put(cacheKey, viewFactory);
}
return viewFactory;
}

async.Future<ViewFactory> fromUrl(String url, DirectiveMap directives) {
ViewFactory viewFactory = viewFactoryCache.get(url);
String cacheKey = _cacheKeyPrefix + url;
ViewFactory viewFactory = viewFactoryCache.get(cacheKey);
if (viewFactory == null) {
return http.get(url, cache: templateCache).then((resp) {
var viewFactoryFromHttp = fromHtml(resp.responseText, directives);
viewFactoryCache.put(url, viewFactoryFromHttp);
return viewFactoryFromHttp;
ViewFactory factory = fromHtml(resp.responseText, directives);
viewFactoryCache.put(cacheKey, factory);
return factory;
});
}
return new async.Future.value(viewFactory);
Expand Down