@viewfly/core 1.1.10 → 2.0.0-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.
@@ -413,19 +413,14 @@ interface ComponentInstance<P> {
413
413
  $useMemo?(currentProps: P, prevProps: P): boolean;
414
414
  }
415
415
  type JSXNode = JSX.Element | JSX.ElementClass | string | number | boolean | null | undefined | Iterable<JSXNode>;
416
- interface ComponentAnnotation {
417
- scope?: Scope;
418
- providers?: Provider[];
419
- }
420
416
  interface ComponentSetup<P = any> {
421
417
  (props: P): (() => JSXNode) | ComponentInstance<P>;
422
- annotation?: ComponentAnnotation;
423
418
  }
424
419
  /**
425
420
  * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
426
421
  */
427
- declare class Component extends ReflectiveInjector {
428
- private readonly parentComponent;
422
+ declare class Component {
423
+ readonly parentComponent: Component | null;
429
424
  readonly type: ComponentSetup;
430
425
  props: Props;
431
426
  readonly key?: Key | undefined;
@@ -444,12 +439,11 @@ declare class Component extends ReflectiveInjector {
444
439
  private isFirstRendering;
445
440
  private refs;
446
441
  private listener;
447
- constructor(parentComponent: Injector | null, type: ComponentSetup, props: Props, key?: Key | undefined);
442
+ constructor(parentComponent: Component | null, type: ComponentSetup, props: Props, key?: Key | undefined);
448
443
  markAsDirtied(): void;
449
444
  markAsChanged(changedComponent?: Component): void;
450
445
  render(update: (template: JSXNode, portalHost?: NativeNode) => void): void;
451
446
  update(newProps: Record<string, any>, updateChildren: (jsxNode: JSXNode) => void, reuseChildren: (skipSubComponentDiff: boolean) => void): void;
452
- provide<T>(providers: Provider<T> | Provider<T>[]): void;
453
447
  destroy(): void;
454
448
  rendered(): void;
455
449
  private invokePropsChangedHooks;
@@ -457,38 +451,15 @@ declare class Component extends ReflectiveInjector {
457
451
  private invokeUpdatedHooks;
458
452
  }
459
453
  /**
460
- * 给组件添加注解
461
- * @param annotation
462
- * @param componentSetup
463
- * @example
464
- * ```ts
465
- * export customScope = new Scope('scopeName')
466
- * export const App = withAnnotation({
467
- * scope: customScope,
468
- * providers: [
469
- * ExampleService
470
- * ]
471
- * }, function(props: Props) {
472
- * return () => {
473
- * return <div>...</div>
474
- * }
475
- * })
476
- * ```
454
+ * 获取当前组件实例
477
455
  */
478
- declare function withAnnotation<T extends ComponentSetup>(annotation: ComponentAnnotation, componentSetup: T): T;
456
+ declare function getCurrentInstance(): Component;
457
+
458
+ declare function createContext(providers: Provider[], scope?: Scope, parentInjector?: Injector): (props: Props) => () => JSXNode | JSXNode[];
479
459
  /**
480
460
  * 通过组件上下文获取 IoC 容器内数据的勾子方法
481
461
  */
482
462
  declare function inject<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T, notFoundValue?: U, flags?: InjectFlags): ExtractValueType<T> | U;
483
- /**
484
- * 获取当前组件实例
485
- */
486
- declare function getCurrentInstance(): Component;
487
-
488
- interface ContextProps extends Props {
489
- providers: Provider[];
490
- }
491
- declare function Context(props: ContextProps): () => ViewFlyNode<string | ComponentSetup<any>>;
492
463
 
493
464
  declare function withMemo<T extends Props = Props>(canUseMemo: ComponentInstance<T>['$useMemo'], render: () => JSXNode): ComponentInstance<T>;
494
465
 
@@ -500,7 +471,7 @@ declare function createRenderer(component: Component, nativeRenderer: NativeRend
500
471
  */
501
472
  declare class RootComponent extends Component {
502
473
  private refresh;
503
- constructor(parentInjector: Injector | null, factory: ComponentSetup, refresh: () => void);
474
+ constructor(factory: ComponentSetup, refresh: () => void);
504
475
  markAsChanged(changedComponent?: Component): void;
505
476
  }
506
477
 
@@ -602,4 +573,4 @@ interface Module {
602
573
  }
603
574
  declare function viewfly<T extends NativeNode>(config: Config): Application<T>;
604
575
 
605
- 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 };
576
+ export { type AbstractInstanceType, type AbstractProvider, type AbstractType, type Application, type Atom, type ClassNames, type ClassProvider, Component, type ComponentInstance, type ComponentSetup, type ComponentView, type Config, type ConstructorProvider, 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, createContext, createDerived, createDynamicRef, createRef, createRenderer, createSignal, forwardRef, getCurrentInstance, getSetupContext, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated, viewfly, watch, withMemo };
@@ -912,7 +912,7 @@ function toRefs(ref) {
912
912
  /**
913
913
  * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
914
914
  */
915
- class Component extends ReflectiveInjector {
915
+ class Component {
916
916
  get dirty() {
917
917
  return this._dirty;
918
918
  }
@@ -920,12 +920,6 @@ class Component extends ReflectiveInjector {
920
920
  return this._changed;
921
921
  }
922
922
  constructor(parentComponent, type, props, key) {
923
- const annotation = type.annotation || {};
924
- const providers = annotation.providers || [];
925
- super(parentComponent, [...providers, {
926
- provide: Injector,
927
- useFactory: () => this
928
- }], annotation.scope);
929
923
  Object.defineProperty(this, "parentComponent", {
930
924
  enumerable: true,
931
925
  configurable: true,
@@ -962,7 +956,6 @@ class Component extends ReflectiveInjector {
962
956
  writable: true,
963
957
  value: new Set()
964
958
  });
965
- // $$view!: ComponentView
966
959
  Object.defineProperty(this, "unmountedCallbacks", {
967
960
  enumerable: true,
968
961
  configurable: true,
@@ -1132,10 +1125,6 @@ class Component extends ReflectiveInjector {
1132
1125
  updateChildren(template);
1133
1126
  this.rendered();
1134
1127
  }
1135
- provide(providers) {
1136
- providers = Array.isArray(providers) ? providers : [providers];
1137
- this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
1138
- }
1139
1128
  destroy() {
1140
1129
  var _a, _b, _c;
1141
1130
  this.listener.destroy();
@@ -1152,13 +1141,12 @@ class Component extends ReflectiveInjector {
1152
1141
  this.parentComponent.changedSubComponents.delete(this);
1153
1142
  }
1154
1143
  this.parentComponent =
1155
- this.parentInjector =
1156
- this.propsChangedDestroyCallbacks =
1157
- this.updatedDestroyCallbacks =
1158
- this.mountCallbacks =
1159
- this.updatedCallbacks =
1160
- this.propsChangedCallbacks =
1161
- this.unmountedCallbacks = null;
1144
+ this.propsChangedDestroyCallbacks =
1145
+ this.updatedDestroyCallbacks =
1146
+ this.mountCallbacks =
1147
+ this.updatedCallbacks =
1148
+ this.propsChangedCallbacks =
1149
+ this.unmountedCallbacks = null;
1162
1150
  this.changedSubComponents.clear();
1163
1151
  }
1164
1152
  rendered() {
@@ -1236,43 +1224,44 @@ class Component extends ReflectiveInjector {
1236
1224
  }
1237
1225
  }
1238
1226
  /**
1239
- * 给组件添加注解
1240
- * @param annotation
1241
- * @param componentSetup
1242
- * @example
1243
- * ```ts
1244
- * export customScope = new Scope('scopeName')
1245
- * export const App = withAnnotation({
1246
- * scope: customScope,
1247
- * providers: [
1248
- * ExampleService
1249
- * ]
1250
- * }, function(props: Props) {
1251
- * return () => {
1252
- * return <div>...</div>
1253
- * }
1254
- * })
1255
- * ```
1227
+ * 获取当前组件实例
1256
1228
  */
1257
- function withAnnotation(annotation, componentSetup) {
1258
- const setup = function setup(props) {
1259
- return componentSetup(props);
1229
+ function getCurrentInstance() {
1230
+ return getSetupContext();
1231
+ }
1232
+
1233
+ const injectMap = new WeakMap();
1234
+ function getParentInjector(component) {
1235
+ let start = component.parentComponent;
1236
+ while (start) {
1237
+ const injector = injectMap.get(start);
1238
+ if (injector) {
1239
+ return injector;
1240
+ }
1241
+ start = start.parentComponent;
1242
+ }
1243
+ return new NullInjector();
1244
+ }
1245
+ function createContext(providers, scope, parentInjector) {
1246
+ return function context(props) {
1247
+ const instance = getCurrentInstance();
1248
+ const injector = new ReflectiveInjector(parentInjector || getParentInjector(instance), providers, scope);
1249
+ injectMap.set(instance, injector);
1250
+ return () => {
1251
+ return props.children;
1252
+ };
1260
1253
  };
1261
- setup.annotation = annotation;
1262
- return setup;
1263
1254
  }
1264
1255
  /**
1265
1256
  * 通过组件上下文获取 IoC 容器内数据的勾子方法
1266
1257
  */
1267
1258
  function inject(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
1268
- const component = getSetupContext();
1269
- return component.get(token, notFoundValue, flags);
1259
+ const component = getCurrentInstance();
1260
+ const injector = getParentInjector(component);
1261
+ return injector.get(token, notFoundValue, flags);
1270
1262
  }
1271
- /**
1272
- * 获取当前组件实例
1273
- */
1274
- function getCurrentInstance() {
1275
- return getSetupContext();
1263
+
1264
+ class NativeRenderer {
1276
1265
  }
1277
1266
 
1278
1267
  function Fragment(props) {
@@ -1294,33 +1283,6 @@ const JSXNodeFactory = {
1294
1283
  }
1295
1284
  };
1296
1285
 
1297
- function Context(props) {
1298
- function createContextComponent(providers) {
1299
- return withAnnotation({
1300
- providers,
1301
- }, (childProps) => {
1302
- return () => {
1303
- return childProps.children;
1304
- };
1305
- });
1306
- }
1307
- let contextComponent = createContextComponent(props.providers);
1308
- onPropsChanged((newProps, oldProps) => {
1309
- if (newProps.providers === oldProps.providers) {
1310
- return;
1311
- }
1312
- contextComponent = createContextComponent(newProps.providers);
1313
- });
1314
- return () => {
1315
- return jsx(contextComponent, {
1316
- children: props.children
1317
- });
1318
- };
1319
- }
1320
-
1321
- class NativeRenderer {
1322
- }
1323
-
1324
1286
  function withMemo(canUseMemo, render) {
1325
1287
  return {
1326
1288
  $useMemo: canUseMemo,
@@ -1408,9 +1370,8 @@ function applyChanges(nativeRenderer, component, atom, context, needMove) {
1408
1370
  view.host = context.host;
1409
1371
  view.isParent = context.isParent;
1410
1372
  }
1411
- }, () => {
1412
- // console.log(skipSubComponentDiff, '----')
1413
- //
1373
+ }, (skipSubComponentDiff) => {
1374
+ reuseComponentView(nativeRenderer, atom, context, needMove, skipSubComponentDiff);
1414
1375
  });
1415
1376
  }
1416
1377
  function diff(nativeRenderer, parentComponent, newAtom, oldAtom, context, needMove) {
@@ -1580,27 +1541,31 @@ function cleanElementChildren(atom, nativeRenderer) {
1580
1541
  }
1581
1542
  }
1582
1543
  function cleanView(nativeRenderer, atom, needClean) {
1583
- if (atom.nativeNode) {
1584
- if (needClean) {
1585
- nativeRenderer.remove(atom.nativeNode, atom.namespace);
1586
- needClean = false;
1587
- }
1588
- if (atom.type === ElementAtomType) {
1589
- const ref = atom.jsxNode.props[refKey];
1590
- applyRefs(ref, atom.nativeNode, false);
1544
+ if (atom.type === ComponentAtomType) {
1545
+ const jsxNode = atom.jsxNode;
1546
+ if (jsxNode.instance.$portalHost) {
1547
+ needClean = true;
1591
1548
  }
1549
+ cleanChildren(atom, nativeRenderer, needClean);
1550
+ jsxNode.destroy();
1551
+ return;
1552
+ }
1553
+ if (needClean) {
1554
+ nativeRenderer.remove(atom.nativeNode, atom.namespace);
1555
+ needClean = false;
1556
+ }
1557
+ if (atom.type === ElementAtomType) {
1558
+ const ref = atom.jsxNode.props[refKey];
1559
+ applyRefs(ref, atom.nativeNode, false);
1592
1560
  }
1561
+ cleanChildren(atom, nativeRenderer, needClean);
1562
+ }
1563
+ function cleanChildren(atom, nativeRenderer, needClean) {
1593
1564
  let child = atom.child;
1594
1565
  while (child) {
1595
- if (child.jsxNode instanceof Component && child.jsxNode.instance.$portalHost) {
1596
- needClean = true;
1597
- }
1598
1566
  cleanView(nativeRenderer, child, needClean);
1599
1567
  child = child.sibling;
1600
1568
  }
1601
- if (atom.jsxNode instanceof Component) {
1602
- atom.jsxNode.destroy();
1603
- }
1604
1569
  }
1605
1570
  function componentRender(nativeRenderer, component, from, context) {
1606
1571
  component.render((template, portalHost) => {
@@ -1653,7 +1618,9 @@ function createChainByNode(jsxNode, prevAtom, elementNamespace) {
1653
1618
  return prevAtom;
1654
1619
  }
1655
1620
  function createChainByChildren(children, prevAtom, elementNamespace) {
1656
- for (const item of children) {
1621
+ const len = children.length;
1622
+ for (let i = 0; i < len; i++) {
1623
+ const item = children[i];
1657
1624
  prevAtom = createChainByNode(item, prevAtom, elementNamespace);
1658
1625
  }
1659
1626
  return prevAtom;
@@ -1745,7 +1712,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1745
1712
  let unBindRefs;
1746
1713
  let bindRefs;
1747
1714
  let updatedChildren = false;
1748
- for (const [key, value] of changes.remove) {
1715
+ let len = changes.remove.length;
1716
+ for (let i = 0; i < len; i++) {
1717
+ const [key, value] = changes.remove[i];
1749
1718
  if (key === 'children') {
1750
1719
  updatedChildren = true;
1751
1720
  cleanElementChildren(oldAtom, nativeRenderer);
@@ -1773,7 +1742,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1773
1742
  }
1774
1743
  nativeRenderer.removeProperty(nativeNode, key, isSvg);
1775
1744
  }
1776
- for (const [key, newValue, oldValue] of changes.replace) {
1745
+ len = changes.replace.length;
1746
+ for (let i = 0; i < len; i++) {
1747
+ const [key, newValue, oldValue] = changes.replace[i];
1777
1748
  if (key === 'children') {
1778
1749
  updatedChildren = true;
1779
1750
  newAtom.child = createChildChain(newValue, isSvg);
@@ -1818,7 +1789,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1818
1789
  }
1819
1790
  nativeRenderer.setProperty(nativeNode, key, newValue, isSvg);
1820
1791
  }
1821
- for (const [key, value] of changes.add) {
1792
+ len = changes.add.length;
1793
+ for (let i = 0; i < len; i++) {
1794
+ const [key, value] = changes.add[i];
1822
1795
  if (key === 'children') {
1823
1796
  updatedChildren = true;
1824
1797
  newAtom.child = createChildChain(value, isSvg);
@@ -1858,7 +1831,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1858
1831
  function applyRefs(refs, nativeNode, binding) {
1859
1832
  if (refs) {
1860
1833
  const refList = Array.isArray(refs) ? refs : [refs];
1861
- for (const item of refList) {
1834
+ const len = refList.length;
1835
+ for (let i = 0; i < len; i++) {
1836
+ const item = refList[i];
1862
1837
  if (item instanceof DynamicRef) {
1863
1838
  binding ? item.bind(nativeNode) : item.unBind(nativeNode);
1864
1839
  }
@@ -1870,8 +1845,8 @@ function applyRefs(refs, nativeNode, binding) {
1870
1845
  * Viewfly 根组件,用于实现组件状态更新事件通知
1871
1846
  */
1872
1847
  class RootComponent extends Component {
1873
- constructor(parentInjector, factory, refresh) {
1874
- super(parentInjector, factory, {});
1848
+ constructor(factory, refresh) {
1849
+ super(null, factory, {});
1875
1850
  Object.defineProperty(this, "refresh", {
1876
1851
  enumerable: true,
1877
1852
  configurable: true,
@@ -2021,16 +1996,15 @@ function viewfly(config) {
2021
1996
  const modules = [];
2022
1997
  let destroyed = false;
2023
1998
  let appHost = null;
2024
- const rootComponent = new RootComponent(context || null, withAnnotation({
2025
- providers: [{
2026
- provide: NativeRenderer,
2027
- useValue: nativeRenderer
2028
- }]
2029
- }, () => {
1999
+ const rootProviders = [];
2000
+ const rootComponent = new RootComponent(() => {
2001
+ const rootContext = createContext(rootProviders, void 0, context);
2030
2002
  return () => {
2031
- return destroyed ? null : root;
2003
+ return jsx(rootContext, {
2004
+ children: destroyed ? null : root
2005
+ });
2032
2006
  };
2033
- }), function () {
2007
+ }, function () {
2034
2008
  if (destroyed || !autoUpdate) {
2035
2009
  return;
2036
2010
  }
@@ -2052,7 +2026,8 @@ function viewfly(config) {
2052
2026
  }
2053
2027
  const app = {
2054
2028
  provide(providers) {
2055
- rootComponent.provide(providers);
2029
+ providers = Array.isArray(providers) ? providers : [providers];
2030
+ rootProviders.push(...providers);
2056
2031
  return app;
2057
2032
  },
2058
2033
  use(module) {
@@ -2102,4 +2077,4 @@ function viewfly(config) {
2102
2077
  return app;
2103
2078
  }
2104
2079
 
2105
- export { Component, Context, DynamicRef, ElementNamespaceMap, ForwardRef, Fragment, Inject, InjectFlags, Injectable, InjectionToken, Injector, JSXNodeFactory, NativeRenderer, NullInjector, Optional, Prop, ReflectiveInjector, RootComponent, Scope, Self, SkipSelf, StaticRef, THROW_IF_NOT_FOUND, Type, computed, createDerived, createDynamicRef, createRef, createRenderer, createSignal, forwardRef, getCurrentInstance, getSetupContext, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated, viewfly, watch, withAnnotation, withMemo };
2080
+ export { Component, DynamicRef, ElementNamespaceMap, ForwardRef, Fragment, Inject, InjectFlags, Injectable, InjectionToken, Injector, JSXNodeFactory, NativeRenderer, NullInjector, Optional, Prop, ReflectiveInjector, RootComponent, Scope, Self, SkipSelf, StaticRef, THROW_IF_NOT_FOUND, Type, computed, createContext, createDerived, createDynamicRef, createRef, createRenderer, createSignal, forwardRef, getCurrentInstance, getSetupContext, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated, viewfly, watch, withMemo };
package/bundles/index.js CHANGED
@@ -914,7 +914,7 @@ function toRefs(ref) {
914
914
  /**
915
915
  * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
916
916
  */
917
- class Component extends ReflectiveInjector {
917
+ class Component {
918
918
  get dirty() {
919
919
  return this._dirty;
920
920
  }
@@ -922,12 +922,6 @@ class Component extends ReflectiveInjector {
922
922
  return this._changed;
923
923
  }
924
924
  constructor(parentComponent, type, props, key) {
925
- const annotation = type.annotation || {};
926
- const providers = annotation.providers || [];
927
- super(parentComponent, [...providers, {
928
- provide: Injector,
929
- useFactory: () => this
930
- }], annotation.scope);
931
925
  Object.defineProperty(this, "parentComponent", {
932
926
  enumerable: true,
933
927
  configurable: true,
@@ -964,7 +958,6 @@ class Component extends ReflectiveInjector {
964
958
  writable: true,
965
959
  value: new Set()
966
960
  });
967
- // $$view!: ComponentView
968
961
  Object.defineProperty(this, "unmountedCallbacks", {
969
962
  enumerable: true,
970
963
  configurable: true,
@@ -1134,10 +1127,6 @@ class Component extends ReflectiveInjector {
1134
1127
  updateChildren(template);
1135
1128
  this.rendered();
1136
1129
  }
1137
- provide(providers) {
1138
- providers = Array.isArray(providers) ? providers : [providers];
1139
- this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
1140
- }
1141
1130
  destroy() {
1142
1131
  var _a, _b, _c;
1143
1132
  this.listener.destroy();
@@ -1154,13 +1143,12 @@ class Component extends ReflectiveInjector {
1154
1143
  this.parentComponent.changedSubComponents.delete(this);
1155
1144
  }
1156
1145
  this.parentComponent =
1157
- this.parentInjector =
1158
- this.propsChangedDestroyCallbacks =
1159
- this.updatedDestroyCallbacks =
1160
- this.mountCallbacks =
1161
- this.updatedCallbacks =
1162
- this.propsChangedCallbacks =
1163
- this.unmountedCallbacks = null;
1146
+ this.propsChangedDestroyCallbacks =
1147
+ this.updatedDestroyCallbacks =
1148
+ this.mountCallbacks =
1149
+ this.updatedCallbacks =
1150
+ this.propsChangedCallbacks =
1151
+ this.unmountedCallbacks = null;
1164
1152
  this.changedSubComponents.clear();
1165
1153
  }
1166
1154
  rendered() {
@@ -1238,43 +1226,44 @@ class Component extends ReflectiveInjector {
1238
1226
  }
1239
1227
  }
1240
1228
  /**
1241
- * 给组件添加注解
1242
- * @param annotation
1243
- * @param componentSetup
1244
- * @example
1245
- * ```ts
1246
- * export customScope = new Scope('scopeName')
1247
- * export const App = withAnnotation({
1248
- * scope: customScope,
1249
- * providers: [
1250
- * ExampleService
1251
- * ]
1252
- * }, function(props: Props) {
1253
- * return () => {
1254
- * return <div>...</div>
1255
- * }
1256
- * })
1257
- * ```
1229
+ * 获取当前组件实例
1258
1230
  */
1259
- function withAnnotation(annotation, componentSetup) {
1260
- const setup = function setup(props) {
1261
- return componentSetup(props);
1231
+ function getCurrentInstance() {
1232
+ return getSetupContext();
1233
+ }
1234
+
1235
+ const injectMap = new WeakMap();
1236
+ function getParentInjector(component) {
1237
+ let start = component.parentComponent;
1238
+ while (start) {
1239
+ const injector = injectMap.get(start);
1240
+ if (injector) {
1241
+ return injector;
1242
+ }
1243
+ start = start.parentComponent;
1244
+ }
1245
+ return new NullInjector();
1246
+ }
1247
+ function createContext(providers, scope, parentInjector) {
1248
+ return function context(props) {
1249
+ const instance = getCurrentInstance();
1250
+ const injector = new ReflectiveInjector(parentInjector || getParentInjector(instance), providers, scope);
1251
+ injectMap.set(instance, injector);
1252
+ return () => {
1253
+ return props.children;
1254
+ };
1262
1255
  };
1263
- setup.annotation = annotation;
1264
- return setup;
1265
1256
  }
1266
1257
  /**
1267
1258
  * 通过组件上下文获取 IoC 容器内数据的勾子方法
1268
1259
  */
1269
1260
  function inject(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
1270
- const component = getSetupContext();
1271
- return component.get(token, notFoundValue, flags);
1261
+ const component = getCurrentInstance();
1262
+ const injector = getParentInjector(component);
1263
+ return injector.get(token, notFoundValue, flags);
1272
1264
  }
1273
- /**
1274
- * 获取当前组件实例
1275
- */
1276
- function getCurrentInstance() {
1277
- return getSetupContext();
1265
+
1266
+ class NativeRenderer {
1278
1267
  }
1279
1268
 
1280
1269
  function Fragment(props) {
@@ -1296,33 +1285,6 @@ const JSXNodeFactory = {
1296
1285
  }
1297
1286
  };
1298
1287
 
1299
- function Context(props) {
1300
- function createContextComponent(providers) {
1301
- return withAnnotation({
1302
- providers,
1303
- }, (childProps) => {
1304
- return () => {
1305
- return childProps.children;
1306
- };
1307
- });
1308
- }
1309
- let contextComponent = createContextComponent(props.providers);
1310
- onPropsChanged((newProps, oldProps) => {
1311
- if (newProps.providers === oldProps.providers) {
1312
- return;
1313
- }
1314
- contextComponent = createContextComponent(newProps.providers);
1315
- });
1316
- return () => {
1317
- return jsx(contextComponent, {
1318
- children: props.children
1319
- });
1320
- };
1321
- }
1322
-
1323
- class NativeRenderer {
1324
- }
1325
-
1326
1288
  function withMemo(canUseMemo, render) {
1327
1289
  return {
1328
1290
  $useMemo: canUseMemo,
@@ -1410,9 +1372,8 @@ function applyChanges(nativeRenderer, component, atom, context, needMove) {
1410
1372
  view.host = context.host;
1411
1373
  view.isParent = context.isParent;
1412
1374
  }
1413
- }, () => {
1414
- // console.log(skipSubComponentDiff, '----')
1415
- //
1375
+ }, (skipSubComponentDiff) => {
1376
+ reuseComponentView(nativeRenderer, atom, context, needMove, skipSubComponentDiff);
1416
1377
  });
1417
1378
  }
1418
1379
  function diff(nativeRenderer, parentComponent, newAtom, oldAtom, context, needMove) {
@@ -1582,27 +1543,31 @@ function cleanElementChildren(atom, nativeRenderer) {
1582
1543
  }
1583
1544
  }
1584
1545
  function cleanView(nativeRenderer, atom, needClean) {
1585
- if (atom.nativeNode) {
1586
- if (needClean) {
1587
- nativeRenderer.remove(atom.nativeNode, atom.namespace);
1588
- needClean = false;
1589
- }
1590
- if (atom.type === ElementAtomType) {
1591
- const ref = atom.jsxNode.props[refKey];
1592
- applyRefs(ref, atom.nativeNode, false);
1546
+ if (atom.type === ComponentAtomType) {
1547
+ const jsxNode = atom.jsxNode;
1548
+ if (jsxNode.instance.$portalHost) {
1549
+ needClean = true;
1593
1550
  }
1551
+ cleanChildren(atom, nativeRenderer, needClean);
1552
+ jsxNode.destroy();
1553
+ return;
1554
+ }
1555
+ if (needClean) {
1556
+ nativeRenderer.remove(atom.nativeNode, atom.namespace);
1557
+ needClean = false;
1558
+ }
1559
+ if (atom.type === ElementAtomType) {
1560
+ const ref = atom.jsxNode.props[refKey];
1561
+ applyRefs(ref, atom.nativeNode, false);
1594
1562
  }
1563
+ cleanChildren(atom, nativeRenderer, needClean);
1564
+ }
1565
+ function cleanChildren(atom, nativeRenderer, needClean) {
1595
1566
  let child = atom.child;
1596
1567
  while (child) {
1597
- if (child.jsxNode instanceof Component && child.jsxNode.instance.$portalHost) {
1598
- needClean = true;
1599
- }
1600
1568
  cleanView(nativeRenderer, child, needClean);
1601
1569
  child = child.sibling;
1602
1570
  }
1603
- if (atom.jsxNode instanceof Component) {
1604
- atom.jsxNode.destroy();
1605
- }
1606
1571
  }
1607
1572
  function componentRender(nativeRenderer, component, from, context) {
1608
1573
  component.render((template, portalHost) => {
@@ -1655,7 +1620,9 @@ function createChainByNode(jsxNode, prevAtom, elementNamespace) {
1655
1620
  return prevAtom;
1656
1621
  }
1657
1622
  function createChainByChildren(children, prevAtom, elementNamespace) {
1658
- for (const item of children) {
1623
+ const len = children.length;
1624
+ for (let i = 0; i < len; i++) {
1625
+ const item = children[i];
1659
1626
  prevAtom = createChainByNode(item, prevAtom, elementNamespace);
1660
1627
  }
1661
1628
  return prevAtom;
@@ -1747,7 +1714,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1747
1714
  let unBindRefs;
1748
1715
  let bindRefs;
1749
1716
  let updatedChildren = false;
1750
- for (const [key, value] of changes.remove) {
1717
+ let len = changes.remove.length;
1718
+ for (let i = 0; i < len; i++) {
1719
+ const [key, value] = changes.remove[i];
1751
1720
  if (key === 'children') {
1752
1721
  updatedChildren = true;
1753
1722
  cleanElementChildren(oldAtom, nativeRenderer);
@@ -1775,7 +1744,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1775
1744
  }
1776
1745
  nativeRenderer.removeProperty(nativeNode, key, isSvg);
1777
1746
  }
1778
- for (const [key, newValue, oldValue] of changes.replace) {
1747
+ len = changes.replace.length;
1748
+ for (let i = 0; i < len; i++) {
1749
+ const [key, newValue, oldValue] = changes.replace[i];
1779
1750
  if (key === 'children') {
1780
1751
  updatedChildren = true;
1781
1752
  newAtom.child = createChildChain(newValue, isSvg);
@@ -1820,7 +1791,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1820
1791
  }
1821
1792
  nativeRenderer.setProperty(nativeNode, key, newValue, isSvg);
1822
1793
  }
1823
- for (const [key, value] of changes.add) {
1794
+ len = changes.add.length;
1795
+ for (let i = 0; i < len; i++) {
1796
+ const [key, value] = changes.add[i];
1824
1797
  if (key === 'children') {
1825
1798
  updatedChildren = true;
1826
1799
  newAtom.child = createChildChain(value, isSvg);
@@ -1860,7 +1833,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1860
1833
  function applyRefs(refs, nativeNode, binding) {
1861
1834
  if (refs) {
1862
1835
  const refList = Array.isArray(refs) ? refs : [refs];
1863
- for (const item of refList) {
1836
+ const len = refList.length;
1837
+ for (let i = 0; i < len; i++) {
1838
+ const item = refList[i];
1864
1839
  if (item instanceof DynamicRef) {
1865
1840
  binding ? item.bind(nativeNode) : item.unBind(nativeNode);
1866
1841
  }
@@ -1872,8 +1847,8 @@ function applyRefs(refs, nativeNode, binding) {
1872
1847
  * Viewfly 根组件,用于实现组件状态更新事件通知
1873
1848
  */
1874
1849
  class RootComponent extends Component {
1875
- constructor(parentInjector, factory, refresh) {
1876
- super(parentInjector, factory, {});
1850
+ constructor(factory, refresh) {
1851
+ super(null, factory, {});
1877
1852
  Object.defineProperty(this, "refresh", {
1878
1853
  enumerable: true,
1879
1854
  configurable: true,
@@ -2023,16 +1998,15 @@ function viewfly(config) {
2023
1998
  const modules = [];
2024
1999
  let destroyed = false;
2025
2000
  let appHost = null;
2026
- const rootComponent = new RootComponent(context || null, withAnnotation({
2027
- providers: [{
2028
- provide: NativeRenderer,
2029
- useValue: nativeRenderer
2030
- }]
2031
- }, () => {
2001
+ const rootProviders = [];
2002
+ const rootComponent = new RootComponent(() => {
2003
+ const rootContext = createContext(rootProviders, void 0, context);
2032
2004
  return () => {
2033
- return destroyed ? null : root;
2005
+ return jsx(rootContext, {
2006
+ children: destroyed ? null : root
2007
+ });
2034
2008
  };
2035
- }), function () {
2009
+ }, function () {
2036
2010
  if (destroyed || !autoUpdate) {
2037
2011
  return;
2038
2012
  }
@@ -2054,7 +2028,8 @@ function viewfly(config) {
2054
2028
  }
2055
2029
  const app = {
2056
2030
  provide(providers) {
2057
- rootComponent.provide(providers);
2031
+ providers = Array.isArray(providers) ? providers : [providers];
2032
+ rootProviders.push(...providers);
2058
2033
  return app;
2059
2034
  },
2060
2035
  use(module) {
@@ -2105,7 +2080,6 @@ function viewfly(config) {
2105
2080
  }
2106
2081
 
2107
2082
  exports.Component = Component;
2108
- exports.Context = Context;
2109
2083
  exports.DynamicRef = DynamicRef;
2110
2084
  exports.ElementNamespaceMap = ElementNamespaceMap;
2111
2085
  exports.ForwardRef = ForwardRef;
@@ -2128,6 +2102,7 @@ exports.StaticRef = StaticRef;
2128
2102
  exports.THROW_IF_NOT_FOUND = THROW_IF_NOT_FOUND;
2129
2103
  exports.Type = Type;
2130
2104
  exports.computed = computed;
2105
+ exports.createContext = createContext;
2131
2106
  exports.createDerived = createDerived;
2132
2107
  exports.createDynamicRef = createDynamicRef;
2133
2108
  exports.createRef = createRef;
@@ -2147,5 +2122,4 @@ exports.onUnmounted = onUnmounted;
2147
2122
  exports.onUpdated = onUpdated;
2148
2123
  exports.viewfly = viewfly;
2149
2124
  exports.watch = watch;
2150
- exports.withAnnotation = withAnnotation;
2151
2125
  exports.withMemo = withMemo;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viewfly/core",
3
- "version": "1.1.10",
3
+ "version": "2.0.0-alpha.0",
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",
@@ -50,7 +50,7 @@
50
50
  "bugs": {
51
51
  "url": "https://github.com/viewfly/viewfly.git/issues"
52
52
  },
53
- "gitHead": "08cdabe11ffa8fb6c9c4bc53898881af2396f735",
53
+ "gitHead": "5fdcdcdca224d1eecdfd6a68abb1c3fb9c01d88a",
54
54
  "dependencies": {
55
55
  "reflect-metadata": "^0.2.2"
56
56
  }