Skip to content

Commit

Permalink
chore: rm setup from component configs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebkolind committed Nov 25, 2023
1 parent 9e9f5c9 commit 6c15774
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/one.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ type Config = {
el: OneElement,
props?: Props,
state?: State,
parent?: Partial<Config>,
parent?: Omit<Config, 'setup'>,
store?: Store,
setup: Function,
created?: Function,
components?: Array<Config>
}

type Components = {
[string]: Array<Config>
[string]: Array<Omit<Config, 'setup'>>
}

type Store = {
Expand All @@ -52,7 +52,7 @@ const store: Store = {

for (const k in components) {
const component = components[k]
component.forEach((c: Config) => {
component.forEach(c => {
if (
typeof c.template === 'string' && c.template.includes(`$store.${key}`) ||
c.el?.textContent.includes(`$store.${key}`)
Expand Down Expand Up @@ -81,8 +81,9 @@ function one(config: Config) {
}

Array.from(els).forEach(async (el) => {
const { setup, created, ...c } = createInstance({
...config,
const { setup, ...cfg } = config
const { created, ...c } = createInstance({
...cfg,
state: { ...config.state },
store: Object.freeze({
get: store.get,
Expand Down Expand Up @@ -163,7 +164,7 @@ function template(config: Omit<Config, 'setup'>) {
tmpl.remove()
}

function createInstance(config: Config): Partial<Config> {
function createInstance(config: Omit<Config, 'setup'>): Omit<Config, 'setup'> {
const name = config.name
const el = config.el
Expand Down Expand Up @@ -215,7 +216,7 @@ function createInstance(config: Config): Partial<Config> {
return config
}
function query(this: Partial<Config>, selector: string) {
function query(this: Omit<Config, 'setup'>, selector: string) {
const el = this.el?.children.length
? this.el.querySelector(selector)
: this.el
Expand All @@ -231,7 +232,7 @@ function query(this: Partial<Config>, selector: string) {
return el
}
function on(this: Partial<Config>, el: OneElement, name: string, handler: Function) {
function on(this: Omit<Config, 'setup'>, el: OneElement, name: string, handler: Function) {
el[`on${name}`] = async () => {
await handler({
store: { get: store.get, set: store.set, length: store.length },
Expand All @@ -257,8 +258,11 @@ function on(this: Partial<Config>, el: OneElement, name: string, handler: Functi
}
}
// TOOD: Support for items that aren't an array of objects
// i.e: [1, 2, 3] or ['foo', 'bar', 'baz']
// How to handle the key in this case?
function loop(
this: Partial<Config>,
this: Omit<Config, 'setup'>,
el: OneElement,
[items, key]: [string, string],
fn: Function,
Expand Down Expand Up @@ -305,7 +309,7 @@ function loop(

el.remove()

const config: Partial<Config> = {
const config = {
key: item[key],
el: clone,
state: item,
Expand All @@ -331,7 +335,7 @@ function loop(
})
}

function fi(this: Partial<Config>, el: OneElement) {
function fi(this: Omit<Config, 'setup'>, el: OneElement) {
let original: ?OneElement = null

const container = document.createElement('one-if')
Expand All @@ -357,7 +361,7 @@ function fi(this: Partial<Config>, el: OneElement) {
}
}

function computed(this: Partial<Config>, name: string, fn: Function) {
function computed(this: Omit<Config, 'setup'>, name: string, fn: Function) {
if (this.state?.[name] != null) {
throw new Error(`One->computed: You already have a state property called "${name}".`)
}
Expand All @@ -379,11 +383,11 @@ function computed(this: Partial<Config>, name: string, fn: Function) {
}
}
function render(config: Partial<Config>) {
function render(config: Omit<Config, 'setup'>) {
traverse(config.el, config)
}
function traverse(el: OneElement, config: Partial<Config>) {
function traverse(el: OneElement, config: Omit<Config, 'setup'>) {
Array.from(el.attributes).forEach(attr => {
if (attr.name === 'o-text') {
const val = value(attr.value, config)
Expand All @@ -408,7 +412,7 @@ function traverse(el: OneElement, config: Partial<Config>) {
}
// TODO: Support for nested keys, like `foo.bar.baz`.
function value(key: string, config: Partial<Config>) {
function value(key: string, config: Omit<Config, 'setup'>) {
const fromStore = key.startsWith('$store.')
const fromProps = key.startsWith('$props.')
Expand Down

0 comments on commit 6c15774

Please sign in to comment.