Skip to content

Commit 92fff69

Browse files
committed
Type improvements and renamings
1 parent a36fc33 commit 92fff69

File tree

10 files changed

+64
-62
lines changed

10 files changed

+64
-62
lines changed

src/core/cache/cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export const EXPANDO = "ng";
1717
*
1818
* @type {Map<number, ExpandoStore>}
1919
*/
20-
export const CACHE = new Map();
20+
export const Cache = new Map();

src/core/compile/compile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
import { SCE_CONTEXTS } from "../sce/sce.js";
3636
import { PREFIX_REGEXP } from "../../shared/constants.js";
3737
import { createEventDirective } from "../../directive/events/events.js";
38-
import { CACHE, EXPANDO } from "../cache/cache.js";
38+
import { Cache, EXPANDO } from "../cache/cache.js";
3939
import { Attributes } from "./attributes.js";
4040
import { ngObserveDirective } from "../../directive/observe/observe.js";
4141

@@ -2673,7 +2673,7 @@ export function CompileProvider($provide, $$sanitizeUriProvider) {
26732673
fragment.appendChild(elementsToRemove[i]);
26742674
}
26752675

2676-
if (CACHE.has(firstElementToRemove[EXPANDO])) {
2676+
if (Cache.has(firstElementToRemove[EXPANDO])) {
26772677
// Copy over user data (that includes AngularJS's $scope etc.). Don't copy private
26782678
// data here because there's no public interface in jQuery to do that and copying over
26792679
// event listeners (which is the main use of private data) wouldn't work anyway.

src/core/compile/compile.spec.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
extend,
1414
} from "../../shared/utils.js";
1515
import { countChildScopes, countWatchers } from "../scope/scope.js";
16-
import { CACHE, EXPANDO } from "../cache/cache.js";
16+
import { Cache, EXPANDO } from "../cache/cache.js";
1717
import { wait } from "../../shared/test-utils";
1818

1919
function isUnknownElement(el) {
@@ -4903,36 +4903,36 @@ describe("$compile", () => {
49034903
// We compile the contents of element (i.e. not element itself)
49044904
// Then delete these contents and check the cache has been reset to zero
49054905
// Clear cache
4906-
CACHE.clear();
4906+
Cache.clear();
49074907
window.angular.module("test1", ["ng"]);
49084908
createInjector(["test1"]).invoke(($compile) => {
4909-
expect(CACHE.size).toEqual(0);
4909+
expect(Cache.size).toEqual(0);
49104910
// First with only elements at the top level
49114911
element = JQLite("<div><div></div></div>");
49124912
$compile(element[0].childNodes)($rootScope);
4913-
// expect(CACHE.size).toEqual(2);
4913+
// expect(Cache.size).toEqual(2);
49144914
element.empty();
4915-
expect(CACHE.size).toEqual(0);
4915+
expect(Cache.size).toEqual(0);
49164916

49174917
// Next with non-empty text nodes at the top level
49184918
// (in this case the compiler will wrap them in a <span>)
49194919
element = JQLite("<div>xxx</div>");
49204920
$compile(element[0].childNodes)($rootScope);
49214921
element.empty();
4922-
expect(CACHE.size).toEqual(0);
4922+
expect(Cache.size).toEqual(0);
49234923

49244924
// Next with comment nodes at the top level
49254925
element = JQLite("<div><!-- comment --></div>");
49264926
$compile(element[0].childNodes)($rootScope);
49274927
element.empty();
4928-
expect(CACHE.size).toEqual(0);
4928+
expect(Cache.size).toEqual(0);
49294929

49304930
// Finally with empty text nodes at the top level
49314931
element = JQLite("<div> \n<div></div> </div>");
49324932
$compile(element[0].childNodes)($rootScope);
4933-
//expect(CACHE.size).toEqual(2);
4933+
//expect(Cache.size).toEqual(2);
49344934
element.empty();
4935-
expect(CACHE.size).toEqual(0);
4935+
expect(Cache.size).toEqual(0);
49364936
});
49374937
});
49384938

@@ -13303,49 +13303,49 @@ describe("$compile", () => {
1330313303
});
1330413304

1330513305
it('should not leak if two "element" transclusions are on the same element', () => {
13306-
const cacheSize = CACHE.size;
13306+
const cacheSize = Cache.size;
1330713307

1330813308
element = $compile(
1330913309
'<div><div ng-repeat="x in xs" ng-if="x==1">{{x}}</div></div>',
1331013310
)($rootScope);
13311-
expect(CACHE.size).toEqual(cacheSize);
13311+
expect(Cache.size).toEqual(cacheSize);
1331213312

1331313313
$rootScope.$apply("xs = [0,1]");
13314-
expect(CACHE.size).toEqual(cacheSize);
13314+
expect(Cache.size).toEqual(cacheSize);
1331513315

1331613316
$rootScope.$apply("xs = [0]");
13317-
expect(CACHE.size).toEqual(cacheSize);
13317+
expect(Cache.size).toEqual(cacheSize);
1331813318

1331913319
$rootScope.$apply("xs = []");
13320-
expect(CACHE.size).toEqual(cacheSize);
13320+
expect(Cache.size).toEqual(cacheSize);
1332113321

1332213322
element.remove();
13323-
expect(CACHE.size).toEqual(cacheSize);
13323+
expect(Cache.size).toEqual(cacheSize);
1332413324
});
1332513325

1332613326
it('should not leak if two "element" transclusions are on the same element', () => {
13327-
const cacheSize = CACHE.size;
13327+
const cacheSize = Cache.size;
1332813328
element = $compile(
1332913329
'<div><div ng-repeat="x in xs" ng-if="val">{{x}}</div></div>',
1333013330
)($rootScope);
1333113331

1333213332
$rootScope.$apply("xs = [0,1]");
1333313333
// At this point we have a bunch of comment placeholders but no real transcluded elements
1333413334
// So the cache only contains the root element's data
13335-
expect(CACHE.size).toEqual(cacheSize);
13335+
expect(Cache.size).toEqual(cacheSize);
1333613336

1333713337
$rootScope.$apply("val = true");
1333813338
// Now we have two concrete transcluded elements plus some comments so two more cache items
13339-
expect(CACHE.size).toEqual(cacheSize);
13339+
expect(Cache.size).toEqual(cacheSize);
1334013340

1334113341
$rootScope.$apply("val = false");
1334213342
// Once again we only have comments so no transcluded elements and the cache is back to just
1334313343
// the root element
13344-
expect(CACHE.size).toEqual(cacheSize);
13344+
expect(Cache.size).toEqual(cacheSize);
1334513345

1334613346
element.remove();
1334713347
// Now we've even removed the root element along with its cache
13348-
expect(CACHE.size).toEqual(cacheSize);
13348+
expect(Cache.size).toEqual(cacheSize);
1334913349
});
1335013350

1335113351
// it("should not leak when continuing the compilation of elements on a scope that was destroyed", () => {
@@ -13370,7 +13370,7 @@ describe("$compile", () => {
1337013370
// link: linkFn,
1337113371
// }));
1337213372
// initInjector("test1");
13373-
// const cacheSize = CACHE.size;
13373+
// const cacheSize = Cache.size;
1337413374
// $templateCache.set("red.html", "<p>red</p>");
1337513375
// const template = $compile(
1337613376
// '<div ng-controller="Leak">' +
@@ -13385,7 +13385,7 @@ describe("$compile", () => {
1338513385
// $rootScope.$digest();
1338613386

1338713387
// expect(linkFn).toHaveBeenCalled();
13388-
// expect(CACHE.size).toEqual(cacheSize + 2);
13388+
// expect(Cache.size).toEqual(cacheSize + 2);
1338913389

1339013390
// $templateCache = new Map();
1339113391
// const destroyedScope = $rootScope.$new();
@@ -13414,7 +13414,7 @@ describe("$compile", () => {
1341413414
$rootScope.$apply(`xs = [${xs}]`);
1341513415
firstRepeatedElem = element.children(".ng-scope").eq(0);
1341613416

13417-
privateData = CACHE.get(firstRepeatedElem[0][EXPANDO]);
13417+
privateData = Cache.get(firstRepeatedElem[0][EXPANDO]);
1341813418
expect(privateData.events).toBeDefined();
1341913419

1342013420
expect(privateData.events.click).toBeDefined();
@@ -13430,7 +13430,7 @@ describe("$compile", () => {
1343013430

1343113431
expect(destroyCount).toBe(2);
1343213432
expect(firstRepeatedElem.data("$scope")).not.toBeDefined();
13433-
privateData = CACHE.get(firstRepeatedElem[0][EXPANDO]);
13433+
privateData = Cache.get(firstRepeatedElem[0][EXPANDO]);
1343413434
expect(privateData && privateData.events).not.toBeDefined();
1343513435
}
1343613436

@@ -14290,7 +14290,7 @@ describe("$compile", () => {
1429014290

1429114291
it("should not leak memory with nested transclusion", () => {
1429214292
let size;
14293-
const initialSize = CACHE.size;
14293+
const initialSize = Cache.size;
1429414294

1429514295
element = JQLite(
1429614296
'<div><ul><li ng-repeat="n in nums">{{n}} => <i ng-if="0 === n%2">Even</i><i ng-if="1 === n%2">Odd</i></li></ul></div>',
@@ -14299,14 +14299,14 @@ describe("$compile", () => {
1429914299

1430014300
$rootScope.nums = [0, 1, 2];
1430114301
$rootScope.$apply();
14302-
size = CACHE.size;
14302+
size = Cache.size;
1430314303

1430414304
$rootScope.nums = [3, 4, 5];
1430514305
$rootScope.$apply();
14306-
expect(CACHE.size).toEqual(size);
14306+
expect(Cache.size).toEqual(size);
1430714307

1430814308
element.remove();
14309-
expect(CACHE.size).toEqual(initialSize);
14309+
expect(Cache.size).toEqual(initialSize);
1431014310
});
1431114311
});
1431214312

src/index.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { Angular } from "./loader";
22

33
export const angular = new Angular();
4-
window["angular"] = angular;
4+
document.addEventListener("DOMContentLoaded", () => angular.init(document), {
5+
once: true,
6+
});
57

6-
if (document.readyState === "complete") {
7-
angular.init(document);
8-
} else {
9-
document.addEventListener("DOMContentLoaded", () => {
10-
angular.init(document);
11-
});
12-
}

src/loader.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import {
88
import { JQLite } from "./shared/jqlite/jqlite.js";
99
import { annotate, createInjector } from "./core/di/injector";
1010
import { NgModule } from "./core/di/ng-module";
11-
import { CACHE } from "./core/cache/cache.js";
11+
import { Cache } from "./core/cache/cache.js";
1212
import { publishExternalAPI } from "./public";
1313
import { VERSION } from "./public";
1414
import { services } from "./router/common/coreservices";
1515
import { unnestR } from "./shared/common";
16+
import { EventBus } from "./core/pubsub/pubsub.js";
1617

1718
const ngMinErr = minErr("ng");
1819
const $injectorMinErr = minErr("$injector");
@@ -29,10 +30,13 @@ const modules = {};
2930

3031
export class Angular {
3132
constructor() {
32-
CACHE.clear(); // a ensure new instance of angular gets a clean cache
33+
Cache.clear(); // a ensure new instance of angular gets a clean Cache
3334

3435
/** @type {Map<number, import("./core/cache/cache").ExpandoStore>} */
35-
this.cache = CACHE;
36+
this.cache = Cache;
37+
38+
/** @type {import('./core/pubsub/pubsub.js').PubSub} */
39+
this.eventBus = EventBus;
3640

3741
/** @type {string} */
3842
this.version = VERSION;
@@ -45,6 +49,7 @@ export class Angular {
4549

4650
/** @type {Function} */
4751
this.doBootstrap;
52+
window["angular"] = this;
4853
publishExternalAPI(this);
4954
}
5055

src/shared/jqlite/jqlite.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getNodeName,
1313
shallowCopy,
1414
} from "../utils.js";
15-
import { CACHE, EXPANDO } from "../../core/cache/cache.js";
15+
import { Cache, EXPANDO } from "../../core/cache/cache.js";
1616

1717
/** @type {number} */
1818
let jqId = 1;
@@ -128,7 +128,7 @@ JQLite.prototype.elements = function () {
128128
};
129129

130130
/**
131-
* Remove all child nodes of the set of matched elements from the DOM and clears CACHE data, associated with the node.
131+
* Remove all child nodes of the set of matched elements from the DOM and clears Cache data, associated with the node.
132132
* @returns {JQLite} The current instance of JQLite.
133133
*/
134134
JQLite.prototype.empty = function () {
@@ -750,7 +750,7 @@ export function snakeToCamel(name) {
750750
*/
751751
export function removeElementData(element, name) {
752752
const expandoId = element[EXPANDO];
753-
const expandoStore = expandoId && CACHE.get(expandoId);
753+
const expandoStore = expandoId && Cache.get(expandoId);
754754

755755
if (expandoStore) {
756756
if (name) {
@@ -774,7 +774,7 @@ export function removeElementData(element, name) {
774774
*/
775775
function getExpando(element, createIfNecessary = false) {
776776
let expandoId = element[EXPANDO];
777-
let expandoStore = expandoId && CACHE.get(expandoId);
777+
let expandoStore = expandoId && Cache.get(expandoId);
778778

779779
if (createIfNecessary && !expandoStore) {
780780
element[EXPANDO] = expandoId = jqNextId();
@@ -783,7 +783,7 @@ function getExpando(element, createIfNecessary = false) {
783783
data: {},
784784
handle: null,
785785
};
786-
CACHE.set(expandoId, expandoStore);
786+
Cache.set(expandoId, expandoStore);
787787
}
788788

789789
return expandoStore;
@@ -899,13 +899,13 @@ export function dealoc(element, onlyDescendants) {
899899
*/
900900
function removeIfEmptyData(element) {
901901
const expandoId = element[EXPANDO];
902-
const { events, data } = CACHE.get(expandoId);
902+
const { events, data } = Cache.get(expandoId);
903903

904904
if (
905905
(!data || !Object.keys(data).length) &&
906906
(!events || !Object.keys(events).length)
907907
) {
908-
CACHE.delete(expandoId);
908+
Cache.delete(expandoId);
909909
element[EXPANDO] = undefined; // don't delete DOM expandos. Chrome don't like it
910910
}
911911
}
@@ -1179,7 +1179,7 @@ export function getBooleanAttrName(element, name) {
11791179
*/
11801180
export function cleanElementData(nodes) {
11811181
for (let i = 0, ii = nodes.length; i < ii; i++) {
1182-
const events = (CACHE.get(nodes[i][EXPANDO]) || {}).events;
1182+
const events = (Cache.get(nodes[i][EXPANDO]) || {}).events;
11831183
if (events && events.$destroy) {
11841184
JQLite(nodes[i]).triggerHandler("$destroy");
11851185
}

src/shared/jqlite/jqlite.spec.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Angular } from "../../loader";
1010
import { createInjector } from "../../core/di/injector";
1111
import { equals } from "../utils.js";
1212
import { browserTrigger } from "../test-utils";
13-
import { CACHE, EXPANDO } from "../../core/cache/cache.js";
13+
import { Cache, EXPANDO } from "../../core/cache/cache.js";
1414

1515
describe("jqLite", () => {
1616
let scope;
@@ -532,7 +532,7 @@ describe("jqLite", () => {
532532

533533
it("should not break on cleanElementData(), if element has no data", () => {
534534
const selected = JQLite([a, b, c]);
535-
spyOn(CACHE, "get").and.returnValue(undefined);
535+
spyOn(Cache, "get").and.returnValue(undefined);
536536
expect(() => {
537537
cleanElementData(selected);
538538
}).not.toThrow();
@@ -552,27 +552,27 @@ describe("jqLite", () => {
552552
});
553553

554554
it("should not add to the cache if the node is a comment or text node", () => {
555-
const initial = CACHE.size;
555+
const initial = Cache.size;
556556
const nodes = JQLite("<!-- some comment --> and some text");
557-
expect(CACHE.size).toEqual(initial);
557+
expect(Cache.size).toEqual(initial);
558558
nodes.data("someKey");
559-
expect(CACHE.size).toEqual(initial);
559+
expect(Cache.size).toEqual(initial);
560560
nodes.data("someKey", "someValue");
561-
expect(CACHE.size).toEqual(initial);
561+
expect(Cache.size).toEqual(initial);
562562
});
563563

564564
describe("removeElementData/getOrSetCacheData helpers", () => {
565565
it("should provide the non-wrapped data calls", () => {
566566
const node = document.createElement("div");
567567
document.body.appendChild(node);
568568

569-
expect(CACHE.has(node[EXPANDO])).toBe(false);
569+
expect(Cache.has(node[EXPANDO])).toBe(false);
570570
expect(getOrSetCacheData(node, "foo")).toBeUndefined();
571-
expect(CACHE.has(node[EXPANDO])).toBe(false);
571+
expect(Cache.has(node[EXPANDO])).toBe(false);
572572

573573
getOrSetCacheData(node, "foo", "bar");
574574

575-
expect(CACHE.has(node[EXPANDO])).toBe(true);
575+
expect(Cache.has(node[EXPANDO])).toBe(true);
576576
expect(getOrSetCacheData(node, "foo")).toBe("bar");
577577
expect(JQLite(node).data("foo")).toBe("bar");
578578

@@ -587,7 +587,7 @@ describe("jqLite", () => {
587587
expect(getOrSetCacheData(node, "bar")).toBeUndefined();
588588

589589
JQLite(node).remove();
590-
expect(CACHE.has(node[EXPANDO])).toBe(false);
590+
expect(Cache.has(node[EXPANDO])).toBe(false);
591591
});
592592
});
593593

types/core/cache/cache.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const EXPANDO: "ng";
1515
*
1616
* @type {Map<number, ExpandoStore>}
1717
*/
18-
export const CACHE: Map<number, ExpandoStore>;
18+
export const Cache: Map<number, ExpandoStore>;
1919
export type ExpandoStore = {
2020
data: {
2121
[x: string]: any;

0 commit comments

Comments
 (0)