-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
244 lines (134 loc) · 4.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
export const Fragment = ({ children }) => children
export const h = function (type, props) {
(props ??= create(null)).nodeName = type
if (!('children' in props || arguments.length < 3)) props.children = arguments.length === 3 ? arguments[2] : slice.call(arguments, 2)
return props
}
export const render = (h, el) => {
let child = el.firstChild
for (h of normalize(h)) {
let node = child
if (typeof h === 'string') {
while (node && node.nodeType != 3) node = node.nextSibling
node ? node.data != h && (node.data = h) : node = document.createTextNode(h)
} else {
const { nodeName, key, skip, memo, ref, children } = h
while (node && !(node.localName === nodeName && (node.$key ??= key) == key)) node = node.nextSibling
node ??= assign(document.createElementNS(h.xmlns ?? nodeName === 'svg' ? svg : el.namespaceURI, nodeName), { $key: key })
if (memo == null || some(node.$memo, node.$memo = memo)) {
const { $props } = node, props = {}
for (const name of keys(assign({}, $props, h))) {
if (omit.has(name)) continue
const value = props[name] = h[name]
if (value === $props?.[name]) continue
if (name.startsWith('set:')) node[name.slice(4)] = value
else if (value == null || value === false) node.removeAttribute(name)
else node.setAttribute(name, value === true ? '' : value)
}
node.$props = props
if (!skip) render(children, node)
if (typeof ref === 'function') (node.$ref = ref)(node)
}
}
node === child ? child = child.nextSibling : before(el, node, child)
}
while (child) {
const next = child.nextSibling
if (child.nodeType === 1) unref(child)
el.removeChild(child)
child = next
}
}
const { isArray, prototype: { slice } } = Array
const { create, assign, keys, prototype: { hasOwnProperty }, hasOwn = (o, k) => hasOwnProperty.call(o, k) } = Object
const svg = 'http://www.w3.org/2000/svg', omit = new Set('nodeName,key,skip,memo,ref,children'.split(','))
const some = (a, b) => isArray(a) && isArray(b) ? a.some((v, i) => v !== b[i]) : a !== b
const normalize = function* (h, buffer = { value: '' }, root = true) {
for (h of isArray(h) ? h : [h]) {
if (h == null || typeof h === 'boolean') continue
if (hasOwn(h, 'nodeName')) {
const { value } = buffer, { nodeName } = h
if (value) yield value, buffer.value = ''
if (typeof nodeName === 'function') {
if (nodeName.constructor.name === 'GeneratorFunction') {
const args = {}, attrs = assign({}, nodeName.attrs)
for (const key of keys(h)) {
const value = h[key]
if (key.startsWith('attr:')) attrs[key.slice(5)] = value
else if (key === 'key' || key === 'memo') attrs[key] = value
else args[key] = value
}
attrs.nodeName = nodeName.is ?? 'div'
attrs.skip = true
attrs.ref = next.bind(null, nodeName, args)
yield attrs
} else yield* normalize(nodeName(h), buffer, false)
} else yield h
} else isArray(h) ? yield* normalize(h, buffer, false) : buffer.value += h
}
if (root && buffer.value) yield buffer.value
}
const next = (gen, h, el) => {
if (!el) return
el.$generator ??= (assign(el, methods), el.$context ??= create(current?.$context ?? null), gen)
const { skip, ref, ...args } = h
el.$ref = dispose.bind(null, ref, el)
assign(el.$args ??= {}, args)
if (!skip) el.next()
}
const dispose = (ref, component, el) => {
if (typeof ref === 'function') ref(el)
if (!el) component.return()
}
const before = (el, node, child) => {
if (node.contains(document.activeElement)) {
const ref = node.nextSibling
while (child && child != node) {
const next = child.nextSibling
el.insertBefore(child, ref)
child = next
}
} else el.insertBefore(node, child)
}
const unref = el => {
for (const child of el.children) unref(child)
const { $ref } = el
if (typeof $ref === 'function') $ref(null)
}
let current = null
const methods = {
render() {
if (!current?.contains(this)) this.next()
},
next() {
const parent = current
current = this
try {
render((this.$iterator ??= this.$generator.call(this, this.$args)).next().value, this)
if (typeof this.$ref === 'function') this.$ref(this)
} catch (value) {
this.throw(value)
} finally {
current = parent
}
},
throw(value) {
for (let el = this; el; el = el.parentNode) if (typeof el.$iterator?.throw === 'function') try {
return render(el.$iterator.throw(value).value, el)
} catch { }
throw value
},
return() {
try {
this.$iterator?.return()
} catch (value) {
this.throw(value)
} finally {
this.$iterator = null
}
}
}
export const context = (fallback, key = Symbol()) => function () {
const ctx = this ?? current
return ctx ? arguments.length === 0 ? key in ctx.$context ? ctx.$context[key] : fallback : ctx.$context[key] = arguments[0] : fallback
}