Skip to content
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

refactor(core): cleanup code with side-effects which was preventing t… #30580

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# Fetch rules_nodejs so we can install our npm dependencies
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "1db950bbd27fb2581866e307c0130983471d4c3cd49c46063a2503ca7b6770a4",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.29.0/rules_nodejs-0.29.0.tar.gz"],
sha256 = "395b7568f20822c13fc5abc65b1eced637446389181fda3a108fdd6ff2cac1e9",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.29.2/rules_nodejs-0.29.2.tar.gz"],
)

# Check the bazel version and download npm dependencies
Expand Down
4 changes: 2 additions & 2 deletions integration/bazel/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# Fetch rules_nodejs so we can install our npm dependencies
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "1db950bbd27fb2581866e307c0130983471d4c3cd49c46063a2503ca7b6770a4",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.29.0/rules_nodejs-0.29.0.tar.gz"],
sha256 = "395b7568f20822c13fc5abc65b1eced637446389181fda3a108fdd6ff2cac1e9",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.29.2/rules_nodejs-0.29.2.tar.gz"],
)

# Fetch sass rules for compiling sass files
Expand Down
146 changes: 1 addition & 145 deletions integration/side-effects/snapshots/core/esm2015.js
Original file line number Diff line number Diff line change
@@ -1,147 +1,3 @@
import { Subject, Subscription } from "rxjs";
import "rxjs";

import "rxjs/operators";

function getGlobal() {
const __globalThis = "undefined" !== typeof globalThis && globalThis;
const __window = "undefined" !== typeof window && window;
const __self = "undefined" !== typeof self && "undefined" !== typeof WorkerGlobalScope && self instanceof WorkerGlobalScope && self;
const __global = "undefined" !== typeof global && global;
return __globalThis || __global || __window || __self;
}

const _global = getGlobal();

let _symbolIterator = null;

function getSymbolIterator() {
if (!_symbolIterator) {
const Symbol = _global["Symbol"];
if (Symbol && Symbol.iterator) _symbolIterator = Symbol.iterator; else {
const keys = Object.getOwnPropertyNames(Map.prototype);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
if ("entries" !== key && "size" !== key && Map.prototype[key] === Map.prototype["entries"]) _symbolIterator = key;
}
}
}
return _symbolIterator;
}

if ("undefined" === typeof ngI18nClosureMode) _global["ngI18nClosureMode"] = "undefined" !== typeof goog && "function" === typeof goog.getMsg;

function flatten(list, dst) {
if (void 0 === dst) dst = list;
for (let i = 0; i < list.length; i++) {
let item = list[i];
if (Array.isArray(item)) {
if (dst === list) dst = list.slice(0, i);
flatten(item, dst);
} else if (dst !== list) dst.push(item);
}
return dst;
}

class EventEmitter extends Subject {
constructor(isAsync = false) {
super();
this.__isAsync = isAsync;
}
emit(value) {
super.next(value);
}
subscribe(generatorOrNext, error, complete) {
let schedulerFn;
let errorFn = err => null;
let completeFn = () => null;
if (generatorOrNext && "object" === typeof generatorOrNext) {
schedulerFn = this.__isAsync ? value => {
setTimeout(() => generatorOrNext.next(value));
} : value => {
generatorOrNext.next(value);
};
if (generatorOrNext.error) errorFn = this.__isAsync ? err => {
setTimeout(() => generatorOrNext.error(err));
} : err => {
generatorOrNext.error(err);
};
if (generatorOrNext.complete) completeFn = this.__isAsync ? () => {
setTimeout(() => generatorOrNext.complete());
} : () => {
generatorOrNext.complete();
};
} else {
schedulerFn = this.__isAsync ? value => {
setTimeout(() => generatorOrNext(value));
} : value => {
generatorOrNext(value);
};
if (error) errorFn = this.__isAsync ? err => {
setTimeout(() => error(err));
} : err => {
error(err);
};
if (complete) completeFn = this.__isAsync ? () => {
setTimeout(() => complete());
} : () => {
complete();
};
}
const sink = super.subscribe(schedulerFn, errorFn, completeFn);
if (generatorOrNext instanceof Subscription) generatorOrNext.add(sink);
return sink;
}
}

class QueryList {
constructor() {
this.dirty = true;
this._results = [];
this.changes = new EventEmitter();
this.length = 0;
}
map(fn) {
return this._results.map(fn);
}
filter(fn) {
return this._results.filter(fn);
}
find(fn) {
return this._results.find(fn);
}
reduce(fn, init) {
return this._results.reduce(fn, init);
}
forEach(fn) {
this._results.forEach(fn);
}
some(fn) {
return this._results.some(fn);
}
toArray() {
return this._results.slice();
}
[getSymbolIterator()]() {
return this._results[getSymbolIterator()]();
}
toString() {
return this._results.toString();
}
reset(resultsTree) {
this._results = flatten(resultsTree);
this.dirty = false;
this.length = this._results.length;
this.last = this._results[this.length - 1];
this.first = this._results[0];
}
notifyOnChanges() {
this.changes.emit(this);
}
setDirty() {
this.dirty = true;
}
destroy() {
this.changes.complete();
this.changes.unsubscribe();
}
}
12 changes: 0 additions & 12 deletions integration/side-effects/snapshots/core/esm5.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,3 @@ import "tslib";
import "rxjs";

import "rxjs/operators";

function getGlobal() {
var __globalThis = "undefined" !== typeof globalThis && globalThis;
var __window = "undefined" !== typeof window && window;
var __self = "undefined" !== typeof self && "undefined" !== typeof WorkerGlobalScope && self instanceof WorkerGlobalScope && self;
var __global = "undefined" !== typeof global && global;
return __globalThis || __global || __window || __self;
}

var _global = getGlobal();

if ("undefined" === typeof ngI18nClosureMode) _global["ngI18nClosureMode"] = "undefined" !== typeof goog && "function" === typeof goog.getMsg;
1 change: 0 additions & 1 deletion modules/benchmarks/src/expanding_rows/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ <h1>Change Detection Benchmark</h1>
<benchmark-root>loading...</benchmark-root>

<script>
window.ngDevMode = location.hash == '#ngDevMode';
addEventListener('DOMContentLoaded', () => {
// DevServer has automatic bootstrap code, so if we already have <scripts> than we don't need to bootstrap
var alreadyBootstraped = document.querySelectorAll('script').length > 1; // 1 for ourselves
Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/linker/query_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {EventEmitter} from '../event_emitter';
import {flatten} from '../util/array_utils';
import {getSymbolIterator} from '../util/symbol';

function symbolIterator<T>(this: QueryList<T>): Iterator<T> {
return ((this as any as{_results: Array<T>})._results as any)[getSymbolIterator()]();
}

/**
* An unmodifiable list of items that Angular keeps up to date when the state
Expand Down Expand Up @@ -50,6 +53,16 @@ export class QueryList<T>/* implements Iterable<T> */ {
// TODO(issue/24571): remove '!'.
readonly last !: T;

constructor() {
// This function should be declared on the prototype, but doing so there will cause the class
// declaration to have side-effects and become not tree-shakable. For this reason we do it in
// the constructor.
// [getSymbolIterator()](): Iterator<T> { ... }
const symbol = getSymbolIterator();
const proto = QueryList.prototype as any;
if (!proto[symbol]) proto[symbol] = symbolIterator;
}

/**
* See
* [Array.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
Expand Down Expand Up @@ -99,8 +112,6 @@ export class QueryList<T>/* implements Iterable<T> */ {
*/
toArray(): T[] { return this._results.slice(); }

[getSymbolIterator()](): Iterator<T> { return (this._results as any)[getSymbolIterator()](); }

toString(): string { return this._results.toString(); }

/**
Expand Down
22 changes: 9 additions & 13 deletions packages/core/src/util/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ declare var global: any /** TODO #9100 */;
// Not yet available in TypeScript: https://github.com/Microsoft/TypeScript/pull/29332
declare var globalThis: any /** TODO #9100 */;

function getGlobal(): any {
const __globalThis = typeof globalThis !== 'undefined' && globalThis;
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 __globalThis = typeof globalThis !== 'undefined' && globalThis;
const __window = typeof window !== 'undefined' && window;
const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
self instanceof WorkerGlobalScope && self;
const __global = typeof global !== 'undefined' && global;

// Always use __globalThis if available, which is the spec-defined global variable across all
// environments, then fallback to __global first, because in Node tests both __global and
// __window may be defined and _global should be __global in that case.
return __globalThis || __global || __window || __self;
}

const _global = getGlobal();
// Always use __globalThis if available, which is the spec-defined global variable across all
// environments, then fallback to __global first, because in Node tests both __global and
// __window may be defined and _global should be __global in that case.
const _global = __globalThis || __global || __window || __self;

/**
* Attention: whenever providing a new value, be sure to add an
Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/util/ng_i18n_closure_mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ if (typeof ngI18nClosureMode === 'undefined') {
// These property accesses can be ignored because ngI18nClosureMode will be set to false
// when optimizing code and the whole if statement will be dropped.
// Make sure to refer to ngI18nClosureMode as ['ngI18nClosureMode'] for closure.
// tslint:disable-next-line:no-toplevel-property-access
global['ngI18nClosureMode'] =
// TODO(FW-1250): validate that this actually, you know, works.
// tslint:disable-next-line:no-toplevel-property-access
typeof goog !== 'undefined' && typeof goog.getMsg === 'function';
// NOTE: we need to have it in IIFE so that the tree-shaker is happy.
(function() {
mhevery marked this conversation as resolved.
Show resolved Hide resolved
// tslint:disable-next-line:no-toplevel-property-access
mhevery marked this conversation as resolved.
Show resolved Hide resolved
global['ngI18nClosureMode'] =
// TODO(FW-1250): validate that this actually, you know, works.
// tslint:disable-next-line:no-toplevel-property-access
typeof goog !== 'undefined' && typeof goog.getMsg === 'function';
})();
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,21 @@
{
"name": "ViewEncapsulation"
},
{
"name": "__global"
},
{
"name": "__globalThis"
},
{
"name": "__self"
},
{
"name": "__values"
},
{
"name": "__window"
},
{
"name": "_currentNamespace"
},
Expand Down Expand Up @@ -359,9 +371,6 @@
{
"name": "getElementDepthCount"
},
{
"name": "getGlobal"
},
{
"name": "getGuardMask"
},
Expand Down
15 changes: 12 additions & 3 deletions packages/core/test/bundling/hello_world/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,21 @@
{
"name": "ViewEncapsulation"
},
{
"name": "__global"
},
{
"name": "__globalThis"
},
{
"name": "__self"
},
{
"name": "__values"
},
{
"name": "__window"
},
{
"name": "_global"
},
Expand Down Expand Up @@ -251,9 +263,6 @@
{
"name": "getDirectiveDef"
},
{
"name": "getGlobal"
},
{
"name": "getHighestElementOrICUContainer"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@
{
"name": "_currentInjector"
},
{
"name": "_global"
},
{
"name": "catchInjectorError"
},
Expand All @@ -119,9 +116,6 @@
{
"name": "getClosureSafeProperty"
},
{
"name": "getGlobal"
},
{
"name": "getInheritedInjectableDef"
},
Expand Down