@viewfly/core 0.0.1-alpha.8 → 0.0.1-alpha.9

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.
@@ -18,11 +18,10 @@ export declare class Renderer {
18
18
  private diff;
19
19
  private createChanges;
20
20
  private cleanView;
21
- private temporarilyRemove;
22
21
  private buildView;
23
22
  private componentRender;
24
23
  private createChainByComponentFactory;
25
- private createChain;
24
+ private createChainByTemplate;
26
25
  private createChainByJSXElement;
27
26
  private createChainByJSXText;
28
27
  private createChainByChildren;
@@ -72,24 +72,6 @@ class JSXText {
72
72
  return target.$$typeOf === this.$$typeOf;
73
73
  }
74
74
  }
75
- function flatChildren(jsxNodes) {
76
- const children = [];
77
- for (const node of jsxNodes) {
78
- if (node instanceof JSXElement || node instanceof JSXComponent) {
79
- children.push(node);
80
- }
81
- else if (typeof node === 'string' && node.length) {
82
- children.push(new JSXText(node));
83
- }
84
- else if (Array.isArray(node)) {
85
- children.push(...flatChildren(node));
86
- }
87
- else if (node !== null && typeof node !== 'undefined') {
88
- children.push(new JSXText(String(node)));
89
- }
90
- }
91
- return children;
92
- }
93
75
  class Props {
94
76
  constructor(props) {
95
77
  this.attrs = new Map();
@@ -104,10 +86,10 @@ class Props {
104
86
  if (key === 'children') {
105
87
  if (props.children !== null && typeof props.children !== 'undefined') {
106
88
  if (Array.isArray(props.children)) {
107
- this.children = flatChildren(props.children);
89
+ this.children = props.children;
108
90
  }
109
91
  else {
110
- this.children = flatChildren([props.children]);
92
+ this.children = [props.children];
111
93
  }
112
94
  }
113
95
  return;
@@ -887,10 +869,10 @@ let Renderer = class Renderer {
887
869
  });
888
870
  },
889
871
  reuseElement: (newAtom, oldAtom, expectIndex, oldIndex) => {
890
- commits.push(() => {
872
+ commits.push((offset) => {
891
873
  newAtom.nativeNode = oldAtom.nativeNode;
892
874
  const host = context.host;
893
- if (expectIndex !== oldIndex) {
875
+ if (expectIndex !== oldIndex - offset) {
894
876
  if (context.isParent) {
895
877
  this.nativeRenderer.prependChild(host, newAtom.nativeNode);
896
878
  }
@@ -914,9 +896,7 @@ let Renderer = class Renderer {
914
896
  atom = atom.sibling;
915
897
  }
916
898
  }
917
- if (applyRefs) {
918
- applyRefs();
919
- }
899
+ applyRefs();
920
900
  });
921
901
  },
922
902
  reuseText: (newAtom, oldAtom) => {
@@ -944,13 +924,24 @@ let Renderer = class Renderer {
944
924
  for (const item of oldChildren) {
945
925
  this.cleanView(item.atom, false);
946
926
  }
927
+ let j = 0;
928
+ let offset = 0;
929
+ const len = oldChildren.length;
947
930
  for (let i = 0; i < commits.length; i++) {
948
931
  const commit = commits[i];
949
- commit();
932
+ while (j < len) {
933
+ const current = oldChildren[j];
934
+ if (current.index <= i) {
935
+ offset++;
936
+ j++;
937
+ continue;
938
+ }
939
+ break;
940
+ }
941
+ commit(offset);
950
942
  }
951
943
  }
952
- createChanges(newAtom, lastIndex, oldChildren, changeCommits) {
953
- let isReuse = false;
944
+ createChanges(newAtom, expectIndex, oldChildren, changeCommits) {
954
945
  for (let i = 0; i < oldChildren.length; i++) {
955
946
  const { atom: diffAtom, index: diffIndex } = oldChildren[i];
956
947
  const key = newAtom.jsxNode.key;
@@ -959,23 +950,16 @@ let Renderer = class Renderer {
959
950
  if (diffKey !== key) {
960
951
  continue;
961
952
  }
962
- isReuse = lastIndex > diffIndex;
963
953
  }
964
954
  if (newAtom.jsxNode.is(diffAtom.jsxNode)) {
965
955
  if (newAtom.jsxNode instanceof JSXElement) {
966
- if (isReuse) {
967
- this.nativeRenderer.remove(diffAtom.nativeNode);
968
- }
969
- changeCommits.reuseElement(newAtom, diffAtom, lastIndex, diffIndex);
956
+ changeCommits.reuseElement(newAtom, diffAtom, expectIndex, diffIndex);
970
957
  }
971
958
  else if (newAtom.jsxNode instanceof JSXText) {
972
959
  changeCommits.reuseText(newAtom, diffAtom);
973
960
  }
974
961
  else {
975
- if (isReuse) {
976
- this.temporarilyRemove(diffAtom);
977
- }
978
- changeCommits.reuseComponent(newAtom, diffAtom, lastIndex, diffIndex);
962
+ changeCommits.reuseComponent(newAtom, diffAtom, expectIndex, diffIndex);
979
963
  }
980
964
  oldChildren.splice(i, 1);
981
965
  return;
@@ -1003,18 +987,6 @@ let Renderer = class Renderer {
1003
987
  atom.jsxNode.destroy();
1004
988
  }
1005
989
  }
1006
- temporarilyRemove(atom) {
1007
- let next = atom.child;
1008
- while (next) {
1009
- if (next.jsxNode instanceof Component) {
1010
- this.temporarilyRemove(next);
1011
- }
1012
- else {
1013
- this.nativeRenderer.remove(next.nativeNode);
1014
- }
1015
- next = next.sibling;
1016
- }
1017
- }
1018
990
  buildView(atom, context) {
1019
991
  if (atom.jsxNode instanceof Component) {
1020
992
  this.componentRender(atom.jsxNode, atom);
@@ -1079,14 +1051,14 @@ let Renderer = class Renderer {
1079
1051
  }
1080
1052
  return new Atom(component, parent);
1081
1053
  }
1082
- createChain(context, template, parent) {
1054
+ createChainByTemplate(context, template, parent) {
1083
1055
  if (template instanceof JSXElement) {
1084
1056
  return this.createChainByJSXElement(context, template, parent);
1085
1057
  }
1086
- if (template instanceof JSXText) {
1087
- return this.createChainByJSXText(template, parent);
1058
+ if (template instanceof JSXComponent) {
1059
+ return this.createChainByComponentFactory(context, template, parent);
1088
1060
  }
1089
- return this.createChainByComponentFactory(context, template, parent);
1061
+ return parent;
1090
1062
  }
1091
1063
  createChainByJSXElement(context, element, parent) {
1092
1064
  const atom = new Atom(element, parent);
@@ -1100,19 +1072,37 @@ let Renderer = class Renderer {
1100
1072
  createChainByChildren(context, children, parent) {
1101
1073
  const atoms = [];
1102
1074
  for (const item of children) {
1103
- const child = this.createChain(context, item, parent);
1104
- if (Array.isArray(child)) {
1105
- atoms.push(...child);
1075
+ if (item instanceof JSXElement) {
1076
+ atoms.push(this.createChainByJSXElement(context, item, parent));
1077
+ continue;
1106
1078
  }
1107
- else {
1108
- atoms.push(child);
1079
+ if (item instanceof JSXComponent) {
1080
+ const childAtom = this.createChainByComponentFactory(context, item, parent);
1081
+ if (Array.isArray(childAtom)) {
1082
+ atoms.push(...childAtom);
1083
+ }
1084
+ else {
1085
+ atoms.push(childAtom);
1086
+ }
1087
+ continue;
1088
+ }
1089
+ if (typeof item === 'string' && item.length) {
1090
+ atoms.push(this.createChainByJSXText(new JSXText(item), parent));
1091
+ continue;
1092
+ }
1093
+ if (Array.isArray(item)) {
1094
+ atoms.push(...this.createChainByChildren(context, item, parent));
1095
+ continue;
1096
+ }
1097
+ if (item !== null && typeof item !== 'undefined') {
1098
+ atoms.push(this.createChainByJSXText(new JSXText(String(item)), parent));
1109
1099
  }
1110
1100
  }
1111
1101
  return atoms;
1112
1102
  }
1113
1103
  linkTemplate(template, component, parent) {
1114
1104
  if (template) {
1115
- const child = this.createChain(component, template, parent);
1105
+ const child = this.createChainByTemplate(component, template, parent);
1116
1106
  this.link(parent, Array.isArray(child) ? child : [child]);
1117
1107
  }
1118
1108
  }
package/bundles/index.js CHANGED
@@ -73,24 +73,6 @@ class JSXText {
73
73
  return target.$$typeOf === this.$$typeOf;
74
74
  }
75
75
  }
76
- function flatChildren(jsxNodes) {
77
- const children = [];
78
- for (const node of jsxNodes) {
79
- if (node instanceof JSXElement || node instanceof JSXComponent) {
80
- children.push(node);
81
- }
82
- else if (typeof node === 'string' && node.length) {
83
- children.push(new JSXText(node));
84
- }
85
- else if (Array.isArray(node)) {
86
- children.push(...flatChildren(node));
87
- }
88
- else if (node !== null && typeof node !== 'undefined') {
89
- children.push(new JSXText(String(node)));
90
- }
91
- }
92
- return children;
93
- }
94
76
  class Props {
95
77
  constructor(props) {
96
78
  this.attrs = new Map();
@@ -105,10 +87,10 @@ class Props {
105
87
  if (key === 'children') {
106
88
  if (props.children !== null && typeof props.children !== 'undefined') {
107
89
  if (Array.isArray(props.children)) {
108
- this.children = flatChildren(props.children);
90
+ this.children = props.children;
109
91
  }
110
92
  else {
111
- this.children = flatChildren([props.children]);
93
+ this.children = [props.children];
112
94
  }
113
95
  }
114
96
  return;
@@ -888,10 +870,10 @@ exports.Renderer = class Renderer {
888
870
  });
889
871
  },
890
872
  reuseElement: (newAtom, oldAtom, expectIndex, oldIndex) => {
891
- commits.push(() => {
873
+ commits.push((offset) => {
892
874
  newAtom.nativeNode = oldAtom.nativeNode;
893
875
  const host = context.host;
894
- if (expectIndex !== oldIndex) {
876
+ if (expectIndex !== oldIndex - offset) {
895
877
  if (context.isParent) {
896
878
  this.nativeRenderer.prependChild(host, newAtom.nativeNode);
897
879
  }
@@ -915,9 +897,7 @@ exports.Renderer = class Renderer {
915
897
  atom = atom.sibling;
916
898
  }
917
899
  }
918
- if (applyRefs) {
919
- applyRefs();
920
- }
900
+ applyRefs();
921
901
  });
922
902
  },
923
903
  reuseText: (newAtom, oldAtom) => {
@@ -945,13 +925,24 @@ exports.Renderer = class Renderer {
945
925
  for (const item of oldChildren) {
946
926
  this.cleanView(item.atom, false);
947
927
  }
928
+ let j = 0;
929
+ let offset = 0;
930
+ const len = oldChildren.length;
948
931
  for (let i = 0; i < commits.length; i++) {
949
932
  const commit = commits[i];
950
- commit();
933
+ while (j < len) {
934
+ const current = oldChildren[j];
935
+ if (current.index <= i) {
936
+ offset++;
937
+ j++;
938
+ continue;
939
+ }
940
+ break;
941
+ }
942
+ commit(offset);
951
943
  }
952
944
  }
953
- createChanges(newAtom, lastIndex, oldChildren, changeCommits) {
954
- let isReuse = false;
945
+ createChanges(newAtom, expectIndex, oldChildren, changeCommits) {
955
946
  for (let i = 0; i < oldChildren.length; i++) {
956
947
  const { atom: diffAtom, index: diffIndex } = oldChildren[i];
957
948
  const key = newAtom.jsxNode.key;
@@ -960,23 +951,16 @@ exports.Renderer = class Renderer {
960
951
  if (diffKey !== key) {
961
952
  continue;
962
953
  }
963
- isReuse = lastIndex > diffIndex;
964
954
  }
965
955
  if (newAtom.jsxNode.is(diffAtom.jsxNode)) {
966
956
  if (newAtom.jsxNode instanceof JSXElement) {
967
- if (isReuse) {
968
- this.nativeRenderer.remove(diffAtom.nativeNode);
969
- }
970
- changeCommits.reuseElement(newAtom, diffAtom, lastIndex, diffIndex);
957
+ changeCommits.reuseElement(newAtom, diffAtom, expectIndex, diffIndex);
971
958
  }
972
959
  else if (newAtom.jsxNode instanceof JSXText) {
973
960
  changeCommits.reuseText(newAtom, diffAtom);
974
961
  }
975
962
  else {
976
- if (isReuse) {
977
- this.temporarilyRemove(diffAtom);
978
- }
979
- changeCommits.reuseComponent(newAtom, diffAtom, lastIndex, diffIndex);
963
+ changeCommits.reuseComponent(newAtom, diffAtom, expectIndex, diffIndex);
980
964
  }
981
965
  oldChildren.splice(i, 1);
982
966
  return;
@@ -1004,18 +988,6 @@ exports.Renderer = class Renderer {
1004
988
  atom.jsxNode.destroy();
1005
989
  }
1006
990
  }
1007
- temporarilyRemove(atom) {
1008
- let next = atom.child;
1009
- while (next) {
1010
- if (next.jsxNode instanceof Component) {
1011
- this.temporarilyRemove(next);
1012
- }
1013
- else {
1014
- this.nativeRenderer.remove(next.nativeNode);
1015
- }
1016
- next = next.sibling;
1017
- }
1018
- }
1019
991
  buildView(atom, context) {
1020
992
  if (atom.jsxNode instanceof Component) {
1021
993
  this.componentRender(atom.jsxNode, atom);
@@ -1080,14 +1052,14 @@ exports.Renderer = class Renderer {
1080
1052
  }
1081
1053
  return new Atom(component, parent);
1082
1054
  }
1083
- createChain(context, template, parent) {
1055
+ createChainByTemplate(context, template, parent) {
1084
1056
  if (template instanceof JSXElement) {
1085
1057
  return this.createChainByJSXElement(context, template, parent);
1086
1058
  }
1087
- if (template instanceof JSXText) {
1088
- return this.createChainByJSXText(template, parent);
1059
+ if (template instanceof JSXComponent) {
1060
+ return this.createChainByComponentFactory(context, template, parent);
1089
1061
  }
1090
- return this.createChainByComponentFactory(context, template, parent);
1062
+ return parent;
1091
1063
  }
1092
1064
  createChainByJSXElement(context, element, parent) {
1093
1065
  const atom = new Atom(element, parent);
@@ -1101,19 +1073,37 @@ exports.Renderer = class Renderer {
1101
1073
  createChainByChildren(context, children, parent) {
1102
1074
  const atoms = [];
1103
1075
  for (const item of children) {
1104
- const child = this.createChain(context, item, parent);
1105
- if (Array.isArray(child)) {
1106
- atoms.push(...child);
1076
+ if (item instanceof JSXElement) {
1077
+ atoms.push(this.createChainByJSXElement(context, item, parent));
1078
+ continue;
1107
1079
  }
1108
- else {
1109
- atoms.push(child);
1080
+ if (item instanceof JSXComponent) {
1081
+ const childAtom = this.createChainByComponentFactory(context, item, parent);
1082
+ if (Array.isArray(childAtom)) {
1083
+ atoms.push(...childAtom);
1084
+ }
1085
+ else {
1086
+ atoms.push(childAtom);
1087
+ }
1088
+ continue;
1089
+ }
1090
+ if (typeof item === 'string' && item.length) {
1091
+ atoms.push(this.createChainByJSXText(new JSXText(item), parent));
1092
+ continue;
1093
+ }
1094
+ if (Array.isArray(item)) {
1095
+ atoms.push(...this.createChainByChildren(context, item, parent));
1096
+ continue;
1097
+ }
1098
+ if (item !== null && typeof item !== 'undefined') {
1099
+ atoms.push(this.createChainByJSXText(new JSXText(String(item)), parent));
1110
1100
  }
1111
1101
  }
1112
1102
  return atoms;
1113
1103
  }
1114
1104
  linkTemplate(template, component, parent) {
1115
1105
  if (template) {
1116
- const child = this.createChain(component, template, parent);
1106
+ const child = this.createChainByTemplate(component, template, parent);
1117
1107
  this.link(parent, Array.isArray(child) ? child : [child]);
1118
1108
  }
1119
1109
  }
@@ -1,5 +1,5 @@
1
1
  import { JSXComponent, ComponentSetup } from './component';
2
- export type JSXChildNode = JSXElement | JSXComponent | string | number | boolean | null | undefined;
2
+ export type JSXChildNode = JSXElement | JSXComponent | string | number | boolean | null | undefined | JSXChildNode[];
3
3
  export interface JSXProps<T = JSXChildNode | JSXChildNode[]> {
4
4
  children?: T;
5
5
  [key: string]: any;
@@ -24,13 +24,12 @@ export declare class JSXText implements JSXTypeof {
24
24
  constructor(text: string);
25
25
  is(target: JSXTypeof): boolean;
26
26
  }
27
- export type JSXNode = JSXElement | JSXComponent | JSXText;
28
27
  export declare class Props {
29
28
  attrs: Map<string, any>;
30
29
  styles: Map<string, string | number>;
31
30
  classes: string;
32
31
  listeners: VElementListeners;
33
- children: JSXNode[];
32
+ children: JSXChildNode[];
34
33
  constructor(props?: JSXProps<JSXChildNode> | JSXProps<JSXChildNode[]> | null);
35
34
  static classToString(config: unknown): any;
36
35
  static classToArray(config: unknown): string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viewfly/core",
3
- "version": "0.0.1-alpha.8",
3
+ "version": "0.0.1-alpha.9",
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",
@@ -37,5 +37,5 @@
37
37
  "bugs": {
38
38
  "url": "https://github.com/viewfly/viewfly.git/issues"
39
39
  },
40
- "gitHead": "462b51e8623348002ba7c6ba2caf7df131534506"
40
+ "gitHead": "c1ea5cf0163aac7b73beee2868f4ea5caa94bb3f"
41
41
  }