-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] update template page & add config pkg
- Loading branch information
Showing
63 changed files
with
1,404 additions
and
41 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
Empty file.
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,8 @@ | ||
{ | ||
"extends": "@thewu/config", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
}, | ||
"includes": ["src/**/*"] | ||
} |
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 +1,2 @@ | ||
export * from "./dom"; | ||
export * from "./renderer"; |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "@thewu/core", | ||
"version": "0.0.7", | ||
"description": "", | ||
"main": "./dist/index.js", | ||
"scripts": { | ||
"dev": "tsc -w", | ||
"build": "tsc" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"files": [ | ||
"dist" | ||
], | ||
"license": "ISC", | ||
"dependencies": { | ||
"reflect-metadata": "^0.2.2", | ||
"@thewu/wux": "workspace:*" | ||
} | ||
} |
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,111 @@ | ||
import "reflect-metadata"; | ||
import { METADATA_PROP_KEY, setProp, setProps } from "./prop"; | ||
import { METADATA_STATE_KEY } from "./state"; | ||
import { assignDefault } from "./default"; | ||
import { WuNode, WuNodeProps } from "../../jsx"; | ||
import { ParsedWuNode, initializeNode } from "../../initialize"; | ||
import { patch } from "../../reconciliation"; | ||
import { Renderer } from "../../renderer"; | ||
import { stream, subscribe } from "@thewu/wux"; | ||
|
||
export interface Component { | ||
render(): WuNode; | ||
initVdom(vdom: ParsedWuNode): void; | ||
createInstance(...args: any[]): Component; | ||
updateProps(props: WuNodeProps): void; | ||
setRenderer(renderer: Renderer): void; | ||
[key: string]: any; | ||
} | ||
|
||
type Props = Record<string, unknown>; | ||
|
||
interface ComponentParams { | ||
props: Props; | ||
} | ||
|
||
interface ComponentParams { | ||
props: Props; | ||
} | ||
|
||
export const Component = | ||
() => | ||
<T extends { new (...args: any[]): any }>(Constructor: T) => | ||
class Component extends Constructor { | ||
props: Props = {}; | ||
vdom: ParsedWuNode | undefined; | ||
renderer: Renderer | undefined; | ||
proxyInstance: any; | ||
|
||
static $instance: Component; | ||
|
||
static createInstance(params: ComponentParams) { | ||
const { props } = params; | ||
const instance = | ||
Component.$instance ?? (Component.$instance = new Component(params)); | ||
|
||
assignDefault(instance, props); | ||
setProps(instance, props); | ||
setProp(instance, props); | ||
|
||
return instance; | ||
} | ||
|
||
constructor(...args: any[]) { | ||
super(...args); | ||
|
||
// When states was changed, trigger patch method | ||
(Reflect.getMetadata(METADATA_STATE_KEY, this) ?? []).forEach( | ||
(stateName: string) => { | ||
stream( | ||
() => { | ||
return JSON.stringify(this[stateName]); | ||
}, | ||
() => { | ||
this.patch(); | ||
}, | ||
); | ||
}, | ||
); | ||
} | ||
|
||
initVdom(vdom: ParsedWuNode) { | ||
if (!this.vdom) { | ||
this.vdom = vdom; | ||
} | ||
} | ||
|
||
// After first mounted, the renderer will be set to the component | ||
setRenderer(renderer: Renderer) { | ||
this.renderer = renderer; | ||
} | ||
|
||
render() { | ||
return super.render.bind(this)(); | ||
} | ||
|
||
// When the state was changed, the patch action will invoke automatically | ||
patch() { | ||
if (!this.vdom) { | ||
throw new Error("Cannot call patch function before render call"); | ||
} | ||
|
||
if (!this.renderer) { | ||
throw new Error("Component mount failed"); | ||
} | ||
|
||
console.info("Patching..."); | ||
|
||
// Move to next tick of event loop to make sure the state always be the latest | ||
Promise.resolve().then(() => { | ||
patch( | ||
this.vdom!, | ||
(this.vdom = initializeNode( | ||
this.render(), | ||
this.vdom!.parentEl, | ||
this.vdom?.parentNode, | ||
)), | ||
this.renderer!, | ||
); | ||
}); | ||
} | ||
}; |
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,25 @@ | ||
import { defineMetadatas } from "../../utils"; | ||
|
||
interface DefaultParams { | ||
prop?: string; | ||
} | ||
|
||
const DEFAULT_FROM_PROP_METADATA_KEY = Symbol("DEFAULT_FROM_PROP_METADATA_KEY"); | ||
|
||
export const Default = | ||
({ prop }: DefaultParams) => | ||
(target: any, propertyKey: string) => { | ||
defineMetadatas(DEFAULT_FROM_PROP_METADATA_KEY, target, { | ||
prop, | ||
propertyKey, | ||
}); | ||
}; | ||
|
||
export const assignDefault = (target: any, props: Record<string, unknown>) => { | ||
const propDefaultMetadata = | ||
Reflect.getMetadata(DEFAULT_FROM_PROP_METADATA_KEY, target) ?? []; | ||
|
||
for (const { prop: propName, propertyKey } of propDefaultMetadata) { | ||
target[propertyKey] = props[propName]; | ||
} | ||
}; |
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,6 @@ | ||
export * from "./component"; | ||
export * from "./state"; | ||
export * from "./prop"; | ||
export * from "./default"; | ||
export * as LifeCycle from "./life-cycle"; | ||
export * from "./life-cycle"; |
24 changes: 24 additions & 0 deletions
24
packages/core/src/component/decorators/life-cycle/common.ts
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,24 @@ | ||
const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1); | ||
|
||
type LifeCycleHook<N extends string> = { | ||
[K in `on${Capitalize<N>}`]: (target: any, propertyKey: string) => void; | ||
} & { | ||
[K in `get${Capitalize<N>}LifeCycleHookName`]: (target: any) => string; | ||
}; | ||
|
||
export const createLifeCycleHook = <N extends string>( | ||
name: N, | ||
metadataKey: symbol, | ||
): LifeCycleHook<N> => { | ||
const capitalizedHookName = capitalize(name); | ||
const decoratorName = `on${capitalizedHookName}`; | ||
const getLifeCycleHookNameName = `get${capitalizedHookName}LifeCycleHookName`; | ||
|
||
return { | ||
[decoratorName]: (target: any, propertyKey: string) => { | ||
Reflect.defineMetadata(metadataKey, propertyKey, target); | ||
}, | ||
[getLifeCycleHookNameName]: (target: any) => | ||
Reflect.getMetadata(metadataKey, target), | ||
} as LifeCycleHook<N>; | ||
}; |
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,2 @@ | ||
export * from "./on-mounted"; | ||
export * from "./on-destroy"; |
6 changes: 6 additions & 0 deletions
6
packages/core/src/component/decorators/life-cycle/on-destroy.ts
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,6 @@ | ||
import { createLifeCycleHook } from "./common"; | ||
|
||
export const { onDestroy, getDestroyLifeCycleHookName } = createLifeCycleHook( | ||
"destroy", | ||
Symbol("DESTROY_METADATA_KEY"), | ||
); |
6 changes: 6 additions & 0 deletions
6
packages/core/src/component/decorators/life-cycle/on-mounted.ts
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,6 @@ | ||
import { createLifeCycleHook } from "./common"; | ||
|
||
export const { onMounted, getMountedLifeCycleHookName } = createLifeCycleHook( | ||
"mounted", | ||
Symbol("MOUNTED_METADATA_KEY"), | ||
); |
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,35 @@ | ||
import "reflect-metadata"; | ||
import { defineMetadatas } from "../../utils"; | ||
|
||
export const METADATA_PROP_KEY = Symbol("METADATA_PROP_KEY"); | ||
|
||
// The Prop decorator map the prop value to the property of instance | ||
export const Prop = | ||
(propName?: string) => (target: any, propertyKey: string) => { | ||
defineMetadatas(METADATA_PROP_KEY, target, { | ||
propertyKey, | ||
propName: propName ?? propertyKey, | ||
}); | ||
}; | ||
|
||
export const setProp = (target: any, props: Record<string, unknown>) => { | ||
const propMetadatas = Reflect.getMetadata(METADATA_PROP_KEY, target) ?? []; | ||
|
||
for (const { propertyKey, propName } of propMetadatas) { | ||
target[propertyKey] = props[propName]; | ||
} | ||
}; | ||
|
||
export const METADATA_PROPS_KEY = Symbol("METADATA_PROP_KEY"); | ||
|
||
export const Props = (target: any, propertyKey: string) => { | ||
defineMetadatas(METADATA_PROPS_KEY, target, propertyKey); | ||
}; | ||
|
||
export const setProps = (target: any, props: Record<string, unknown>) => { | ||
const propsKeys = Reflect.getMetadata(METADATA_PROPS_KEY, target) ?? []; | ||
|
||
for (const propertyKey of propsKeys) { | ||
target[propertyKey] = { ...props }; | ||
} | ||
}; |
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,30 @@ | ||
import "reflect-metadata"; | ||
import { createReactiveFactory } from "@thewu/wux"; | ||
import { defineMetadatas, isObject } from "../../utils"; | ||
|
||
export const METADATA_STATE_KEY = Symbol("METADATA_STATE_KEY"); | ||
export const METADATA_STATE_VALUE = Symbol("METADATA_STATE_VALUE"); | ||
|
||
export const State = (target: any, propertyKey: string): any => { | ||
const reactive = createReactiveFactory(); | ||
const reactiveProperty = reactive( | ||
{ value: target[propertyKey] }, | ||
{ recursion: true }, | ||
); | ||
|
||
defineMetadatas(METADATA_STATE_KEY, target, propertyKey); | ||
|
||
return { | ||
get: () => reactiveProperty.value, | ||
set: (value: unknown) => { | ||
if (isObject(value) || Array.isArray(value)) { | ||
reactiveProperty.value = reactive(value, { | ||
recursion: true, | ||
}); | ||
return true; | ||
} | ||
|
||
reactiveProperty.value = value; | ||
}, | ||
}; | ||
}; |
Empty file.
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 @@ | ||
export * from "./decorators"; |
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,6 @@ | ||
export * from "./renderer"; | ||
export * from "./jsx"; | ||
export * from "./utils"; | ||
export * from "./initialize"; | ||
export * from "./reconciliation"; | ||
export * from "./component"; |
Oops, something went wrong.