Skip to content

Commit

Permalink
allow createApp by Component (#19)
Browse files Browse the repository at this point in the history
* fix logger bugs

* allow createApp by Component
  • Loading branch information
yinxulai authored Apr 17, 2024
1 parent 745007c commit f72e8dd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "airx",
"version": "0.2.3",
"version": "0.2.4",
"description": "a front framework with jsx",
"main": "./output/umd/index.js",
"module": "./output/esm/index.js",
Expand Down
18 changes: 14 additions & 4 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AirxElement } from './element'
import { AirxComponent, AirxElement, createElement } from './element'
import { Plugin, PluginContext } from './render'
import { browserRender, serverRender } from './render'

Expand Down Expand Up @@ -30,21 +30,31 @@ export interface AirxApp {
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function createApp(element: AirxElement<any>): AirxApp {
export function createApp(element: AirxElement<any> | AirxComponent): AirxApp {
const appContext = new PluginContext()

const ensureAsElement = (element: AirxElement | AirxComponent): AirxElement => {
if (typeof element === 'function') {
return createElement(element, {})
}

return element
}

const app: AirxApp = {
plugin: (...plugins: Plugin[]) => {
appContext.registerPlugin(...plugins)
return app
},

mount: (container: HTMLElement) => {
browserRender(appContext, element, container)
browserRender(appContext, ensureAsElement(element), container)
return app
},

renderToHTML: (): Promise<string> => {
return new Promise<string>(resolve => {
serverRender(appContext, element, resolve)
serverRender(appContext, ensureAsElement(element), resolve)
})
}
}
Expand Down

0 comments on commit f72e8dd

Please sign in to comment.