Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
atellmer committed Nov 22, 2022
1 parent dedf80b commit a9d70ca
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/fiber/fiber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ function mutateFiber(options: MutateFiberOptions) {
}

if (!fiber.nativeElement && detectIsVirtualNode(fiber.instance)) {
fiber.nativeElement = platform.createNativeElement(fiber);
fiber.nativeElement = platform.createNativeElement(fiber.instance);
}
}

Expand Down
20 changes: 14 additions & 6 deletions packages/core/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import type { NestedArray } from '../shared';

const detectIsFunction = (o: any): o is Function => typeof o === 'function';
const detectIsUndefined = (o: any) => typeof o === 'undefined';
const detectIsNumber = (o: any) => typeof o === 'number';
const detectIsString = (o: any) => typeof o === 'string';
const detectIsObject = (o: any) => typeof o === 'object';
const detectIsBoolean = (o: any) => typeof o === 'boolean';

const detectIsUndefined = (o: any): o is undefined => typeof o === 'undefined';

const detectIsNumber = (o: any): o is number => typeof o === 'number';

const detectIsString = (o: any): o is string => typeof o === 'string';

const detectIsObject = (o: any): o is object => typeof o === 'object';

const detectIsBoolean = (o: any): o is boolean => typeof o === 'boolean';

const detectIsArray = (o: any): o is Array<any> => Array.isArray(o);
const detectIsNull = (o: any) => o === null;

const detectIsNull = (o: any): o is null => o === null;

const detectIsEmpty = (o: any) => detectIsNull(o) || detectIsUndefined(o);

function error(str: string) {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/platform/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { type Fiber } from '../fiber';
import { type TaskPriority } from '../constants';
import { type ComponentFactory } from '../component';
import { type VirtualNode } from '../view';

export type Platform = {
scheduleCallback: (callback: () => void, options?: ScheduleCallbackOptions) => void;
shouldYeildToHost: () => boolean;
createNativeElement: <N>(fiber: Fiber<N>) => N;
createNativeElement: <N>(vNode: VirtualNode) => N;
applyCommit: <N>(fiber: Fiber<N>) => void;
finishCommitWork: () => void;
detectIsPortal: (factory: ComponentFactory) => boolean;
Expand Down
47 changes: 27 additions & 20 deletions packages/core/src/view/view.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EMPTY_NODE, ATTR_KEY } from '../constants';
import { detectIsArray, detectIsEmpty, detectIsFunction } from '../helpers';
import { detectIsArray, detectIsEmpty, detectIsFunction, detectIsString } from '../helpers';
import type { DarkElementKey } from '../shared';
import type { ComponentFactory, StandardComponentProps } from '../component';
import { NodeType, type ViewDef } from './types';
Expand Down Expand Up @@ -56,9 +56,13 @@ class CommentVirtualNode extends VirtualNode {
}

const detectIsVirtualNode = (vNode: unknown): vNode is VirtualNode => vNode instanceof VirtualNode;

const detectIsTagVirtualNode = (vNode: unknown): vNode is TagVirtualNode => vNode instanceof TagVirtualNode;

const detectIsCommentVirtualNode = (vNode: unknown): vNode is CommentVirtualNode => vNode instanceof CommentVirtualNode;

const detectIsTextVirtualNode = (vNode: unknown): vNode is TextVirtualNode => vNode instanceof TextVirtualNode;

const detectIsEmptyVirtualNode = (vNode: unknown): boolean =>
detectIsCommentVirtualNode(vNode) && vNode.value === EMPTY_NODE;

Expand All @@ -68,20 +72,10 @@ function getVirtualNodeKey(vNode: TagVirtualNode): DarkElementKey | null {
return !detectIsEmpty(key) ? key : null;
}

function Text(source: string | StandardComponentProps['slot']): string | TextVirtualNode {
const text =
typeof source === 'string' ? new TextVirtualNode(source) : detectIsTextVirtualNode(source) ? source.value : '';

return text;
}

function Comment(text: string): CommentVirtualNodeFactory {
const factory = () => new CommentVirtualNode(text);

factory[$$virtualNode] = true;
const createEmptyVirtualNode = () => new CommentVirtualNode(EMPTY_NODE);

return factory;
}
const detectIsVirtualNodeFactory = (factory: unknown): factory is VirtualNodeFactory =>
detectIsFunction(factory) && factory[$$virtualNode] === true;

function View(def: ViewDef): TagVirtualNodeFactory {
const factory = () => {
Expand All @@ -101,10 +95,23 @@ function View(def: ViewDef): TagVirtualNodeFactory {
return factory;
}

const createEmptyVirtualNode = () => new CommentVirtualNode(EMPTY_NODE);
function Text(source: string | StandardComponentProps['slot']): string | TextVirtualNode {
const text = detectIsString(source)
? new TextVirtualNode(source)
: detectIsTextVirtualNode(source)
? source.value
: '';

const detectIsVirtualNodeFactory = (factory: unknown): factory is VirtualNodeFactory =>
detectIsFunction(factory) && factory[$$virtualNode] === true;
return text;
}

function Comment(text: string): CommentVirtualNodeFactory {
const factory = () => new CommentVirtualNode(text);

factory[$$virtualNode] = true;

return factory;
}

export {
VirtualNode,
Expand All @@ -117,9 +124,9 @@ export {
detectIsTextVirtualNode,
detectIsEmptyVirtualNode,
getVirtualNodeKey,
Text,
Comment,
View,
createEmptyVirtualNode,
detectIsVirtualNodeFactory,
View,
Text,
Comment,
};
12 changes: 2 additions & 10 deletions packages/platform-browser/src/dom/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const attrBlackListMap = {

let fragmentsMap: Map<Element, DOMFragment> = new Map();

function createElement(vNode: VirtualNode): DOMElement {
function createNativeElement(vNode: VirtualNode): DOMElement {
const map = {
[NodeType.TAG]: (vNode: VirtualNode) => {
const tagNode = vNode as TagVirtualNode;
Expand All @@ -55,7 +55,7 @@ function createElement(vNode: VirtualNode): DOMElement {
return map[vNode.type](vNode);
}

function detectIsSvgElement(tagName) {
function detectIsSvgElement(tagName: string) {
const tagMap = {
svg: true,
circle: true,
Expand Down Expand Up @@ -85,14 +85,6 @@ function detectIsSvgElement(tagName) {
return Boolean(tagMap[tagName]);
}

function createNativeElement(fiber: Fiber<Element>): DOMElement {
if (!detectIsVirtualNode(fiber.instance)) {
throw new Error('[Dark]: createNativeElement receives only virtual node!');
}

return createElement(fiber.instance);
}

function applyRef(ref: MutableRef, element: Element) {
if (detectIsRef(ref)) {
ref.current = element;
Expand Down

0 comments on commit a9d70ca

Please sign in to comment.