Skip to content

Commit

Permalink
refactor(mount): move mount to it's own file
Browse files Browse the repository at this point in the history
  • Loading branch information
sebkolind committed Jun 6, 2024
1 parent 7b39abd commit e591ca0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 60 deletions.
4 changes: 1 addition & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const config: Config = {
coverageDirectory: 'coverage',

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],
coveragePathIgnorePatterns: ['/node_modules/', 'src/main.ts'],

// Indicates which provider should be used to instrument code for coverage
coverageProvider: 'v8',
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/component.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mount } from '../main';
import { mount } from '../mount';
import { tags } from '../tags';
import { type Component } from '../types';
import { getByText, getByTestId, fireEvent } from '@testing-library/dom';
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/main.test.ts → src/__tests__/mount.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, mount, tags } from '../main';
import { Component, tags } from '../main';
import { mount } from '../mount';

describe('main', () => {
describe('mount', () => {
test('`null` element', () => {
const el = document.querySelector('.not-defined');

Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/walker.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fireEvent, getByRole, getByText } from '@testing-library/dom';
import { Component, mount } from '../main';
import { Component } from '../main';
import { mount } from '../mount';
import { tags } from '../tags';

const { div, p, button, ul, li } = tags;
Expand Down
59 changes: 6 additions & 53 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,13 @@
import type { Children, Context, Component, TentNode, Attrs } from './types';
import { mount } from './mount';
import { createTag, tags } from './tags';
import { walker } from './walker';

function mount<S extends {} = {}, A extends Attrs = {}>(
element: HTMLElement | Element | TentNode<A> | null,
component: Component<S, A>,
) {
if (element == null) return;

let node: TentNode<A>;
const { view, mounted } = component;
const state = 'state' in component ? component.state : ({} as S);
const el = element as TentNode<A>;

el.$tent = { attributes: {} };

const handler = {
get(obj: S, key: string) {
if (typeof obj[key] === 'object' && obj[key] != null) {
return new Proxy(obj[key], handler);
} else {
return obj[key];
}
},
set(obj: S, prop: string, value: unknown) {
if (!obj.hasOwnProperty(prop)) {
throw new Error(
`The property "${String(prop)}" does not exist on the state object.`,
);
}
if (obj[prop] === value) return true;

const s = Reflect.set(obj, prop, value);

walker(node, view({ state: proxy, el }));

return s;
},
};

const proxy = new Proxy<S>({ ...state }, handler);

node = view({ state: proxy, el });
node.$tent = { attributes: {} };

el.append(node);

mounted?.({ state: proxy, el });
}
import type { Children, Component, Context, TentNode } from './types';

export {
tags,
mount,
createTag,
type Context,
type TentNode,
mount,
tags,
type Children,
type Component,
type Context,
type TentNode,
};
51 changes: 51 additions & 0 deletions src/mount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { Component, TentNode, Attrs } from './types';
import { walker } from './walker';

function mount<S extends {} = {}, A extends Attrs = {}>(
element: HTMLElement | Element | TentNode<A> | null,
component: Component<S, A>,
) {
if (element == null) return;

let node: TentNode<A>;
const { view, mounted } = component;
const state = 'state' in component ? component.state : ({} as S);
const el = element as TentNode<A>;

el.$tent = { attributes: {} };

const handler = {
get(obj: S, key: string) {
if (typeof obj[key] === 'object' && obj[key] != null) {
return new Proxy(obj[key], handler);
} else {
return obj[key];
}
},
set(obj: S, prop: string, value: unknown) {
if (!obj.hasOwnProperty(prop)) {
throw new Error(
`The property "${String(prop)}" does not exist on the state object.`,
);
}
if (obj[prop] === value) return true;

const s = Reflect.set(obj, prop, value);

walker(node, view({ state: proxy, el }));

return s;
},
};

const proxy = new Proxy<S>({ ...state }, handler);

node = view({ state: proxy, el });
node.$tent = { attributes: {} };

el.append(node);

mounted?.({ state: proxy, el });
}

export { mount };

0 comments on commit e591ca0

Please sign in to comment.