refactor: remove `global` from facade/lang (#14837)

This commit is contained in:
Miško Hevery 2017-03-01 15:18:10 -08:00 committed by Chuck Jazdzewski
parent b0e0839075
commit 84a65cf788
26 changed files with 88 additions and 123 deletions

View File

@ -14,7 +14,8 @@ import {merge} from 'rxjs/observable/merge';
import {share} from 'rxjs/operator/share'; import {share} from 'rxjs/operator/share';
import {ErrorHandler} from '../src/error_handler'; import {ErrorHandler} from '../src/error_handler';
import {scheduleMicroTask, stringify} from '../src/facade/lang'; import {stringify} from '../src/facade/lang';
import {scheduleMicroTask} from '../src/util';
import {isPromise} from '../src/util/lang'; import {isPromise} from '../src/util/lang';
import {ApplicationInitStatus} from './application_init'; import {ApplicationInitStatus} from './application_init';

View File

@ -6,7 +6,8 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {getSymbolIterator, isJsObject, isPrimitive, looseIdentical} from '../facade/lang'; import {isJsObject, isPrimitive, looseIdentical} from '../facade/lang';
import {getSymbolIterator} from '../util';
export {looseIdentical} from '../facade/lang'; export {looseIdentical} from '../facade/lang';

View File

@ -13,7 +13,7 @@
*/ */
export * from './metadata'; export * from './metadata';
export * from './version'; export * from './version';
export * from './util'; export {Class, ClassDefinition, TypeDecorator} from './util/decorators';
export * from './di'; export * from './di';
export {createPlatform, assertPlatform, destroyPlatform, getPlatform, PlatformRef, ApplicationRef, enableProdMode, isDevMode, createPlatformFactory, NgProbeToken} from './application_ref'; export {createPlatform, assertPlatform, destroyPlatform, getPlatform, PlatformRef, ApplicationRef, enableProdMode, isDevMode, createPlatformFactory, NgProbeToken} from './application_ref';
export {APP_ID, PACKAGE_ROOT_URL, PLATFORM_INITIALIZER, PLATFORM_ID, APP_BOOTSTRAP_LISTENER} from './application_tokens'; export {APP_ID, PACKAGE_ROOT_URL, PLATFORM_INITIALIZER, PLATFORM_ID, APP_BOOTSTRAP_LISTENER} from './application_tokens';

View File

@ -23,5 +23,6 @@ export {ReflectionCapabilities as ɵReflectionCapabilities} from './reflection/r
export {ReflectorReader as ɵReflectorReader} from './reflection/reflector_reader'; export {ReflectorReader as ɵReflectorReader} from './reflection/reflector_reader';
export {GetterFn as ɵGetterFn, MethodFn as ɵMethodFn, SetterFn as ɵSetterFn} from './reflection/types'; export {GetterFn as ɵGetterFn, MethodFn as ɵMethodFn, SetterFn as ɵSetterFn} from './reflection/types';
export {DirectRenderer as ɵDirectRenderer, RenderDebugInfo as ɵRenderDebugInfo} from './render/api'; export {DirectRenderer as ɵDirectRenderer, RenderDebugInfo as ɵRenderDebugInfo} from './render/api';
export {global as ɵglobal} from './util';
export {makeDecorator as ɵmakeDecorator} from './util/decorators'; export {makeDecorator as ɵmakeDecorator} from './util/decorators';
export {isObservable as ɵisObservable, isPromise as ɵisPromise, merge as ɵmerge} from './util/lang'; export {isObservable as ɵisObservable, isPromise as ɵisPromise, merge as ɵmerge} from './util/lang';

View File

@ -9,7 +9,7 @@
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {EventEmitter} from '../event_emitter'; import {EventEmitter} from '../event_emitter';
import {getSymbolIterator} from '../facade/lang'; import {getSymbolIterator} from '../util';
/** /**

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {global} from '../facade/lang'; import {global} from '../util';
/** /**
* A scope function for the Web Tracing Framework (WTF). * A scope function for the Web Tracing Framework (WTF).

View File

@ -6,9 +6,9 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {global, isPresent, stringify} from '../facade/lang'; import {isPresent, stringify} from '../facade/lang';
import {Type, isType} from '../type'; import {Type, isType} from '../type';
import {global} from '../util';
import {PlatformReflectionCapabilities} from './platform_reflection_capabilities'; import {PlatformReflectionCapabilities} from './platform_reflection_capabilities';
import {GetterFn, MethodFn, SetterFn} from './types'; import {GetterFn, MethodFn, SetterFn} from './types';
@ -21,7 +21,7 @@ export const DELEGATE_CTOR =
export class ReflectionCapabilities implements PlatformReflectionCapabilities { export class ReflectionCapabilities implements PlatformReflectionCapabilities {
private _reflect: any; private _reflect: any;
constructor(reflect?: any) { this._reflect = reflect || global.Reflect; } constructor(reflect?: any) { this._reflect = reflect || global['Reflect']; }
isReflectionEnabled(): boolean { return true; } isReflectionEnabled(): boolean { return true; }

View File

@ -7,7 +7,7 @@
*/ */
import {Injectable} from '../di'; import {Injectable} from '../di';
import {scheduleMicroTask} from '../facade/lang'; import {scheduleMicroTask} from '../util';
import {NgZone} from '../zone/ng_zone'; import {NgZone} from '../zone/ng_zone';
/** /**

View File

@ -6,5 +6,42 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
// Public API for util // TODO(jteplitz602): Load WorkerGlobalScope from lib.webworker.d.ts file #3492
export {Class, ClassDefinition, TypeDecorator} from './util/decorators'; declare var WorkerGlobalScope: any /** TODO #9100 */;
// CommonJS / Node have global context exposed as "global" variable.
// We don't want to include the whole node.d.ts this this compilation unit so we'll just fake
// the global "global" var for now.
declare var global: any /** TODO #9100 */;
const __window = typeof window !== 'undefined' && window;
const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope && self;
const __global = typeof global !== 'undefined' && global;
const _global: {[name: string]: any} = __window || __global || __self;
export {_global as global};
// When Symbol.iterator doesn't exist, retrieves the key used in es6-shim
declare const Symbol: any;
let _symbolIterator: any = null;
export function getSymbolIterator(): string|symbol {
if (!_symbolIterator) {
var Symbol = _global['Symbol'];
if (Symbol && Symbol.iterator) {
_symbolIterator = Symbol.iterator;
} else {
// es6-shim specific logic
const keys = Object.getOwnPropertyNames(Map.prototype);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
if (key !== 'entries' && key !== 'size' &&
(Map as any).prototype[key] === Map.prototype['entries']) {
_symbolIterator = key;
}
}
}
}
return _symbolIterator;
}
export function scheduleMicroTask(fn: Function) {
Zone.current.scheduleMicroTask('scheduleMicrotask', fn);
}

View File

@ -6,11 +6,12 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {global, stringify} from '../facade/lang'; import {stringify} from '../facade/lang';
import {Type} from '../type'; import {Type} from '../type';
import {global} from '../util';
let _nextClassId = 0; let _nextClassId = 0;
const Reflect = global.Reflect; const Reflect = global['Reflect'];
/** /**
* Declares the interface to be used with {@link Class}. * Declares the interface to be used with {@link Class}.

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {getSymbolIterator} from '../../src/facade/lang'; import {getSymbolIterator} from '@angular/core/src/util';
export class TestIterable { export class TestIterable {
list: number[]; list: number[];

View File

@ -6,8 +6,8 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {global} from '@angular/common/src/facade/lang';
import {Compiler, SystemJsNgModuleLoader} from '@angular/core'; import {Compiler, SystemJsNgModuleLoader} from '@angular/core';
import {global} from '@angular/core/src/util';
import {async} from '@angular/core/testing'; import {async} from '@angular/core/testing';
import {afterEach, beforeEach, describe, expect, it} from '@angular/core/testing/testing_internal'; import {afterEach, beforeEach, describe, expect, it} from '@angular/core/testing/testing_internal';
@ -24,14 +24,14 @@ export function main() {
describe('SystemJsNgModuleLoader', () => { describe('SystemJsNgModuleLoader', () => {
let oldSystem: any = null; let oldSystem: any = null;
beforeEach(() => { beforeEach(() => {
oldSystem = (global as any).System; oldSystem = global['System'];
(global as any).System = mockSystem({ global['System'] = mockSystem({
'test.ngfactory': 'test.ngfactory':
{'default': 'test module factory', 'NamedNgFactory': 'test NamedNgFactory'}, {'default': 'test module factory', 'NamedNgFactory': 'test NamedNgFactory'},
'prefixed/test/suffixed': {'NamedNgFactory': 'test module factory'} 'prefixed/test/suffixed': {'NamedNgFactory': 'test module factory'}
}); });
}); });
afterEach(() => { (global as any).System = oldSystem; }); afterEach(() => { global['System'] = oldSystem; });
it('loads a default factory by appending the factory suffix', async(() => { it('loads a default factory by appending the factory suffix', async(() => {
const loader = new SystemJsNgModuleLoader(new Compiler()); const loader = new SystemJsNgModuleLoader(new Compiler());

View File

@ -12,7 +12,7 @@ import {Testability} from '@angular/core/src/testability/testability';
import {NgZone} from '@angular/core/src/zone/ng_zone'; import {NgZone} from '@angular/core/src/zone/ng_zone';
import {AsyncTestCompleter, SpyObject, beforeEach, describe, expect, inject, it} from '@angular/core/testing/testing_internal'; import {AsyncTestCompleter, SpyObject, beforeEach, describe, expect, inject, it} from '@angular/core/testing/testing_internal';
import {scheduleMicroTask} from '../../src/facade/lang'; import {scheduleMicroTask} from '../../src/util';

View File

@ -8,15 +8,14 @@
import {Inject} from '@angular/core'; import {Inject} from '@angular/core';
import {reflector} from '@angular/core/src/reflection/reflection'; import {reflector} from '@angular/core/src/reflection/reflection';
import {global} from '@angular/core/src/util';
import {Class, makeDecorator, makePropDecorator} from '@angular/core/src/util/decorators'; import {Class, makeDecorator, makePropDecorator} from '@angular/core/src/util/decorators';
import {global} from '../../src/facade/lang';
class DecoratedParent {} class DecoratedParent {}
class DecoratedChild extends DecoratedParent {} class DecoratedChild extends DecoratedParent {}
export function main() { export function main() {
const Reflect = global.Reflect; const Reflect = global['Reflect'];
const TerminalDecorator = makeDecorator('TerminalDecorator', {terminal: true}); const TerminalDecorator = makeDecorator('TerminalDecorator', {terminal: true});
const TestDecorator = makeDecorator( const TestDecorator = makeDecorator(
@ -161,4 +160,4 @@ export function main() {
}); });
}); });
}); });
} }

View File

@ -10,7 +10,8 @@ import {NgZone} from '@angular/core/src/zone/ng_zone';
import {async, fakeAsync, flushMicrotasks} from '@angular/core/testing'; import {async, fakeAsync, flushMicrotasks} from '@angular/core/testing';
import {AsyncTestCompleter, Log, beforeEach, describe, expect, inject, it, xit} from '@angular/core/testing/testing_internal'; import {AsyncTestCompleter, Log, beforeEach, describe, expect, inject, it, xit} from '@angular/core/testing/testing_internal';
import {browserDetection} from '@angular/platform-browser/testing/browser_util'; import {browserDetection} from '@angular/platform-browser/testing/browser_util';
import {isPresent, scheduleMicroTask} from '../../src/facade/lang'; import {isPresent} from '../../src/facade/lang';
import {scheduleMicroTask} from '../../src/util';
const needsLongerTimers = browserDetection.isSlow || browserDetection.isEdge; const needsLongerTimers = browserDetection.isSlow || browserDetection.isEdge;
const resultTimer = 1000; const resultTimer = 1000;

View File

@ -7,7 +7,6 @@
*/ */
import {ChangeDetectorRef, ComponentRef, DebugElement, ElementRef, NgZone, getDebugNode} from '@angular/core'; import {ChangeDetectorRef, ComponentRef, DebugElement, ElementRef, NgZone, getDebugNode} from '@angular/core';
import {scheduleMicroTask} from './facade/lang';
/** /**
@ -186,3 +185,7 @@ export class ComponentFixture<T> {
} }
} }
} }
function scheduleMicroTask(fn: Function) {
Zone.current.scheduleMicroTask('scheduleMicrotask', fn);
}

View File

@ -7,9 +7,9 @@
*/ */
import {ɵisPromise as isPromise, ɵmerge as merge} from '@angular/core'; import {ɵisPromise as isPromise, ɵmerge as merge} from '@angular/core';
import {global} from '@angular/core/src/util';
import {AsyncTestCompleter} from './async_test_completer'; import {AsyncTestCompleter} from './async_test_completer';
import {global} from './facade/lang';
import {getTestBed, inject} from './test_bed'; import {getTestBed, inject} from './test_bed';
export {AsyncTestCompleter} from './async_test_completer'; export {AsyncTestCompleter} from './async_test_completer';

View File

@ -6,68 +6,11 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
export interface BrowserNodeGlobal {
Object: typeof Object;
Array: typeof Array;
Map: typeof Map;
Set: typeof Set;
Date: DateConstructor;
RegExp: RegExpConstructor;
JSON: typeof JSON;
Math: any; // typeof Math;
assert(condition: any): void;
Reflect: any;
getAngularTestability: Function;
getAllAngularTestabilities: Function;
getAllAngularRootElements: Function;
frameworkStabilizers: Array<Function>;
setTimeout: Function;
clearTimeout: Function;
setInterval: Function;
clearInterval: Function;
encodeURI: Function;
}
// TODO(jteplitz602): Load WorkerGlobalScope from lib.webworker.d.ts file #3492
declare var WorkerGlobalScope: any /** TODO #9100 */;
// CommonJS / Node have global context exposed as "global" variable.
// We don't want to include the whole node.d.ts this this compilation unit so we'll just fake
// the global "global" var for now.
declare var global: any /** TODO #9100 */;
let globalScope: BrowserNodeGlobal;
if (typeof window === 'undefined') {
if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
// TODO: Replace any with WorkerGlobalScope from lib.webworker.d.ts #3492
globalScope = <any>self;
} else {
globalScope = <any>global;
}
} else {
globalScope = <any>window;
}
export function scheduleMicroTask(fn: Function) {
Zone.current.scheduleMicroTask('scheduleMicrotask', fn);
}
// Need to declare a new variable for global here since TypeScript
// exports the original value of the symbol.
const _global: BrowserNodeGlobal = globalScope;
export {_global as global};
export function getTypeNameForDebugging(type: any): string { export function getTypeNameForDebugging(type: any): string {
return type['name'] || typeof type; return type['name'] || typeof type;
} }
// TODO: remove calls to assert in production environment
// Note: Can't just export this and import in in other files
// as `assert` is a reserved keyword in Dart
_global.assert = function assert(condition) {
// TODO: to be fixed properly via #2830, noop for now
};
export function isPresent(obj: any): boolean { export function isPresent(obj: any): boolean {
return obj != null; return obj != null;
} }
@ -150,28 +93,6 @@ export function setValueOnPath(global: any, path: string, value: any) {
obj[parts.shift()] = value; obj[parts.shift()] = value;
} }
// When Symbol.iterator doesn't exist, retrieves the key used in es6-shim
declare const Symbol: any;
let _symbolIterator: any = null;
export function getSymbolIterator(): string|symbol {
if (!_symbolIterator) {
if ((<any>globalScope).Symbol && Symbol.iterator) {
_symbolIterator = Symbol.iterator;
} else {
// es6-shim specific logic
const keys = Object.getOwnPropertyNames(Map.prototype);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
if (key !== 'entries' && key !== 'size' &&
(Map as any).prototype[key] === Map.prototype['entries']) {
_symbolIterator = key;
}
}
}
}
return _symbolIterator;
}
export function isPrimitive(obj: any): boolean { export function isPrimitive(obj: any): boolean {
return !isJsObject(obj); return !isJsObject(obj);
} }

View File

@ -7,7 +7,7 @@
*/ */
import {ResourceLoader} from '@angular/compiler'; import {ResourceLoader} from '@angular/compiler';
import {global} from '../facade/lang'; import {ɵglobal as global} from '@angular/core';
/** /**
* An implementation of ResourceLoader that uses a template cache to avoid doing an actual * An implementation of ResourceLoader that uses a template cache to avoid doing an actual

View File

@ -6,8 +6,9 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {ɵglobal as global} from '@angular/core';
import {setRootDomAdapter} from '../dom/dom_adapter'; import {setRootDomAdapter} from '../dom/dom_adapter';
import {global, isBlank, isPresent, setValueOnPath} from '../facade/lang'; import {isBlank, isPresent, setValueOnPath} from '../facade/lang';
import {GenericBrowserDomAdapter} from './generic_browser_adapter'; import {GenericBrowserDomAdapter} from './generic_browser_adapter';

View File

@ -6,16 +6,16 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {GetTestability, Testability, TestabilityRegistry, setTestabilityGetter} from '@angular/core'; import {GetTestability, Testability, TestabilityRegistry, setTestabilityGetter, ɵglobal as global} from '@angular/core';
import {getDOM} from '../dom/dom_adapter'; import {getDOM} from '../dom/dom_adapter';
import {global, isPresent} from '../facade/lang'; import {isPresent} from '../facade/lang';
export class BrowserGetTestability implements GetTestability { export class BrowserGetTestability implements GetTestability {
static init() { setTestabilityGetter(new BrowserGetTestability()); } static init() { setTestabilityGetter(new BrowserGetTestability()); }
addToWindow(registry: TestabilityRegistry): void { addToWindow(registry: TestabilityRegistry): void {
global.getAngularTestability = (elem: any, findInAncestors: boolean = true) => { global['getAngularTestability'] = (elem: any, findInAncestors: boolean = true) => {
const testability = registry.findTestabilityInTree(elem, findInAncestors); const testability = registry.findTestabilityInTree(elem, findInAncestors);
if (testability == null) { if (testability == null) {
throw new Error('Could not find testability for element.'); throw new Error('Could not find testability for element.');
@ -23,12 +23,12 @@ export class BrowserGetTestability implements GetTestability {
return testability; return testability;
}; };
global.getAllAngularTestabilities = () => registry.getAllTestabilities(); global['getAllAngularTestabilities'] = () => registry.getAllTestabilities();
global.getAllAngularRootElements = () => registry.getAllRootElements(); global['getAllAngularRootElements'] = () => registry.getAllRootElements();
const whenAllStable = (callback: any /** TODO #9100 */) => { const whenAllStable = (callback: any /** TODO #9100 */) => {
const testabilities = global.getAllAngularTestabilities(); const testabilities = global['getAllAngularTestabilities']();
let count = testabilities.length; let count = testabilities.length;
let didWork = false; let didWork = false;
const decrement = function(didWork_: any /** TODO #9100 */) { const decrement = function(didWork_: any /** TODO #9100 */) {

View File

@ -6,12 +6,10 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {ReflectiveInjector} from '@angular/core'; import {ReflectiveInjector, ɵglobal as global} from '@angular/core';
import {ApplicationRef, ApplicationRef_} from '@angular/core/src/application_ref'; import {ApplicationRef, ApplicationRef_} from '@angular/core/src/application_ref';
import {SpyObject} from '@angular/core/testing/testing_internal'; import {SpyObject} from '@angular/core/testing/testing_internal';
import {global} from '../../../src/facade/lang';
export class SpyApplicationRef extends SpyObject { export class SpyApplicationRef extends SpyObject {
constructor() { super(ApplicationRef_); } constructor() { super(ApplicationRef_); }
} }

View File

@ -6,11 +6,9 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {NgZone} from '@angular/core'; import {NgZone, ɵglobal as global} from '@angular/core';
import {ɵgetDOM as getDOM} from '@angular/platform-browser'; import {ɵgetDOM as getDOM} from '@angular/platform-browser';
import {global} from './facade/lang';
export let browserDetection: BrowserDetection; export let browserDetection: BrowserDetection;
export class BrowserDetection { export class BrowserDetection {

View File

@ -7,10 +7,9 @@
*/ */
import {ɵglobal as global} from '@angular/core';
import {ɵgetDOM as getDOM} from '@angular/platform-browser'; import {ɵgetDOM as getDOM} from '@angular/platform-browser';
import {global} from './facade/lang';
/** /**

View File

@ -11,7 +11,6 @@ import {Inject, Injectable, Optional} from '@angular/core';
import {DOCUMENT, ɵgetDOM as getDOM} from '@angular/platform-browser'; import {DOCUMENT, ɵgetDOM as getDOM} from '@angular/platform-browser';
import {Subject} from 'rxjs/Subject'; import {Subject} from 'rxjs/Subject';
import * as url from 'url'; import * as url from 'url';
import {scheduleMicroTask} from './facade/lang';
import {INITIAL_CONFIG, PlatformConfig} from './tokens'; import {INITIAL_CONFIG, PlatformConfig} from './tokens';
@ -88,3 +87,7 @@ export class ServerPlatformLocation implements PlatformLocation {
back(): void { throw new Error('Not implemented'); } back(): void { throw new Error('Not implemented'); }
} }
export function scheduleMicroTask(fn: Function) {
Zone.current.scheduleMicroTask('scheduleMicrotask', fn);
}

View File

@ -8,8 +8,9 @@
const parse5 = require('parse5'); const parse5 = require('parse5');
import {ɵglobal as global} from '@angular/core';
import {ɵDomAdapter as DomAdapter, ɵsetRootDomAdapter as setRootDomAdapter} from '@angular/platform-browser'; import {ɵDomAdapter as DomAdapter, ɵsetRootDomAdapter as setRootDomAdapter} from '@angular/platform-browser';
import {isPresent, isBlank, global, setValueOnPath} from '../src/facade/lang'; import {isPresent, isBlank, setValueOnPath} from '../src/facade/lang';
import {SelectorMatcher, CssSelector} from '@angular/compiler'; import {SelectorMatcher, CssSelector} from '@angular/compiler';
let treeAdapter: any; let treeAdapter: any;