@viewfly/core 0.0.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export * from './component';
2
+ export * from './jsx-element';
3
+ export * from './root.component';
@@ -0,0 +1,194 @@
1
+ import { Provider, ReflectiveInjector, AbstractType, Type, InjectionToken, InjectFlags, Injector } from '@tanbo/di';
2
+ import { JSXProps, JSXElement, Props } from './jsx-element';
3
+ export interface ComponentFactory {
4
+ (context: Injector): Component;
5
+ }
6
+ export type JSXTemplate = JSXElement | ComponentFactory | null | void;
7
+ export interface ComponentSetup {
8
+ (props: JSXProps<any>): () => JSXTemplate;
9
+ }
10
+ /**
11
+ * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
12
+ */
13
+ export declare class Component extends ReflectiveInjector {
14
+ setup: ComponentSetup;
15
+ config: JSXProps<any> | null;
16
+ destroyCallbacks: LifeCycleCallback[];
17
+ mountCallbacks: LifeCycleCallback[];
18
+ propsChangedCallbacks: PropsChangedCallback<any>[];
19
+ updatedCallbacks: LifeCycleCallback[];
20
+ props: Props;
21
+ get dirty(): boolean;
22
+ get changed(): boolean;
23
+ protected _dirty: boolean;
24
+ protected _changed: boolean;
25
+ private parentComponent;
26
+ private updatedDestroyCallbacks;
27
+ private propsChangedDestroyCallbacks;
28
+ private isFirstRending;
29
+ constructor(context: Injector, setup: ComponentSetup, config: JSXProps<any> | null);
30
+ addProvide<T>(providers: Provider<T> | Provider<T>[]): void;
31
+ init(): {
32
+ template: JSXTemplate;
33
+ render: () => JSXTemplate;
34
+ };
35
+ markAsDirtied(): void;
36
+ markAsChanged(): void;
37
+ rendered(): void;
38
+ invokePropsChangedHooks(newProps: JSXProps<any> | null): void;
39
+ destroy(): void;
40
+ private invokeMountHooks;
41
+ private invokeUpdatedHooks;
42
+ }
43
+ export interface LifeCycleCallback {
44
+ (): void | (() => void);
45
+ }
46
+ export interface PropsChangedCallback<T extends JSXProps<any>> {
47
+ (currentProps: T | null, oldProps: T | null): void | (() => void);
48
+ }
49
+ /**
50
+ * 当组件第一次渲染完成时触发
51
+ * @param callback
52
+ * ```tsx
53
+ * function App() {
54
+ * onMount(() => {
55
+ * console.log('App mounted!')
56
+ * })
57
+ * return () => <div>...</div>
58
+ * }
59
+ * ```
60
+ */
61
+ export declare function onMount(callback: LifeCycleCallback): void;
62
+ /**
63
+ * 当组件视图更新后调用
64
+ * @param callback
65
+ * ```tsx
66
+ * function App() {
67
+ * onUpdated(() => {
68
+ * console.log('App updated!')
69
+ * return () => {
70
+ * console.log('destroy prev update!')
71
+ * }
72
+ * })
73
+ * return () => <div>...</div>
74
+ * }
75
+ * ```
76
+ */
77
+ export declare function onUpdated(callback: LifeCycleCallback): () => void;
78
+ /**
79
+ * 当组件 props 更新地调用
80
+ * @param callback
81
+ * @example
82
+ * ```tsx
83
+ * function YourComponent(props) {
84
+ * onPropsChanged((currentProps, prevProps) => {
85
+ * console.log(currentProps, prevProps)
86
+ *
87
+ * return () => {
88
+ * console.log('destroy prev changed!')
89
+ * }
90
+ * })
91
+ * return () => {
92
+ * return <div>xxx</div>
93
+ * }
94
+ * }
95
+ * ```
96
+ */
97
+ export declare function onPropsChanged<T extends JSXProps<any>>(callback: PropsChangedCallback<T>): () => void;
98
+ /**
99
+ * 当组件销毁时调用回调函数
100
+ * @param callback
101
+ */
102
+ export declare function onDestroy(callback: () => void): void;
103
+ export interface RefListener<T> {
104
+ (current: T): void | (() => void);
105
+ }
106
+ export declare class Ref<T> {
107
+ private callback;
108
+ private component;
109
+ private unListenFn;
110
+ constructor(callback: RefListener<T>, component: Component);
111
+ update(value: T): void;
112
+ unListen(): void;
113
+ }
114
+ /**
115
+ * 用于节点渲染完成时获取 DOM 节点
116
+ * @param callback 获取 DOM 节点的回调函数
117
+ * @example
118
+ * ```tsx
119
+ * function App() {
120
+ * const ref = useRef(node => {
121
+ * function fn() {
122
+ * // do something...
123
+ * }
124
+ * node.addEventListener('click', fn)
125
+ * return () => {
126
+ * node.removeEventListener('click', fn)
127
+ * }
128
+ * })
129
+ * return () => {
130
+ * return <div ref={ref}>xxx</div>
131
+ * }
132
+ * }
133
+ * ```
134
+ */
135
+ export declare function useRef<T>(callback: RefListener<T>): Ref<T>;
136
+ declare const depsKey: unique symbol;
137
+ /**
138
+ * 组件状态实例,直接调用可以获取最新的状态,通过 set 方法可以更新状态
139
+ * ```
140
+ */
141
+ export interface Signal<T> {
142
+ /**
143
+ * 直接调用一个 Signal 实例,可以获取最新状态
144
+ */
145
+ (): T;
146
+ /**
147
+ * 更新组件状态的方法,可以传入最新的值,或者传入一个函数,并返回最新的值
148
+ * @param newState
149
+ */
150
+ set(newState: T | ((oldState: T) => T)): void;
151
+ [depsKey]: Set<LifeCycleCallback>;
152
+ }
153
+ /**
154
+ * 组件状态管理器
155
+ * @param state 初始状态
156
+ * @example
157
+ * ```tsx
158
+ * function App() {
159
+ * // 初始化状态
160
+ * const state = useSignal(1)
161
+ *
162
+ * return () => {
163
+ * <div>
164
+ * <div>当前值为:{state()}</div>
165
+ * <div>
166
+ * <button type="button" onClick={() => {
167
+ * // 当点击时更新状态
168
+ * state.set(state() + 1)
169
+ * }
170
+ * }>updateState</button>
171
+ * </div>
172
+ * </div>
173
+ * }
174
+ * }
175
+ */
176
+ export declare function useSignal<T>(state: T): Signal<T>;
177
+ /**
178
+ * 监听状态变化,当任意一个状态发生变更时,触发回调。
179
+ * useEffect 会返回一个取消监听的函数,调用此函数,可以取消监听。
180
+ * 当在组件中调用时,组件销毁时会自动取消监听。
181
+ * @param deps 依赖的状态 Signal,可以是一个 Signal,只可以一个数包含 Signal 的数组
182
+ * @param effect 状态变更后的回调函数
183
+ */
184
+ export declare function useEffect(deps: Signal<any> | Signal<any>[], effect: LifeCycleCallback): () => void;
185
+ /**
186
+ * 通过 IoC 容器当前组件提供上下文共享数据的方法
187
+ * @param provider
188
+ */
189
+ export declare function provide(provider: Provider | Provider[]): Component;
190
+ /**
191
+ * 通过组件上下文获取 IoC 容器内数据的勾子方法
192
+ */
193
+ export declare function inject<T>(token: Type<T> | AbstractType<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
194
+ export {};
@@ -0,0 +1,39 @@
1
+ import { ComponentFactory, ComponentSetup } from './component';
2
+ export type JSXChildNode = JSXElement | ComponentFactory | string | number | boolean | null | undefined;
3
+ export interface JSXProps<T = JSXChildNode | JSXChildNode[]> {
4
+ children?: T;
5
+ [key: string]: any;
6
+ [key: symbol]: any;
7
+ }
8
+ export declare function Fragment(props: Props | null): () => JSXFragment;
9
+ export declare class JSXFragment {
10
+ props: Props | null;
11
+ constructor(props: Props | null);
12
+ }
13
+ export declare function jsx<T extends JSXChildNode>(name: string, config: JSXProps<T> | null): JSXElement;
14
+ export declare function jsx<T extends JSXChildNode>(setup: ComponentSetup, config: JSXProps<T> | null): ComponentFactory;
15
+ export declare function jsxs<T extends JSXChildNode[]>(name: string, config: JSXProps<T> | null): JSXElement;
16
+ export declare function jsxs<T extends JSXChildNode[]>(setup: ComponentSetup, config: JSXProps<T> | null): ComponentFactory;
17
+ export interface VElementListeners {
18
+ [listenKey: string]: <T extends Event>(ev: T) => any;
19
+ }
20
+ export declare class JSXText {
21
+ text: string;
22
+ constructor(text: string);
23
+ }
24
+ export type VNode = JSXElement | ComponentFactory | JSXText;
25
+ export declare class Props {
26
+ attrs: Map<string, any>;
27
+ styles: Map<string, string | number>;
28
+ classes: Set<string>;
29
+ listeners: VElementListeners;
30
+ children: VNode[];
31
+ constructor(props: JSXProps<JSXChildNode> | JSXProps<JSXChildNode[]> | null);
32
+ static classToArray(config: unknown): string[];
33
+ }
34
+ export declare class JSXElement {
35
+ name: string;
36
+ config: JSXProps<any> | null;
37
+ props: Props;
38
+ constructor(name: string, config: JSXProps<any> | null);
39
+ }
@@ -0,0 +1,10 @@
1
+ import { Subject } from '@tanbo/stream';
2
+ import { Component, ComponentSetup } from './component';
3
+ /**
4
+ * Viewfly 根组件,用于实现组件状态更新事件通知
5
+ */
6
+ export declare class RootComponent extends Component {
7
+ changeEmitter: Subject<void>;
8
+ constructor(factory: ComponentSetup);
9
+ markAsChanged(): void;
10
+ }
@@ -0,0 +1,5 @@
1
+ import 'reflect-metadata';
2
+ export * from '@tanbo/di';
3
+ export * from './foundation/_api';
4
+ export * from './model/_api';
5
+ export * from './viewfly';
@@ -0,0 +1,36 @@
1
+ import { Provider, ReflectiveInjector } from '@tanbo/di';
2
+ import { NativeNode } from './foundation/_api';
3
+ import { ComponentFactory, JSXElement } from './model/_api';
4
+ export type RootNode = JSXElement | ComponentFactory;
5
+ /**
6
+ * Viewfly 配置项
7
+ */
8
+ export interface Config {
9
+ /** 应用根节点 */
10
+ host: NativeNode;
11
+ /** Viewfly IoC 容器中提供者集合 */
12
+ providers?: Provider[];
13
+ /** 是否自动更新视图 */
14
+ autoUpdate?: boolean;
15
+ /** 根节点 */
16
+ root: RootNode;
17
+ }
18
+ /**
19
+ * Viewfly 核心类,用于启动一个 Viewfly 应用
20
+ */
21
+ export declare class Viewfly extends ReflectiveInjector {
22
+ private config;
23
+ private destroyed;
24
+ private rootComponent;
25
+ private subscription;
26
+ constructor(config: Config);
27
+ /**
28
+ * 启动 Viewfly
29
+ */
30
+ start(): void;
31
+ /**
32
+ * 销毁 Viewfly 实例
33
+ */
34
+ destroy(): void;
35
+ private createRootComponent;
36
+ }
@@ -0,0 +1,8 @@
1
+ import { jsx, jsxs, Fragment } from '@viewfly/core'
2
+
3
+ export const jsxDEV = jsx
4
+
5
+ export {
6
+ jsxs,
7
+ Fragment
8
+ }
@@ -0,0 +1,16 @@
1
+ import { jsx, jsxs, Fragment, JSXElement } from '@viewfly/core'
2
+
3
+ export {
4
+ jsxs,
5
+ jsx,
6
+ Fragment
7
+ }
8
+
9
+ declare namespace JSX {
10
+ interface IntrinsicElements {
11
+ [elemName: string]: JSXElement
12
+ }
13
+
14
+ interface Element extends JSXElement {
15
+ }
16
+ }
package/jsx-runtime.js ADDED
@@ -0,0 +1,7 @@
1
+ import { jsx, jsxs, Fragment } from '@viewfly/core'
2
+
3
+ export {
4
+ jsxs,
5
+ jsx,
6
+ Fragment
7
+ }
@@ -0,0 +1,17 @@
1
+ (function (factory) {
2
+ if (typeof module === 'object' && typeof module.exports === 'object') {
3
+ var v = factory(require, exports)
4
+ if (v !== undefined) module.exports = v
5
+ }
6
+ else if (typeof define === 'function' && define.amd) {
7
+ define(['require', 'exports', '@viewfly/core'], factory)
8
+ }
9
+ })(function (require, exports) {
10
+ 'use strict'
11
+ Object.defineProperty(exports, '__esModule', { value: true })
12
+ exports.Fragment = exports.jsx = exports.jsxs = void 0
13
+ const core_1 = require('@viewfly/core')
14
+ Object.defineProperty(exports, 'jsx', { enumerable: true, get: function () { return core_1.jsx } })
15
+ Object.defineProperty(exports, 'jsxs', { enumerable: true, get: function () { return core_1.jsxs } })
16
+ Object.defineProperty(exports, 'Fragment', { enumerable: true, get: function () { return core_1.Fragment } })
17
+ })
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@viewfly/core",
3
+ "version": "0.0.1-alpha.0",
4
+ "description": "Viewfly is a simple and easy-to-use JavaScript framework with an intuitive development experience.",
5
+ "main": "./bundles/index.js",
6
+ "module": "./bundles/index.esm.js",
7
+ "typings": "./bundles/public-api.d.ts",
8
+ "scripts": {
9
+ "start": "webpack-dev-server",
10
+ "test": "cross-env env=test jest",
11
+ "test-c": "cross-env env=test jest --coverage",
12
+ "build:lib": "rimraf bundles && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
13
+ "publish:lib": "npm run build:lib && npm publish --access=public"
14
+ },
15
+ "license": "MIT",
16
+ "keywords": [],
17
+ "dependencies": {
18
+ "@tanbo/di": "^1.1.4",
19
+ "@tanbo/stream": "^1.1.9"
20
+ },
21
+ "devDependencies": {
22
+ "@rollup/plugin-commonjs": "^23.0.2",
23
+ "@rollup/plugin-typescript": "^9.0.2",
24
+ "rimraf": "^3.0.2",
25
+ "rollup": "^3.2.5",
26
+ "tslib": "^2.4.1"
27
+ },
28
+ "author": {
29
+ "name": "Tanbo",
30
+ "email": "tanbohb@qq.com"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/viewfly/viewfly.git"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/viewfly/viewfly.git/issues"
38
+ }
39
+ }