-
Notifications
You must be signed in to change notification settings - Fork 25.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(compiler): Add implementation for a XHR that uses a template cache to load template files. #7940
Closed
Closed
feat(compiler): Add implementation for a XHR that uses a template cache to load template files. #7940
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
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,48 @@ | ||
library angular2.src.services.xhr_cache; | ||
|
||
import 'dart:async' show Future; | ||
import 'dart:html'; | ||
import 'dart:js' as js; | ||
import 'package:angular2/core.dart'; | ||
import 'package:angular2/src/compiler/xhr.dart'; | ||
import 'package:angular2/src/facade/exceptions.dart' show BaseException; | ||
|
||
/** | ||
* An implementation of XHR that uses a template cache to avoid doing an actual | ||
* XHR. | ||
* | ||
* The template cache needs to be built and loaded into window.$templateCache | ||
* via a separate mechanism. | ||
*/ | ||
@Injectable() | ||
class CachedXHR extends XHR { | ||
js.JsObject _cache; | ||
String _baseUri; | ||
|
||
CachedXHR() { | ||
if (js.context.hasProperty(r'$templateCache')) { | ||
this._cache = js.context[r'$templateCache']; | ||
} else { | ||
throw new BaseException( | ||
r'CachedXHR: Template cache was not found in $templateCache.'); | ||
} | ||
this._baseUri = window.location.protocol + | ||
'//' + | ||
window.location.host + | ||
window.location.pathname; | ||
int lastSlash = this._baseUri.lastIndexOf('/'); | ||
this._baseUri = this._baseUri.substring(0, lastSlash + 1); | ||
} | ||
|
||
Future<String> get(String url) { | ||
if (url.startsWith(this._baseUri)) { | ||
url = url.substring(this._baseUri.length); | ||
} | ||
if (this._cache.hasProperty(url)) { | ||
return new Future.value(this._cache[url]); | ||
} else { | ||
return new Future.error( | ||
'CachedXHR: Did not find cached template for ' + url); | ||
} | ||
} | ||
} |
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,31 @@ | ||
import {XHR} from 'angular2/src/compiler/xhr'; | ||
import {BaseException} from 'angular2/src/facade/exceptions'; | ||
import {global} from 'angular2/src/facade/lang'; | ||
import {PromiseWrapper} from 'angular2/src/facade/promise'; | ||
|
||
/** | ||
* An implementation of XHR that uses a template cache to avoid doing an actual | ||
* XHR. | ||
* | ||
* The template cache needs to be built and loaded into window.$templateCache | ||
* via a separate mechanism. | ||
*/ | ||
export class CachedXHR extends XHR { | ||
private _cache: {[url: string]: string}; | ||
|
||
constructor() { | ||
super(); | ||
this._cache = (<any>global).$templateCache; | ||
if (this._cache == null) { | ||
throw new BaseException('CachedXHR: Template cache was not found in $templateCache.'); | ||
} | ||
} | ||
|
||
get(url: string): Promise<string> { | ||
if (this._cache.hasOwnProperty(url)) { | ||
return PromiseWrapper.resolve(this._cache[url]); | ||
} else { | ||
return PromiseWrapper.reject('CachedXHR: Did not find cached template for ' + url, null); | ||
} | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
modules/angular2/test/platform/browser/xhr_cache_setter.dart
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,16 @@ | ||
import 'dart:js' as js; | ||
|
||
void setTemplateCache(Map cache) { | ||
if (cache == null) { | ||
if (js.context.hasProperty(r'$templateCache')) { | ||
js.context.deleteProperty(r'$templateCache'); | ||
} | ||
return; | ||
} | ||
|
||
js.JsObject jsMap = new js.JsObject(js.context['Object']); | ||
for (String key in cache.keys) { | ||
jsMap[key] = cache[key]; | ||
} | ||
js.context[r'$templateCache'] = jsMap; | ||
} |
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,3 @@ | ||
export function setTemplateCache(cache): void { | ||
(<any>window).$templateCache = cache; | ||
} |
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,83 @@ | ||
import {Component, provide} from 'angular2/core'; | ||
import {UrlResolver, XHR} from 'angular2/compiler'; | ||
import { | ||
AsyncTestCompleter, | ||
beforeEach, | ||
beforeEachProviders, | ||
ComponentFixture, | ||
ddescribe, | ||
describe, | ||
expect, | ||
fakeAsync, | ||
iit, | ||
inject, | ||
it, | ||
TestComponentBuilder, | ||
tick, | ||
xit | ||
} from 'angular2/testing_internal'; | ||
import {BaseException} from 'angular2/src/facade/exceptions'; | ||
import {CachedXHR} from 'angular2/src/platform/browser/xhr_cache'; | ||
import {setTemplateCache} from './xhr_cache_setter'; | ||
|
||
export function main() { | ||
describe('CachedXHR', () => { | ||
var xhr: CachedXHR; | ||
|
||
function createCachedXHR(): CachedXHR { | ||
setTemplateCache({'test.html': '<div>Hello</div>'}); | ||
return new CachedXHR(); | ||
} | ||
beforeEachProviders(() => [ | ||
provide(UrlResolver, {useClass: TestUrlResolver}), | ||
provide(XHR, {useFactory: createCachedXHR}) | ||
]); | ||
|
||
it('should throw exception if $templateCache is not found', () => { | ||
setTemplateCache(null); | ||
expect(() => { xhr = new CachedXHR(); }) | ||
.toThrowErrorWith('CachedXHR: Template cache was not found in $templateCache.'); | ||
}); | ||
|
||
it('should resolve the Promise with the cached file content on success', | ||
inject([AsyncTestCompleter], (async) => { | ||
setTemplateCache({'test.html': '<div>Hello</div>'}); | ||
xhr = new CachedXHR(); | ||
xhr.get('test.html') | ||
.then((text) => { | ||
expect(text).toEqual('<div>Hello</div>'); | ||
async.done(); | ||
}); | ||
})); | ||
|
||
it('should reject the Promise on failure', inject([AsyncTestCompleter], (async) => { | ||
xhr = new CachedXHR(); | ||
xhr.get('unknown.html') | ||
.then((text) => { throw new BaseException('Not expected to succeed.'); }) | ||
.catch((error) => { async.done(); }); | ||
})); | ||
|
||
it('should allow fakeAsync Tests to load components with templateUrl synchronously', | ||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => { | ||
let fixture: ComponentFixture; | ||
tcb.createAsync(TestComponent).then((f) => { fixture = f; }); | ||
|
||
// This should initialize the fixture. | ||
tick(); | ||
|
||
expect(fixture.debugElement.children[0].nativeElement).toHaveText('Hello'); | ||
}))); | ||
}); | ||
} | ||
|
||
@Component({selector: 'test-cmp', templateUrl: 'test.html'}) | ||
class TestComponent { | ||
} | ||
|
||
class TestUrlResolver extends UrlResolver { | ||
resolve(baseUrl: string, url: string): string { | ||
// Don't use baseUrl to get the same URL as templateUrl. | ||
// This is to remove any difference between Dart and TS tests. | ||
return url; | ||
} | ||
} |
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
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.
Could you also add a test that creates a component via
fakeAsync
and the cache?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.
Done.