fix(platform-browser-dynamic): Rename CACHED_TEMPLATE_PROVIDER to RESOURCE_CACHE_PROVIDER (#10866)
* fix(platform-browser-dynamic): Rename CACHED_TEMPLATE_PROVIDER to RESOURCE_CACHE_PROVIDER Closes #9741 BREAKING CHANGE: `CACHED_TEMPLATE_PROVIDER` is now renamed to `RESOURCE_CACHE_PROVIDER` Before: ```js import {CACHED_TEMPLATE_PROVIDER} from '@angular/platform-browser-dynamic'; ``` After: ```js import {RESOURCE_CACHE_PROVIDER} from '@angular/platform-browser-dynamic'; ``` * Rename XHR -> ResourceLoader
This commit is contained in:
@ -6,21 +6,22 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {XHR, analyzeAppProvidersForDeprecatedConfiguration, platformCoreDynamic} from '@angular/compiler';
|
||||
import {ResourceLoader, analyzeAppProvidersForDeprecatedConfiguration, platformCoreDynamic} from '@angular/compiler';
|
||||
import {ApplicationRef, COMPILER_OPTIONS, CUSTOM_ELEMENTS_SCHEMA, CompilerFactory, CompilerOptions, ComponentRef, NgModule, PlatformRef, Provider, Type, createPlatformFactory} from '@angular/core';
|
||||
import {BrowserModule, WORKER_SCRIPT, WorkerAppModule, platformWorkerUi} from '@angular/platform-browser';
|
||||
|
||||
import {Console} from './core_private';
|
||||
import {INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS} from './src/platform_providers';
|
||||
import {CachedXHR} from './src/xhr/xhr_cache';
|
||||
import {XHRImpl} from './src/xhr/xhr_impl';
|
||||
import {CachedResourceLoader} from './src/resource_loader/resource_loader_cache';
|
||||
import {ResourceLoaderImpl} from './src/resource_loader/resource_loader_impl';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @experimental
|
||||
*/
|
||||
export const CACHED_TEMPLATE_PROVIDER: Provider[] = [{provide: XHR, useClass: CachedXHR}];
|
||||
export const RESOURCE_CACHE_PROVIDER: Provider[] =
|
||||
[{provide: ResourceLoader, useClass: CachedResourceLoader}];
|
||||
|
||||
/**
|
||||
* @experimental API related to bootstrapping are still under review.
|
||||
@ -46,12 +47,12 @@ export function bootstrapWorkerUi(
|
||||
/**
|
||||
* @experimental API related to bootstrapping are still under review.
|
||||
*/
|
||||
export const platformWorkerAppDynamic =
|
||||
createPlatformFactory(platformCoreDynamic, 'workerAppDynamic', [{
|
||||
provide: COMPILER_OPTIONS,
|
||||
useValue: {providers: [{provide: XHR, useClass: XHRImpl}]},
|
||||
multi: true
|
||||
}]);
|
||||
export const platformWorkerAppDynamic = createPlatformFactory(
|
||||
platformCoreDynamic, 'workerAppDynamic', [{
|
||||
provide: COMPILER_OPTIONS,
|
||||
useValue: {providers: [{provide: ResourceLoader, useClass: ResourceLoaderImpl}]},
|
||||
multi: true
|
||||
}]);
|
||||
|
||||
function normalizeArray(arr: any[]): any[] {
|
||||
return arr ? arr : [];
|
||||
|
@ -6,18 +6,18 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {XHR} from '@angular/compiler';
|
||||
import {ResourceLoader} from '@angular/compiler';
|
||||
import {COMPILER_OPTIONS} from '@angular/core';
|
||||
|
||||
import {INTERNAL_BROWSER_PLATFORM_PROVIDERS} from '../platform_browser_private';
|
||||
|
||||
import {XHRImpl} from './xhr/xhr_impl';
|
||||
import {ResourceLoaderImpl} from './resource_loader/resource_loader_impl';
|
||||
|
||||
export const INTERNAL_BROWSER_DYNAMIC_PLATFORM_PROVIDERS: any[] = [
|
||||
INTERNAL_BROWSER_PLATFORM_PROVIDERS,
|
||||
{
|
||||
provide: COMPILER_OPTIONS,
|
||||
useValue: {providers: [{provide: XHR, useClass: XHRImpl}]},
|
||||
useValue: {providers: [{provide: ResourceLoader, useClass: ResourceLoaderImpl}]},
|
||||
multi: true
|
||||
},
|
||||
];
|
||||
|
@ -6,25 +6,26 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {XHR} from '@angular/compiler';
|
||||
import {ResourceLoader} from '@angular/compiler';
|
||||
import {BaseException} from '@angular/core';
|
||||
import {global} from '../facade/lang';
|
||||
|
||||
/**
|
||||
* An implementation of XHR that uses a template cache to avoid doing an actual
|
||||
* XHR.
|
||||
* An implementation of ResourceLoader that uses a template cache to avoid doing an actual
|
||||
* ResourceLoader.
|
||||
*
|
||||
* The template cache needs to be built and loaded into window.$templateCache
|
||||
* via a separate mechanism.
|
||||
*/
|
||||
export class CachedXHR extends XHR {
|
||||
export class CachedResourceLoader extends ResourceLoader {
|
||||
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.');
|
||||
throw new BaseException(
|
||||
'CachedResourceLoader: Template cache was not found in $templateCache.');
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,7 +33,8 @@ export class CachedXHR extends XHR {
|
||||
if (this._cache.hasOwnProperty(url)) {
|
||||
return Promise.resolve(this._cache[url]);
|
||||
} else {
|
||||
return <Promise<any>>Promise.reject('CachedXHR: Did not find cached template for ' + url);
|
||||
return <Promise<any>>Promise.reject(
|
||||
'CachedResourceLoader: Did not find cached template for ' + url);
|
||||
}
|
||||
}
|
||||
}
|
@ -5,13 +5,13 @@
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
import {XHR} from '@angular/compiler';
|
||||
import {ResourceLoader} from '@angular/compiler';
|
||||
import {Injectable} from '@angular/core';
|
||||
|
||||
import {isPresent} from '../facade/lang';
|
||||
|
||||
@Injectable()
|
||||
export class XHRImpl extends XHR {
|
||||
export class ResourceLoaderImpl extends ResourceLoader {
|
||||
get(url: string): Promise<string> {
|
||||
var resolve: (result: any) => void;
|
||||
var reject: (error: any) => void;
|
||||
@ -25,7 +25,8 @@ export class XHRImpl extends XHR {
|
||||
|
||||
xhr.onload = function() {
|
||||
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
|
||||
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
|
||||
// response/responseType properties were introduced in ResourceLoader Level2 spec (supported
|
||||
// by IE10)
|
||||
var response = isPresent(xhr.response) ? xhr.response : xhr.responseText;
|
||||
|
||||
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
|
@ -6,29 +6,29 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {UrlResolver, XHR} from '@angular/compiler';
|
||||
import {ResourceLoader, UrlResolver} from '@angular/compiler';
|
||||
import {BaseException, Component} from '@angular/core';
|
||||
import {TestBed, fakeAsync, flushMicrotasks, tick} from '@angular/core/testing';
|
||||
import {AsyncTestCompleter, TestComponentBuilder, beforeEach, beforeEachProviders, ddescribe, describe, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
||||
import {expect} from '@angular/platform-browser/testing/matchers';
|
||||
|
||||
import {CachedXHR} from '../../src/xhr/xhr_cache';
|
||||
import {CachedResourceLoader} from '../../src/resource_loader/resource_loader_cache';
|
||||
|
||||
import {setTemplateCache} from './xhr_cache_setter';
|
||||
import {setTemplateCache} from './resource_loader_cache_setter';
|
||||
|
||||
export function main() {
|
||||
describe('CachedXHR', () => {
|
||||
var xhr: CachedXHR;
|
||||
describe('CachedResourceLoader', () => {
|
||||
var xhr: CachedResourceLoader;
|
||||
|
||||
function createCachedXHR(): CachedXHR {
|
||||
function createCachedResourceLoader(): CachedResourceLoader {
|
||||
setTemplateCache({'test.html': '<div>Hello</div>'});
|
||||
return new CachedXHR();
|
||||
return new CachedResourceLoader();
|
||||
}
|
||||
beforeEach(() => {
|
||||
TestBed.configureCompiler({
|
||||
providers: [
|
||||
{provide: UrlResolver, useClass: TestUrlResolver},
|
||||
{provide: XHR, useFactory: createCachedXHR}
|
||||
{provide: ResourceLoader, useFactory: createCachedResourceLoader}
|
||||
]
|
||||
});
|
||||
});
|
||||
@ -36,14 +36,14 @@ export function main() {
|
||||
it('should throw exception if $templateCache is not found', () => {
|
||||
setTemplateCache(null);
|
||||
expect(() => {
|
||||
xhr = new CachedXHR();
|
||||
}).toThrowError('CachedXHR: Template cache was not found in $templateCache.');
|
||||
xhr = new CachedResourceLoader();
|
||||
}).toThrowError('CachedResourceLoader: Template cache was not found in $templateCache.');
|
||||
});
|
||||
|
||||
it('should resolve the Promise with the cached file content on success',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
setTemplateCache({'test.html': '<div>Hello</div>'});
|
||||
xhr = new CachedXHR();
|
||||
xhr = new CachedResourceLoader();
|
||||
xhr.get('test.html').then((text) => {
|
||||
expect(text).toEqual('<div>Hello</div>');
|
||||
async.done();
|
||||
@ -52,7 +52,7 @@ export function main() {
|
||||
|
||||
it('should reject the Promise on failure',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
xhr = new CachedXHR();
|
||||
xhr = new CachedResourceLoader();
|
||||
xhr.get('unknown.html')
|
||||
.then((text) => { throw new BaseException('Not expected to succeed.'); })
|
||||
.catch((error) => { async.done(); });
|
@ -7,11 +7,11 @@
|
||||
*/
|
||||
|
||||
import {AsyncTestCompleter, beforeEach, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
||||
import {XHRImpl} from '../../src/xhr/xhr_impl';
|
||||
import {ResourceLoaderImpl} from '../../src/resource_loader/resource_loader_impl';
|
||||
|
||||
export function main() {
|
||||
describe('XHRImpl', () => {
|
||||
var xhr: XHRImpl;
|
||||
describe('ResourceLoaderImpl', () => {
|
||||
var resourceLoader: ResourceLoaderImpl;
|
||||
|
||||
// TODO(juliemr): This file currently won't work with dart unit tests run using
|
||||
// exclusive it or describe (iit or ddescribe). This is because when
|
||||
@ -22,11 +22,11 @@ export function main() {
|
||||
var url200 = '/base/modules/@angular/platform-browser/test/browser/static_assets/200.html';
|
||||
var url404 = '/bad/path/404.html';
|
||||
|
||||
beforeEach(() => { xhr = new XHRImpl(); });
|
||||
beforeEach(() => { resourceLoader = new ResourceLoaderImpl(); });
|
||||
|
||||
it('should resolve the Promise with the file content on success',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
xhr.get(url200).then((text) => {
|
||||
resourceLoader.get(url200).then((text) => {
|
||||
expect(text.trim()).toEqual('<p>hey</p>');
|
||||
async.done();
|
||||
});
|
||||
@ -34,7 +34,7 @@ export function main() {
|
||||
|
||||
it('should reject the Promise on failure',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
xhr.get(url404).catch((e) => {
|
||||
resourceLoader.get(url404).catch((e) => {
|
||||
expect(e).toEqual(`Failed to load ${url404}`);
|
||||
async.done();
|
||||
return null;
|
@ -6,11 +6,11 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {XHR} from '@angular/compiler';
|
||||
import {ResourceLoader} from '@angular/compiler';
|
||||
import {Component} from '@angular/core';
|
||||
import {TestBed, async, fakeAsync, inject, tick} from '@angular/core/testing';
|
||||
|
||||
import {XHRImpl} from '../src/xhr/xhr_impl';
|
||||
import {ResourceLoaderImpl} from '../src/resource_loader/resource_loader_impl';
|
||||
|
||||
|
||||
|
||||
@ -46,12 +46,13 @@ export function main() {
|
||||
|
||||
afterEach(() => { expect(actuallyDone).toEqual(true); });
|
||||
|
||||
it('should run async tests with XHRs', async(() => {
|
||||
var xhr = new XHRImpl();
|
||||
xhr.get('/base/modules/@angular/platform-browser/test/static_assets/test.html')
|
||||
it('should run async tests with ResourceLoaders', async(() => {
|
||||
var resourceLoader = new ResourceLoaderImpl();
|
||||
resourceLoader
|
||||
.get('/base/modules/@angular/platform-browser/test/static_assets/test.html')
|
||||
.then(() => { actuallyDone = true; });
|
||||
}),
|
||||
10000); // Long timeout here because this test makes an actual XHR.
|
||||
10000); // Long timeout here because this test makes an actual ResourceLoader.
|
||||
});
|
||||
|
||||
describe('using the test injector with the inject helper', () => {
|
||||
@ -61,8 +62,10 @@ export function main() {
|
||||
{providers: [{provide: FancyService, useValue: new FancyService()}]});
|
||||
});
|
||||
|
||||
it('provides a real XHR instance',
|
||||
inject([XHR], (xhr: XHR) => { expect(xhr instanceof XHRImpl).toBeTruthy(); }));
|
||||
it('provides a real ResourceLoader instance',
|
||||
inject([ResourceLoader], (resourceLoader: ResourceLoader) => {
|
||||
expect(resourceLoader instanceof ResourceLoaderImpl).toBeTruthy();
|
||||
}));
|
||||
|
||||
it('should allow the use of fakeAsync',
|
||||
fakeAsync(inject([FancyService], (service: any /** TODO #9100 */) => {
|
||||
@ -96,7 +99,7 @@ export function main() {
|
||||
|
||||
var restoreJasmineIt = () => { jasmine.getEnv().it = originalJasmineIt; };
|
||||
|
||||
it('should fail when an XHR fails', (done: any /** TODO #9100 */) => {
|
||||
it('should fail when an ResourceLoader fails', (done: any /** TODO #9100 */) => {
|
||||
var itPromise = patchJasmineIt();
|
||||
|
||||
it('should fail with an error from a promise', async(() => {
|
||||
@ -125,7 +128,8 @@ export function main() {
|
||||
.toEqual('from external template\n');
|
||||
});
|
||||
}),
|
||||
10000); // Long timeout here because this test makes an actual XHR, and is slow on Edge.
|
||||
10000); // Long timeout here because this test makes an actual ResourceLoader, and is slow
|
||||
// on Edge.
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user