@viewfly/core 1.1.9 → 1.2.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,18 +439,31 @@ 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;
456
450
  private invokeMountHooks;
457
451
  private invokeUpdatedHooks;
458
452
  }
453
+ /**
454
+ * 获取当前组件实例
455
+ */
456
+ declare function getCurrentInstance(): Component;
457
+
458
+ declare function createContext(providers: Provider[], scope?: Scope, parentInjector?: Injector): (props: Props) => () => JSXNode | JSXNode[];
459
+ /**
460
+ * 通过组件上下文获取 IoC 容器内数据的勾子方法
461
+ */
462
+ declare function inject<T extends Type<any> | AbstractType<any> | InjectionToken<any>, U = never>(token: T, notFoundValue?: U, flags?: InjectFlags): ExtractValueType<T> | U;
463
+ interface ComponentAnnotation {
464
+ scope?: Scope;
465
+ providers?: Provider[];
466
+ }
459
467
  /**
460
468
  * 给组件添加注解
461
469
  * @param annotation
@@ -476,15 +484,6 @@ declare class Component extends ReflectiveInjector {
476
484
  * ```
477
485
  */
478
486
  declare function withAnnotation<T extends ComponentSetup>(annotation: ComponentAnnotation, componentSetup: T): T;
479
- /**
480
- * 通过组件上下文获取 IoC 容器内数据的勾子方法
481
- */
482
- 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
487
  interface ContextProps extends Props {
489
488
  providers: Provider[];
490
489
  }
@@ -500,7 +499,7 @@ declare function createRenderer(component: Component, nativeRenderer: NativeRend
500
499
  */
501
500
  declare class RootComponent extends Component {
502
501
  private refresh;
503
- constructor(parentInjector: Injector | null, factory: ComponentSetup, refresh: () => void);
502
+ constructor(factory: ComponentSetup, refresh: () => void);
504
503
  markAsChanged(changedComponent?: Component): void;
505
504
  }
506
505
 
@@ -602,4 +601,4 @@ interface Module {
602
601
  }
603
602
  declare function viewfly<T extends NativeNode>(config: Config): Application<T>;
604
603
 
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 };
604
+ 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, createContext, createDerived, createDynamicRef, createRef, createRenderer, createSignal, forwardRef, getCurrentInstance, getSetupContext, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated, viewfly, watch, withAnnotation, withMemo };
@@ -2,7 +2,12 @@ import 'reflect-metadata';
2
2
 
3
3
  class ForwardRef {
4
4
  constructor(forwardRefFn) {
5
- this.forwardRefFn = forwardRefFn;
5
+ Object.defineProperty(this, "forwardRefFn", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: forwardRefFn
10
+ });
6
11
  }
7
12
  getRef() {
8
13
  return this.forwardRefFn();
@@ -21,9 +26,24 @@ function forwardRef(fn) {
21
26
  */
22
27
  class Annotations {
23
28
  constructor() {
24
- this.classes = new Map();
25
- this.props = new Map();
26
- this.params = new Map();
29
+ Object.defineProperty(this, "classes", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: new Map()
34
+ });
35
+ Object.defineProperty(this, "props", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: new Map()
40
+ });
41
+ Object.defineProperty(this, "params", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: new Map()
46
+ });
27
47
  }
28
48
  setClassMetadata(token, params) {
29
49
  this.classes.set(token, params);
@@ -110,7 +130,12 @@ function getAnnotations(target) {
110
130
 
111
131
  class Scope {
112
132
  constructor(name) {
113
- this.name = name;
133
+ Object.defineProperty(this, "name", {
134
+ enumerable: true,
135
+ configurable: true,
136
+ writable: true,
137
+ value: name
138
+ });
114
139
  }
115
140
  toString() {
116
141
  return this.name || '[anonymous provide scope]';
@@ -134,7 +159,12 @@ const Injectable = function InjectableDecorator(options) {
134
159
  /* eslint-disable-next-line */
135
160
  class InjectionToken {
136
161
  constructor(description) {
137
- this.description = description;
162
+ Object.defineProperty(this, "description", {
163
+ enumerable: true,
164
+ configurable: true,
165
+ writable: true,
166
+ value: description
167
+ });
138
168
  }
139
169
  toString() {
140
170
  return this.description || '[anonymous injection token]';
@@ -236,7 +266,12 @@ const nullInjectorErrorFn = (token) => {
236
266
  };
237
267
  class NullInjector {
238
268
  constructor() {
239
- this.parentInjector = null;
269
+ Object.defineProperty(this, "parentInjector", {
270
+ enumerable: true,
271
+ configurable: true,
272
+ writable: true,
273
+ value: null
274
+ });
240
275
  }
241
276
  /* eslint-disable-next-line */
242
277
  get(token, notFoundValue = THROW_IF_NOT_FOUND, _) {
@@ -412,9 +447,30 @@ const provideScopeError = (token) => {
412
447
  */
413
448
  class ReflectiveInjector {
414
449
  constructor(parentInjector, staticProviders, scope) {
415
- this.parentInjector = parentInjector;
416
- this.scope = scope;
417
- this.recordValues = new Map();
450
+ Object.defineProperty(this, "parentInjector", {
451
+ enumerable: true,
452
+ configurable: true,
453
+ writable: true,
454
+ value: parentInjector
455
+ });
456
+ Object.defineProperty(this, "scope", {
457
+ enumerable: true,
458
+ configurable: true,
459
+ writable: true,
460
+ value: scope
461
+ });
462
+ Object.defineProperty(this, "normalizedProviders", {
463
+ enumerable: true,
464
+ configurable: true,
465
+ writable: true,
466
+ value: void 0
467
+ });
468
+ Object.defineProperty(this, "recordValues", {
469
+ enumerable: true,
470
+ configurable: true,
471
+ writable: true,
472
+ value: new Map()
473
+ });
418
474
  this.normalizedProviders = staticProviders.map(provide => {
419
475
  return normalizeProvider(provide);
420
476
  });
@@ -620,8 +676,18 @@ const ComponentAtomType = Symbol('Component');
620
676
 
621
677
  class Listener {
622
678
  constructor(effect) {
623
- this.effect = effect;
624
- this.destroyCallbacks = [];
679
+ Object.defineProperty(this, "effect", {
680
+ enumerable: true,
681
+ configurable: true,
682
+ writable: true,
683
+ value: effect
684
+ });
685
+ Object.defineProperty(this, "destroyCallbacks", {
686
+ enumerable: true,
687
+ configurable: true,
688
+ writable: true,
689
+ value: []
690
+ });
625
691
  }
626
692
  destroy() {
627
693
  this.destroyCallbacks.forEach(fn => fn());
@@ -732,9 +798,24 @@ function onUnmounted(callback) {
732
798
 
733
799
  class DynamicRef {
734
800
  constructor(callback) {
735
- this.callback = callback;
736
- this.unBindMap = new Map();
737
- this.targetCaches = new Set();
801
+ Object.defineProperty(this, "callback", {
802
+ enumerable: true,
803
+ configurable: true,
804
+ writable: true,
805
+ value: callback
806
+ });
807
+ Object.defineProperty(this, "unBindMap", {
808
+ enumerable: true,
809
+ configurable: true,
810
+ writable: true,
811
+ value: new Map()
812
+ });
813
+ Object.defineProperty(this, "targetCaches", {
814
+ enumerable: true,
815
+ configurable: true,
816
+ writable: true,
817
+ value: new Set()
818
+ });
738
819
  }
739
820
  bind(value) {
740
821
  if (typeof value !== 'object' || value === null) {
@@ -793,6 +874,12 @@ class StaticRef extends DynamicRef {
793
874
  isInit = true;
794
875
  }
795
876
  });
877
+ Object.defineProperty(this, "current", {
878
+ enumerable: true,
879
+ configurable: true,
880
+ writable: true,
881
+ value: void 0
882
+ });
796
883
  Object.defineProperty(this, 'current', {
797
884
  get() {
798
885
  if (value === initValue) {
@@ -825,7 +912,7 @@ function toRefs(ref) {
825
912
  /**
826
913
  * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
827
914
  */
828
- class Component extends ReflectiveInjector {
915
+ class Component {
829
916
  get dirty() {
830
917
  return this._dirty;
831
918
  }
@@ -833,23 +920,109 @@ class Component extends ReflectiveInjector {
833
920
  return this._changed;
834
921
  }
835
922
  constructor(parentComponent, type, props, key) {
836
- const annotation = type.annotation || {};
837
- const providers = annotation.providers || [];
838
- super(parentComponent, [...providers, {
839
- provide: Injector,
840
- useFactory: () => this
841
- }], annotation.scope);
842
- this.parentComponent = parentComponent;
843
- this.type = type;
844
- this.props = props;
845
- this.key = key;
846
- this.changedSubComponents = new Set();
847
- this._dirty = true;
848
- this._changed = true;
849
- this.isFirstRendering = true;
850
- this.refs = null;
851
- this.listener = new Listener(() => {
852
- this.markAsDirtied();
923
+ Object.defineProperty(this, "parentComponent", {
924
+ enumerable: true,
925
+ configurable: true,
926
+ writable: true,
927
+ value: parentComponent
928
+ });
929
+ Object.defineProperty(this, "type", {
930
+ enumerable: true,
931
+ configurable: true,
932
+ writable: true,
933
+ value: type
934
+ });
935
+ Object.defineProperty(this, "props", {
936
+ enumerable: true,
937
+ configurable: true,
938
+ writable: true,
939
+ value: props
940
+ });
941
+ Object.defineProperty(this, "key", {
942
+ enumerable: true,
943
+ configurable: true,
944
+ writable: true,
945
+ value: key
946
+ });
947
+ Object.defineProperty(this, "instance", {
948
+ enumerable: true,
949
+ configurable: true,
950
+ writable: true,
951
+ value: void 0
952
+ });
953
+ Object.defineProperty(this, "changedSubComponents", {
954
+ enumerable: true,
955
+ configurable: true,
956
+ writable: true,
957
+ value: new Set()
958
+ });
959
+ Object.defineProperty(this, "unmountedCallbacks", {
960
+ enumerable: true,
961
+ configurable: true,
962
+ writable: true,
963
+ value: void 0
964
+ });
965
+ Object.defineProperty(this, "mountCallbacks", {
966
+ enumerable: true,
967
+ configurable: true,
968
+ writable: true,
969
+ value: void 0
970
+ });
971
+ Object.defineProperty(this, "propsChangedCallbacks", {
972
+ enumerable: true,
973
+ configurable: true,
974
+ writable: true,
975
+ value: void 0
976
+ });
977
+ Object.defineProperty(this, "updatedCallbacks", {
978
+ enumerable: true,
979
+ configurable: true,
980
+ writable: true,
981
+ value: void 0
982
+ });
983
+ Object.defineProperty(this, "updatedDestroyCallbacks", {
984
+ enumerable: true,
985
+ configurable: true,
986
+ writable: true,
987
+ value: void 0
988
+ });
989
+ Object.defineProperty(this, "propsChangedDestroyCallbacks", {
990
+ enumerable: true,
991
+ configurable: true,
992
+ writable: true,
993
+ value: void 0
994
+ });
995
+ Object.defineProperty(this, "_dirty", {
996
+ enumerable: true,
997
+ configurable: true,
998
+ writable: true,
999
+ value: true
1000
+ });
1001
+ Object.defineProperty(this, "_changed", {
1002
+ enumerable: true,
1003
+ configurable: true,
1004
+ writable: true,
1005
+ value: true
1006
+ });
1007
+ Object.defineProperty(this, "isFirstRendering", {
1008
+ enumerable: true,
1009
+ configurable: true,
1010
+ writable: true,
1011
+ value: true
1012
+ });
1013
+ Object.defineProperty(this, "refs", {
1014
+ enumerable: true,
1015
+ configurable: true,
1016
+ writable: true,
1017
+ value: null
1018
+ });
1019
+ Object.defineProperty(this, "listener", {
1020
+ enumerable: true,
1021
+ configurable: true,
1022
+ writable: true,
1023
+ value: new Listener(() => {
1024
+ this.markAsDirtied();
1025
+ })
853
1026
  });
854
1027
  }
855
1028
  markAsDirtied() {
@@ -952,10 +1125,6 @@ class Component extends ReflectiveInjector {
952
1125
  updateChildren(template);
953
1126
  this.rendered();
954
1127
  }
955
- provide(providers) {
956
- providers = Array.isArray(providers) ? providers : [providers];
957
- this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
958
- }
959
1128
  destroy() {
960
1129
  var _a, _b, _c;
961
1130
  this.listener.destroy();
@@ -972,13 +1141,12 @@ class Component extends ReflectiveInjector {
972
1141
  this.parentComponent.changedSubComponents.delete(this);
973
1142
  }
974
1143
  this.parentComponent =
975
- this.parentInjector =
976
- this.propsChangedDestroyCallbacks =
977
- this.updatedDestroyCallbacks =
978
- this.mountCallbacks =
979
- this.updatedCallbacks =
980
- this.propsChangedCallbacks =
981
- this.unmountedCallbacks = null;
1144
+ this.propsChangedDestroyCallbacks =
1145
+ this.updatedDestroyCallbacks =
1146
+ this.mountCallbacks =
1147
+ this.updatedCallbacks =
1148
+ this.propsChangedCallbacks =
1149
+ this.unmountedCallbacks = null;
982
1150
  this.changedSubComponents.clear();
983
1151
  }
984
1152
  rendered() {
@@ -1055,39 +1223,6 @@ class Component extends ReflectiveInjector {
1055
1223
  }
1056
1224
  }
1057
1225
  }
1058
- /**
1059
- * 给组件添加注解
1060
- * @param annotation
1061
- * @param componentSetup
1062
- * @example
1063
- * ```ts
1064
- * export customScope = new Scope('scopeName')
1065
- * export const App = withAnnotation({
1066
- * scope: customScope,
1067
- * providers: [
1068
- * ExampleService
1069
- * ]
1070
- * }, function(props: Props) {
1071
- * return () => {
1072
- * return <div>...</div>
1073
- * }
1074
- * })
1075
- * ```
1076
- */
1077
- function withAnnotation(annotation, componentSetup) {
1078
- const setup = function setup(props) {
1079
- return componentSetup(props);
1080
- };
1081
- setup.annotation = annotation;
1082
- return setup;
1083
- }
1084
- /**
1085
- * 通过组件上下文获取 IoC 容器内数据的勾子方法
1086
- */
1087
- function inject(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
1088
- const component = getSetupContext();
1089
- return component.get(token, notFoundValue, flags);
1090
- }
1091
1226
  /**
1092
1227
  * 获取当前组件实例
1093
1228
  */
@@ -1114,6 +1249,63 @@ const JSXNodeFactory = {
1114
1249
  }
1115
1250
  };
1116
1251
 
1252
+ const injectMap = new WeakMap();
1253
+ function getInjector(start) {
1254
+ while (start) {
1255
+ const injector = injectMap.get(start);
1256
+ if (injector) {
1257
+ return injector;
1258
+ }
1259
+ start = start.parentComponent;
1260
+ }
1261
+ return new NullInjector();
1262
+ }
1263
+ function createContext(providers, scope, parentInjector) {
1264
+ return function context(props) {
1265
+ const instance = getCurrentInstance();
1266
+ const injector = new ReflectiveInjector(parentInjector || getInjector(instance), providers, scope);
1267
+ injectMap.set(instance, injector);
1268
+ return () => {
1269
+ return props.children;
1270
+ };
1271
+ };
1272
+ }
1273
+ /**
1274
+ * 通过组件上下文获取 IoC 容器内数据的勾子方法
1275
+ */
1276
+ function inject(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
1277
+ const component = getCurrentInstance();
1278
+ const injector = getInjector(component);
1279
+ return injector.get(token, notFoundValue, flags);
1280
+ }
1281
+ /**
1282
+ * 给组件添加注解
1283
+ * @param annotation
1284
+ * @param componentSetup
1285
+ * @example
1286
+ * ```ts
1287
+ * export customScope = new Scope('scopeName')
1288
+ * export const App = withAnnotation({
1289
+ * scope: customScope,
1290
+ * providers: [
1291
+ * ExampleService
1292
+ * ]
1293
+ * }, function(props: Props) {
1294
+ * return () => {
1295
+ * return <div>...</div>
1296
+ * }
1297
+ * })
1298
+ * ```
1299
+ */
1300
+ function withAnnotation(annotation, componentSetup) {
1301
+ return function (props) {
1302
+ const instance = getCurrentInstance();
1303
+ const parentInjector = injectMap.get(instance) || getInjector(instance.parentComponent);
1304
+ const injector = new ReflectiveInjector(parentInjector, annotation.providers || [], annotation.scope);
1305
+ injectMap.set(instance, injector);
1306
+ return componentSetup(props);
1307
+ };
1308
+ }
1117
1309
  function Context(props) {
1118
1310
  function createContextComponent(providers) {
1119
1311
  return withAnnotation({
@@ -1228,9 +1420,8 @@ function applyChanges(nativeRenderer, component, atom, context, needMove) {
1228
1420
  view.host = context.host;
1229
1421
  view.isParent = context.isParent;
1230
1422
  }
1231
- }, () => {
1232
- // console.log(skipSubComponentDiff, '----')
1233
- //
1423
+ }, (skipSubComponentDiff) => {
1424
+ reuseComponentView(nativeRenderer, atom, context, needMove, skipSubComponentDiff);
1234
1425
  });
1235
1426
  }
1236
1427
  function diff(nativeRenderer, parentComponent, newAtom, oldAtom, context, needMove) {
@@ -1400,27 +1591,31 @@ function cleanElementChildren(atom, nativeRenderer) {
1400
1591
  }
1401
1592
  }
1402
1593
  function cleanView(nativeRenderer, atom, needClean) {
1403
- if (atom.nativeNode) {
1404
- if (needClean) {
1405
- nativeRenderer.remove(atom.nativeNode, atom.namespace);
1406
- needClean = false;
1407
- }
1408
- if (atom.type === ElementAtomType) {
1409
- const ref = atom.jsxNode.props[refKey];
1410
- applyRefs(ref, atom.nativeNode, false);
1594
+ if (atom.type === ComponentAtomType) {
1595
+ const jsxNode = atom.jsxNode;
1596
+ if (jsxNode.instance.$portalHost) {
1597
+ needClean = true;
1411
1598
  }
1599
+ cleanChildren(atom, nativeRenderer, needClean);
1600
+ jsxNode.destroy();
1601
+ return;
1602
+ }
1603
+ if (needClean) {
1604
+ nativeRenderer.remove(atom.nativeNode, atom.namespace);
1605
+ needClean = false;
1606
+ }
1607
+ if (atom.type === ElementAtomType) {
1608
+ const ref = atom.jsxNode.props[refKey];
1609
+ applyRefs(ref, atom.nativeNode, false);
1412
1610
  }
1611
+ cleanChildren(atom, nativeRenderer, needClean);
1612
+ }
1613
+ function cleanChildren(atom, nativeRenderer, needClean) {
1413
1614
  let child = atom.child;
1414
1615
  while (child) {
1415
- if (child.jsxNode instanceof Component && child.jsxNode.instance.$portalHost) {
1416
- needClean = true;
1417
- }
1418
1616
  cleanView(nativeRenderer, child, needClean);
1419
1617
  child = child.sibling;
1420
1618
  }
1421
- if (atom.jsxNode instanceof Component) {
1422
- atom.jsxNode.destroy();
1423
- }
1424
1619
  }
1425
1620
  function componentRender(nativeRenderer, component, from, context) {
1426
1621
  component.render((template, portalHost) => {
@@ -1473,7 +1668,9 @@ function createChainByNode(jsxNode, prevAtom, elementNamespace) {
1473
1668
  return prevAtom;
1474
1669
  }
1475
1670
  function createChainByChildren(children, prevAtom, elementNamespace) {
1476
- for (const item of children) {
1671
+ const len = children.length;
1672
+ for (let i = 0; i < len; i++) {
1673
+ const item = children[i];
1477
1674
  prevAtom = createChainByNode(item, prevAtom, elementNamespace);
1478
1675
  }
1479
1676
  return prevAtom;
@@ -1565,7 +1762,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1565
1762
  let unBindRefs;
1566
1763
  let bindRefs;
1567
1764
  let updatedChildren = false;
1568
- for (const [key, value] of changes.remove) {
1765
+ let len = changes.remove.length;
1766
+ for (let i = 0; i < len; i++) {
1767
+ const [key, value] = changes.remove[i];
1569
1768
  if (key === 'children') {
1570
1769
  updatedChildren = true;
1571
1770
  cleanElementChildren(oldAtom, nativeRenderer);
@@ -1593,7 +1792,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1593
1792
  }
1594
1793
  nativeRenderer.removeProperty(nativeNode, key, isSvg);
1595
1794
  }
1596
- for (const [key, newValue, oldValue] of changes.replace) {
1795
+ len = changes.replace.length;
1796
+ for (let i = 0; i < len; i++) {
1797
+ const [key, newValue, oldValue] = changes.replace[i];
1597
1798
  if (key === 'children') {
1598
1799
  updatedChildren = true;
1599
1800
  newAtom.child = createChildChain(newValue, isSvg);
@@ -1638,7 +1839,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1638
1839
  }
1639
1840
  nativeRenderer.setProperty(nativeNode, key, newValue, isSvg);
1640
1841
  }
1641
- for (const [key, value] of changes.add) {
1842
+ len = changes.add.length;
1843
+ for (let i = 0; i < len; i++) {
1844
+ const [key, value] = changes.add[i];
1642
1845
  if (key === 'children') {
1643
1846
  updatedChildren = true;
1644
1847
  newAtom.child = createChildChain(value, isSvg);
@@ -1678,7 +1881,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1678
1881
  function applyRefs(refs, nativeNode, binding) {
1679
1882
  if (refs) {
1680
1883
  const refList = Array.isArray(refs) ? refs : [refs];
1681
- for (const item of refList) {
1884
+ const len = refList.length;
1885
+ for (let i = 0; i < len; i++) {
1886
+ const item = refList[i];
1682
1887
  if (item instanceof DynamicRef) {
1683
1888
  binding ? item.bind(nativeNode) : item.unBind(nativeNode);
1684
1889
  }
@@ -1690,9 +1895,14 @@ function applyRefs(refs, nativeNode, binding) {
1690
1895
  * Viewfly 根组件,用于实现组件状态更新事件通知
1691
1896
  */
1692
1897
  class RootComponent extends Component {
1693
- constructor(parentInjector, factory, refresh) {
1694
- super(parentInjector, factory, {});
1695
- this.refresh = refresh;
1898
+ constructor(factory, refresh) {
1899
+ super(null, factory, {});
1900
+ Object.defineProperty(this, "refresh", {
1901
+ enumerable: true,
1902
+ configurable: true,
1903
+ writable: true,
1904
+ value: refresh
1905
+ });
1696
1906
  }
1697
1907
  markAsChanged(changedComponent) {
1698
1908
  this._changed = true;
@@ -1836,16 +2046,15 @@ function viewfly(config) {
1836
2046
  const modules = [];
1837
2047
  let destroyed = false;
1838
2048
  let appHost = null;
1839
- const rootComponent = new RootComponent(context || null, withAnnotation({
1840
- providers: [{
1841
- provide: NativeRenderer,
1842
- useValue: nativeRenderer
1843
- }]
1844
- }, () => {
2049
+ const rootProviders = [];
2050
+ const rootComponent = new RootComponent(() => {
2051
+ const rootContext = createContext(rootProviders, void 0, context);
1845
2052
  return () => {
1846
- return destroyed ? null : root;
2053
+ return jsx(rootContext, {
2054
+ children: destroyed ? null : root
2055
+ });
1847
2056
  };
1848
- }), function () {
2057
+ }, function () {
1849
2058
  if (destroyed || !autoUpdate) {
1850
2059
  return;
1851
2060
  }
@@ -1867,7 +2076,8 @@ function viewfly(config) {
1867
2076
  }
1868
2077
  const app = {
1869
2078
  provide(providers) {
1870
- rootComponent.provide(providers);
2079
+ providers = Array.isArray(providers) ? providers : [providers];
2080
+ rootProviders.push(...providers);
1871
2081
  return app;
1872
2082
  },
1873
2083
  use(module) {
@@ -1917,4 +2127,4 @@ function viewfly(config) {
1917
2127
  return app;
1918
2128
  }
1919
2129
 
1920
- 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 };
2130
+ 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, createContext, createDerived, createDynamicRef, createRef, createRenderer, createSignal, forwardRef, getCurrentInstance, getSetupContext, inject, jsx, jsxs, makeError, normalizeProvider, onMounted, onPropsChanged, onUnmounted, onUpdated, viewfly, watch, withAnnotation, withMemo };
package/bundles/index.js CHANGED
@@ -4,7 +4,12 @@ require('reflect-metadata');
4
4
 
5
5
  class ForwardRef {
6
6
  constructor(forwardRefFn) {
7
- this.forwardRefFn = forwardRefFn;
7
+ Object.defineProperty(this, "forwardRefFn", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: forwardRefFn
12
+ });
8
13
  }
9
14
  getRef() {
10
15
  return this.forwardRefFn();
@@ -23,9 +28,24 @@ function forwardRef(fn) {
23
28
  */
24
29
  class Annotations {
25
30
  constructor() {
26
- this.classes = new Map();
27
- this.props = new Map();
28
- this.params = new Map();
31
+ Object.defineProperty(this, "classes", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: new Map()
36
+ });
37
+ Object.defineProperty(this, "props", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: new Map()
42
+ });
43
+ Object.defineProperty(this, "params", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: new Map()
48
+ });
29
49
  }
30
50
  setClassMetadata(token, params) {
31
51
  this.classes.set(token, params);
@@ -112,7 +132,12 @@ function getAnnotations(target) {
112
132
 
113
133
  class Scope {
114
134
  constructor(name) {
115
- this.name = name;
135
+ Object.defineProperty(this, "name", {
136
+ enumerable: true,
137
+ configurable: true,
138
+ writable: true,
139
+ value: name
140
+ });
116
141
  }
117
142
  toString() {
118
143
  return this.name || '[anonymous provide scope]';
@@ -136,7 +161,12 @@ const Injectable = function InjectableDecorator(options) {
136
161
  /* eslint-disable-next-line */
137
162
  class InjectionToken {
138
163
  constructor(description) {
139
- this.description = description;
164
+ Object.defineProperty(this, "description", {
165
+ enumerable: true,
166
+ configurable: true,
167
+ writable: true,
168
+ value: description
169
+ });
140
170
  }
141
171
  toString() {
142
172
  return this.description || '[anonymous injection token]';
@@ -238,7 +268,12 @@ const nullInjectorErrorFn = (token) => {
238
268
  };
239
269
  class NullInjector {
240
270
  constructor() {
241
- this.parentInjector = null;
271
+ Object.defineProperty(this, "parentInjector", {
272
+ enumerable: true,
273
+ configurable: true,
274
+ writable: true,
275
+ value: null
276
+ });
242
277
  }
243
278
  /* eslint-disable-next-line */
244
279
  get(token, notFoundValue = THROW_IF_NOT_FOUND, _) {
@@ -414,9 +449,30 @@ const provideScopeError = (token) => {
414
449
  */
415
450
  class ReflectiveInjector {
416
451
  constructor(parentInjector, staticProviders, scope) {
417
- this.parentInjector = parentInjector;
418
- this.scope = scope;
419
- this.recordValues = new Map();
452
+ Object.defineProperty(this, "parentInjector", {
453
+ enumerable: true,
454
+ configurable: true,
455
+ writable: true,
456
+ value: parentInjector
457
+ });
458
+ Object.defineProperty(this, "scope", {
459
+ enumerable: true,
460
+ configurable: true,
461
+ writable: true,
462
+ value: scope
463
+ });
464
+ Object.defineProperty(this, "normalizedProviders", {
465
+ enumerable: true,
466
+ configurable: true,
467
+ writable: true,
468
+ value: void 0
469
+ });
470
+ Object.defineProperty(this, "recordValues", {
471
+ enumerable: true,
472
+ configurable: true,
473
+ writable: true,
474
+ value: new Map()
475
+ });
420
476
  this.normalizedProviders = staticProviders.map(provide => {
421
477
  return normalizeProvider(provide);
422
478
  });
@@ -622,8 +678,18 @@ const ComponentAtomType = Symbol('Component');
622
678
 
623
679
  class Listener {
624
680
  constructor(effect) {
625
- this.effect = effect;
626
- this.destroyCallbacks = [];
681
+ Object.defineProperty(this, "effect", {
682
+ enumerable: true,
683
+ configurable: true,
684
+ writable: true,
685
+ value: effect
686
+ });
687
+ Object.defineProperty(this, "destroyCallbacks", {
688
+ enumerable: true,
689
+ configurable: true,
690
+ writable: true,
691
+ value: []
692
+ });
627
693
  }
628
694
  destroy() {
629
695
  this.destroyCallbacks.forEach(fn => fn());
@@ -734,9 +800,24 @@ function onUnmounted(callback) {
734
800
 
735
801
  class DynamicRef {
736
802
  constructor(callback) {
737
- this.callback = callback;
738
- this.unBindMap = new Map();
739
- this.targetCaches = new Set();
803
+ Object.defineProperty(this, "callback", {
804
+ enumerable: true,
805
+ configurable: true,
806
+ writable: true,
807
+ value: callback
808
+ });
809
+ Object.defineProperty(this, "unBindMap", {
810
+ enumerable: true,
811
+ configurable: true,
812
+ writable: true,
813
+ value: new Map()
814
+ });
815
+ Object.defineProperty(this, "targetCaches", {
816
+ enumerable: true,
817
+ configurable: true,
818
+ writable: true,
819
+ value: new Set()
820
+ });
740
821
  }
741
822
  bind(value) {
742
823
  if (typeof value !== 'object' || value === null) {
@@ -795,6 +876,12 @@ class StaticRef extends DynamicRef {
795
876
  isInit = true;
796
877
  }
797
878
  });
879
+ Object.defineProperty(this, "current", {
880
+ enumerable: true,
881
+ configurable: true,
882
+ writable: true,
883
+ value: void 0
884
+ });
798
885
  Object.defineProperty(this, 'current', {
799
886
  get() {
800
887
  if (value === initValue) {
@@ -827,7 +914,7 @@ function toRefs(ref) {
827
914
  /**
828
915
  * Viewfly 组件管理类,用于管理组件的生命周期,上下文等
829
916
  */
830
- class Component extends ReflectiveInjector {
917
+ class Component {
831
918
  get dirty() {
832
919
  return this._dirty;
833
920
  }
@@ -835,23 +922,109 @@ class Component extends ReflectiveInjector {
835
922
  return this._changed;
836
923
  }
837
924
  constructor(parentComponent, type, props, key) {
838
- const annotation = type.annotation || {};
839
- const providers = annotation.providers || [];
840
- super(parentComponent, [...providers, {
841
- provide: Injector,
842
- useFactory: () => this
843
- }], annotation.scope);
844
- this.parentComponent = parentComponent;
845
- this.type = type;
846
- this.props = props;
847
- this.key = key;
848
- this.changedSubComponents = new Set();
849
- this._dirty = true;
850
- this._changed = true;
851
- this.isFirstRendering = true;
852
- this.refs = null;
853
- this.listener = new Listener(() => {
854
- this.markAsDirtied();
925
+ Object.defineProperty(this, "parentComponent", {
926
+ enumerable: true,
927
+ configurable: true,
928
+ writable: true,
929
+ value: parentComponent
930
+ });
931
+ Object.defineProperty(this, "type", {
932
+ enumerable: true,
933
+ configurable: true,
934
+ writable: true,
935
+ value: type
936
+ });
937
+ Object.defineProperty(this, "props", {
938
+ enumerable: true,
939
+ configurable: true,
940
+ writable: true,
941
+ value: props
942
+ });
943
+ Object.defineProperty(this, "key", {
944
+ enumerable: true,
945
+ configurable: true,
946
+ writable: true,
947
+ value: key
948
+ });
949
+ Object.defineProperty(this, "instance", {
950
+ enumerable: true,
951
+ configurable: true,
952
+ writable: true,
953
+ value: void 0
954
+ });
955
+ Object.defineProperty(this, "changedSubComponents", {
956
+ enumerable: true,
957
+ configurable: true,
958
+ writable: true,
959
+ value: new Set()
960
+ });
961
+ Object.defineProperty(this, "unmountedCallbacks", {
962
+ enumerable: true,
963
+ configurable: true,
964
+ writable: true,
965
+ value: void 0
966
+ });
967
+ Object.defineProperty(this, "mountCallbacks", {
968
+ enumerable: true,
969
+ configurable: true,
970
+ writable: true,
971
+ value: void 0
972
+ });
973
+ Object.defineProperty(this, "propsChangedCallbacks", {
974
+ enumerable: true,
975
+ configurable: true,
976
+ writable: true,
977
+ value: void 0
978
+ });
979
+ Object.defineProperty(this, "updatedCallbacks", {
980
+ enumerable: true,
981
+ configurable: true,
982
+ writable: true,
983
+ value: void 0
984
+ });
985
+ Object.defineProperty(this, "updatedDestroyCallbacks", {
986
+ enumerable: true,
987
+ configurable: true,
988
+ writable: true,
989
+ value: void 0
990
+ });
991
+ Object.defineProperty(this, "propsChangedDestroyCallbacks", {
992
+ enumerable: true,
993
+ configurable: true,
994
+ writable: true,
995
+ value: void 0
996
+ });
997
+ Object.defineProperty(this, "_dirty", {
998
+ enumerable: true,
999
+ configurable: true,
1000
+ writable: true,
1001
+ value: true
1002
+ });
1003
+ Object.defineProperty(this, "_changed", {
1004
+ enumerable: true,
1005
+ configurable: true,
1006
+ writable: true,
1007
+ value: true
1008
+ });
1009
+ Object.defineProperty(this, "isFirstRendering", {
1010
+ enumerable: true,
1011
+ configurable: true,
1012
+ writable: true,
1013
+ value: true
1014
+ });
1015
+ Object.defineProperty(this, "refs", {
1016
+ enumerable: true,
1017
+ configurable: true,
1018
+ writable: true,
1019
+ value: null
1020
+ });
1021
+ Object.defineProperty(this, "listener", {
1022
+ enumerable: true,
1023
+ configurable: true,
1024
+ writable: true,
1025
+ value: new Listener(() => {
1026
+ this.markAsDirtied();
1027
+ })
855
1028
  });
856
1029
  }
857
1030
  markAsDirtied() {
@@ -954,10 +1127,6 @@ class Component extends ReflectiveInjector {
954
1127
  updateChildren(template);
955
1128
  this.rendered();
956
1129
  }
957
- provide(providers) {
958
- providers = Array.isArray(providers) ? providers : [providers];
959
- this.normalizedProviders.unshift(...providers.map(i => normalizeProvider(i)));
960
- }
961
1130
  destroy() {
962
1131
  var _a, _b, _c;
963
1132
  this.listener.destroy();
@@ -974,13 +1143,12 @@ class Component extends ReflectiveInjector {
974
1143
  this.parentComponent.changedSubComponents.delete(this);
975
1144
  }
976
1145
  this.parentComponent =
977
- this.parentInjector =
978
- this.propsChangedDestroyCallbacks =
979
- this.updatedDestroyCallbacks =
980
- this.mountCallbacks =
981
- this.updatedCallbacks =
982
- this.propsChangedCallbacks =
983
- this.unmountedCallbacks = null;
1146
+ this.propsChangedDestroyCallbacks =
1147
+ this.updatedDestroyCallbacks =
1148
+ this.mountCallbacks =
1149
+ this.updatedCallbacks =
1150
+ this.propsChangedCallbacks =
1151
+ this.unmountedCallbacks = null;
984
1152
  this.changedSubComponents.clear();
985
1153
  }
986
1154
  rendered() {
@@ -1057,39 +1225,6 @@ class Component extends ReflectiveInjector {
1057
1225
  }
1058
1226
  }
1059
1227
  }
1060
- /**
1061
- * 给组件添加注解
1062
- * @param annotation
1063
- * @param componentSetup
1064
- * @example
1065
- * ```ts
1066
- * export customScope = new Scope('scopeName')
1067
- * export const App = withAnnotation({
1068
- * scope: customScope,
1069
- * providers: [
1070
- * ExampleService
1071
- * ]
1072
- * }, function(props: Props) {
1073
- * return () => {
1074
- * return <div>...</div>
1075
- * }
1076
- * })
1077
- * ```
1078
- */
1079
- function withAnnotation(annotation, componentSetup) {
1080
- const setup = function setup(props) {
1081
- return componentSetup(props);
1082
- };
1083
- setup.annotation = annotation;
1084
- return setup;
1085
- }
1086
- /**
1087
- * 通过组件上下文获取 IoC 容器内数据的勾子方法
1088
- */
1089
- function inject(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
1090
- const component = getSetupContext();
1091
- return component.get(token, notFoundValue, flags);
1092
- }
1093
1228
  /**
1094
1229
  * 获取当前组件实例
1095
1230
  */
@@ -1116,6 +1251,63 @@ const JSXNodeFactory = {
1116
1251
  }
1117
1252
  };
1118
1253
 
1254
+ const injectMap = new WeakMap();
1255
+ function getInjector(start) {
1256
+ while (start) {
1257
+ const injector = injectMap.get(start);
1258
+ if (injector) {
1259
+ return injector;
1260
+ }
1261
+ start = start.parentComponent;
1262
+ }
1263
+ return new NullInjector();
1264
+ }
1265
+ function createContext(providers, scope, parentInjector) {
1266
+ return function context(props) {
1267
+ const instance = getCurrentInstance();
1268
+ const injector = new ReflectiveInjector(parentInjector || getInjector(instance), providers, scope);
1269
+ injectMap.set(instance, injector);
1270
+ return () => {
1271
+ return props.children;
1272
+ };
1273
+ };
1274
+ }
1275
+ /**
1276
+ * 通过组件上下文获取 IoC 容器内数据的勾子方法
1277
+ */
1278
+ function inject(token, notFoundValue = THROW_IF_NOT_FOUND, flags) {
1279
+ const component = getCurrentInstance();
1280
+ const injector = getInjector(component);
1281
+ return injector.get(token, notFoundValue, flags);
1282
+ }
1283
+ /**
1284
+ * 给组件添加注解
1285
+ * @param annotation
1286
+ * @param componentSetup
1287
+ * @example
1288
+ * ```ts
1289
+ * export customScope = new Scope('scopeName')
1290
+ * export const App = withAnnotation({
1291
+ * scope: customScope,
1292
+ * providers: [
1293
+ * ExampleService
1294
+ * ]
1295
+ * }, function(props: Props) {
1296
+ * return () => {
1297
+ * return <div>...</div>
1298
+ * }
1299
+ * })
1300
+ * ```
1301
+ */
1302
+ function withAnnotation(annotation, componentSetup) {
1303
+ return function (props) {
1304
+ const instance = getCurrentInstance();
1305
+ const parentInjector = injectMap.get(instance) || getInjector(instance.parentComponent);
1306
+ const injector = new ReflectiveInjector(parentInjector, annotation.providers || [], annotation.scope);
1307
+ injectMap.set(instance, injector);
1308
+ return componentSetup(props);
1309
+ };
1310
+ }
1119
1311
  function Context(props) {
1120
1312
  function createContextComponent(providers) {
1121
1313
  return withAnnotation({
@@ -1230,9 +1422,8 @@ function applyChanges(nativeRenderer, component, atom, context, needMove) {
1230
1422
  view.host = context.host;
1231
1423
  view.isParent = context.isParent;
1232
1424
  }
1233
- }, () => {
1234
- // console.log(skipSubComponentDiff, '----')
1235
- //
1425
+ }, (skipSubComponentDiff) => {
1426
+ reuseComponentView(nativeRenderer, atom, context, needMove, skipSubComponentDiff);
1236
1427
  });
1237
1428
  }
1238
1429
  function diff(nativeRenderer, parentComponent, newAtom, oldAtom, context, needMove) {
@@ -1402,27 +1593,31 @@ function cleanElementChildren(atom, nativeRenderer) {
1402
1593
  }
1403
1594
  }
1404
1595
  function cleanView(nativeRenderer, atom, needClean) {
1405
- if (atom.nativeNode) {
1406
- if (needClean) {
1407
- nativeRenderer.remove(atom.nativeNode, atom.namespace);
1408
- needClean = false;
1409
- }
1410
- if (atom.type === ElementAtomType) {
1411
- const ref = atom.jsxNode.props[refKey];
1412
- applyRefs(ref, atom.nativeNode, false);
1596
+ if (atom.type === ComponentAtomType) {
1597
+ const jsxNode = atom.jsxNode;
1598
+ if (jsxNode.instance.$portalHost) {
1599
+ needClean = true;
1413
1600
  }
1601
+ cleanChildren(atom, nativeRenderer, needClean);
1602
+ jsxNode.destroy();
1603
+ return;
1604
+ }
1605
+ if (needClean) {
1606
+ nativeRenderer.remove(atom.nativeNode, atom.namespace);
1607
+ needClean = false;
1608
+ }
1609
+ if (atom.type === ElementAtomType) {
1610
+ const ref = atom.jsxNode.props[refKey];
1611
+ applyRefs(ref, atom.nativeNode, false);
1414
1612
  }
1613
+ cleanChildren(atom, nativeRenderer, needClean);
1614
+ }
1615
+ function cleanChildren(atom, nativeRenderer, needClean) {
1415
1616
  let child = atom.child;
1416
1617
  while (child) {
1417
- if (child.jsxNode instanceof Component && child.jsxNode.instance.$portalHost) {
1418
- needClean = true;
1419
- }
1420
1618
  cleanView(nativeRenderer, child, needClean);
1421
1619
  child = child.sibling;
1422
1620
  }
1423
- if (atom.jsxNode instanceof Component) {
1424
- atom.jsxNode.destroy();
1425
- }
1426
1621
  }
1427
1622
  function componentRender(nativeRenderer, component, from, context) {
1428
1623
  component.render((template, portalHost) => {
@@ -1475,7 +1670,9 @@ function createChainByNode(jsxNode, prevAtom, elementNamespace) {
1475
1670
  return prevAtom;
1476
1671
  }
1477
1672
  function createChainByChildren(children, prevAtom, elementNamespace) {
1478
- for (const item of children) {
1673
+ const len = children.length;
1674
+ for (let i = 0; i < len; i++) {
1675
+ const item = children[i];
1479
1676
  prevAtom = createChainByNode(item, prevAtom, elementNamespace);
1480
1677
  }
1481
1678
  return prevAtom;
@@ -1567,7 +1764,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1567
1764
  let unBindRefs;
1568
1765
  let bindRefs;
1569
1766
  let updatedChildren = false;
1570
- for (const [key, value] of changes.remove) {
1767
+ let len = changes.remove.length;
1768
+ for (let i = 0; i < len; i++) {
1769
+ const [key, value] = changes.remove[i];
1571
1770
  if (key === 'children') {
1572
1771
  updatedChildren = true;
1573
1772
  cleanElementChildren(oldAtom, nativeRenderer);
@@ -1595,7 +1794,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1595
1794
  }
1596
1795
  nativeRenderer.removeProperty(nativeNode, key, isSvg);
1597
1796
  }
1598
- for (const [key, newValue, oldValue] of changes.replace) {
1797
+ len = changes.replace.length;
1798
+ for (let i = 0; i < len; i++) {
1799
+ const [key, newValue, oldValue] = changes.replace[i];
1599
1800
  if (key === 'children') {
1600
1801
  updatedChildren = true;
1601
1802
  newAtom.child = createChildChain(newValue, isSvg);
@@ -1640,7 +1841,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1640
1841
  }
1641
1842
  nativeRenderer.setProperty(nativeNode, key, newValue, isSvg);
1642
1843
  }
1643
- for (const [key, value] of changes.add) {
1844
+ len = changes.add.length;
1845
+ for (let i = 0; i < len; i++) {
1846
+ const [key, value] = changes.add[i];
1644
1847
  if (key === 'children') {
1645
1848
  updatedChildren = true;
1646
1849
  newAtom.child = createChildChain(value, isSvg);
@@ -1680,7 +1883,9 @@ function updateNativeNodeProperties(nativeRenderer, newAtom, oldAtom, parentComp
1680
1883
  function applyRefs(refs, nativeNode, binding) {
1681
1884
  if (refs) {
1682
1885
  const refList = Array.isArray(refs) ? refs : [refs];
1683
- for (const item of refList) {
1886
+ const len = refList.length;
1887
+ for (let i = 0; i < len; i++) {
1888
+ const item = refList[i];
1684
1889
  if (item instanceof DynamicRef) {
1685
1890
  binding ? item.bind(nativeNode) : item.unBind(nativeNode);
1686
1891
  }
@@ -1692,9 +1897,14 @@ function applyRefs(refs, nativeNode, binding) {
1692
1897
  * Viewfly 根组件,用于实现组件状态更新事件通知
1693
1898
  */
1694
1899
  class RootComponent extends Component {
1695
- constructor(parentInjector, factory, refresh) {
1696
- super(parentInjector, factory, {});
1697
- this.refresh = refresh;
1900
+ constructor(factory, refresh) {
1901
+ super(null, factory, {});
1902
+ Object.defineProperty(this, "refresh", {
1903
+ enumerable: true,
1904
+ configurable: true,
1905
+ writable: true,
1906
+ value: refresh
1907
+ });
1698
1908
  }
1699
1909
  markAsChanged(changedComponent) {
1700
1910
  this._changed = true;
@@ -1838,16 +2048,15 @@ function viewfly(config) {
1838
2048
  const modules = [];
1839
2049
  let destroyed = false;
1840
2050
  let appHost = null;
1841
- const rootComponent = new RootComponent(context || null, withAnnotation({
1842
- providers: [{
1843
- provide: NativeRenderer,
1844
- useValue: nativeRenderer
1845
- }]
1846
- }, () => {
2051
+ const rootProviders = [];
2052
+ const rootComponent = new RootComponent(() => {
2053
+ const rootContext = createContext(rootProviders, void 0, context);
1847
2054
  return () => {
1848
- return destroyed ? null : root;
2055
+ return jsx(rootContext, {
2056
+ children: destroyed ? null : root
2057
+ });
1849
2058
  };
1850
- }), function () {
2059
+ }, function () {
1851
2060
  if (destroyed || !autoUpdate) {
1852
2061
  return;
1853
2062
  }
@@ -1869,7 +2078,8 @@ function viewfly(config) {
1869
2078
  }
1870
2079
  const app = {
1871
2080
  provide(providers) {
1872
- rootComponent.provide(providers);
2081
+ providers = Array.isArray(providers) ? providers : [providers];
2082
+ rootProviders.push(...providers);
1873
2083
  return app;
1874
2084
  },
1875
2085
  use(module) {
@@ -1943,6 +2153,7 @@ exports.StaticRef = StaticRef;
1943
2153
  exports.THROW_IF_NOT_FOUND = THROW_IF_NOT_FOUND;
1944
2154
  exports.Type = Type;
1945
2155
  exports.computed = computed;
2156
+ exports.createContext = createContext;
1946
2157
  exports.createDerived = createDerived;
1947
2158
  exports.createDynamicRef = createDynamicRef;
1948
2159
  exports.createRef = createRef;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viewfly/core",
3
- "version": "1.1.9",
3
+ "version": "1.2.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": "eb5d5ce380467cb05637ef42fdb539c327b19feb",
53
+ "gitHead": "8d13d8667f8875f24bc5218e9e4136d9efd9565a",
54
54
  "dependencies": {
55
55
  "reflect-metadata": "^0.2.2"
56
56
  }