Skip to content

Commit

Permalink
refactor(getComponentConstructor): convert to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei committed Sep 29, 2020
1 parent e8e8046 commit 5758c80
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/Ractive/RactiveDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export interface Constructor<T extends Ractive<T>, U extends InitOpts<T> = InitO
new (opts?: U): T;
}

export type Component = Static | Promise<Static>;
export type Component = typeof Static | Promise<typeof Static>;

export class Static<T extends Ractive<T> = Ractive> extends Ractive<T> {
/** Create a new component with this constructor as a starting point. */
Expand Down Expand Up @@ -418,4 +418,8 @@ export class Static<T extends Ractive<T> = Ractive> extends Ractive<T> {
static _cssDef: CSSDefinition;

static adapt: (Adaptor | string)[];

static default: any;

static _fn: Function;
}
4 changes: 2 additions & 2 deletions src/types/Generic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Ractive } from 'src/Ractive/RactiveDefinition';
import type Fragment from 'view/Fragment';
import type Component from 'view/items/Component';
import type ComponentItem from 'view/items/Component';

import type { Macro } from './Macro';
import type { ParseFn } from './Parse';
Expand Down Expand Up @@ -38,7 +38,7 @@ export interface Meta {
target: string | false;
up?: Fragment;
external?: boolean;
anchor?: Component;
anchor?: ComponentItem;
bubble: () => void;
findNextNode: () => any;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { noRegistryFunctionReturn } from 'config/errors';
import { findInstance } from 'shared/registry';
import type { Ractive, Static } from 'src/Ractive/RactiveDefinition';
import { isFunction, isString } from 'utils/is';
import { warnIfDebug } from 'utils/log';
import { hasOwn } from 'utils/object';

// finds the component constructor in the registry or view hierarchy registries
export default function getComponentConstructor(ractive, name) {
export default function getComponentConstructor(ractive: Ractive, name: string): typeof Static {
const instance = findInstance('components', ractive, name);
let Component;
let Component: typeof Static;

if (instance) {
Component = instance.components[name];
Component = <typeof Static>instance.components[name];

if (Component && !Component.isInstance) {
if (Component.default && Component.default.isInstance) Component = Component.default;
else if (!Component.then && isFunction(Component)) {
if (Component.default?.isInstance) Component = Component.default;
// TSRChange - change match to from `!Component.then` to `instanceof Promise`
else if (!(Component instanceof Promise) && isFunction(Component)) {
// function option, execute and store for reset
const fn = Component.bind(instance);
fn.isOwner = hasOwn(instance.components, name);
Expand Down

0 comments on commit 5758c80

Please sign in to comment.