lahama 1.0.0 → 1.1.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.
- package/dist/lahama.js +242 -0
- package/package.json +4 -3
package/dist/lahama.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
function withoutNulls(arr) {
|
|
2
|
+
return arr.filter((item) => item != null)
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
const DOM_TYPES = {
|
|
6
|
+
TEXT : 'text',
|
|
7
|
+
ELEMENT : 'element',
|
|
8
|
+
FRAGMENT : 'fragment',
|
|
9
|
+
};
|
|
10
|
+
function h(tag, props = {} , children = []) {
|
|
11
|
+
return {
|
|
12
|
+
tag,
|
|
13
|
+
props,
|
|
14
|
+
children: mapTextNodes(withoutNulls(children)),
|
|
15
|
+
type : DOM_TYPES.ELEMENT,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function mapTextNodes(children) {
|
|
19
|
+
return children.map((child) =>
|
|
20
|
+
typeof child === 'string' ? hString(child) : child)
|
|
21
|
+
}
|
|
22
|
+
function hString(str) {
|
|
23
|
+
return { type : DOM_TYPES.TEXT, value : str}
|
|
24
|
+
}
|
|
25
|
+
function hFragment(vNodes) {
|
|
26
|
+
return {
|
|
27
|
+
type : DOM_TYPES.FRAGMENT,
|
|
28
|
+
children : mapTextNodes(withoutNulls(vNodes)),
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function addEventListener(eventName, handler, el) {
|
|
33
|
+
el.addEventListener(eventName, handler);
|
|
34
|
+
return handler
|
|
35
|
+
}
|
|
36
|
+
function addEventListeners(listeners = {}, el) {
|
|
37
|
+
const addedListeners = {};
|
|
38
|
+
Object.entries(listeners).forEach(([eventName, handler]) => {
|
|
39
|
+
addedListeners[eventName] = addEventListener(eventName, handler, el);
|
|
40
|
+
});
|
|
41
|
+
return addedListeners
|
|
42
|
+
}
|
|
43
|
+
function removeEventListeners(listeners = {}, el) {
|
|
44
|
+
Object.entries(listeners).forEach(([eventName, handler]) => {
|
|
45
|
+
el.removeEventListener(eventName, handler);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function destroyDom(vdom) {
|
|
50
|
+
const { type } = vdom;
|
|
51
|
+
switch (type) {
|
|
52
|
+
case DOM_TYPES.TEXT : {
|
|
53
|
+
removeTextNode(vdom);
|
|
54
|
+
break
|
|
55
|
+
}
|
|
56
|
+
case DOM_TYPES.ELEMENT : {
|
|
57
|
+
removeElementNode(vdom);
|
|
58
|
+
break
|
|
59
|
+
}
|
|
60
|
+
case DOM_TYPES.FRAGMENT : {
|
|
61
|
+
removeFragmentNode(vdom);
|
|
62
|
+
break
|
|
63
|
+
}
|
|
64
|
+
default : {
|
|
65
|
+
throw new Error(`Can't destroy DOM of type ${type}`)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
delete vdom.el;
|
|
69
|
+
}
|
|
70
|
+
function removeTextNode(vdom) {
|
|
71
|
+
const { el } = vdom;
|
|
72
|
+
el.remove();
|
|
73
|
+
}
|
|
74
|
+
function removeElementNode(vdom) {
|
|
75
|
+
const { el , children, listeners } = vdom;
|
|
76
|
+
el.remove();
|
|
77
|
+
children.forEach(destroyDom);
|
|
78
|
+
if (listeners) {
|
|
79
|
+
removeEventListeners(listeners, el);
|
|
80
|
+
delete vdom.listeners;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function removeFragmentNode(vdom) {
|
|
84
|
+
const { children } = vdom;
|
|
85
|
+
children.forEach(destroyDom);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function setAttributes(el, attrs) {
|
|
89
|
+
const { class : className, style, ...otherAttrs} = attrs;
|
|
90
|
+
if (className) {
|
|
91
|
+
setClass(el, className);
|
|
92
|
+
}
|
|
93
|
+
if (style) {
|
|
94
|
+
Object.entries(style).forEach(([prop, value]) => {
|
|
95
|
+
setStyle(el, prop, value);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
for (const [name, value] of Object.entries(otherAttrs)) {
|
|
99
|
+
setAttribute(el, name, value);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function setClass(el, className) {
|
|
103
|
+
el.className = '';
|
|
104
|
+
if (typeof className === 'string') {
|
|
105
|
+
el.className = className;
|
|
106
|
+
}
|
|
107
|
+
if (Array.isArray(className)) {
|
|
108
|
+
el.classList.add(...className);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function setStyle(el, name, value) {
|
|
112
|
+
el.style[name] = value;
|
|
113
|
+
}
|
|
114
|
+
function removeAttributeCustom(el, name) {
|
|
115
|
+
el[name] = null;
|
|
116
|
+
el.removeAttribute(name);
|
|
117
|
+
}
|
|
118
|
+
function setAttribute(el, name, value) {
|
|
119
|
+
if (value == null) {
|
|
120
|
+
removeAttributeCustom(el, name);
|
|
121
|
+
} else if (name.startsWith('data-')) {
|
|
122
|
+
el.setAttribute(name, value);
|
|
123
|
+
} else {
|
|
124
|
+
el[name] = value;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function mountDom(vdom, parentEl) {
|
|
129
|
+
switch (vdom.type) {
|
|
130
|
+
case DOM_TYPES.TEXT : {
|
|
131
|
+
createTextNode(vdom, parentEl);
|
|
132
|
+
break
|
|
133
|
+
}
|
|
134
|
+
case DOM_TYPES.ELEMENT : {
|
|
135
|
+
createElementNode(vdom, parentEl);
|
|
136
|
+
break
|
|
137
|
+
}
|
|
138
|
+
case DOM_TYPES.FRAGMENT : {
|
|
139
|
+
createFragmentNodes(vdom, parentEl);
|
|
140
|
+
break
|
|
141
|
+
}
|
|
142
|
+
default : {
|
|
143
|
+
throw new Error(`Can't mount DOM of type: ${vdom.type}`)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
function createTextNode(vdom, parentEl) {
|
|
148
|
+
const { value } = vdom;
|
|
149
|
+
const textNode = document.createTextNode(value);
|
|
150
|
+
vdom.el = textNode;
|
|
151
|
+
parentEl.append(textNode);
|
|
152
|
+
}
|
|
153
|
+
function createElementNode(vdom, parentEl) {
|
|
154
|
+
const { tag, props , children } = vdom;
|
|
155
|
+
const element = document.createElement(tag);
|
|
156
|
+
addProps(element, props , vdom);
|
|
157
|
+
vdom.el = element;
|
|
158
|
+
children.forEach((child) => mountDom(child, element));
|
|
159
|
+
parentEl.append(element);
|
|
160
|
+
}
|
|
161
|
+
function addProps(el, props, vdom) {
|
|
162
|
+
const { on : events, ...attrs } = props;
|
|
163
|
+
vdom.listeners = addEventListeners(events, el);
|
|
164
|
+
setAttributes(el, attrs);
|
|
165
|
+
}
|
|
166
|
+
function createFragmentNodes(vdom, parentEl) {
|
|
167
|
+
const { children } = vdom;
|
|
168
|
+
vdom.el = parentEl;
|
|
169
|
+
children.forEach((child) => mountDom(child, parentEl));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
class Dispatcher {
|
|
173
|
+
#subs = new Map()
|
|
174
|
+
#afterHandlers = []
|
|
175
|
+
subscribe(commandName, handler) {
|
|
176
|
+
if (!this.#subs.has(commandName)) {
|
|
177
|
+
this.#subs.set(commandName, []);
|
|
178
|
+
}
|
|
179
|
+
const handlersArray = this.#subs.get(commandName);
|
|
180
|
+
if (handlersArray.includes(handler)) {
|
|
181
|
+
return () => {
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
handlersArray.push(handler);
|
|
185
|
+
return () => {
|
|
186
|
+
const idx = handlersArray.indexOf(handler);
|
|
187
|
+
handlersArray.splice(idx, 1);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
afterEveryCommand(handler) {
|
|
191
|
+
this.#afterHandlers.push(handler);
|
|
192
|
+
return () => {
|
|
193
|
+
const idx = this.#afterHandlers.indexOf(handler);
|
|
194
|
+
this.#afterHandlers.splice(idx, 1);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
dispatch(commandName, payload) {
|
|
198
|
+
if (this.#subs.has(commandName)) {
|
|
199
|
+
this.#subs.get(commandName).forEach((handler) => handler(payload));
|
|
200
|
+
} else {
|
|
201
|
+
console.warn(`No handlers for command : ${commandName}`);
|
|
202
|
+
}
|
|
203
|
+
this.#afterHandlers.forEach((handler) => handler());
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function createApp({ state, view, reducers = {} }) {
|
|
208
|
+
let parentEl = null;
|
|
209
|
+
let vdom = null;
|
|
210
|
+
const dispatcher = new Dispatcher();
|
|
211
|
+
const subscriptions = [dispatcher.afterEveryCommand(renderApp)];
|
|
212
|
+
function emit(eventName, payload) {
|
|
213
|
+
dispatcher.dispatch(eventName, payload);
|
|
214
|
+
}
|
|
215
|
+
for (const actionName in reducers) {
|
|
216
|
+
const reducer = reducers[actionName];
|
|
217
|
+
const subs = dispatcher.subscribe(actionName, (payload) => {
|
|
218
|
+
state = reducer(state, payload);
|
|
219
|
+
});
|
|
220
|
+
subscriptions.push(subs);
|
|
221
|
+
}
|
|
222
|
+
function renderApp() {
|
|
223
|
+
if (vdom) {
|
|
224
|
+
destroyDom(vdom);
|
|
225
|
+
}
|
|
226
|
+
vdom = view(state, emit);
|
|
227
|
+
mountDom(vdom, parentEl);
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
mount(_parentEl) {
|
|
231
|
+
parentEl = _parentEl;
|
|
232
|
+
renderApp();
|
|
233
|
+
},
|
|
234
|
+
unmount() {
|
|
235
|
+
destroyDom(vdom);
|
|
236
|
+
vdom = null;
|
|
237
|
+
subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
238
|
+
},
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export { createApp, h, hFragment, hString };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lahama",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/lahama.js",
|
|
6
6
|
"files": [
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"lint": "eslint src",
|
|
13
13
|
"lint:fix": "eslint src --fix",
|
|
14
14
|
"test": "vitest",
|
|
15
|
-
"test:run"
|
|
15
|
+
"test:run": "vitest run"
|
|
16
16
|
},
|
|
17
17
|
"keywords": [],
|
|
18
18
|
"author": "",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@eslint/js": "^9.39.0",
|
|
31
|
-
"globals": "^16.5.0"
|
|
31
|
+
"globals": "^16.5.0",
|
|
32
|
+
"lahama": "^1.1.0"
|
|
32
33
|
}
|
|
33
34
|
}
|