@viewfly/core 1.0.4 → 1.1.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.
@@ -198,7 +198,7 @@ declare function Fragment(props: Props): () => JSXNode | JSXNode[];
198
198
  interface ContextProps extends Props {
199
199
  providers: Provider[];
200
200
  }
201
- declare function Context(props: ContextProps): () => JSXNode | JSXNode[];
201
+ declare function Context(props: ContextProps): () => ViewFlyNode<string | ComponentSetup<any>>;
202
202
  type Key = number | string;
203
203
  declare function jsx(type: string | ComponentSetup, props: Props & Record<string, any>, key?: Key): ViewFlyNode;
204
204
  declare const jsxs: typeof jsx;
@@ -211,23 +211,68 @@ declare const JSXNodeFactory: {
211
211
  createNode<T = string | ComponentSetup<any>>(type: T, props: Props & Record<string, any>, key?: Key): ViewFlyNode<T>;
212
212
  };
213
213
 
214
+ declare const TextAtomType: unique symbol;
215
+ declare const ElementAtomType: unique symbol;
216
+ declare const ComponentAtomType: unique symbol;
217
+ type ElementNamespace = string | undefined;
218
+ interface TextAtom {
219
+ type: typeof TextAtomType;
220
+ index: number;
221
+ jsxNode: string;
222
+ nodeType: string;
223
+ key?: null;
224
+ nativeNode: NativeNode | null;
225
+ child: Atom | null;
226
+ sibling: Atom | null;
227
+ namespace: ElementNamespace;
228
+ }
229
+ interface ElementAtom {
230
+ type: typeof ElementAtomType;
231
+ index: number;
232
+ nodeType: string;
233
+ key?: Key;
234
+ jsxNode: ViewFlyNode<string>;
235
+ nativeNode: NativeNode | null;
236
+ child: Atom | null;
237
+ sibling: Atom | null;
238
+ namespace: ElementNamespace;
239
+ }
240
+ interface ComponentAtom {
241
+ type: typeof ComponentAtomType;
242
+ index: number;
243
+ nodeType: ComponentSetup;
244
+ key?: Key;
245
+ jsxNode: ViewFlyNode<ComponentSetup> | Component;
246
+ nativeNode: NativeNode | null;
247
+ child: Atom | null;
248
+ sibling: Atom | null;
249
+ namespace: ElementNamespace;
250
+ }
251
+ type Atom = TextAtom | ElementAtom | ComponentAtom;
252
+ interface ComponentView {
253
+ atom: ComponentAtom;
254
+ host: NativeNode;
255
+ isParent: boolean;
256
+ rootHost: NativeNode;
257
+ }
258
+
214
259
  type NativeNode = Record<string, any>;
215
260
  declare abstract class NativeRenderer<ElementNode = NativeNode, TextNode = NativeNode> {
216
- abstract createElement(name: string, isSvg: boolean): ElementNode;
217
- abstract createTextNode(textContent: string, isSvg: boolean): TextNode;
218
- abstract setProperty(node: ElementNode, key: string, value: any, isSvg: boolean): void;
219
- abstract appendChild(parent: ElementNode, newChild: ElementNode | TextNode, isSvg: boolean): void;
220
- abstract prependChild(parent: ElementNode, newChild: ElementNode | TextNode, isSvg: boolean): void;
221
- abstract removeProperty(node: ElementNode, key: string, isSvg: boolean): void;
222
- abstract setStyle(target: ElementNode, key: string, value: any, isSvg: boolean): void;
223
- abstract removeStyle(target: ElementNode, key: string, isSvg: boolean): void;
224
- abstract setClass(target: ElementNode, value: string, isSvg: boolean): void;
225
- abstract listen<T = any>(node: ElementNode, type: string, callback: (ev: T) => any, isSvg: boolean): void;
226
- abstract unListen(node: ElementNode, type: string, callback: (ev: any) => any, isSvg: boolean): void;
227
- abstract remove(node: ElementNode | TextNode, isSvg: boolean): void;
228
- abstract cleanChildren(node: ElementNode, isSvg: boolean): void;
229
- abstract syncTextContent(target: TextNode, content: string, isSvg: boolean): void;
230
- abstract insertAfter(newNode: ElementNode | TextNode, ref: ElementNode | TextNode, isSvg: boolean): void;
261
+ abstract createElement(name: string, namespace: ElementNamespace): ElementNode;
262
+ abstract createTextNode(textContent: string, namespace: ElementNamespace): TextNode;
263
+ abstract setProperty(node: ElementNode, key: string, value: any, namespace: ElementNamespace): void;
264
+ abstract appendChild(parent: ElementNode, newChild: ElementNode | TextNode, namespace: ElementNamespace): void;
265
+ abstract prependChild(parent: ElementNode, newChild: ElementNode | TextNode, namespace: ElementNamespace): void;
266
+ abstract removeProperty(node: ElementNode, key: string, namespace: ElementNamespace): void;
267
+ abstract setStyle(target: ElementNode, key: string, value: any, namespace: ElementNamespace): void;
268
+ abstract removeStyle(target: ElementNode, key: string, namespace: ElementNamespace): void;
269
+ abstract setClass(target: ElementNode, value: string, namespace: ElementNamespace): void;
270
+ abstract listen<T = any>(node: ElementNode, type: string, callback: (ev: T) => any, namespace: ElementNamespace): void;
271
+ abstract unListen(node: ElementNode, type: string, callback: (ev: any) => any, namespace: ElementNamespace): void;
272
+ abstract remove(node: ElementNode | TextNode, namespace: ElementNamespace): void;
273
+ abstract cleanChildren(node: ElementNode, namespace: ElementNamespace): void;
274
+ abstract syncTextContent(target: TextNode, content: string, namespace: ElementNamespace): void;
275
+ abstract insertAfter(newNode: ElementNode | TextNode, ref: ElementNode | TextNode, namespace: ElementNamespace): void;
231
276
  }
232
277
 
233
278
  declare namespace JSX {
@@ -260,6 +305,7 @@ declare namespace JSX {
260
305
  }
261
306
  }
262
307
 
308
+ declare function getSetupContext(need?: boolean): Component;
263
309
  type ClassNames = string | Record<string, unknown> | false | null | undefined | ClassNames[];
264
310
  interface ComponentInstance<P> {
265
311
  $portalHost?: NativeNode;
@@ -295,9 +341,9 @@ declare class Component extends ReflectiveInjector {
295
341
  private propsChangedDestroyCallbacks?;
296
342
  protected _dirty: boolean;
297
343
  protected _changed: boolean;
298
- private unWatch?;
299
344
  private isFirstRendering;
300
345
  private refs;
346
+ private listener;
301
347
  constructor(parentComponent: Injector | null, type: ComponentSetup, props: Props, key?: Key | undefined);
302
348
  markAsDirtied(): void;
303
349
  markAsChanged(changedComponent?: Component): void;
@@ -314,7 +360,7 @@ interface LifeCycleCallback {
314
360
  (): void | (() => void);
315
361
  }
316
362
  interface PropsChangedCallback<T extends Props> {
317
- (currentProps: T | null, oldProps: T | null): void | (() => void);
363
+ (currentProps: T, oldProps: T): void | (() => void);
318
364
  }
319
365
  /**
320
366
  * 当组件第一次渲染完成时触发
@@ -412,13 +458,53 @@ declare class StaticRef<T> extends DynamicRef<T> {
412
458
  constructor();
413
459
  }
414
460
  declare function createRef<T, U = ExtractInstanceType<T>>(): StaticRef<U>;
415
- declare const depsKey: unique symbol;
416
461
  /**
417
- * 组件状态实例,直接调用可以获取最新的状态,通过 set 方法可以更新状态
462
+ * 给组件添加注解
463
+ * @param annotation
464
+ * @param componentSetup
465
+ * @example
466
+ * ```ts
467
+ * export customScope = new Scope('scopeName')
468
+ * export const App = withAnnotation({
469
+ * scope: customScope,
470
+ * providers: [
471
+ * ExampleService
472
+ * ]
473
+ * }, function(props: Props) {
474
+ * return () => {
475
+ * return <div>...</div>
476
+ * }
477
+ * })
418
478
  * ```
419
479
  */
480
+ declare function withAnnotation<T extends ComponentSetup>(annotation: ComponentAnnotation, componentSetup: T): T;
481
+ /**
482
+ * 通过组件上下文获取 IoC 容器内数据的勾子方法
483
+ */
484
+ declare function inject<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T, notFoundValue?: U, flags?: InjectFlags): ExtractValueType<T> | U;
485
+ /**
486
+ * 获取当前组件实例
487
+ */
488
+ declare function getCurrentInstance(): Component;
489
+
490
+ declare function withMemo<T extends Props = Props>(canUseMemo: ComponentInstance<T>['$useMemo'], render: () => JSXNode): ComponentInstance<T>;
491
+
492
+ declare const ElementNamespaceMap: Record<string, string>;
493
+ declare function createRenderer(component: Component, nativeRenderer: NativeRenderer, namespace: ElementNamespace): (host: NativeNode) => void;
494
+
495
+ /**
496
+ * Viewfly 根组件,用于实现组件状态更新事件通知
497
+ */
498
+ declare class RootComponent extends Component {
499
+ private refresh;
500
+ constructor(parentInjector: Injector | null, factory: ComponentSetup, refresh: () => void);
501
+ markAsChanged(changedComponent?: Component): void;
502
+ }
503
+
504
+ /**
505
+ * 组件状态实例,直接调用可以获取最新的状态,通过 set 方法可以更新状态
506
+ */
420
507
  interface Signal<T> {
421
- $isSignal: true;
422
508
  /**
423
509
  * 直接调用一个 Signal 实例,可以获取最新状态
424
510
  */
@@ -428,7 +514,6 @@ interface Signal<T> {
428
514
  * @param newState
429
515
  */
430
516
  set(newState: T): void;
431
- [depsKey]: Set<LifeCycleCallback>;
432
517
  }
433
518
  /**
434
519
  * 组件状态管理器
@@ -456,14 +541,15 @@ interface Signal<T> {
456
541
  declare function createSignal<T>(state: T): Signal<T>;
457
542
  /**
458
543
  * 使用派生值,Viewfly 会收集回调函数内同步执行时访问的 Signal,
459
- * 并在你获取 useDerived 函数返回的 Signal 的值时,自动计算最新的值。
544
+ * 并在你获取 createDerived 函数返回的 Signal 的值时,自动计算最新的值。
460
545
  *
461
- * @param callback
546
+ * @param fn
462
547
  * @param isContinue 可选的停止函数,在每次值更新后调用,当返回值为 false 时,将不再监听依赖的变化
463
548
  */
464
- declare function createDerived<T>(callback: () => T, isContinue?: (data: T) => unknown): Signal<T>;
465
- interface WatchCallback<T, U> {
466
- (newValue: T, oldValue: U): void | (() => void);
549
+ declare function computed<T>(fn: () => T, isContinue?: (data: T) => unknown): Signal<T>;
550
+ declare const createDerived: typeof computed;
551
+ interface WatchCallback<T> {
552
+ (newValue: T, oldValue: T): void | (() => void);
467
553
  }
468
554
  /**
469
555
  * 监听状态变化,当任意一个状态发生变更时,触发回调。
@@ -472,102 +558,17 @@ interface WatchCallback<T, U> {
472
558
  * @param deps 依赖的状态 Signal,可以是一个 Signal,只可以一个数包含 Signal 的数组,或者是一个求值函数
473
559
  * @param callback 状态变更后的回调函数
474
560
  */
475
- declare function watch<T>(deps: Signal<T>, callback: WatchCallback<T, T>): () => void;
476
- declare function watch<T>(deps: [Signal<T>], callback: WatchCallback<[T], [T]>): () => void;
477
- declare function watch<T, T1>(deps: [Signal<T>, Signal<T1>], callback: WatchCallback<[T, T1], [T, T1]>): () => void;
478
- declare function watch<T, T1, T2>(deps: [Signal<T>, Signal<T1>, Signal<T2>], callback: WatchCallback<[T, T1, T2], [T, T1, T2]>): () => void;
479
- 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;
480
- 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;
481
- 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;
482
- 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;
483
- 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;
484
- declare function watch<T>(deps: () => T, callback: WatchCallback<T, T>): () => void;
485
- declare function watch<T = any>(deps: Signal<any>[], callback: WatchCallback<T[], T[]>): () => void;
486
- /**
487
- * 给组件添加注解
488
- * @param annotation
489
- * @param componentSetup
490
- * @example
491
- * ```ts
492
- * export customScope = new Scope('scopeName')
493
- * export const App = withAnnotation({
494
- * scope: customScope,
495
- * providers: [
496
- * ExampleService
497
- * ]
498
- * }, function(props: Props) {
499
- * return () => {
500
- * return <div>...</div>
501
- * }
502
- * })
503
- * ```
504
- */
505
- declare function withAnnotation<T extends ComponentSetup>(annotation: ComponentAnnotation, componentSetup: T): T;
506
- /**
507
- * 通过组件上下文获取 IoC 容器内数据的勾子方法
508
- */
509
- declare function inject<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T, notFoundValue?: U, flags?: InjectFlags): ExtractValueType<T> | U;
510
- /**
511
- * 获取当前组件实例
512
- */
513
- declare function getCurrentInstance(): Component;
514
-
515
- declare function withMemo<T extends Props = Props>(canUseMemo: ComponentInstance<T>['$useMemo'], render: () => JSXNode): ComponentInstance<T>;
516
-
517
- declare function createRenderer(component: Component, nativeRenderer: NativeRenderer): (host: NativeNode) => void;
518
-
519
- /**
520
- * Viewfly 根组件,用于实现组件状态更新事件通知
521
- */
522
- declare class RootComponent extends Component {
523
- private refresh;
524
- constructor(parentInjector: Injector | null, factory: ComponentSetup, refresh: () => void);
525
- markAsChanged(changedComponent?: Component): void;
526
- }
527
-
528
- declare const TextAtomType: unique symbol;
529
- declare const ElementAtomType: unique symbol;
530
- declare const ComponentAtomType: unique symbol;
531
- interface TextAtom {
532
- type: typeof TextAtomType;
533
- index: number;
534
- jsxNode: string;
535
- nodeType: string;
536
- key?: null;
537
- nativeNode: NativeNode | null;
538
- child: Atom | null;
539
- sibling: Atom | null;
540
- isSvg: boolean;
541
- }
542
- interface ElementAtom {
543
- type: typeof ElementAtomType;
544
- index: number;
545
- nodeType: string;
546
- key?: Key;
547
- jsxNode: ViewFlyNode<string>;
548
- nativeNode: NativeNode | null;
549
- child: Atom | null;
550
- sibling: Atom | null;
551
- isSvg: boolean;
552
- }
553
- interface ComponentAtom {
554
- type: typeof ComponentAtomType;
555
- index: number;
556
- nodeType: ComponentSetup;
557
- key?: Key;
558
- jsxNode: ViewFlyNode<ComponentSetup> | Component;
559
- nativeNode: NativeNode | null;
560
- child: Atom | null;
561
- sibling: Atom | null;
562
- isSvg: boolean;
563
- }
564
- type Atom = TextAtom | ElementAtom | ComponentAtom;
565
- interface ComponentView {
566
- atom: ComponentAtom;
567
- host: NativeNode;
568
- isParent: boolean;
569
- rootHost: NativeNode;
570
- }
561
+ declare function watch<T>(deps: Signal<T>, callback: WatchCallback<T>): () => void;
562
+ declare function watch<T>(deps: [Signal<T>], callback: WatchCallback<[T]>): () => void;
563
+ declare function watch<T, T1>(deps: [Signal<T>, Signal<T1>], callback: WatchCallback<[T, T1]>): () => void;
564
+ declare function watch<T, T1, T2>(deps: [Signal<T>, Signal<T1>, Signal<T2>], callback: WatchCallback<[T, T1, T2]>): () => void;
565
+ declare function watch<T, T1, T2, T3>(deps: [Signal<T>, Signal<T1>, Signal<T2>, Signal<T3>], callback: WatchCallback<[T, T1, T2, T3]>): () => void;
566
+ 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]>): () => void;
567
+ 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]>): () => void;
568
+ 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]>): () => void;
569
+ 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]>): () => void;
570
+ declare function watch<T>(deps: () => T, callback: WatchCallback<T>): () => void;
571
+ declare function watch<T = any>(deps: Signal<any>[], callback: WatchCallback<T[]>): () => void;
571
572
 
572
573
  /**
573
574
  * Viewfly 配置项
@@ -581,6 +582,8 @@ interface Config {
581
582
  context?: Injector;
582
583
  /** 是否自动更新视图 */
583
584
  autoUpdate?: boolean;
585
+ /** 根节点命名空间 */
586
+ elementNamespace?: ElementNamespace;
584
587
  }
585
588
  interface Application<T extends NativeNode = NativeNode> {
586
589
  provide(providers: Provider | Provider[]): Application<T>;
@@ -596,4 +599,4 @@ interface Module {
596
599
  }
597
600
  declare function viewfly<T extends NativeNode>(config: Config): Application<T>;
598
601
 
599
- export { type AbstractInstanceType, type AbstractProvider, type AbstractType, type Application, type Atom, type ClassNames, type ClassProvider, Component, type ComponentAnnotation, type ComponentInstance, type ComponentSetup, type ComponentView, type Config, type ConstructorProvider, Context, type ContextProps, DynamicRef, type ExistingProvider, type ExtractInstanceType, type ExtractValueType, type FactoryProvider, ForwardRef, Fragment, Inject, type InjectDecorator, InjectFlags, Injectable, type InjectableDecorator, type InjectableOptions, InjectionToken, Injector, JSX, type JSXNode, JSXNodeFactory, type Key, type LifeCycleCallback, type Module, type NativeNode, NativeRenderer, type NormalizedProvider, NullInjector, Optional, type OptionalDecorator, Prop, type PropDecorator, type Props, type PropsChangedCallback, type ProvideScope, type Provider, type RefListener, type ReflectiveDependency, ReflectiveInjector, RootComponent, Scope, Self, type SelfDecorator, type Signal, SkipSelf, type SkipSelfDecorator, type StaticProvider, StaticRef, THROW_IF_NOT_FOUND, Type, type TypeProvider, type ValueProvider, type ViewFlyNode, type WatchCallback, createDerived, createDynamicRef, createRef, createRenderer, createSignal, forwardRef, getCurrentInstance, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated, viewfly, watch, withAnnotation, withMemo };
602
+ export { type AbstractInstanceType, type AbstractProvider, type AbstractType, type Application, type Atom, type ClassNames, type ClassProvider, Component, type ComponentAnnotation, type ComponentInstance, type ComponentSetup, type ComponentView, type Config, type ConstructorProvider, Context, type ContextProps, DynamicRef, type ElementNamespace, ElementNamespaceMap, type ExistingProvider, type ExtractInstanceType, type ExtractValueType, type FactoryProvider, ForwardRef, Fragment, Inject, type InjectDecorator, InjectFlags, Injectable, type InjectableDecorator, type InjectableOptions, InjectionToken, Injector, JSX, type JSXNode, JSXNodeFactory, type Key, type LifeCycleCallback, type Module, type NativeNode, NativeRenderer, type NormalizedProvider, NullInjector, Optional, type OptionalDecorator, Prop, type PropDecorator, type Props, type PropsChangedCallback, type ProvideScope, type Provider, type RefListener, type ReflectiveDependency, ReflectiveInjector, RootComponent, Scope, Self, type SelfDecorator, type Signal, SkipSelf, type SkipSelfDecorator, type StaticProvider, StaticRef, THROW_IF_NOT_FOUND, Type, type TypeProvider, type ValueProvider, type ViewFlyNode, type WatchCallback, computed, createDerived, createDynamicRef, createRef, createRenderer, createSignal, forwardRef, getCurrentInstance, getSetupContext, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated, viewfly, watch, withAnnotation, withMemo };