@simpreact/simpreact 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,109 +1,69 @@
1
- import { SIMP_ELEMENT_CHILD_FLAG_ELEMENT, SIMP_ELEMENT_CHILD_FLAG_LIST, SIMP_ELEMENT_FLAG_FC, SIMP_ELEMENT_FLAG_FRAGMENT, SIMP_ELEMENT_FLAG_HOST, SIMP_ELEMENT_FLAG_PORTAL, SIMP_ELEMENT_FLAG_TEXT, } from './createElement.js';
1
+ import { noop } from '../shared/index.js';
2
2
  import { lifecycleEventBus } from './lifecycleEventBus.js';
3
3
  import { processStack, UNMOUNT_ENTER, UNMOUNT_EXIT } from './processStack.js';
4
4
  import { unmountRef } from './ref.js';
5
- import { isHostLike } from './utils.js';
5
+ import { _pushUnmountChildrenFrame } from './unmountingChildren.js';
6
+ import { _clearElementHostReference, bitScanForwardIndex } from './utils.js';
7
+ const unmountHandlers = [
8
+ _unmountHostElement,
9
+ _unmountFunctionalElement,
10
+ noop,
11
+ _unmountPortalElement,
12
+ _unmountFragmentElement,
13
+ ];
6
14
  export function unmount(element, renderRuntime) {
7
15
  if (renderRuntime.renderStack.length !== 0) {
8
16
  throw new Error('Cannot unmount while rendering.');
9
17
  }
10
- _pushUnmountEnterFrame(element, renderRuntime);
18
+ _pushUnmountEnterFrame(element, { renderRuntime });
11
19
  processStack(renderRuntime);
12
20
  }
13
21
  export function _unmount(frame) {
14
- const current = frame.node;
15
- if (frame.kind === UNMOUNT_EXIT) {
16
- if ((current.flag & SIMP_ELEMENT_FLAG_FC) !== 0) {
17
- current.unmounted = true;
18
- lifecycleEventBus.publish({ type: 'unmounted', element: current, renderRuntime: frame.meta.renderRuntime });
19
- current.store = null;
20
- return;
21
- }
22
- if ((current.flag & SIMP_ELEMENT_FLAG_HOST) !== 0) {
23
- unmountRef(current);
24
- frame.meta.renderRuntime.hostAdapter.unmountProps(current.reference, current, frame.meta.renderRuntime);
25
- frame.meta.renderRuntime.hostAdapter.detachElementFromReference(current.reference, frame.meta.renderRuntime);
26
- }
27
- return;
28
- }
29
- if ((current.flag & SIMP_ELEMENT_FLAG_FC) !== 0) {
30
- if (current.unmounted) {
31
- return;
32
- }
33
- _pushUnmountExitFrame(current, frame.meta.renderRuntime);
34
- if (current.children) {
35
- _pushUnmountEnterFrame(current.children, frame.meta.renderRuntime);
36
- }
37
- return;
38
- }
39
- if ((current.flag & SIMP_ELEMENT_FLAG_TEXT) !== 0) {
40
- return;
41
- }
42
- if ((current.flag & SIMP_ELEMENT_FLAG_PORTAL) !== 0) {
43
- _remove(current.children, current.ref, frame.meta.renderRuntime);
44
- return;
45
- }
46
- if ((current.flag & SIMP_ELEMENT_FLAG_HOST) !== 0) {
47
- _pushUnmountExitFrame(current, frame.meta.renderRuntime);
48
- }
49
- if (current.childFlag === SIMP_ELEMENT_CHILD_FLAG_ELEMENT) {
50
- _pushUnmountEnterFrame(current.children, frame.meta.renderRuntime);
51
- return;
52
- }
53
- if (current.childFlag === SIMP_ELEMENT_CHILD_FLAG_LIST) {
54
- _pushUnmountArrayChildrenFrame(current, frame.meta.renderRuntime);
55
- }
22
+ unmountHandlers[bitScanForwardIndex(frame.node.flag)](frame);
56
23
  }
57
- export function _pushUnmountEnterFrame(element, renderRuntime) {
58
- renderRuntime.renderStack.push({
24
+ export function _pushUnmountEnterFrame(element, meta) {
25
+ meta.renderRuntime.renderStack.push({
59
26
  node: element,
60
27
  kind: UNMOUNT_ENTER,
61
- meta: {
62
- renderRuntime,
63
- },
28
+ meta,
64
29
  });
65
30
  }
66
- export function _pushUnmountExitFrame(element, renderRuntime) {
67
- renderRuntime.renderStack.push({
31
+ export function _pushUnmountExitFrame(element, meta) {
32
+ meta.renderRuntime.renderStack.push({
68
33
  node: element,
69
34
  kind: UNMOUNT_EXIT,
70
- meta: {
71
- renderRuntime,
72
- },
35
+ meta,
73
36
  });
74
37
  }
75
- export function _pushUnmountArrayChildrenFrame(element, renderRuntime) {
76
- const children = element.children;
77
- for (let i = children.length - 1; i >= 0; i -= 1) {
78
- _pushUnmountEnterFrame(children[i], renderRuntime);
38
+ function _unmountFunctionalElement(frame) {
39
+ const current = frame.node;
40
+ if (current.unmounted) {
41
+ return;
79
42
  }
43
+ if (frame.kind === UNMOUNT_EXIT) {
44
+ current.unmounted = true;
45
+ lifecycleEventBus.publish({ type: 'unmounted', element: current, renderRuntime: frame.meta.renderRuntime });
46
+ current.store = null;
47
+ return;
48
+ }
49
+ _pushUnmountExitFrame(current, frame.meta);
50
+ _pushUnmountChildrenFrame(current, frame.meta);
80
51
  }
81
- export function _clearElementHostReference(element, parentHostReference, renderRuntime) {
82
- while (element != null) {
83
- if (isHostLike(element.flag)) {
84
- renderRuntime.hostAdapter.removeChild(parentHostReference, element.reference);
85
- return;
86
- }
87
- const children = element.children;
88
- const childFlag = element.childFlag;
89
- if ((element.flag & SIMP_ELEMENT_FLAG_FC) !== 0) {
90
- element = children;
91
- continue;
92
- }
93
- if ((element.flag & SIMP_ELEMENT_FLAG_FRAGMENT) !== 0) {
94
- switch (childFlag) {
95
- case SIMP_ELEMENT_CHILD_FLAG_LIST:
96
- for (let i = 0, len = children.length; i < len; ++i) {
97
- _clearElementHostReference(children[i], parentHostReference, renderRuntime);
98
- }
99
- return;
100
- case SIMP_ELEMENT_CHILD_FLAG_ELEMENT:
101
- element = children;
102
- }
103
- }
52
+ function _unmountHostElement(frame) {
53
+ const current = frame.node;
54
+ if (frame.kind === UNMOUNT_EXIT) {
55
+ unmountRef(current);
56
+ frame.meta.renderRuntime.hostAdapter.unmountProps(current.reference, current, frame.meta.renderRuntime);
57
+ frame.meta.renderRuntime.hostAdapter.detachElementFromReference(current.reference, frame.meta.renderRuntime);
58
+ return;
104
59
  }
60
+ _pushUnmountExitFrame(current, frame.meta);
61
+ _pushUnmountChildrenFrame(current, frame.meta);
62
+ }
63
+ function _unmountPortalElement(frame) {
64
+ _clearElementHostReference(frame.node.children, frame.node.ref, frame.meta.renderRuntime);
65
+ _pushUnmountChildrenFrame(frame.node, frame.meta);
105
66
  }
106
- export function _remove(element, parentReference, renderRuntime) {
107
- _clearElementHostReference(element, parentReference, renderRuntime);
108
- _pushUnmountEnterFrame(element, renderRuntime);
67
+ function _unmountFragmentElement(frame) {
68
+ _pushUnmountChildrenFrame(frame.node, frame.meta);
109
69
  }
@@ -0,0 +1,4 @@
1
+ import { type SimpElement } from './createElement.js';
2
+ import { type UnmountChildrenFrame, type UnmountFrameMeta } from './processStack.js';
3
+ export declare function _pushUnmountChildrenFrame(parent: SimpElement, meta: UnmountFrameMeta): void;
4
+ export declare function _unmountChildren(frame: UnmountChildrenFrame): void;
@@ -0,0 +1,23 @@
1
+ import { SIMP_ELEMENT_CHILD_FLAG_ELEMENT, SIMP_ELEMENT_CHILD_FLAG_LIST } from './createElement.js';
2
+ import { UNMOUNT_CHILDREN_ENTER } from './processStack.js';
3
+ import { _pushUnmountEnterFrame } from './unmounting.js';
4
+ export function _pushUnmountChildrenFrame(parent, meta) {
5
+ meta.renderRuntime.renderStack.push({
6
+ node: parent,
7
+ kind: UNMOUNT_CHILDREN_ENTER,
8
+ meta,
9
+ });
10
+ }
11
+ export function _unmountChildren(frame) {
12
+ switch (frame.node.childFlag) {
13
+ case SIMP_ELEMENT_CHILD_FLAG_LIST:
14
+ const children = frame.node.children;
15
+ for (let i = children.length - 1; i >= 0; i -= 1) {
16
+ _pushUnmountEnterFrame(children[i], frame.meta);
17
+ }
18
+ break;
19
+ case SIMP_ELEMENT_CHILD_FLAG_ELEMENT:
20
+ _pushUnmountEnterFrame(frame.node.children, frame.meta);
21
+ break;
22
+ }
23
+ }
package/core/utils.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import type { Nullable } from '../shared/index.js';
1
+ import type { Maybe, Nullable } from '../shared/index.js';
2
2
  import { type SimpElement } from './createElement.js';
3
3
  import type { SimpRenderRuntime } from './runtime.js';
4
4
  export declare function bitScanForwardIndex(flag: number): number;
5
5
  export declare function isHostLike(flag: number): boolean;
6
6
  export declare function findParentReferenceFromElement(element: SimpElement): unknown | null;
7
7
  export declare function placeElementBeforeAnchor(element: SimpElement, anchor: unknown, parentReference: unknown, renderRuntime: SimpRenderRuntime): void;
8
- export declare function resolveAnchorReference(rightSibling: Nullable<SimpElement>): unknown | null;
8
+ export declare function resolveAnchorReference(subtreeRightBoundary: Nullable<SimpElement>): unknown | null;
9
9
  export declare function findHostReferenceFromElement(element: Nullable<SimpElement>): unknown | null;
10
10
  export declare function getLongestIncreasingSubsequenceIndexes(sequence: Int32Array): Int32Array;
11
+ export declare function _clearElementHostReference(element: Maybe<SimpElement>, parentHostReference: unknown, renderRuntime: SimpRenderRuntime): void;
package/core/utils.js CHANGED
@@ -1,4 +1,4 @@
1
- import { SIMP_ELEMENT_CHILD_FLAG_ELEMENT, SIMP_ELEMENT_CHILD_FLAG_LIST, SIMP_ELEMENT_FLAG_HOST, SIMP_ELEMENT_FLAG_PORTAL, SIMP_ELEMENT_FLAG_TEXT, } from './createElement.js';
1
+ import { SIMP_ELEMENT_CHILD_FLAG_ELEMENT, SIMP_ELEMENT_CHILD_FLAG_LIST, SIMP_ELEMENT_FLAG_FC, SIMP_ELEMENT_FLAG_FRAGMENT, SIMP_ELEMENT_FLAG_HOST, SIMP_ELEMENT_FLAG_PORTAL, SIMP_ELEMENT_FLAG_TEXT, } from './createElement.js';
2
2
  export function bitScanForwardIndex(flag) {
3
3
  const lsb = (flag & -flag) >>> 0;
4
4
  return 31 - Math.clz32(lsb);
@@ -43,8 +43,8 @@ export function placeElementBeforeAnchor(element, anchor, parentReference, rende
43
43
  }
44
44
  }
45
45
  }
46
- export function resolveAnchorReference(rightSibling) {
47
- let current = rightSibling;
46
+ export function resolveAnchorReference(subtreeRightBoundary) {
47
+ let current = subtreeRightBoundary;
48
48
  while (current != null) {
49
49
  const reference = findHostReferenceFromElement(current);
50
50
  if (reference != null) {
@@ -141,3 +141,28 @@ export function getLongestIncreasingSubsequenceIndexes(sequence) {
141
141
  }
142
142
  return indexes;
143
143
  }
144
+ export function _clearElementHostReference(element, parentHostReference, renderRuntime) {
145
+ while (element != null) {
146
+ if (isHostLike(element.flag)) {
147
+ renderRuntime.hostAdapter.removeChild(parentHostReference, element.reference);
148
+ return;
149
+ }
150
+ const children = element.children;
151
+ const childFlag = element.childFlag;
152
+ if ((element.flag & SIMP_ELEMENT_FLAG_FC) !== 0) {
153
+ element = children;
154
+ continue;
155
+ }
156
+ if ((element.flag & SIMP_ELEMENT_FLAG_FRAGMENT) !== 0) {
157
+ switch (childFlag) {
158
+ case SIMP_ELEMENT_CHILD_FLAG_LIST:
159
+ for (let i = 0, len = children.length; i < len; ++i) {
160
+ _clearElementHostReference(children[i], parentHostReference, renderRuntime);
161
+ }
162
+ return;
163
+ case SIMP_ELEMENT_CHILD_FLAG_ELEMENT:
164
+ element = children;
165
+ }
166
+ }
167
+ }
168
+ }
@@ -1,7 +1,7 @@
1
- import type { SimpElement, SimpRenderRuntime } from '../../core/internal.js';
1
+ import type { SimpElement } from '../../core/internal.js';
2
2
  import type { Maybe } from '../../shared/index.js';
3
- export declare function patchDangerInnerHTML(prevValue: Maybe<{
3
+ type DangerInnerHTMLValue = {
4
4
  __html: string;
5
- }>, nextValue: Maybe<{
6
- __html: string;
7
- }>, prevElement: Maybe<SimpElement>, nextElement: SimpElement, dom: Element, renderRuntime: SimpRenderRuntime): void;
5
+ };
6
+ export declare function patchDangerInnerHTML(prevValue: Maybe<DangerInnerHTMLValue>, nextValue: Maybe<DangerInnerHTMLValue>, nextElement: SimpElement, dom: Element): void;
7
+ export {};
@@ -1,21 +1,15 @@
1
- import { unmount } from '../../core/internal.js';
2
- export function patchDangerInnerHTML(prevValue, nextValue, prevElement, nextElement, dom, renderRuntime) {
3
- const prevHTML = prevValue?.__html || '';
4
- const nextHTML = nextValue?.__html || '';
1
+ export function patchDangerInnerHTML(prevValue, nextValue, nextElement, dom) {
2
+ const prevHTML = prevValue?.__html;
3
+ const nextHTML = nextValue?.__html;
5
4
  if (nextElement.children) {
6
- console.warn('Avoid setting both children and props.dangerouslySetInnerHTML at the same time — this can cause unpredictable behavior.');
5
+ console.warn('Avoid setting both children and props.dangerouslySetInnerHTML at the same time — this causes unpredictable behavior.');
7
6
  }
8
- if (prevHTML !== nextHTML) {
9
- if (nextHTML != null && !isSameInnerHTML(dom, nextHTML)) {
10
- if (prevElement != null) {
11
- if (prevElement.children) {
12
- // TODO: the HOST element can hold several children.
13
- unmount(prevElement.children, renderRuntime);
14
- prevElement.children = undefined;
15
- }
16
- }
17
- dom.innerHTML = nextHTML;
18
- }
7
+ if (prevHTML === nextHTML) {
8
+ return;
9
+ }
10
+ const nextInnerHTML = nextHTML ?? '';
11
+ if (!isSameInnerHTML(dom, nextInnerHTML)) {
12
+ dom.innerHTML = nextInnerHTML;
19
13
  }
20
14
  }
21
15
  function isSameInnerHTML(dom, innerHTML) {
@@ -7,7 +7,7 @@ import { patchStyle } from './style.js';
7
7
  export function mountProps(dom, element, namespace, renderRuntime) {
8
8
  if (!isFormElement(element)) {
9
9
  for (const propName in element.props) {
10
- patchDefaultElementPropAndAttrs(propName, dom, null, element, null, element.props[propName], namespace, renderRuntime);
10
+ patchDefaultElementPropAndAttrs(propName, dom, element, null, element.props[propName], namespace, renderRuntime);
11
11
  }
12
12
  return;
13
13
  }
@@ -41,12 +41,12 @@ export function patchProps(dom, prevElement, nextElement, namespace, renderRunti
41
41
  const prevValue = prevProps[propName];
42
42
  const nextValue = nextProps[propName];
43
43
  if (prevValue !== nextValue) {
44
- patchDefaultElementPropAndAttrs(propName, dom, prevElement, nextElement, prevValue, nextValue, namespace, renderRuntime);
44
+ patchDefaultElementPropAndAttrs(propName, dom, nextElement, prevValue, nextValue, namespace, renderRuntime);
45
45
  }
46
46
  }
47
47
  for (const propName in prevProps) {
48
48
  if (nextProps[propName] == null && prevProps[propName] != null) {
49
- patchDefaultElementPropAndAttrs(propName, dom, prevElement, nextElement, prevProps[propName], null, namespace, renderRuntime);
49
+ patchDefaultElementPropAndAttrs(propName, dom, nextElement, prevProps[propName], null, namespace, renderRuntime);
50
50
  }
51
51
  }
52
52
  return;
@@ -122,7 +122,7 @@ function patchFormElementsPropAndAttrs(propName, dom, element, isControlled, pre
122
122
  }
123
123
  }
124
124
  }
125
- function patchDefaultElementPropAndAttrs(propName, dom, prevElement, nextElement, prevValue, nextValue, namespace, renderRuntime) {
125
+ function patchDefaultElementPropAndAttrs(propName, dom, nextElement, prevValue, nextValue, namespace, renderRuntime) {
126
126
  switch (propName) {
127
127
  case 'children':
128
128
  case 'className':
@@ -153,7 +153,7 @@ function patchDefaultElementPropAndAttrs(propName, dom, prevElement, nextElement
153
153
  patchStyle(prevValue, nextValue, dom);
154
154
  break;
155
155
  case 'dangerouslySetInnerHTML':
156
- patchDangerInnerHTML(prevValue, nextValue, prevElement, nextElement, dom, renderRuntime);
156
+ patchDangerInnerHTML(prevValue, nextValue, nextElement, dom);
157
157
  break;
158
158
  default:
159
159
  if (isPropNameEventName(propName)) {
package/hooks/index.js CHANGED
@@ -9,6 +9,7 @@ function getHooksSpecificStore(store) {
9
9
  }
10
10
  return hooksSpecificStore;
11
11
  }
12
+ window.__SIMP_HOOKS_SPECIFIC_STORE_BY_ELEMENT_STORE__ = hooksSpecificStoreByElementStore;
12
13
  lifecycleEventBus.subscribe(event => {
13
14
  if ((event.element.flag & SIMP_ELEMENT_FLAG_FC) === 0) {
14
15
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simpreact/simpreact",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/dPaskhin/simpreact#readme",
6
6
  "main": "./core/index.js",