-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mount): move
mount
to it's own file
- Loading branch information
Showing
6 changed files
with
64 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |