@unsetsoft/ryunixjs 1.0.0-alpha.0 → 1.0.2

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.
package/package.json CHANGED
@@ -1,14 +1,38 @@
1
1
  {
2
2
  "name": "@unsetsoft/ryunixjs",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.2",
4
4
  "license": "MIT",
5
5
  "main": "./dist/Ryunix.js",
6
+ "types": "./dist/Ryunix.d.ts",
6
7
  "private": false,
8
+ "bugs": {
9
+ "url": "https://github.com/UnSetSoft/Ryunixjs/issues"
10
+ },
11
+ "homepage": "https://github.com/UnSetSoft/Ryunixjs#readme",
7
12
  "scripts": {
8
- "build": "rollup ./lib/ryunix.js --file ./dist/Ryunix.js --format umd --name Ryunix",
9
- "prepublishOnly": "yarn build"
13
+ "build:js": "rollup ./src/main.js --file ./dist/Ryunix.js --format umd --name Ryunix",
14
+ "prepublishOnly": "npm run build:js",
15
+ "postinstall": "npm run build:js",
16
+ "nightly:release": "npm publish --tag nightly",
17
+ "release": "npm publish",
18
+ "lint": "eslint . --ext .ts --fix --max-warnings=0 --config .eslintrc.js --no-eslintrc"
19
+ },
20
+ "dependencies": {
21
+ "eslint": "8.56.0",
22
+ "lodash": "^4.17.20",
23
+ "rollup": "4.9.2"
24
+ },
25
+ "engines": {
26
+ "node": ">=18.16.0"
27
+ },
28
+ "keywords": [
29
+ "ryunixjs"
30
+ ],
31
+ "publishConfig": {
32
+ "registry": "https://registry.npmjs.org"
10
33
  },
11
- "devDependencies": {
12
- "rollup": "3.24.0"
13
- }
14
- }
34
+ "files": [
35
+ "dist",
36
+ "src"
37
+ ]
38
+ }
@@ -0,0 +1,71 @@
1
+ import { updateDom } from './dom'
2
+ import { cancelEffects, runEffects } from './effects'
3
+ import { EFFECT_TAGS, vars } from '../utils/index'
4
+
5
+ /**
6
+ * The function commits changes made to the virtual DOM to the actual DOM.
7
+ */
8
+ const commitRoot = () => {
9
+ vars.deletions.forEach(commitWork)
10
+ if (vars.wipRoot && vars.wipRoot.child) {
11
+ commitWork(vars.wipRoot.child)
12
+ vars.currentRoot = vars.wipRoot
13
+ }
14
+ vars.wipRoot = undefined
15
+ }
16
+
17
+ /**
18
+ * The function commits changes made to the DOM based on the effect tag of the fiber.
19
+ * @param fiber - A fiber is a unit of work in Ryunix's reconciliation process. It represents a
20
+ * component and its state at a particular point in time. The `commitWork` function takes a fiber as a
21
+ * parameter to commit the changes made during the reconciliation process to the actual DOM.
22
+ * @returns The function does not return anything, it performs side effects by manipulating the DOM.
23
+ */
24
+ const commitWork = (fiber) => {
25
+ if (!fiber) {
26
+ return
27
+ }
28
+
29
+ let domParentFiber = fiber.parent
30
+ while (!domParentFiber.dom) {
31
+ domParentFiber = domParentFiber.parent
32
+ }
33
+ const domParent = domParentFiber.dom
34
+
35
+ if (fiber.effectTag === EFFECT_TAGS.PLACEMENT) {
36
+ if (fiber.dom != undefined) {
37
+ domParent.appendChild(fiber.dom)
38
+ }
39
+ runEffects(fiber)
40
+ } else if (fiber.effectTag === EFFECT_TAGS.UPDATE) {
41
+ cancelEffects(fiber)
42
+ if (fiber.dom != undefined) {
43
+ updateDom(fiber.dom, fiber.alternate.props, fiber.props)
44
+ }
45
+ runEffects(fiber)
46
+ } else if (fiber.effectTag === EFFECT_TAGS.DELETION) {
47
+ cancelEffects(fiber)
48
+ commitDeletion(fiber, domParent)
49
+ return
50
+ }
51
+
52
+ commitWork(fiber.child)
53
+ commitWork(fiber.sibling)
54
+ }
55
+
56
+ /**
57
+ * The function removes a fiber's corresponding DOM node from its parent node or recursively removes
58
+ * its child's DOM node until it finds a node to remove.
59
+ * @param fiber - a fiber node in a fiber tree, which represents a component or an element in the Ryunix
60
+ * application.
61
+ * @param domParent - The parent DOM element from which the fiber's DOM element needs to be removed.
62
+ */
63
+ const commitDeletion = (fiber, domParent) => {
64
+ if (fiber && fiber.dom) {
65
+ domParent.removeChild(fiber.dom)
66
+ } else if (fiber && fiber.child) {
67
+ commitDeletion(fiber.child, domParent)
68
+ }
69
+ }
70
+
71
+ export { commitDeletion, commitWork, commitRoot }
@@ -0,0 +1,41 @@
1
+ import { createDom } from './dom'
2
+ import { reconcileChildren } from './reconciler'
3
+ import { vars } from '../utils/index'
4
+
5
+ /**
6
+ * This function updates a function component by setting up a work-in-progress fiber, resetting the
7
+ * hook index, creating an empty hooks array, rendering the component, and reconciling its children.
8
+ * @param fiber - The fiber parameter is an object that represents a node in the fiber tree. It
9
+ * contains information about the component, its props, state, and children. In this function, it is
10
+ * used to update the state of the component and its children.
11
+ */
12
+ const updateFunctionComponent = (fiber) => {
13
+ vars.wipFiber = fiber
14
+ vars.hookIndex = 0
15
+ vars.wipFiber.hooks = []
16
+ const children = fiber.type(fiber.props)
17
+ let childArr = []
18
+ if (Array.isArray(children)) {
19
+ // Fragment results returns array
20
+ childArr = [...children]
21
+ } else {
22
+ // Normal function component returns single root node
23
+ childArr = [children]
24
+ }
25
+ reconcileChildren(fiber, childArr)
26
+ }
27
+
28
+ /**
29
+ * This function updates a host component's DOM element and reconciles its children.
30
+ * @param fiber - A fiber is a unit of work in Ryunix that represents a component and its state. It
31
+ * contains information about the component's type, props, and children, as well as pointers to other
32
+ * fibers in the tree.
33
+ */
34
+ const updateHostComponent = (fiber) => {
35
+ if (!fiber.dom) {
36
+ fiber.dom = createDom(fiber)
37
+ }
38
+ reconcileChildren(fiber, fiber.props.children)
39
+ }
40
+
41
+ export { updateFunctionComponent, updateHostComponent }
@@ -0,0 +1,78 @@
1
+ import { RYUNIX_TYPES, STRINGS } from '../utils/index'
2
+
3
+ const Fragment = (props) => {
4
+ return props.children
5
+ }
6
+
7
+ const childArray = (children, out) => {
8
+ out = out || []
9
+ if (children == undefined || typeof children == STRINGS.boolean) {
10
+ // pass
11
+ } else if (Array.isArray(children)) {
12
+ children.some((child) => {
13
+ childArray(child, out)
14
+ })
15
+ } else {
16
+ out.push(children)
17
+ }
18
+ return out
19
+ }
20
+
21
+ const cloneElement = (element, props) => {
22
+ return {
23
+ ...element,
24
+ props: {
25
+ ...element.props,
26
+ ...props,
27
+ },
28
+ }
29
+ }
30
+
31
+ /**
32
+ * The function creates a new element with the given type, props, and children.
33
+ * @param type - The type of the element to be created, such as "div", "span", "h1", etc.
34
+ * @param props - The `props` parameter is an object that contains the properties or attributes of the
35
+ * element being created. These properties can include things like `className`, `id`, `style`, and any
36
+ * other custom attributes that the user wants to add to the element. The `props` object is spread
37
+ * using the spread
38
+ * @param children - The `children` parameter is a rest parameter that allows the function to accept
39
+ * any number of arguments after the `props` parameter. These arguments will be treated as children
40
+ * elements of the created element. The `map` function is used to iterate over each child and create a
41
+ * new element if it is not
42
+ * @returns A JavaScript object with a `type` property and a `props` property. The `type` property is
43
+ * set to the `type` argument passed into the function, and the `props` property is an object that
44
+ * includes any additional properties passed in the `props` argument, as well as a `children` property
45
+ * that is an array of any child elements passed in the `...children` argument
46
+ */
47
+
48
+ const createElement = (type, props, ...children) => {
49
+ children = childArray(children, [])
50
+ return {
51
+ type,
52
+ props: {
53
+ ...props,
54
+ children: children.map((child) =>
55
+ typeof child === STRINGS.object ? child : createTextElement(child),
56
+ ),
57
+ },
58
+ }
59
+ }
60
+
61
+ /**
62
+ * The function creates a text element with a given text value.
63
+ * @param text - The text content that will be used to create a new text element.
64
+ * @returns A JavaScript object with a `type` property set to `"TEXT_ELEMENT"` and a `props` property
65
+ * that contains a `nodeValue` property set to the `text` parameter and an empty `children` array.
66
+ */
67
+
68
+ const createTextElement = (text) => {
69
+ return {
70
+ type: RYUNIX_TYPES.TEXT_ELEMENT,
71
+ props: {
72
+ nodeValue: text,
73
+ children: [],
74
+ },
75
+ }
76
+ }
77
+
78
+ export { createElement, createTextElement, Fragment, cloneElement }
package/src/lib/dom.js ADDED
@@ -0,0 +1,96 @@
1
+ import { isEvent, isGone, isNew, isProperty } from './effects'
2
+ import { RYUNIX_TYPES, STRINGS, reg, OLD_STRINGS } from '../utils/index'
3
+
4
+ /**
5
+ * The function creates a new DOM element based on the given fiber object and updates its properties.
6
+ * @param fiber - The fiber parameter is an object that represents a node in the fiber tree. It
7
+ * contains information about the element type, props, and children of the node.
8
+ * @returns The `createDom` function returns a newly created DOM element based on the `fiber` object
9
+ * passed as an argument. If the `fiber` object represents a text element, a text node is created using
10
+ * `document.createTextNode("")`. Otherwise, a new element is created using
11
+ * `document.createElement(fiber.type)`. The function then calls the `updateDom` function to update the
12
+ * properties of the newly created
13
+ */
14
+ const createDom = (fiber) => {
15
+ const dom =
16
+ fiber.type == RYUNIX_TYPES.TEXT_ELEMENT
17
+ ? document.createTextNode('')
18
+ : document.createElement(fiber.type)
19
+
20
+ updateDom(dom, {}, fiber.props)
21
+
22
+ return dom
23
+ }
24
+
25
+ /**
26
+ * The function updates the DOM by removing old event listeners and properties, and adding new ones
27
+ * based on the previous and next props.
28
+ * @param dom - The DOM element that needs to be updated with new props.
29
+ * @param prevProps - An object representing the previous props (properties) of a DOM element.
30
+ * @param nextProps - An object containing the new props that need to be updated in the DOM.
31
+ */
32
+ const updateDom = (dom, prevProps, nextProps) => {
33
+ Object.keys(prevProps)
34
+ .filter(isEvent)
35
+ .filter((key) => isGone(nextProps)(key) || isNew(prevProps, nextProps)(key))
36
+ .forEach((name) => {
37
+ const eventType = name.toLowerCase().substring(2)
38
+ dom.removeEventListener(eventType, prevProps[name])
39
+ })
40
+
41
+ Object.keys(prevProps)
42
+ .filter(isProperty)
43
+ .filter(isGone(nextProps))
44
+ .forEach((name) => {
45
+ dom[name] = ''
46
+ })
47
+
48
+ Object.keys(nextProps)
49
+ .filter(isProperty)
50
+ .filter(isNew(prevProps, nextProps))
51
+ .forEach((name) => {
52
+ if (name === STRINGS.style) {
53
+ DomStyle(dom, nextProps['ryunix-style'])
54
+ } else if (name === OLD_STRINGS.style) {
55
+ DomStyle(dom, nextProps.style)
56
+ } else if (name === STRINGS.className) {
57
+ if (nextProps['ryunix-class'] === '') {
58
+ throw new Error('data-class cannot be empty.')
59
+ }
60
+
61
+ prevProps['ryunix-class'] &&
62
+ dom.classList.remove(...prevProps['ryunix-class'].split(/\s+/))
63
+ dom.classList.add(...nextProps['ryunix-class'].split(/\s+/))
64
+ } else if (name === OLD_STRINGS.className) {
65
+ if (nextProps.className === '') {
66
+ throw new Error('className cannot be empty.')
67
+ }
68
+
69
+ prevProps.className &&
70
+ dom.classList.remove(...prevProps.className.split(/\s+/))
71
+ dom.classList.add(...nextProps.className.split(/\s+/))
72
+ } else {
73
+ dom[name] = nextProps[name]
74
+ }
75
+ })
76
+
77
+ Object.keys(nextProps)
78
+ .filter(isEvent)
79
+ .filter(isNew(prevProps, nextProps))
80
+ .forEach((name) => {
81
+ const eventType = name.toLowerCase().substring(2)
82
+ dom.addEventListener(eventType, nextProps[name])
83
+ })
84
+ }
85
+
86
+ const DomStyle = (dom, style) => {
87
+ dom.style = Object.keys(style).reduce((acc, styleName) => {
88
+ const key = styleName.replace(reg, function (v) {
89
+ return '-' + v.toLowerCase()
90
+ })
91
+ acc += `${key}: ${style[styleName]};`
92
+ return acc
93
+ }, '')
94
+ }
95
+
96
+ export { DomStyle, createDom, updateDom }
@@ -0,0 +1,55 @@
1
+ import { RYUNIX_TYPES, STRINGS } from '../utils/index'
2
+
3
+ const isEvent = (key) => key.startsWith('on')
4
+ const isProperty = (key) => key !== STRINGS.children && !isEvent(key)
5
+ const isNew = (prev, next) => (key) => prev[key] !== next[key]
6
+ const isGone = (next) => (key) => !(key in next)
7
+ const hasDepsChanged = (prevDeps, nextDeps) =>
8
+ !prevDeps ||
9
+ !nextDeps ||
10
+ prevDeps.length !== nextDeps.length ||
11
+ prevDeps.some((dep, index) => dep !== nextDeps[index])
12
+
13
+ /**
14
+ * The function cancels all effect hooks in a given fiber.
15
+ * @param fiber - The "fiber" parameter is likely referring to a data structure used in React.js to
16
+ * represent a component and its state. It contains information about the component's props, state, and
17
+ * children, as well as metadata used by React to manage updates and rendering. The function
18
+ * "cancelEffects" is likely intended
19
+ */
20
+ const cancelEffects = (fiber) => {
21
+ if (fiber.hooks) {
22
+ fiber.hooks
23
+ .filter((hook) => hook.tag === RYUNIX_TYPES.RYUNIX_EFFECT && hook.cancel)
24
+ .forEach((effectHook) => {
25
+ effectHook.cancel()
26
+ })
27
+ }
28
+ }
29
+
30
+ /**
31
+ * The function runs all effect hooks in a given fiber.
32
+ * @param fiber - The "fiber" parameter is likely referring to a data structure used in the
33
+ * implementation of a fiber-based reconciliation algorithm, such as the one used in React. A fiber
34
+ * represents a unit of work that needs to be performed by the reconciliation algorithm, and it
35
+ * contains information about a component and its children, as
36
+ */
37
+ const runEffects = (fiber) => {
38
+ if (fiber.hooks) {
39
+ fiber.hooks
40
+ .filter((hook) => hook.tag === RYUNIX_TYPES.RYUNIX_EFFECT && hook.effect)
41
+ .forEach((effectHook) => {
42
+ effectHook.cancel = effectHook.effect()
43
+ })
44
+ }
45
+ }
46
+
47
+ export {
48
+ runEffects,
49
+ cancelEffects,
50
+ isEvent,
51
+ isProperty,
52
+ isNew,
53
+ isGone,
54
+ hasDepsChanged,
55
+ }
@@ -0,0 +1,202 @@
1
+ import { RYUNIX_TYPES, STRINGS, vars } from '../utils/index'
2
+ import { isEqual } from 'lodash'
3
+ import { Fragment, createElement, cloneElement } from './createElement'
4
+ /**
5
+ * @description The function creates a state.
6
+ * @param initial - The initial value of the state for the hook.
7
+ * @returns The `useStore` function returns an array with two elements: the current state value and a
8
+ * `setState` function that can be used to update the state.
9
+ */
10
+ const useStore = (initial) => {
11
+ const oldHook =
12
+ vars.wipFiber.alternate &&
13
+ vars.wipFiber.alternate.hooks &&
14
+ vars.wipFiber.alternate.hooks[vars.hookIndex]
15
+ const hook = {
16
+ state: oldHook ? oldHook.state : initial,
17
+ queue: [],
18
+ }
19
+
20
+ const actions = oldHook ? oldHook.queue : []
21
+ actions.forEach((action) => {
22
+ hook.state =
23
+ typeof action === STRINGS.function ? action(hook.state) : action
24
+ })
25
+
26
+ /**
27
+ * The function `setState` updates the state of a component in Ryunix by adding an action to a queue
28
+ * and setting up a new work-in-progress root.
29
+ * @param action - The `action` parameter is an object that represents a state update to be performed
30
+ * on a component. It contains information about the type of update to be performed and any new data
31
+ * that needs to be applied to the component's state.
32
+ */
33
+ const setState = (action) => {
34
+ hook.queue.push(action)
35
+ vars.wipRoot = {
36
+ dom: vars.currentRoot.dom,
37
+ props: vars.currentRoot.props,
38
+ alternate: vars.currentRoot,
39
+ }
40
+ vars.nextUnitOfWork = vars.wipRoot
41
+ vars.deletions = []
42
+ }
43
+
44
+ if (vars.wipFiber && vars.wipFiber.hooks) {
45
+ vars.wipFiber.hooks.push(hook)
46
+ vars.hookIndex++
47
+ }
48
+ return [hook.state, setState]
49
+ }
50
+
51
+ /**
52
+ * This is a function that creates a hook for managing side effects in Ryunix components.
53
+ * @param effect - The effect function that will be executed after the component has rendered or when
54
+ * the dependencies have changed. It can perform side effects such as fetching data, updating the DOM,
55
+ * or subscribing to events.
56
+ * @param deps - An array of dependencies that the effect depends on. If any of the dependencies change
57
+ * between renders, the effect will be re-run. If the array is empty, the effect will only run once on
58
+ * mount and never again.
59
+ */
60
+
61
+ const useEffect = (callback, deps) => {
62
+ const oldHook =
63
+ vars.wipFiber.alternate &&
64
+ vars.wipFiber.alternate.hooks &&
65
+ vars.wipFiber.alternate.hooks[vars.hookIndex]
66
+
67
+ const hook = {
68
+ type: RYUNIX_TYPES.RYUNIX_EFFECT,
69
+ deps,
70
+ }
71
+
72
+ if (!oldHook) {
73
+ // invoke callback if this is the first time
74
+ callback()
75
+ } else {
76
+ if (!isEqual(oldHook.deps, hook.deps)) {
77
+ callback()
78
+ }
79
+ }
80
+
81
+ if (vars.wipFiber.hooks) {
82
+ vars.wipFiber.hooks.push(hook)
83
+ vars.hookIndex++
84
+ }
85
+ }
86
+
87
+ /**
88
+ * The `useQuery` function is a custom hook in JavaScript that retrieves query parameters from the URL
89
+ * and stores them in a hook for easy access.
90
+ * @returns The `useQuery` function returns the `query` property of the `hook` object.
91
+ */
92
+ const useQuery = () => {
93
+ const oldHook =
94
+ vars.wipFiber.alternate &&
95
+ vars.wipFiber.alternate.hooks &&
96
+ vars.wipFiber.alternate.hooks[vars.hookIndex]
97
+
98
+ const hasOld = oldHook ? oldHook : undefined
99
+
100
+ const urlSearchParams = new URLSearchParams(window.location.search)
101
+ const params = Object.fromEntries(urlSearchParams.entries())
102
+ const Query = hasOld ? hasOld : params
103
+
104
+ const hook = {
105
+ type: RYUNIX_TYPES.RYUNIX_URL_QUERY,
106
+ query: Query,
107
+ }
108
+
109
+ if (vars.wipFiber.hooks) {
110
+ vars.wipFiber.hooks.push(hook)
111
+ vars.hookIndex++
112
+ }
113
+
114
+ return hook.query
115
+ }
116
+
117
+ const useRef = (initial) => {
118
+ const oldHook =
119
+ vars.wipFiber.alternate &&
120
+ vars.wipFiber.alternate.hooks &&
121
+ vars.wipFiber.alternate.hooks[vars.hookIndex]
122
+
123
+ const hook = {
124
+ type: RYUNIX_TYPES.RYUNIX_REF,
125
+ value: oldHook ? oldHook.value : { current: initial },
126
+ }
127
+
128
+ if (vars.wipFiber.hooks) {
129
+ vars.wipFiber.hooks.push(hook)
130
+ vars.hookIndex++
131
+ }
132
+
133
+ return hook.value
134
+ }
135
+
136
+ const useMemo = (comp, deps) => {
137
+ const oldHook =
138
+ vars.wipFiber.alternate &&
139
+ vars.wipFiber.alternate.hooks &&
140
+ vars.wipFiber.alternate.hooks[vars.hookIndex]
141
+
142
+ const hook = {
143
+ type: RYUNIX_TYPES.RYUNIX_MEMO,
144
+ value: null,
145
+ deps,
146
+ }
147
+
148
+ if (oldHook) {
149
+ if (isEqual(oldHook.deps, hook.deps)) {
150
+ hook.value = oldHook.value
151
+ } else {
152
+ hook.value = comp()
153
+ }
154
+ } else {
155
+ hook.value = comp()
156
+ }
157
+
158
+ if (vars.wipFiber.hooks) {
159
+ vars.wipFiber.hooks.push(hook)
160
+ vars.hookIndex++
161
+ }
162
+
163
+ return hook.value
164
+ }
165
+ const useCallback = (callback, deps) => {
166
+ return useMemo(() => callback, deps)
167
+ }
168
+
169
+ const useRouter = (routes) => {
170
+ const [location, setLocation] = useStore(window.location.pathname)
171
+
172
+ const navigate = (path) => {
173
+ window.history.pushState({}, '', path)
174
+ setLocation(path)
175
+ }
176
+
177
+ useEffect(() => {
178
+ const onPopState = () => {
179
+ setLocation(window.location.pathname)
180
+ }
181
+
182
+ window.addEventListener('popstate', onPopState)
183
+ return () => {
184
+ window.removeEventListener('popstate', onPopState)
185
+ }
186
+ }, [])
187
+
188
+ const currentRoute = routes.find((route) => route.path === location)
189
+ const Children = () => (currentRoute ? currentRoute.component : null)
190
+
191
+ return { Children, navigate }
192
+ }
193
+
194
+ export {
195
+ useStore,
196
+ useEffect,
197
+ useQuery,
198
+ useRef,
199
+ useMemo,
200
+ useCallback,
201
+ useRouter,
202
+ }
@@ -0,0 +1,39 @@
1
+ import { createElement, cloneElement, Fragment } from './createElement'
2
+ import { render, init } from './render'
3
+ import {
4
+ useStore,
5
+ useEffect,
6
+ useQuery,
7
+ useRef,
8
+ useMemo,
9
+ useCallback,
10
+ useRouter,
11
+ } from './hooks'
12
+ import * as Dom from './dom'
13
+ import * as Workers from './workers'
14
+ import * as Reconciler from './reconciler'
15
+ import * as Components from './components'
16
+ import * as Commits from './commits'
17
+
18
+ export {
19
+ useStore,
20
+ useEffect,
21
+ useQuery,
22
+ useRef,
23
+ useMemo,
24
+ useCallback,
25
+ useRouter,
26
+ Fragment,
27
+ }
28
+
29
+ export default {
30
+ createElement,
31
+ render,
32
+ init,
33
+ Fragment,
34
+ Dom,
35
+ Workers,
36
+ Reconciler,
37
+ Components,
38
+ Commits,
39
+ }
@@ -0,0 +1,62 @@
1
+ import { EFFECT_TAGS, vars } from '../utils/index'
2
+
3
+ /**
4
+ * This function reconciles the children of a fiber node with a new set of elements, creating new
5
+ * fibers for new elements, updating existing fibers for elements with the same type, and marking old
6
+ * fibers for deletion if they are not present in the new set of elements.
7
+ * @param wipFiber - A work-in-progress fiber object representing a component or element in the virtual
8
+ * DOM tree.
9
+ * @param elements - an array of elements representing the new children to be rendered in the current
10
+ * fiber's subtree
11
+ */
12
+ const reconcileChildren = (wipFiber, elements) => {
13
+ let index = 0
14
+ let oldFiber = wipFiber.alternate && wipFiber.alternate.child
15
+ let prevSibling
16
+
17
+ while (index < elements.length || oldFiber != undefined) {
18
+ const element = elements[index]
19
+ let newFiber
20
+
21
+ const sameType = oldFiber && element && element.type == oldFiber.type
22
+
23
+ if (sameType) {
24
+ newFiber = {
25
+ type: oldFiber ? oldFiber.type : undefined,
26
+ props: element.props,
27
+ dom: oldFiber ? oldFiber.dom : undefined,
28
+ parent: wipFiber,
29
+ alternate: oldFiber,
30
+ effectTag: EFFECT_TAGS.UPDATE,
31
+ }
32
+ }
33
+ if (element && !sameType) {
34
+ newFiber = {
35
+ type: element.type,
36
+ props: element.props,
37
+ dom: undefined,
38
+ parent: wipFiber,
39
+ alternate: undefined,
40
+ effectTag: EFFECT_TAGS.PLACEMENT,
41
+ }
42
+ }
43
+ if (oldFiber && !sameType) {
44
+ oldFiber.effectTag = EFFECT_TAGS.DELETION
45
+ vars.deletions.push(oldFiber)
46
+ }
47
+
48
+ if (oldFiber) {
49
+ oldFiber = oldFiber.sibling
50
+ }
51
+
52
+ if (index === 0) {
53
+ wipFiber.child = newFiber
54
+ } else if (element && prevSibling) {
55
+ prevSibling.sibling = newFiber
56
+ }
57
+
58
+ prevSibling = newFiber
59
+ index++
60
+ }
61
+ }
62
+ export { reconcileChildren }