Kristiyan Kostadinov 3c66b100dd perf(common): remove unused methods from DomAdapter (#41102)
The `DomAdapter` is present in all Angular apps and its methods aren't tree shakeable.
These changes remove the methods that either aren't being used anymore or were only
used by our own tests. Note that these changes aren't breaking, because the adapter
is an internal API.

The following methods were removed:
* `getProperty` - only used within our own tests.
* `log` - Guaranteed to be defined on `console`.
* `logGroup` and `logGroupEnd` - Only used in one place. It was in the DomAdapter for built-in null checking.
* `logGroupEnd` - Only used in one place. It was placed in the DomAdapter for built in null checking.
* `performanceNow` - Only used in one place that has to be invoked through the browser console.
* `supportsCookies` - Unused.
* `getCookie` - Unused.
* `getLocation` and `getHistory` - Only used in one place which appears to have access to the DOM
already, because it had direct accesses to `window`. Furthermore, even if this was being used
in a non-browser context already, the `DominoAdapter` was set up to throw an error.

The following APIs were changed to be more compact:
* `supportsDOMEvents` - Changed to a readonly property.
* `remove` - No longer returns the removed node.

PR Close #41102
2021-03-10 11:48:24 -08:00

104 lines
3.3 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* 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 {ɵgetDOM as getDOM} from '@angular/common';
import {Injector, NgModuleRef} from '@angular/core';
import {ArgumentType, initServicesIfNeeded, NodeCheckFn, NodeDef, rootRenderNodes, Services, ViewData, viewDef, ViewDefinition, ViewDefinitionFactory, ViewFlags, ViewUpdateFn} from '@angular/core/src/view/index';
import {TestBed} from '@angular/core/testing';
export function isBrowser() {
return getDOM().supportsDOMEvents;
}
export const ARG_TYPE_VALUES = [ArgumentType.Inline, ArgumentType.Dynamic];
export function checkNodeInlineOrDynamic(
check: NodeCheckFn, view: ViewData, nodeIndex: number, argType: ArgumentType,
values: any[]): any {
switch (argType) {
case ArgumentType.Inline:
return (<any>check)(view, nodeIndex, argType, ...values);
case ArgumentType.Dynamic:
return check(view, nodeIndex, argType, values);
}
}
export function createRootView(
def: ViewDefinition, context?: any, projectableNodes?: any[][],
rootSelectorOrNode?: any): ViewData {
initServicesIfNeeded();
return Services.createRootView(
TestBed.inject(Injector), projectableNodes || [], rootSelectorOrNode, def,
TestBed.inject(NgModuleRef), context);
}
export function createEmbeddedView(parent: ViewData, anchorDef: NodeDef, context?: any): ViewData {
return Services.createEmbeddedView(parent, anchorDef, anchorDef.element!.template !, context);
}
export function compViewDef(
nodes: NodeDef[], updateDirectives?: null|ViewUpdateFn, updateRenderer?: null|ViewUpdateFn,
viewFlags: ViewFlags = ViewFlags.None): ViewDefinition {
const def = viewDef(viewFlags, nodes, updateDirectives, updateRenderer);
def.nodes.forEach((node, index) => {
if (node.nodeIndex !== index) {
throw new Error('nodeIndex should be the same as the index of the node');
}
// This check should be removed when we start reordering nodes at runtime
if (node.checkIndex > -1 && node.checkIndex !== node.nodeIndex) {
throw new Error(`nodeIndex and checkIndex should be the same, got ${node.nodeIndex} !== ${
node.checkIndex}`);
}
});
return def;
}
export function compViewDefFactory(
nodes: NodeDef[], updateDirectives?: null|ViewUpdateFn, updateRenderer?: null|ViewUpdateFn,
viewFlags: ViewFlags = ViewFlags.None): ViewDefinitionFactory {
return () => compViewDef(nodes, updateDirectives, updateRenderer, viewFlags);
}
export function createAndGetRootNodes(
viewDef: ViewDefinition, ctx?: any): {rootNodes: any[], view: ViewData} {
const view = createRootView(viewDef, ctx);
const rootNodes = rootRenderNodes(view);
return {rootNodes, view};
}
let removeNodes: Node[];
beforeEach(() => {
removeNodes = [];
});
afterEach(() => {
removeNodes.forEach((node) => getDOM().remove(node));
});
export function recordNodeToRemove(node: Node) {
removeNodes.push(node);
}
export function callMostRecentEventListenerHandler(spy: any, params: any) {
const mostRecent = spy.calls.mostRecent();
if (!mostRecent) {
return;
}
const obj = mostRecent.object;
const args = mostRecent.args;
const eventName = args[0];
const handler = args[1];
handler && handler.apply(obj, [{type: eventName}]);
}