lahama 1.1.0 → 2.0.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.
Files changed (2) hide show
  1. package/dist/lahama.js +42 -16
  2. package/package.json +3 -3
package/dist/lahama.js CHANGED
@@ -1,6 +1,9 @@
1
1
  function withoutNulls(arr) {
2
2
  return arr.filter((item) => item != null)
3
3
  }
4
+ const a = { };
5
+ const b = { };
6
+ console.log(a === b);
4
7
 
5
8
  const DOM_TYPES = {
6
9
  TEXT : 'text',
@@ -125,18 +128,18 @@ function setAttribute(el, name, value) {
125
128
  }
126
129
  }
127
130
 
128
- function mountDom(vdom, parentEl) {
131
+ function mountDom(vdom, parentEl, index) {
129
132
  switch (vdom.type) {
130
133
  case DOM_TYPES.TEXT : {
131
- createTextNode(vdom, parentEl);
134
+ createTextNode(vdom, parentEl, index);
132
135
  break
133
136
  }
134
137
  case DOM_TYPES.ELEMENT : {
135
- createElementNode(vdom, parentEl);
138
+ createElementNode(vdom, parentEl, index);
136
139
  break
137
140
  }
138
141
  case DOM_TYPES.FRAGMENT : {
139
- createFragmentNodes(vdom, parentEl);
142
+ createFragmentNodes(vdom, parentEl, index);
140
143
  break
141
144
  }
142
145
  default : {
@@ -144,29 +147,46 @@ function mountDom(vdom, parentEl) {
144
147
  }
145
148
  }
146
149
  }
147
- function createTextNode(vdom, parentEl) {
150
+ function insert(el, parentEl, index) {
151
+ if (index == null) {
152
+ parentEl.append(el);
153
+ return
154
+ }
155
+ if (index < 0) {
156
+ throw new Error(
157
+ `Index must be a positive integer, got &{index}`)
158
+ }
159
+ const children = parentEl.childNodes;
160
+ if (index >= children.length) {
161
+ parentEl.append(el);
162
+ } else {
163
+ parentEl.insertBefore(el, children[index]);
164
+ }
165
+ }
166
+ function createTextNode(vdom, parentEl, index) {
148
167
  const { value } = vdom;
149
168
  const textNode = document.createTextNode(value);
150
169
  vdom.el = textNode;
151
- parentEl.append(textNode);
170
+ insert(textNode, parentEl, index);
152
171
  }
153
- function createElementNode(vdom, parentEl) {
172
+ function createElementNode(vdom, parentEl, index) {
154
173
  const { tag, props , children } = vdom;
155
174
  const element = document.createElement(tag);
156
175
  addProps(element, props , vdom);
157
176
  vdom.el = element;
177
+ //!function mountDom(vnode, parentEl) {
158
178
  children.forEach((child) => mountDom(child, element));
159
- parentEl.append(element);
179
+ insert(element, parentEl, index);
160
180
  }
161
181
  function addProps(el, props, vdom) {
162
182
  const { on : events, ...attrs } = props;
163
183
  vdom.listeners = addEventListeners(events, el);
164
184
  setAttributes(el, attrs);
165
185
  }
166
- function createFragmentNodes(vdom, parentEl) {
186
+ function createFragmentNodes(vdom, parentEl, index) {
167
187
  const { children } = vdom;
168
188
  vdom.el = parentEl;
169
- children.forEach((child) => mountDom(child, parentEl));
189
+ children.forEach((child, i) => mountDom(child, parentEl, index ? index + i : null));
170
190
  }
171
191
 
172
192
  class Dispatcher {
@@ -207,6 +227,7 @@ class Dispatcher {
207
227
  function createApp({ state, view, reducers = {} }) {
208
228
  let parentEl = null;
209
229
  let vdom = null;
230
+ let isMounted = false;
210
231
  const dispatcher = new Dispatcher();
211
232
  const subscriptions = [dispatcher.afterEveryCommand(renderApp)];
212
233
  function emit(eventName, payload) {
@@ -220,23 +241,28 @@ function createApp({ state, view, reducers = {} }) {
220
241
  subscriptions.push(subs);
221
242
  }
222
243
  function renderApp() {
223
- if (vdom) {
224
- destroyDom(vdom);
225
- }
226
- vdom = view(state, emit);
227
- mountDom(vdom, parentEl);
244
+ view(state, emit);
245
+ vdom = patchDom();
228
246
  }
229
247
  return {
230
248
  mount(_parentEl) {
249
+ if (isMounted) {
250
+ throw new Error("The application is already mounted")
251
+ }
231
252
  parentEl = _parentEl;
232
- renderApp();
253
+ vdom = view(state, emit);
254
+ mountDom(vdom, parentEl);
255
+ isMounted = true;
233
256
  },
234
257
  unmount() {
235
258
  destroyDom(vdom);
236
259
  vdom = null;
237
260
  subscriptions.forEach((unsubscribe) => unsubscribe());
261
+ isMounted = false;
238
262
  },
239
263
  }
240
264
  }
265
+ function patchDom(vdom, newVdom, parentEl) {
266
+ }
241
267
 
242
268
  export { createApp, h, hFragment, hString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lahama",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "",
5
5
  "main": "dist/lahama.js",
6
6
  "files": [
@@ -20,11 +20,11 @@
20
20
  "type": "module",
21
21
  "devDependencies": {
22
22
  "eslint": "^9.39.0",
23
- "jsdom": "^27.1.0",
23
+ "jsdom": "^27.2.0",
24
24
  "rollup": "^4.52.5",
25
25
  "rollup-plugin-cleanup": "^3.2.1",
26
26
  "rollup-plugin-filesize": "^10.0.0",
27
- "vitest": "^4.0.6"
27
+ "vitest": "^4.0.15"
28
28
  },
29
29
  "dependencies": {
30
30
  "@eslint/js": "^9.39.0",