@simpreact/simpreact 0.0.8 → 0.0.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.
Files changed (75) hide show
  1. package/compat/core.js +101 -15
  2. package/compat/hooks.js +15 -0
  3. package/compat/index.d.ts +10 -1
  4. package/compat/renderRuntime.js +47 -12
  5. package/component/index.js +96 -94
  6. package/context/index.js +27 -17
  7. package/core/createElement.js +14 -17
  8. package/core/flags.js +31 -0
  9. package/core/hostOperations.js +5 -13
  10. package/core/index.d.ts +38 -8
  11. package/core/index.js +3 -2
  12. package/core/internal.d.ts +136 -16
  13. package/core/internal.js +8 -16
  14. package/core/lifecycleEventBus.js +35 -16
  15. package/core/memo.js +4 -1
  16. package/core/mounting.js +70 -150
  17. package/core/mountingChildren.js +11 -29
  18. package/core/patching.js +122 -181
  19. package/core/patchingChildren.js +74 -145
  20. package/core/portal.js +1 -1
  21. package/core/processStack.js +115 -45
  22. package/core/ref.js +1 -0
  23. package/core/rerender.js +20 -22
  24. package/core/runtime.js +10 -2
  25. package/core/unmounting.js +41 -49
  26. package/core/unmountingChildren.js +9 -12
  27. package/core/utils.js +38 -16
  28. package/dom/attach-element-to-dom.js +16 -8
  29. package/dom/events.js +11 -15
  30. package/dom/index.d.ts +2 -0
  31. package/dom/props/attrMaps.js +90 -0
  32. package/dom/props/controlled/select.js +8 -10
  33. package/dom/props/props.js +13 -14
  34. package/hooks/index.d.ts +3 -0
  35. package/hooks/index.js +107 -84
  36. package/package.json +10 -5
  37. package/compat/context.d.ts +0 -7
  38. package/compat/core.d.ts +0 -48
  39. package/compat/dom.d.ts +0 -10
  40. package/compat/hooks.d.ts +0 -26
  41. package/compat/jsx-runtime.d.ts +0 -10
  42. package/compat/renderRuntime.d.ts +0 -6
  43. package/core/createElement.d.ts +0 -39
  44. package/core/fragment.d.ts +0 -5
  45. package/core/hostAdapter.d.ts +0 -23
  46. package/core/hostOperations.d.ts +0 -5
  47. package/core/lifecycleEventBus.d.ts +0 -39
  48. package/core/memo.d.ts +0 -8
  49. package/core/mounting.d.ts +0 -7
  50. package/core/mountingChildren.d.ts +0 -4
  51. package/core/patching.d.ts +0 -8
  52. package/core/patchingChildren.d.ts +0 -6
  53. package/core/portal.d.ts +0 -2
  54. package/core/processStack.d.ts +0 -106
  55. package/core/ref.d.ts +0 -18
  56. package/core/rerender.d.ts +0 -4
  57. package/core/runtime.d.ts +0 -17
  58. package/core/unmounting.d.ts +0 -7
  59. package/core/unmountingChildren.d.ts +0 -4
  60. package/core/utils.d.ts +0 -11
  61. package/dom/attach-element-to-dom.d.ts +0 -5
  62. package/dom/domAdapter.d.ts +0 -3
  63. package/dom/events.d.ts +0 -27
  64. package/dom/namespace.d.ts +0 -2
  65. package/dom/props/controlled/index.d.ts +0 -7
  66. package/dom/props/controlled/input.d.ts +0 -7
  67. package/dom/props/controlled/select.d.ts +0 -6
  68. package/dom/props/controlled/textarea.d.ts +0 -6
  69. package/dom/props/dangerInnerHTML.d.ts +0 -7
  70. package/dom/props/index.d.ts +0 -1
  71. package/dom/props/props.d.ts +0 -5
  72. package/dom/props/style.d.ts +0 -1
  73. package/dom/render.d.ts +0 -8
  74. package/shared/lang.d.ts +0 -3
  75. package/shared/utils.d.ts +0 -5
package/core/mounting.js CHANGED
@@ -1,134 +1,92 @@
1
+ import { noop } from '../shared/index.js';
1
2
  import { createTextElement, normalizeRoot, SIMP_ELEMENT_CHILD_FLAG_TEXT, } from './createElement.js';
2
- import { _pushHostOperationPlaceElement } from './hostOperations.js';
3
- import { lifecycleEventBus } from './lifecycleEventBus.js';
4
- import { _pushMountChildrenFrame } from './mountingChildren.js';
5
- import { MOUNT_ENTER, MOUNT_EXIT, processStack } from './processStack.js';
3
+ import { pushHostOperationPlaceElement } from './hostOperations.js';
4
+ import { getLifecycleEventBus } from './lifecycleEventBus.js';
5
+ import { pushMountChildrenFrame } from './mountingChildren.js';
6
+ import { acquireMountFrame, MOUNT_ENTER, MOUNT_EXIT, processStack } from './processStack.js';
6
7
  import { applyRef } from './ref.js';
7
- import { MOUNTING_PHASE } from './runtime.js';
8
8
  import { bitScanForwardIndex } from './utils.js';
9
- const mountHandlers = [_mountHostElement, _mountFunctionalElement, _mountTextElement, _mountPortal, _mountFragment];
9
+ const mountEnterHandlers = [mountHostEnter, mountFCEnter, mountTextElement, mountPortalEnter, mountFragment];
10
+ const mountExitHandlers = [mountHostExit, mountFCExit, noop, mountPortalExit, noop];
10
11
  export function mount(element, parentReference, subtreeRightBoundary, context, hostNamespace, renderRuntime) {
11
12
  if (renderRuntime.renderStack.length !== 0) {
12
13
  throw new Error('Cannot mount while rendering.');
13
14
  }
14
- _pushMountEnterFrame(element, {
15
- parentReference,
16
- subtreeRightBoundary,
17
- context,
18
- hostNamespace,
19
- renderRuntime,
20
- placeHolderElement: null,
21
- });
15
+ pushMountEnterFrame(element, renderRuntime, parentReference, subtreeRightBoundary, context, hostNamespace, null);
22
16
  processStack(renderRuntime);
23
17
  }
24
- export function _mount(frame) {
25
- mountHandlers[bitScanForwardIndex(frame.node.flag)](frame);
18
+ export function mountEnter(frame) {
19
+ mountEnterHandlers[bitScanForwardIndex(frame.node.flag)](frame);
26
20
  }
27
- export function _pushMountEnterFrame(element, meta) {
28
- meta.renderRuntime.renderStack.push({
29
- node: element,
30
- kind: MOUNT_ENTER,
31
- meta,
32
- });
21
+ export function mountExit(frame) {
22
+ mountExitHandlers[bitScanForwardIndex(frame.node.flag)](frame);
33
23
  }
34
- function _pushMountExitFrame(element, meta) {
35
- meta.renderRuntime.renderStack.push({
36
- node: element,
37
- kind: MOUNT_EXIT,
38
- meta,
39
- });
24
+ export function pushMountEnterFrame(element, renderRuntime, parentReference, subtreeRightBoundary, context, hostNamespace, placeHolderElement) {
25
+ renderRuntime.renderStack.push(acquireMountFrame(renderRuntime, element, MOUNT_ENTER, parentReference, subtreeRightBoundary, context, hostNamespace, placeHolderElement));
26
+ }
27
+ function pushMountExitFrame(element, renderRuntime, parentReference, subtreeRightBoundary, context, hostNamespace, placeHolderElement) {
28
+ renderRuntime.renderStack.push(acquireMountFrame(renderRuntime, element, MOUNT_EXIT, parentReference, subtreeRightBoundary, context, hostNamespace, placeHolderElement));
40
29
  }
41
- function _mountHostElement(frame) {
30
+ function mountHostEnter(frame) {
42
31
  const element = frame.node;
43
- const { parentReference, subtreeRightBoundary, context, hostNamespace, renderRuntime } = frame.meta;
44
- if (frame.kind === MOUNT_EXIT) {
45
- if (element.childFlag === SIMP_ELEMENT_CHILD_FLAG_TEXT) {
46
- renderRuntime.hostAdapter.setTextContent(element.reference, element.props.children);
47
- }
48
- if (element.props) {
49
- renderRuntime.hostAdapter.mountProps(element.reference, element, renderRuntime, hostNamespace);
50
- }
51
- if (element.className) {
52
- renderRuntime.hostAdapter.setClassname(element.reference, element.className, hostNamespace);
53
- }
54
- if (parentReference) {
55
- _pushHostOperationPlaceElement(element, {
56
- renderRuntime,
57
- subtreeRightBoundary,
58
- parentReference,
59
- });
60
- }
61
- applyRef(element);
62
- return;
63
- }
32
+ const { parentReference, subtreeRightBoundary, context, hostNamespace, renderRuntime } = frame;
64
33
  const hostNamespaces = renderRuntime.hostAdapter.getHostNamespaces(element, hostNamespace);
65
34
  const hostReference = (element.reference = renderRuntime.hostAdapter.createReference(element.type, hostNamespaces?.self));
66
35
  renderRuntime.hostAdapter.attachElementToReference(element, hostReference, renderRuntime);
67
- _pushMountExitFrame(element, {
68
- renderRuntime,
69
- hostNamespace: hostNamespaces?.self,
70
- subtreeRightBoundary,
71
- context,
72
- parentReference,
73
- placeHolderElement: null,
74
- });
75
- _pushMountChildrenFrame(element, {
76
- renderRuntime,
77
- hostNamespace: hostNamespaces?.children,
78
- subtreeRightBoundary,
79
- context,
80
- parentReference: hostReference,
81
- children: element.children,
82
- });
36
+ pushMountExitFrame(element, renderRuntime, parentReference, subtreeRightBoundary, context, hostNamespaces?.self, null);
37
+ pushMountChildrenFrame(element, renderRuntime, element.children, hostReference, subtreeRightBoundary, context, hostNamespaces?.children);
83
38
  }
84
- function _mountFunctionalElement(frame) {
39
+ function mountHostExit(frame) {
85
40
  const element = frame.node;
86
- const { parentReference, subtreeRightBoundary, context, hostNamespace, renderRuntime } = frame.meta;
87
- if (frame.kind === MOUNT_EXIT) {
88
- lifecycleEventBus.publish({ type: 'mounted', element, renderRuntime });
89
- return;
41
+ const { parentReference, subtreeRightBoundary, hostNamespace, renderRuntime } = frame;
42
+ if (element.childFlag === SIMP_ELEMENT_CHILD_FLAG_TEXT) {
43
+ renderRuntime.hostAdapter.setTextContent(element.reference, element.props.children);
90
44
  }
45
+ if (element.props) {
46
+ renderRuntime.hostAdapter.mountProps(element.reference, element, renderRuntime, hostNamespace);
47
+ }
48
+ if (element.className) {
49
+ renderRuntime.hostAdapter.setClassname(element.reference, element.className, hostNamespace);
50
+ }
51
+ if (parentReference) {
52
+ pushHostOperationPlaceElement(element, renderRuntime, parentReference, subtreeRightBoundary);
53
+ }
54
+ applyRef(element);
55
+ }
56
+ function mountFCEnter(frame) {
57
+ const element = frame.node;
58
+ const { parentReference, subtreeRightBoundary, context, hostNamespace, renderRuntime } = frame;
91
59
  if (context) {
92
60
  element.context = context;
93
61
  }
94
62
  if (element.unmounted) {
95
63
  element.unmounted = false;
96
64
  }
97
- element.store = { latestElement: null, hostNamespace: null, forceRerender: false };
98
- element.store.latestElement = element;
99
65
  if (hostNamespace) {
100
- element.store.hostNamespace = hostNamespace;
66
+ element.hostNamespace = hostNamespace;
101
67
  }
102
- // FC element always has Maybe<SimpElement> children due to a normalization process.
103
68
  let children;
104
- let triedToRerenderUnsubscribe;
105
69
  try {
106
- renderRuntime.renderPhase = MOUNTING_PHASE;
107
- renderRuntime.currentRenderingFCElement = element;
108
- let triedToRerender = false;
109
70
  let rerenderCounter = 0;
110
- triedToRerenderUnsubscribe = lifecycleEventBus.subscribe(event => {
111
- if (event.type === 'triedToRerender' && event.element === element) {
112
- triedToRerender = true;
113
- }
114
- });
71
+ renderRuntime.activeRenderElement = element;
72
+ renderRuntime.pendingRerenderFlag = false;
115
73
  do {
116
- triedToRerender = false;
74
+ renderRuntime.pendingRerenderFlag = false;
117
75
  if (++rerenderCounter >= 25) {
118
76
  throw new Error('Too many re-renders.');
119
77
  }
120
- lifecycleEventBus.publish({
78
+ getLifecycleEventBus(renderRuntime).publish({
121
79
  type: 'beforeRender',
122
80
  element,
123
81
  renderRuntime,
124
82
  });
125
83
  children = renderRuntime.renderer(element.type, element, renderRuntime);
126
- lifecycleEventBus.publish({
84
+ getLifecycleEventBus(renderRuntime).publish({
127
85
  type: 'afterRender',
128
86
  element,
129
87
  renderRuntime,
130
88
  });
131
- } while (triedToRerender);
89
+ } while (renderRuntime.pendingRerenderFlag);
132
90
  normalizeRoot(element, children, false);
133
91
  children = element.children;
134
92
  }
@@ -140,80 +98,42 @@ function _mountFunctionalElement(frame) {
140
98
  handled: false,
141
99
  renderRuntime,
142
100
  };
143
- lifecycleEventBus.publish(event);
101
+ getLifecycleEventBus(renderRuntime).publish(event);
144
102
  if (!event.handled) {
145
103
  throw new Error('Error occurred during rendering a component', { cause: event.error });
146
104
  }
147
105
  return;
148
106
  }
149
107
  finally {
150
- triedToRerenderUnsubscribe();
151
- renderRuntime.renderPhase = null;
152
- renderRuntime.currentRenderingFCElement = null;
108
+ renderRuntime.activeRenderElement = null;
153
109
  }
154
- _pushMountExitFrame(element, {
155
- renderRuntime,
156
- hostNamespace,
157
- subtreeRightBoundary,
158
- context,
159
- parentReference,
160
- placeHolderElement: null,
161
- });
162
- _pushMountChildrenFrame(element, {
163
- renderRuntime,
164
- hostNamespace,
165
- children: children,
166
- context: element.context,
167
- parentReference,
168
- subtreeRightBoundary,
110
+ pushMountExitFrame(element, renderRuntime, parentReference, subtreeRightBoundary, context, hostNamespace, null);
111
+ pushMountChildrenFrame(element, renderRuntime, children, parentReference, subtreeRightBoundary, element.context, hostNamespace);
112
+ }
113
+ function mountFCExit(frame) {
114
+ getLifecycleEventBus(frame.renderRuntime).publish({
115
+ type: 'mounted',
116
+ element: frame.node,
117
+ renderRuntime: frame.renderRuntime,
169
118
  });
170
119
  }
171
- function _mountTextElement(frame) {
172
- frame.node.reference = frame.meta.renderRuntime.hostAdapter.createTextReference(frame.node.children);
173
- _pushHostOperationPlaceElement(frame.node, frame.meta);
120
+ function mountTextElement(frame) {
121
+ frame.node.reference = frame.renderRuntime.hostAdapter.createTextReference(frame.node.children);
122
+ pushHostOperationPlaceElement(frame.node, frame.renderRuntime, frame.parentReference, frame.subtreeRightBoundary);
174
123
  }
175
- function _mountPortal(frame) {
124
+ function mountPortalEnter(frame) {
176
125
  const element = frame.node;
177
- const { parentReference, subtreeRightBoundary, context, renderRuntime } = frame.meta;
178
- if (frame.kind === MOUNT_EXIT) {
179
- element.reference = frame.meta.placeHolderElement.reference;
180
- return;
181
- }
126
+ const { parentReference, subtreeRightBoundary, context, renderRuntime } = frame;
182
127
  const placeHolderElement = createTextElement('');
183
- _pushMountExitFrame(element, {
184
- renderRuntime,
185
- subtreeRightBoundary,
186
- context: null,
187
- parentReference: null,
188
- hostNamespace: null,
189
- placeHolderElement,
190
- });
191
- _pushMountChildrenFrame(element, {
192
- renderRuntime,
193
- hostNamespace: renderRuntime.hostAdapter.getHostNamespaces(element.children, undefined)?.self,
194
- subtreeRightBoundary,
195
- context,
196
- parentReference: element.ref,
197
- children: element.children,
198
- });
199
- _pushMountEnterFrame(placeHolderElement, {
200
- renderRuntime,
201
- subtreeRightBoundary,
202
- context: null,
203
- parentReference,
204
- hostNamespace: null,
205
- placeHolderElement: null,
206
- });
128
+ pushMountExitFrame(element, renderRuntime, null, subtreeRightBoundary, null, null, placeHolderElement);
129
+ pushMountChildrenFrame(element, renderRuntime, element.children, element.ref, subtreeRightBoundary, context, renderRuntime.hostAdapter.getHostNamespaces(element.children, undefined)?.self);
130
+ pushMountEnterFrame(placeHolderElement, renderRuntime, parentReference, subtreeRightBoundary, null, null, null);
131
+ }
132
+ function mountPortalExit(frame) {
133
+ frame.node.reference = frame.placeHolderElement.reference;
207
134
  }
208
- function _mountFragment(frame) {
135
+ function mountFragment(frame) {
209
136
  const element = frame.node;
210
- const { parentReference, hostNamespace, context, renderRuntime, subtreeRightBoundary } = frame.meta;
211
- _pushMountChildrenFrame(element, {
212
- renderRuntime,
213
- hostNamespace,
214
- children: element.children,
215
- parentReference,
216
- context,
217
- subtreeRightBoundary: subtreeRightBoundary,
218
- });
137
+ const { parentReference, hostNamespace, context, renderRuntime, subtreeRightBoundary } = frame;
138
+ pushMountChildrenFrame(element, renderRuntime, element.children, parentReference, subtreeRightBoundary, context, hostNamespace);
219
139
  }
@@ -1,46 +1,28 @@
1
1
  import { SIMP_ELEMENT_CHILD_FLAG_ELEMENT, SIMP_ELEMENT_CHILD_FLAG_LIST } from './createElement.js';
2
- import { _pushMountEnterFrame } from './mounting.js';
3
- import { MOUNT_CHILDREN_ENTER } from './processStack.js';
2
+ import { pushMountEnterFrame } from './mounting.js';
3
+ import { acquireMountChildrenFrame } from './processStack.js';
4
4
  import { isHostLike } from './utils.js';
5
- export function _pushMountChildrenFrame(parent, meta) {
6
- meta.renderRuntime.renderStack.push({
7
- node: parent,
8
- kind: MOUNT_CHILDREN_ENTER,
9
- meta,
10
- });
5
+ export function pushMountChildrenFrame(parent, renderRuntime, children, parentReference, subtreeRightBoundary, context, hostNamespace) {
6
+ renderRuntime.renderStack.push(acquireMountChildrenFrame(renderRuntime, parent, children, parentReference, subtreeRightBoundary, context, hostNamespace));
11
7
  }
12
- export function _mountChildren(frame) {
8
+ export function mountChildren(frame) {
13
9
  const parentElement = frame.node;
14
- const { parentReference, hostNamespace, renderRuntime, context } = frame.meta;
15
- const subtreeRightBoundary = isHostLike(parentElement.flag) ? null : frame.meta.subtreeRightBoundary;
10
+ const { parentReference, hostNamespace, renderRuntime, context } = frame;
11
+ const subtreeRightBoundary = isHostLike(parentElement.flag) ? null : frame.subtreeRightBoundary;
16
12
  switch (parentElement.childFlag) {
17
13
  case SIMP_ELEMENT_CHILD_FLAG_ELEMENT: {
18
- const children = frame.meta.children;
14
+ const children = frame.children;
19
15
  children.parent = parentElement;
20
- _pushMountEnterFrame(children, {
21
- renderRuntime,
22
- hostNamespace,
23
- subtreeRightBoundary,
24
- context,
25
- parentReference,
26
- placeHolderElement: null,
27
- });
16
+ pushMountEnterFrame(children, renderRuntime, parentReference, subtreeRightBoundary, context, hostNamespace, null);
28
17
  break;
29
18
  }
30
19
  case SIMP_ELEMENT_CHILD_FLAG_LIST: {
31
- const children = frame.meta.children;
20
+ const children = frame.children;
32
21
  for (let i = children.length - 1; i >= 0; i--) {
33
22
  const child = children[i];
34
23
  child.parent = parentElement;
35
24
  const rightSibling = children[child.index + 1] || subtreeRightBoundary;
36
- _pushMountEnterFrame(child, {
37
- renderRuntime,
38
- parentReference,
39
- context,
40
- hostNamespace,
41
- subtreeRightBoundary: rightSibling,
42
- placeHolderElement: null,
43
- });
25
+ pushMountEnterFrame(child, renderRuntime, parentReference, rightSibling, context, hostNamespace, null);
44
26
  }
45
27
  }
46
28
  }