@viewfly/core 1.0.0-alpha.2 → 1.0.0-alpha.21

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.
@@ -1,245 +0,0 @@
1
- import { AbstractType, ExtractValueType, InjectFlags, InjectionToken, Injector, Provider, ReflectiveInjector, Type } from '../di/_api';
2
- import { Key, Props } from './jsx-element';
3
- import { ComponentView } from './_utils';
4
- /**
5
- * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
6
- */
7
- export declare class Component extends ReflectiveInjector {
8
- private readonly parentComponent;
9
- readonly type: JSXInternal.ComponentSetup;
10
- props: Props;
11
- readonly key?: Key | undefined;
12
- instance: JSXInternal.ComponentInstance<Props>;
13
- template: JSXInternal.ViewNode;
14
- changedSubComponents: Set<Component>;
15
- get dirty(): boolean;
16
- get changed(): boolean;
17
- $$view: ComponentView;
18
- unmountedCallbacks?: LifeCycleCallback[] | null;
19
- mountCallbacks?: LifeCycleCallback[] | null;
20
- propsChangedCallbacks?: PropsChangedCallback<any>[] | null;
21
- updatedCallbacks?: LifeCycleCallback[] | null;
22
- private updatedDestroyCallbacks?;
23
- private propsChangedDestroyCallbacks?;
24
- protected _dirty: boolean;
25
- protected _changed: boolean;
26
- private unWatch?;
27
- private isFirstRendering;
28
- private refs;
29
- constructor(parentComponent: Injector | null, type: JSXInternal.ComponentSetup, props: Props, key?: Key | undefined);
30
- markAsDirtied(): void;
31
- markAsChanged(changedComponent?: Component): void;
32
- render(): {
33
- template: any;
34
- portalHost: import("./injection-tokens").NativeNode | undefined;
35
- };
36
- update(newProps: Props, forceUpdate?: boolean): any;
37
- provide<T>(providers: Provider<T> | Provider<T>[]): void;
38
- rendered(): void;
39
- destroy(): void;
40
- private invokePropsChangedHooks;
41
- private invokeMountHooks;
42
- private invokeUpdatedHooks;
43
- }
44
- export interface LifeCycleCallback {
45
- (): void | (() => void);
46
- }
47
- export interface PropsChangedCallback<T extends Props> {
48
- (currentProps: T | null, oldProps: T | null): void | (() => void);
49
- }
50
- /**
51
- * 当组件第一次渲染完成时触发
52
- * @param callback
53
- * ```tsx
54
- * function App() {
55
- * onMount(() => {
56
- * console.log('App mounted!')
57
- * })
58
- * return () => <div>...</div>
59
- * }
60
- * ```
61
- */
62
- export declare function onMounted(callback: LifeCycleCallback): void;
63
- /**
64
- * 当组件视图更新后调用
65
- * @param callback
66
- * ```tsx
67
- * function App() {
68
- * onUpdated(() => {
69
- * console.log('App updated!')
70
- * return () => {
71
- * console.log('destroy prev update!')
72
- * }
73
- * })
74
- * return () => <div>...</div>
75
- * }
76
- * ```
77
- */
78
- export declare function onUpdated(callback: LifeCycleCallback): () => void;
79
- /**
80
- * 当组件 props 更新地调用
81
- * @param callback
82
- * @example
83
- * ```tsx
84
- * function YourComponent(props) {
85
- * onPropsChanged((currentProps, prevProps) => {
86
- * console.log(currentProps, prevProps)
87
- *
88
- * return () => {
89
- * console.log('destroy prev changed!')
90
- * }
91
- * })
92
- * return () => {
93
- * return <div>xxx</div>
94
- * }
95
- * }
96
- * ```
97
- */
98
- export declare function onPropsChanged<T extends Props>(callback: PropsChangedCallback<T>): () => void;
99
- /**
100
- * 当组件销毁时调用回调函数
101
- * @param callback
102
- */
103
- export declare function onUnmounted(callback: () => void): void;
104
- export interface RefListener<T> {
105
- (current: T): void | (() => void);
106
- }
107
- export type ExtractInstanceType<T, U = T extends (...args: any) => any ? ReturnType<T> : T> = U extends JSXInternal.ComponentInstance<any> ? Omit<U, keyof JSXInternal.ComponentInstance<any>> : U extends Function ? never : T;
108
- export interface AbstractInstanceType<T extends Record<string, any>> {
109
- (): T & JSXInternal.ComponentInstance<any>;
110
- }
111
- export declare class DynamicRef<T> {
112
- private callback;
113
- private unBindMap;
114
- private targetCaches;
115
- constructor(callback: RefListener<T>);
116
- bind(value: T): void;
117
- unBind(value: T): void;
118
- }
119
- /**
120
- * 用于节点渲染完成时获取 DOM 节点
121
- * @param callback 获取 DOM 节点的回调函数
122
- * @example
123
- * ```tsx
124
- * function App() {
125
- * const ref = createDynamicRef(node => {
126
- * function fn() {
127
- * // do something...
128
- * }
129
- * node.addEventListener('click', fn)
130
- * return () => {
131
- * node.removeEventListener('click', fn)
132
- * }
133
- * })
134
- * return () => {
135
- * return <div ref={ref}>xxx</div>
136
- * }
137
- * }
138
- * ```
139
- */
140
- export declare function createDynamicRef<T, U = ExtractInstanceType<T>>(callback: RefListener<U>): DynamicRef<U>;
141
- export declare class StaticRef<T> extends DynamicRef<T> {
142
- readonly current: T | null;
143
- constructor();
144
- }
145
- export declare function createRef<T, U = ExtractInstanceType<T>>(): StaticRef<U>;
146
- declare const depsKey: unique symbol;
147
- /**
148
- * 组件状态实例,直接调用可以获取最新的状态,通过 set 方法可以更新状态
149
- * ```
150
- */
151
- export interface Signal<T> {
152
- $isSignal: true;
153
- /**
154
- * 直接调用一个 Signal 实例,可以获取最新状态
155
- */
156
- (): T;
157
- /**
158
- * 更新组件状态的方法,可以传入最新的值
159
- * @param newState
160
- */
161
- set(newState: T): void;
162
- [depsKey]: Set<LifeCycleCallback>;
163
- }
164
- /**
165
- * 组件状态管理器
166
- * @param state 初始状态
167
- * @example
168
- * ```tsx
169
- * function App() {
170
- * // 初始化状态
171
- * const state = createSignal(1)
172
- *
173
- * return () => {
174
- * <div>
175
- * <div>当前值为:{state()}</div>
176
- * <div>
177
- * <button type="button" onClick={() => {
178
- * // 当点击时更新状态
179
- * state.set(state() + 1)
180
- * }
181
- * }>updateState</button>
182
- * </div>
183
- * </div>
184
- * }
185
- * }
186
- */
187
- export declare function createSignal<T>(state: T): Signal<T>;
188
- /**
189
- * 使用派生值,Viewfly 会收集回调函数内同步执行时访问的 Signal,
190
- * 并在你获取 useDerived 函数返回的 Signal 的值时,自动计算最新的值。
191
- *
192
- * @param callback
193
- * @param isContinue 可选的停止函数,在每次值更新后调用,当返回值为 false 时,将不再监听依赖的变化
194
- */
195
- export declare function createDerived<T>(callback: () => T, isContinue?: (data: T) => unknown): Signal<T>;
196
- export interface WatchCallback<T, U> {
197
- (newValue: T, oldValue: U): void | (() => void);
198
- }
199
- /**
200
- * 监听状态变化,当任意一个状态发生变更时,触发回调。
201
- * watch 会返回一个取消监听的函数,调用此函数,可以取消监听。
202
- * 当在组件中调用时,组件销毁时会自动取消监听。
203
- * @param deps 依赖的状态 Signal,可以是一个 Signal,只可以一个数包含 Signal 的数组,或者是一个求值函数
204
- * @param callback 状态变更后的回调函数
205
- */
206
- export declare function watch<T>(deps: Signal<T>, callback: WatchCallback<T, T>): () => void;
207
- export declare function watch<T>(deps: [Signal<T>], callback: WatchCallback<[T], [T]>): () => void;
208
- export declare function watch<T, T1>(deps: [Signal<T>, Signal<T1>], callback: WatchCallback<[T, T1], [T, T1]>): () => void;
209
- export declare function watch<T, T1, T2>(deps: [Signal<T>, Signal<T1>, Signal<T2>], callback: WatchCallback<[T, T1, T2], [T, T1, T2]>): () => void;
210
- export declare function watch<T, T1, T2, T3>(deps: [Signal<T>, Signal<T1>, Signal<T2>, Signal<T3>], callback: WatchCallback<[T, T1, T2, T3], [T, T1, T2, T3]>): () => void;
211
- export declare function watch<T, T1, T2, T3, T4>(deps: [Signal<T>, Signal<T1>, Signal<T2>, Signal<T3>, Signal<T4>], callback: WatchCallback<[T, T1, T2, T3, T4], [T, T1, T2, T3, T4]>): () => void;
212
- export declare function watch<T, T1, T2, T3, T4, T5>(deps: [Signal<T>, Signal<T1>, Signal<T2>, Signal<T3>, Signal<T4>, Signal<T5>], callback: WatchCallback<[T, T1, T2, T3, T4, T5], [T, T1, T2, T3, T4, T5]>): () => void;
213
- export declare function watch<T, T1, T2, T3, T4, T5, T6>(deps: [Signal<T>, Signal<T1>, Signal<T2>, Signal<T3>, Signal<T4>, Signal<T5>, Signal<T6>], callback: WatchCallback<[T, T1, T2, T3, T4, T5, T6], [T, T1, T2, T3, T4, T5, T6]>): () => void;
214
- export declare function watch<T, T1, T2, T3, T4, T5, T6, T7>(deps: [Signal<T>, Signal<T1>, Signal<T2>, Signal<T3>, Signal<T4>, Signal<T5>, Signal<T6>, Signal<T7>], callback: WatchCallback<[T, T1, T2, T3, T4, T5, T6, T7], [T, T1, T2, T3, T4, T5, T6, T7]>): () => void;
215
- export declare function watch<T>(deps: () => T, callback: WatchCallback<T, T>): () => void;
216
- export declare function watch<T = any>(deps: Signal<any>[], callback: WatchCallback<T[], T[]>): () => void;
217
- /**
218
- * 给组件添加注解
219
- * @param annotation
220
- * @param componentSetup
221
- * @example
222
- * ```ts
223
- * export customScope = new Scope('scopeName')
224
- * export const App = withAnnotation({
225
- * scope: customScope,
226
- * providers: [
227
- * ExampleService
228
- * ]
229
- * }, function(props: Props) {
230
- * return () => {
231
- * return <div>...</div>
232
- * }
233
- * })
234
- * ```
235
- */
236
- export declare function withAnnotation<T extends JSXInternal.ComponentSetup>(annotation: JSXInternal.ComponentAnnotation, componentSetup: T): T;
237
- /**
238
- * 通过组件上下文获取 IoC 容器内数据的勾子方法
239
- */
240
- export declare function inject<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T, flags?: InjectFlags, notFoundValue?: U): ExtractValueType<T> | U;
241
- /**
242
- * 获取当前组件实例
243
- */
244
- export declare function getCurrentInstance(): Component;
245
- export {};
@@ -1,18 +0,0 @@
1
- export type NativeNode = Record<string, any>;
2
- export declare abstract class NativeRenderer<ElementNode = NativeNode, TextNode = NativeNode> {
3
- abstract createElement(name: string, isSvg: boolean): ElementNode;
4
- abstract createTextNode(textContent: string, isSvg: boolean): TextNode;
5
- abstract setProperty(node: ElementNode, key: string, value: any, isSvg: boolean): void;
6
- abstract appendChild(parent: ElementNode, newChild: ElementNode | TextNode, isSvg: boolean): void;
7
- abstract prependChild(parent: ElementNode, newChild: ElementNode | TextNode, isSvg: boolean): void;
8
- abstract removeProperty(node: ElementNode, key: string, isSvg: boolean): void;
9
- abstract setStyle(target: ElementNode, key: string, value: any, isSvg: boolean): void;
10
- abstract removeStyle(target: ElementNode, key: string, isSvg: boolean): void;
11
- abstract setClass(target: ElementNode, value: string, isSvg: boolean): void;
12
- abstract listen<T = any>(node: ElementNode, type: string, callback: (ev: T) => any, isSvg: boolean): void;
13
- abstract unListen(node: ElementNode, type: string, callback: (ev: any) => any, isSvg: boolean): void;
14
- abstract remove(node: ElementNode | TextNode, isSvg: boolean): void;
15
- abstract cleanChildren(node: ElementNode, isSvg: boolean): void;
16
- abstract syncTextContent(target: TextNode, content: string, isSvg: boolean): void;
17
- abstract insertAfter(newNode: ElementNode | TextNode, ref: ElementNode | TextNode, isSvg: boolean): void;
18
- }
@@ -1,19 +0,0 @@
1
- import { ListenDelegate } from './_utils';
2
- export interface Props {
3
- children?: JSXInternal.ViewNode | JSXInternal.ViewNode[];
4
- [key: string]: any;
5
- [key: symbol]: any;
6
- }
7
- export declare function Fragment(props: Props): () => any;
8
- export type Key = number | string;
9
- export declare function jsx(type: string | JSXInternal.ComponentSetup, props: Props, key?: Key): JSXNode;
10
- export declare const jsxs: typeof jsx;
11
- export interface JSXNode<T = string | JSXInternal.ComponentSetup> {
12
- type: T;
13
- props: Props;
14
- key?: Key;
15
- on?: Record<string, ListenDelegate>;
16
- }
17
- export declare const JSXNodeFactory: {
18
- createNode<T = string | JSXInternal.ComponentSetup<any>>(type: T, props: Props, key?: Key): JSXNode<T>;
19
- };
@@ -1,2 +0,0 @@
1
- import { Props } from './jsx-element';
2
- export declare function withMemo<T extends Props = Props>(canUseMemo: JSXInternal.ComponentInstance<T>['$useMemo'], render: () => JSXInternal.ViewNode): JSXInternal.ComponentInstance<T>;
@@ -1,3 +0,0 @@
1
- import { NativeNode, NativeRenderer } from './injection-tokens';
2
- import { Component } from './component';
3
- export declare function createRenderer(component: Component, nativeRenderer: NativeRenderer): (host: NativeNode) => void;
@@ -1,10 +0,0 @@
1
- import { Component } from './component';
2
- import { Injector } from '../di/_api';
3
- /**
4
- * Viewfly 根组件,用于实现组件状态更新事件通知
5
- */
6
- export declare class RootComponent extends Component {
7
- private refresh;
8
- constructor(parentInjector: Injector | null, factory: JSXInternal.ComponentSetup, refresh: () => void);
9
- markAsChanged(changedComponent?: Component): void;
10
- }
@@ -1,43 +0,0 @@
1
- import { Key } from './jsx-element';
2
- import { ExtractInstanceType, DynamicRef } from './component';
3
- import { Scope } from '../di/injectable';
4
- import { NativeNode } from './injection-tokens';
5
- import { Provider } from '../di/provider';
6
- export type ViewNode = JSXInternal.ViewNode;
7
- declare global {
8
- namespace JSXInternal {
9
- type ClassNames = string | Record<string, unknown> | false | null | undefined | ClassNames[];
10
- interface ComponentInstance<P> {
11
- $portalHost?: NativeNode;
12
- $render(): ViewNode;
13
- $useMemo?(currentProps: P, prevProps: P): boolean;
14
- }
15
- type ViewNode = Element | JSXInternal.ElementClass | string | number | boolean | null | undefined | Iterable<ViewNode>;
16
- interface ComponentAnnotation {
17
- scope?: Scope;
18
- providers?: Provider[];
19
- }
20
- interface ComponentSetup<P = any> {
21
- (props: P): (() => ViewNode) | ComponentInstance<P>;
22
- annotation?: ComponentAnnotation;
23
- }
24
- type Element<P = any, C extends string | ComponentSetup<P> = string | ComponentSetup<P>> = C extends string ? IntrinsicElements[C] : (() => Element) | ComponentInstance<P>;
25
- interface IntrinsicAttributes {
26
- key?: Key;
27
- ref?: any;
28
- }
29
- interface RefAttributes<T> extends IntrinsicAttributes {
30
- ref?: DynamicRef<ExtractInstanceType<T>> | DynamicRef<ExtractInstanceType<T>>[];
31
- }
32
- interface ElementClass<P = any> extends ComponentInstance<P> {
33
- }
34
- interface ElementChildrenAttribute {
35
- }
36
- interface IntrinsicElements {
37
- [name: string]: any;
38
- }
39
- interface IntrinsicClassAttributes<T> {
40
- ref?: DynamicRef<T>;
41
- }
42
- }
43
- }
@@ -1,5 +0,0 @@
1
- import 'reflect-metadata';
2
- export * from './di/_api';
3
- export * from './_utils/make-error';
4
- export * from './foundation/_api';
5
- export * from './viewfly';
@@ -1,29 +0,0 @@
1
- import type { Provider } from './di/_api';
2
- import { NativeNode, NativeRenderer } from './foundation/_api';
3
- import { Injector } from './di/_api';
4
- /**
5
- * Viewfly 配置项
6
- */
7
- export interface Config {
8
- /** 根节点 */
9
- root: JSXInternal.ViewNode;
10
- /** 平台渲染器 */
11
- nativeRenderer: NativeRenderer;
12
- /** 应用的上下文 */
13
- context?: Injector;
14
- /** 是否自动更新视图 */
15
- autoUpdate?: boolean;
16
- }
17
- export interface Application<T extends NativeNode = NativeNode> {
18
- provide(providers: Provider | Provider[]): Application<T>;
19
- mount(host: T): Application<T>;
20
- use(module: Module | Module[]): Application<T>;
21
- render(): Application<T>;
22
- destroy(): void;
23
- }
24
- export interface Module {
25
- setup?(app: Application): void;
26
- onAfterStartup?(app: Application): void;
27
- onDestroy?(): void;
28
- }
29
- export declare function viewfly<T extends NativeNode>(config: Config): Application<T>;