@viewfly/core 0.0.24 → 0.0.25

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,3 +1,4 @@
1
+ import { Component, JSXElement, JSXInternal, JSXText, NativeNode, Props } from '@viewfly/core';
1
2
  export interface ListenDelegate {
2
3
  delegate: () => any;
3
4
  listenFn: ((...args: any[]) => any) | void;
@@ -11,3 +12,15 @@ export declare const refKey = "ref";
11
12
  export declare function getObjectChanges(newProps: Record<string, any>, oldProps: Record<string, any>): ObjectChanges;
12
13
  export declare function classToString(config: unknown): string;
13
14
  export declare function styleToObject(style: string | Record<string, any>): Record<string, any>;
15
+ export interface Atom {
16
+ jsxNode: JSXElement | JSXText | Component;
17
+ parent: Atom | null;
18
+ nativeNode: NativeNode | null;
19
+ child: Atom | null;
20
+ sibling: Atom | null;
21
+ }
22
+ export interface ComponentView {
23
+ atom: Atom;
24
+ template: JSXInternal.JSXNode;
25
+ render(newProps: Props, oldProps: Props): JSXInternal.JSXNode;
26
+ }
@@ -1,5 +1,6 @@
1
1
  import { AbstractType, InjectFlags, InjectionToken, Injector, Provider, ReflectiveInjector, Type } from '../di/_api';
2
2
  import { JSXTypeof, Key, Props } from './jsx-element';
3
+ import { ComponentView } from './_utils';
3
4
  import { JSXInternal } from './types';
4
5
  export declare class JSXComponent {
5
6
  props: Props;
@@ -11,10 +12,11 @@ export declare class JSXComponent {
11
12
  * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
12
13
  */
13
14
  export declare class Component extends ReflectiveInjector implements JSXTypeof {
14
- type: JSXInternal.ElementClass;
15
+ type: JSXInternal.ComponentConstructor;
15
16
  props: Props;
16
17
  key?: Key | undefined;
17
- $$typeOf: JSXInternal.ElementClass<any>;
18
+ $$typeOf: JSXInternal.ComponentConstructor<any>;
19
+ $$view: ComponentView;
18
20
  destroyCallbacks: LifeCycleCallback[];
19
21
  mountCallbacks: LifeCycleCallback[];
20
22
  propsChangedCallbacks: PropsChangedCallback<any>[];
@@ -28,12 +30,12 @@ export declare class Component extends ReflectiveInjector implements JSXTypeof {
28
30
  private propsChangedDestroyCallbacks;
29
31
  private unWatch?;
30
32
  private isFirstRending;
31
- constructor(context: Injector, type: JSXInternal.ElementClass, props: Props, key?: Key | undefined);
33
+ constructor(context: Injector, type: JSXInternal.ComponentConstructor, props: Props, key?: Key | undefined);
32
34
  is(target: JSXTypeof): boolean;
33
35
  provide<T>(providers: Provider<T> | Provider<T>[]): void;
34
36
  setup(): {
35
- template: JSXInternal.JSXNode;
36
- render: (newProps: Props, oldProps: Props) => JSXInternal.JSXNode;
37
+ template: any;
38
+ render: (newProps: Props, oldProps: Props) => any;
37
39
  };
38
40
  markAsDirtied(): void;
39
41
  markAsChanged(): void;
@@ -110,13 +112,13 @@ export type ExtractInstanceType<T, U = T extends (...args: any) => any ? ReturnT
110
112
  export interface AbstractInstanceType<T extends Record<string, any>> {
111
113
  (): T & JSXInternal.ComponentInstance<any>;
112
114
  }
113
- export declare class Ref<T, U> {
115
+ export declare class Ref<T> {
114
116
  private callback;
115
117
  private unBindMap;
116
118
  private targetCaches;
117
- constructor(callback: RefListener<U>);
118
- bind(value: U): void;
119
- unBind(value: U): void;
119
+ constructor(callback: RefListener<T>);
120
+ bind(value: T): void;
121
+ unBind(value: T): void;
120
122
  }
121
123
  /**
122
124
  * 用于节点渲染完成时获取 DOM 节点
@@ -139,7 +141,7 @@ export declare class Ref<T, U> {
139
141
  * }
140
142
  * ```
141
143
  */
142
- export declare function useRef<T, U = ExtractInstanceType<T>>(callback: RefListener<U>): Ref<T, U>;
144
+ export declare function useRef<T, U = ExtractInstanceType<T>>(callback: RefListener<U>): Ref<U>;
143
145
  declare const depsKey: unique symbol;
144
146
  /**
145
147
  * 组件状态实例,直接调用可以获取最新的状态,通过 set 方法可以更新状态
@@ -6,13 +6,13 @@ export interface Props {
6
6
  [key: string]: any;
7
7
  [key: symbol]: any;
8
8
  }
9
- export declare function Fragment(props: Props): () => JSXInternal.JSXNode | JSXInternal.JSXNode[];
9
+ export declare function Fragment(props: Props): () => any;
10
10
  export type Key = number | string;
11
11
  export declare function jsx(name: string, props: Props, key?: Key): JSXElement;
12
- export declare function jsx(setup: JSXInternal.ElementClass, props: Props, key?: Key): JSXComponent;
12
+ export declare function jsx(setup: JSXInternal.ComponentConstructor, props: Props, key?: Key): JSXComponent;
13
13
  export declare const jsxs: typeof jsx;
14
14
  export interface JSXTypeof {
15
- $$typeOf: string | JSXInternal.ElementClass;
15
+ $$typeOf: string | JSXInternal.ComponentConstructor;
16
16
  is(target: JSXTypeof): boolean;
17
17
  }
18
18
  export declare class JSXText implements JSXTypeof {
@@ -1,3 +1,3 @@
1
1
  import { Props } from './jsx-element';
2
2
  import { JSXInternal } from './types';
3
- export declare function withMemo<T extends Props = Props>(shouldUpdate: JSXInternal.ComponentInstance<T>['$shouldUpdate'], render: () => JSXInternal.Element): JSXInternal.ComponentInstance<T>;
3
+ export declare function withMemo<T extends Props = Props>(shouldUpdate: JSXInternal.ComponentInstance<T>['$shouldUpdate'], render: () => JSXInternal.JSXNode): JSXInternal.ComponentInstance<T>;
@@ -10,7 +10,6 @@ export declare class Renderer {
10
10
  private nativeRenderer;
11
11
  private rootComponentRef;
12
12
  private hostRef;
13
- private componentAtomCaches;
14
13
  private isInit;
15
14
  constructor(nativeRenderer: NativeRenderer, rootComponentRef: RootComponentRef, hostRef: HostRef);
16
15
  render(): void;
@@ -6,6 +6,6 @@ import { Injector } from '../di/_api';
6
6
  */
7
7
  export declare class RootComponent extends Component {
8
8
  onChange: (() => void) | null;
9
- constructor(factory: JSXInternal.ElementClass, parentInjector: Injector);
9
+ constructor(factory: JSXInternal.ComponentConstructor, parentInjector: Injector);
10
10
  markAsChanged(): void;
11
11
  }
@@ -8,22 +8,23 @@ export declare namespace JSXInternal {
8
8
  $shouldUpdate?(currentProps: P, prevProps: P): boolean;
9
9
  }
10
10
  type JSXNode = Element | JSXInternal.ElementClass | string | number | boolean | null | undefined | JSXNode[];
11
- interface Element<P = any, C extends string | ElementClass<P> = string | ElementClass<P>> {
12
- }
11
+ type ComponentConstructor<P = any> = (props: P) => (() => Element) | ComponentInstance<P>;
12
+ type Element<P = any, C extends string | ComponentConstructor<P> = string | ComponentConstructor<P>> = C extends string ? IntrinsicElements[C] : (() => Element) | ComponentInstance<P>;
13
13
  interface IntrinsicAttributes {
14
14
  key?: Key;
15
15
  ref?: any;
16
16
  }
17
17
  interface RefAttributes<T> extends IntrinsicAttributes {
18
- ref?: Ref<T, ExtractInstanceType<T>> | Ref<T, ExtractInstanceType<T>>[];
18
+ ref?: Ref<ExtractInstanceType<T>> | Ref<ExtractInstanceType<T>>[];
19
19
  }
20
- interface ElementClass<P = any> {
21
- (props?: P): () => (JSXNode | ComponentInstance<P>);
20
+ interface ElementClass<P = any> extends ComponentInstance<P> {
22
21
  }
23
22
  interface ElementChildrenAttribute {
24
23
  }
25
24
  interface IntrinsicElements {
25
+ [name: string]: any;
26
26
  }
27
- interface IntrinsicClassAttributes<T> extends RefAttributes<T> {
27
+ interface IntrinsicClassAttributes<T> {
28
+ ref?: Ref<T>;
28
29
  }
29
30
  }
@@ -1210,28 +1210,24 @@ class RootComponentRef {
1210
1210
  }
1211
1211
  class HostRef {
1212
1212
  }
1213
- class Atom {
1214
- constructor(jsxNode, parent) {
1215
- this.jsxNode = jsxNode;
1216
- this.parent = parent;
1217
- this.nativeNode = null;
1218
- this.child = null;
1219
- this.sibling = null;
1220
- }
1221
- }
1222
1213
  let Renderer = class Renderer {
1223
1214
  constructor(nativeRenderer, rootComponentRef, hostRef) {
1224
1215
  this.nativeRenderer = nativeRenderer;
1225
1216
  this.rootComponentRef = rootComponentRef;
1226
1217
  this.hostRef = hostRef;
1227
- this.componentAtomCaches = new WeakMap();
1228
1218
  this.isInit = true;
1229
1219
  }
1230
1220
  render() {
1231
1221
  const component = this.rootComponentRef.component;
1232
1222
  const host = this.hostRef.host;
1233
1223
  if (this.isInit) {
1234
- const atom = new Atom(component, null);
1224
+ const atom = {
1225
+ jsxNode: component,
1226
+ parent: null,
1227
+ sibling: null,
1228
+ child: null,
1229
+ nativeNode: null
1230
+ };
1235
1231
  this.buildView(atom, {
1236
1232
  isParent: true,
1237
1233
  host
@@ -1251,7 +1247,7 @@ let Renderer = class Renderer {
1251
1247
  component.rendered();
1252
1248
  }
1253
1249
  else if (component.changed) {
1254
- const atom = this.componentAtomCaches.get(component).atom.child;
1250
+ const atom = component.$$view.atom.child;
1255
1251
  this.reconcileElement(atom, context);
1256
1252
  component.rendered();
1257
1253
  }
@@ -1264,7 +1260,7 @@ let Renderer = class Renderer {
1264
1260
  }
1265
1261
  }
1266
1262
  getPrevSibling(component) {
1267
- let atom = this.componentAtomCaches.get(component).atom.child;
1263
+ let atom = component.$$view.atom.child;
1268
1264
  const childAtoms = [];
1269
1265
  while (atom) {
1270
1266
  childAtoms.push(atom);
@@ -1306,7 +1302,7 @@ let Renderer = class Renderer {
1306
1302
  }
1307
1303
  }
1308
1304
  applyChanges(component, context) {
1309
- const { atom, render } = this.componentAtomCaches.get(component);
1305
+ const { atom, render } = component.$$view;
1310
1306
  const diffAtom = atom.child;
1311
1307
  const template = render(component.props, component.props);
1312
1308
  if (template) {
@@ -1331,16 +1327,16 @@ let Renderer = class Renderer {
1331
1327
  const changeCommits = {
1332
1328
  updateComponent: (newAtom, reusedAtom, expectIndex, diffIndex) => {
1333
1329
  commits.push((offset) => {
1334
- const { render, template } = this.componentAtomCaches.get(reusedAtom.jsxNode);
1330
+ const { render, template } = reusedAtom.jsxNode.$$view;
1335
1331
  const newProps = newAtom.jsxNode.props;
1336
1332
  const oldProps = reusedAtom.jsxNode.props;
1337
1333
  newAtom.jsxNode = reusedAtom.jsxNode;
1338
1334
  const newTemplate = render(newProps, oldProps);
1339
- this.componentAtomCaches.set(newAtom.jsxNode, {
1335
+ newAtom.jsxNode.$$view = {
1340
1336
  render,
1341
1337
  template: newTemplate,
1342
1338
  atom: newAtom
1343
- });
1339
+ };
1344
1340
  if (newTemplate === template) {
1345
1341
  this.reuseComponentView(newAtom, reusedAtom, context, expectIndex !== diffIndex - offset);
1346
1342
  return;
@@ -1565,19 +1561,31 @@ let Renderer = class Renderer {
1565
1561
  if (template) {
1566
1562
  this.linkTemplate(template, component, from);
1567
1563
  }
1568
- this.componentAtomCaches.set(component, {
1564
+ component.$$view = {
1569
1565
  render,
1570
1566
  template,
1571
1567
  atom: from
1572
- });
1568
+ };
1573
1569
  return from;
1574
1570
  }
1575
1571
  createChainByComponentFactory(context, factory, parent) {
1576
1572
  const component = factory.createInstance(context);
1577
- return new Atom(component, parent);
1573
+ return {
1574
+ jsxNode: component,
1575
+ parent,
1576
+ sibling: null,
1577
+ child: null,
1578
+ nativeNode: null
1579
+ };
1578
1580
  }
1579
1581
  createChainByJSXElement(context, element, parent) {
1580
- const atom = new Atom(element, parent);
1582
+ const atom = {
1583
+ jsxNode: element,
1584
+ parent,
1585
+ sibling: null,
1586
+ child: null,
1587
+ nativeNode: null
1588
+ };
1581
1589
  if (Reflect.has(element.props, 'children')) {
1582
1590
  const jsxChildren = element.props.children;
1583
1591
  const children = this.createChainByChildren(context, Array.isArray(jsxChildren) ? jsxChildren : [jsxChildren], atom, []);
@@ -1586,7 +1594,13 @@ let Renderer = class Renderer {
1586
1594
  return atom;
1587
1595
  }
1588
1596
  createChainByJSXText(node, parent) {
1589
- return new Atom(node, parent);
1597
+ return {
1598
+ jsxNode: node,
1599
+ parent,
1600
+ sibling: null,
1601
+ child: null,
1602
+ nativeNode: null
1603
+ };
1590
1604
  }
1591
1605
  createChainByChildren(context, children, parent, atoms) {
1592
1606
  for (const item of children) {
package/bundles/index.js CHANGED
@@ -1212,28 +1212,24 @@ class RootComponentRef {
1212
1212
  }
1213
1213
  class HostRef {
1214
1214
  }
1215
- class Atom {
1216
- constructor(jsxNode, parent) {
1217
- this.jsxNode = jsxNode;
1218
- this.parent = parent;
1219
- this.nativeNode = null;
1220
- this.child = null;
1221
- this.sibling = null;
1222
- }
1223
- }
1224
1215
  exports.Renderer = class Renderer {
1225
1216
  constructor(nativeRenderer, rootComponentRef, hostRef) {
1226
1217
  this.nativeRenderer = nativeRenderer;
1227
1218
  this.rootComponentRef = rootComponentRef;
1228
1219
  this.hostRef = hostRef;
1229
- this.componentAtomCaches = new WeakMap();
1230
1220
  this.isInit = true;
1231
1221
  }
1232
1222
  render() {
1233
1223
  const component = this.rootComponentRef.component;
1234
1224
  const host = this.hostRef.host;
1235
1225
  if (this.isInit) {
1236
- const atom = new Atom(component, null);
1226
+ const atom = {
1227
+ jsxNode: component,
1228
+ parent: null,
1229
+ sibling: null,
1230
+ child: null,
1231
+ nativeNode: null
1232
+ };
1237
1233
  this.buildView(atom, {
1238
1234
  isParent: true,
1239
1235
  host
@@ -1253,7 +1249,7 @@ exports.Renderer = class Renderer {
1253
1249
  component.rendered();
1254
1250
  }
1255
1251
  else if (component.changed) {
1256
- const atom = this.componentAtomCaches.get(component).atom.child;
1252
+ const atom = component.$$view.atom.child;
1257
1253
  this.reconcileElement(atom, context);
1258
1254
  component.rendered();
1259
1255
  }
@@ -1266,7 +1262,7 @@ exports.Renderer = class Renderer {
1266
1262
  }
1267
1263
  }
1268
1264
  getPrevSibling(component) {
1269
- let atom = this.componentAtomCaches.get(component).atom.child;
1265
+ let atom = component.$$view.atom.child;
1270
1266
  const childAtoms = [];
1271
1267
  while (atom) {
1272
1268
  childAtoms.push(atom);
@@ -1308,7 +1304,7 @@ exports.Renderer = class Renderer {
1308
1304
  }
1309
1305
  }
1310
1306
  applyChanges(component, context) {
1311
- const { atom, render } = this.componentAtomCaches.get(component);
1307
+ const { atom, render } = component.$$view;
1312
1308
  const diffAtom = atom.child;
1313
1309
  const template = render(component.props, component.props);
1314
1310
  if (template) {
@@ -1333,16 +1329,16 @@ exports.Renderer = class Renderer {
1333
1329
  const changeCommits = {
1334
1330
  updateComponent: (newAtom, reusedAtom, expectIndex, diffIndex) => {
1335
1331
  commits.push((offset) => {
1336
- const { render, template } = this.componentAtomCaches.get(reusedAtom.jsxNode);
1332
+ const { render, template } = reusedAtom.jsxNode.$$view;
1337
1333
  const newProps = newAtom.jsxNode.props;
1338
1334
  const oldProps = reusedAtom.jsxNode.props;
1339
1335
  newAtom.jsxNode = reusedAtom.jsxNode;
1340
1336
  const newTemplate = render(newProps, oldProps);
1341
- this.componentAtomCaches.set(newAtom.jsxNode, {
1337
+ newAtom.jsxNode.$$view = {
1342
1338
  render,
1343
1339
  template: newTemplate,
1344
1340
  atom: newAtom
1345
- });
1341
+ };
1346
1342
  if (newTemplate === template) {
1347
1343
  this.reuseComponentView(newAtom, reusedAtom, context, expectIndex !== diffIndex - offset);
1348
1344
  return;
@@ -1567,19 +1563,31 @@ exports.Renderer = class Renderer {
1567
1563
  if (template) {
1568
1564
  this.linkTemplate(template, component, from);
1569
1565
  }
1570
- this.componentAtomCaches.set(component, {
1566
+ component.$$view = {
1571
1567
  render,
1572
1568
  template,
1573
1569
  atom: from
1574
- });
1570
+ };
1575
1571
  return from;
1576
1572
  }
1577
1573
  createChainByComponentFactory(context, factory, parent) {
1578
1574
  const component = factory.createInstance(context);
1579
- return new Atom(component, parent);
1575
+ return {
1576
+ jsxNode: component,
1577
+ parent,
1578
+ sibling: null,
1579
+ child: null,
1580
+ nativeNode: null
1581
+ };
1580
1582
  }
1581
1583
  createChainByJSXElement(context, element, parent) {
1582
- const atom = new Atom(element, parent);
1584
+ const atom = {
1585
+ jsxNode: element,
1586
+ parent,
1587
+ sibling: null,
1588
+ child: null,
1589
+ nativeNode: null
1590
+ };
1583
1591
  if (Reflect.has(element.props, 'children')) {
1584
1592
  const jsxChildren = element.props.children;
1585
1593
  const children = this.createChainByChildren(context, Array.isArray(jsxChildren) ? jsxChildren : [jsxChildren], atom, []);
@@ -1588,7 +1596,13 @@ exports.Renderer = class Renderer {
1588
1596
  return atom;
1589
1597
  }
1590
1598
  createChainByJSXText(node, parent) {
1591
- return new Atom(node, parent);
1599
+ return {
1600
+ jsxNode: node,
1601
+ parent,
1602
+ sibling: null,
1603
+ child: null,
1604
+ nativeNode: null
1605
+ };
1592
1606
  }
1593
1607
  createChainByChildren(context, children, parent, atoms) {
1594
1608
  for (const item of children) {
@@ -8,8 +8,7 @@ import { NativeElements } from '@viewfly/platform-browser';
8
8
  declare const jsxDEV: typeof jsx;
9
9
  export { jsx, jsxs, Fragment, jsxDEV };
10
10
  export declare namespace JSX {
11
- interface Element extends JSXInternal.Element {
12
- }
11
+ type Element = JSXInternal.Element;
13
12
  interface ElementClass extends JSXInternal.ElementClass {
14
13
  }
15
14
  interface IntrinsicElements extends NativeElements, JSXInternal.IntrinsicElements {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viewfly/core",
3
- "version": "0.0.24",
3
+ "version": "0.0.25",
4
4
  "description": "Viewfly is a simple and easy-to-use JavaScript framework with an intuitive development experience.",
5
5
  "main": "./bundles/index.js",
6
6
  "module": "./bundles/index.esm.js",
@@ -49,5 +49,5 @@
49
49
  "bugs": {
50
50
  "url": "https://github.com/viewfly/viewfly.git/issues"
51
51
  },
52
- "gitHead": "74ff7f03fca43463908a7cab8352ae1bf35a7f6c"
52
+ "gitHead": "0d39c19943b47221b99a6471e2614f55716c6659"
53
53
  }