Skip to content

Commit

Permalink
refactor: types
Browse files Browse the repository at this point in the history
  • Loading branch information
sebkolind committed Nov 26, 2023
1 parent 01ea019 commit f4ceff5
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions lib/one.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Config = {
state?: State,
parent?: Omit<Config, 'setup'>,
store?: Store,
setup: Function,
setup?: Function,
created?: Function,
components?: Array<Config>,
loop?: {
Expand All @@ -37,7 +37,6 @@ type Config = {
}

type Component = Omit<Config, 'setup'>

type Components = {
[string]: Array<Component>
}
Expand Down Expand Up @@ -115,25 +114,21 @@ function one(config: Config): void {

template(c)

if (config.components) {
config.components.forEach(component => {
one({
...component,
parent: c
})
config.components?.forEach(component => {
one({
...component,
parent: c
})
}
})

if (setup) {
setup({
state: c.state,
props: c.props,
parent: c.parent,
store: { get: store.get, set: store.set, length: store.length },
query: query.bind(c),
computed: computed.bind(c)
})
}
setup?.({
state: c.state,
props: c.props,
parent: c.parent,
store: { get: store.get, set: store.set, length: store.length },
query: query.bind(c),
computed: computed.bind(c)
})

render(c)
})
Expand Down Expand Up @@ -163,19 +158,14 @@ function template(config: Component) {
throw new Error(`One->template: Could not find component ${name}.`)
}

if (!config.el) {
throw new Error('One->template: Could not find the element.')
}

const el = config.el
clone.$one = config.el?.$one
clone.$one = config.el.$one

for (const attr of config.el.attributes) {
if (config.props && (config.props: Object)[attr.name]) continue
clone.setAttribute(attr.name, attr.value)
}

el.replaceWith(clone)
config.el.replaceWith(clone)
config.el = clone

component.el = clone
Expand Down Expand Up @@ -237,7 +227,7 @@ function createInstance(config: Component): Component {
}

function query(this: Component, selector: string) {
const el: ?OneElement = this.el?.children.length
const el: ?OneElement = this.el.children.length
? (this.el.querySelector(selector): any)
: this.el

Expand Down Expand Up @@ -282,7 +272,7 @@ function loop(
this: Component,
el: OneElement,
[items, key]: [string, ?Key],
fn: Function,
fn?: Function,
parent: ?OneElement = null
) {
const parentNode: OneElement = parent ?? (el.parentNode: any)
Expand Down Expand Up @@ -350,9 +340,7 @@ function loop(
parentNode.append(clone)
}

if (fn) {
fn({ el: clone, item })
}
fn?.({ el: clone, item })
})
}

Expand Down

0 comments on commit f4ceff5

Please sign in to comment.