Skip to content

Commit

Permalink
refactor: cleanup unnecessary exports in kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Sep 19, 2022
1 parent b299e7b commit 045d80d
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 183 deletions.
6 changes: 0 additions & 6 deletions packages/aurelia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,8 @@ export {
kebabCase,
pascalCase,
toArray,
// nextId,
// resetId,
// mergeDistinct,
// isNumberOrBigInt,
// isStringOrDate,
bound,
// mergeArrays,
// mergeObjects,
// firstDefined,
// getPrototypeChain,
} from '@aurelia/kernel';
Expand Down
125 changes: 1 addition & 124 deletions packages/kernel/src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { emptyArray } from './platform';
import { Constructable, Overwrite } from './interfaces';
import { createObject } from './utilities';

Expand Down Expand Up @@ -42,37 +41,6 @@ export function isArrayIndex(value: unknown): value is number | string {
}
}

/**
* Determines if the value passed is a number or bigint for parsing purposes
*
* @param value - Value to evaluate
*/
export function isNumberOrBigInt(value: unknown): value is number | bigint {
switch (typeof value) {
case 'number':
case 'bigint':
return true;
default:
return false;
}
}

/**
* Determines if the value passed is a string or Date for parsing purposes
*
* @param value - Value to evaluate
*/
export function isStringOrDate(value: unknown): value is string | Date {
switch (typeof value) {
case 'string':
return true;
case 'object':
return value instanceof Date;
default:
return false;
}
}

/**
* Base implementation of camel and kebab cases
*/
Expand Down Expand Up @@ -252,7 +220,7 @@ export const kebabCase = (function () {
*/
export function toArray<T = unknown>(input: ArrayLike<T>): T[] {
// benchmark: http://jsben.ch/xjsyF
const { length } = input;
const length = input.length;
const arr = Array(length) as T[];
let i = 0;
for (; i < length; ++i) {
Expand All @@ -261,80 +229,6 @@ export function toArray<T = unknown>(input: ArrayLike<T>): T[] {
return arr;
}

const ids: Record<string, number> = {};

/**
* Retrieve the next ID in a sequence for a given string, starting with `1`.
*
* Used by Aurelia to assign unique ID's to controllers and resources.
*
* Aurelia will always prepend the context name with `au$`, so as long as you avoid
* using that convention you should be safe from collisions.
*/
export function nextId(context: string): number {
if (ids[context] === void 0) {
ids[context] = 0;
}
return ++ids[context];
}

/**
* Reset the ID for the given string, so that `nextId` will return `1` again for the next call.
*
* Used by Aurelia to reset ID's in between unit tests.
*/
export function resetId(context: string): void {
ids[context] = 0;
}

/**
* Efficiently merge and deduplicate the (primitive) values in two arrays.
*
* Does not deduplicate existing values in the first array.
*
* Guards against null or undefined arrays.
*
* Returns `emptyArray` if both arrays are either `null`, `undefined` or `emptyArray`
*
* @param slice - If `true`, always returns a new array copy (unless neither array is/has a value)
*/
export function mergeDistinct<T>(
arr1: readonly T[] | T[] | null | undefined,
arr2: readonly T[] | T[] | null | undefined,
slice: boolean,
): T[] {
if (arr1 === void 0 || arr1 === null || arr1 === emptyArray) {
if (arr2 === void 0 || arr2 === null || arr2 === emptyArray) {
return emptyArray as T[];
} else {
return slice ? arr2.slice(0) : arr2 as T[];
}
} else if (arr2 === void 0 || arr2 === null || arr2 === emptyArray) {
return slice ? arr1.slice(0) : arr1 as T[];
}

const lookup: Record<string, true | undefined> = {};
const arr3 = slice ? arr1.slice(0) : arr1 as (readonly T[]) & T[];

let len1 = arr1.length;
let len2 = arr2.length;

while (len1-- > 0) {
lookup[arr1[len1] as unknown as string] = true;
}

let item;
while (len2-- > 0) {
item = arr2[len2];
if (lookup[item as unknown as string] === void 0) {
arr3.push(item);
lookup[item as unknown as string] = true;
}
}

return arr3;
}

/**
* Decorator. (lazily) bind the method to the class instance on first call.
*/
Expand Down Expand Up @@ -376,23 +270,6 @@ export function mergeArrays<T>(...arrays: (readonly T[] | undefined)[]): T[] {
return result;
}

export function mergeObjects<T extends object>(...objects: readonly (T | undefined)[]): T {
const result: T = {} as unknown as T;
const objectsLen = objects.length;
let object: T | undefined;
let key: keyof T;
let i = 0;
for (; objectsLen > i; ++i) {
object = objects[i];
if (object !== void 0) {
for (key in object) {
result[key] = object[key];
}
}
}
return result;
}

export function firstDefined<T>(...values: readonly (T | undefined)[]): T {
const len = values.length;
let value: T | undefined;
Expand Down
6 changes: 0 additions & 6 deletions packages/kernel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,8 @@ export {
kebabCase,
pascalCase,
toArray,
nextId,
resetId,
mergeDistinct,
isNumberOrBigInt,
isStringOrDate,
bound,
mergeArrays,
mergeObjects,
firstDefined,
getPrototypeChain,
isNativeFunction,
Expand Down
11 changes: 8 additions & 3 deletions packages/runtime-html/src/resources/custom-attributes/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ export class Focus implements ICustomAttributeViewModel {
* @internal
*/
private _needsApply: boolean = false;
/** @internal */ private readonly _element: INode<HTMLElement>;
/** @internal */ private readonly _platform: IPlatform;

public constructor(
private readonly _element: INode<HTMLElement>,
private readonly _platform: IPlatform,
) {}
element: INode<HTMLElement>,
platform: IPlatform,
) {
this._element = element;
this._platform = platform;
}

public binding(): void {
this.valueChanged();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Constructable, nextId, onResolve } from '@aurelia/kernel';
import { Constructable, onResolve } from '@aurelia/kernel';
import { createElement, RenderPlan } from '../../create-element';
import { HydrateElementInstruction, IInstruction } from '../../renderer';
import { IPlatform } from '../../platform';
Expand Down Expand Up @@ -27,7 +27,6 @@ function toLookup(

export class AuRender implements ICustomElementViewModel {
/** @internal */ protected static inject = [IPlatform, IInstruction, IHydrationContext, IRendering];
public readonly id: number = nextId('au$component');

@bindable
public component?: MaybeSubjectPromise = void 0;
Expand Down
21 changes: 10 additions & 11 deletions packages/runtime-html/src/resources/template-controllers/if.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
import { nextId, onResolve } from '@aurelia/kernel';
import { onResolve } from '@aurelia/kernel';
import { IRenderLocation } from '../../dom';
import { IViewFactory } from '../../templating/view';
import { templateController } from '../custom-attribute';
Expand All @@ -13,8 +13,6 @@ import type { INode } from '../../dom';
export class If implements ICustomAttributeViewModel {
/** @internal */ protected static inject = [IViewFactory, IRenderLocation, IWorkTracker];

public readonly id: number = nextId('au$component');

public elseFactory?: IViewFactory = void 0;
public elseView?: ISyntheticView = void 0;
public ifView?: ISyntheticView = void 0;
Expand Down Expand Up @@ -178,12 +176,13 @@ export class If implements ICustomAttributeViewModel {
templateController('if')(If);

export class Else implements ICustomAttributeViewModel {
public static inject = [IViewFactory];
public readonly id: number = nextId('au$component');
/** @internal */ public static inject = [IViewFactory];

public constructor(
private readonly factory: IViewFactory,
) {}
/** @internal */ private readonly _factory: IViewFactory;

public constructor(factory: IViewFactory) {
this._factory = factory;
}

public link(
controller: IHydratableController,
Expand All @@ -194,12 +193,12 @@ export class Else implements ICustomAttributeViewModel {
const children = controller.children!;
const ifBehavior: If | ICustomAttributeController = children[children.length - 1] as If | ICustomAttributeController;
if (ifBehavior instanceof If) {
ifBehavior.elseFactory = this.factory;
ifBehavior.elseFactory = this._factory;
} else if (ifBehavior.viewModel instanceof If) {
ifBehavior.viewModel.elseFactory = this.factory;
ifBehavior.viewModel.elseFactory = this._factory;
} else {
if (__DEV__)
throw new Error(`AUR0810: Unsupported If behavior`); // TODO: create error code
throw new Error(`AUR0810: Unsupported If behavior`);
else
throw new Error(`AUR0810`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nextId, onResolve } from '@aurelia/kernel';
import { onResolve } from '@aurelia/kernel';
import { IRenderLocation, setEffectiveParentNode } from '../../dom';
import { IPlatform } from '../../platform';
import { IViewFactory } from '../../templating/view';
Expand All @@ -17,8 +17,6 @@ export class Portal<T extends Node & ParentNode = Node & ParentNode> implements

public readonly $controller!: ICustomAttributeController<this>;

public readonly id: number = nextId('au$component');

@bindable({ primary: true })
public target: PortalTarget;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Task, TaskAbortError, TaskStatus } from '@aurelia/platform';
import { ILogger, nextId, onResolve, resolveAll } from '@aurelia/kernel';
import { ILogger, onResolve, resolveAll } from '@aurelia/kernel';
import { Scope } from '@aurelia/runtime';
import { bindable } from '../../bindable';
import { INode, IRenderLocation } from '../../dom';
Expand All @@ -23,7 +23,6 @@ import { isPromise } from '../../utilities';

@templateController('promise')
export class PromiseTemplateController implements ICustomAttributeViewModel {
public readonly id: number = nextId('au$component');
public readonly $controller!: ICustomAttributeController<this>; // This is set by the controller after this instance is constructed
private view!: ISyntheticView;

Expand Down Expand Up @@ -166,7 +165,6 @@ export class PromiseTemplateController implements ICustomAttributeViewModel {

@templateController('pending')
export class PendingTemplateController implements ICustomAttributeViewModel {
public readonly id: number = nextId('au$component');
public readonly $controller!: ICustomAttributeController<this>; // This is set by the controller after this instance is constructed

@bindable({ mode: BindingMode.toView }) public value!: Promise<unknown>;
Expand Down Expand Up @@ -214,7 +212,6 @@ export class PendingTemplateController implements ICustomAttributeViewModel {

@templateController('then')
export class FulfilledTemplateController implements ICustomAttributeViewModel {
public readonly id: number = nextId('au$component');
public readonly $controller!: ICustomAttributeController<this>; // This is set by the controller after this instance is constructed

@bindable({ mode: BindingMode.fromView }) public value!: unknown;
Expand Down Expand Up @@ -263,7 +260,6 @@ export class FulfilledTemplateController implements ICustomAttributeViewModel {

@templateController('catch')
export class RejectedTemplateController implements ICustomAttributeViewModel {
public readonly id: number = nextId('au$component');
public readonly $controller!: ICustomAttributeController<this>; // This is set by the controller after this instance is constructed

@bindable({ mode: BindingMode.fromView }) public value!: unknown;
Expand Down
31 changes: 19 additions & 12 deletions packages/runtime-html/src/resources/template-controllers/repeat.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { nextId, IDisposable, onResolve } from '@aurelia/kernel';
import { type IDisposable, onResolve } from '@aurelia/kernel';
import {
applyMutationsToIndices,
BindingBehaviorExpression,
BindingContext,
Collection,
type Collection,
CollectionObserver,
DestructuringAssignmentExpression,
ExpressionKind,
ForOfStatement,
getCollectionObserver,
IndexMap,
IOverrideContext,
IsBindingBehavior,
type IndexMap,
type IOverrideContext,
type IsBindingBehavior,
Scope,
synchronizeIndices,
ValueConverterExpression,
Expand All @@ -21,7 +21,7 @@ import { IViewFactory } from '../../templating/view';
import { templateController } from '../custom-attribute';
import { IController } from '../../templating/controller';
import { bindable } from '../../bindable';
import { isPromise, rethrow } from '../../utilities';
import { isArray, isPromise, rethrow } from '../../utilities';

import type { PropertyBinding } from '../../binding/property-binding';
import type { LifecycleFlags, ISyntheticView, ICustomAttributeController, IHydratableController, ICustomAttributeViewModel, IHydratedController, IHydratedParentController, ControllerVisitor } from '../../templating/controller';
Expand All @@ -39,7 +39,6 @@ const wrappedExprs = [

export class Repeat<C extends Collection = unknown[]> implements ICustomAttributeViewModel {
/** @internal */ protected static inject = [IRenderLocation, IController, IViewFactory];
public readonly id: number = nextId('au$component');

public views: ISyntheticView[] = [];
public key?: string = void 0;
Expand All @@ -60,11 +59,19 @@ export class Repeat<C extends Collection = unknown[]> implements ICustomAttribut
/** @internal */ private _normalizedItems?: unknown[] = void 0;
/** @internal */ private _hasDestructuredLocal: boolean = false;

/** @internal */ private readonly _location: IRenderLocation;
/** @internal */ private readonly _parent: IHydratableController;
/** @internal */ private readonly _factory: IViewFactory;

public constructor(
/** @internal */ private readonly _location: IRenderLocation,
/** @internal */ private readonly _parent: IHydratableController,
/** @internal */ private readonly _factory: IViewFactory
) {}
location: IRenderLocation,
parent: IHydratableController,
factory: IViewFactory,
) {
this._location = location;
this._parent = parent;
this._factory = factory;
}

public binding(
_initiator: IHydratedController,
Expand Down Expand Up @@ -216,7 +223,7 @@ export class Repeat<C extends Collection = unknown[]> implements ICustomAttribut
/** @internal */
private _normalizeToArray(): void {
const items: Items<C> = this.items;
if (items instanceof Array) {
if (isArray(items)) {
this._normalizedItems = items;
return;
}
Expand Down
Loading

0 comments on commit 045d80d

Please sign in to comment.