-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.go
More file actions
673 lines (617 loc) · 15.7 KB
/
engine.go
File metadata and controls
673 lines (617 loc) · 15.7 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
package hypp
// This file is based on https://github.com/jorgebucaran/hyperapp/blob/main/index.js
import (
"fmt"
"sort"
"strings"
"github.com/macabot/hypp/js"
"github.com/macabot/hypp/window"
)
const svgNS = "http://www.w3.org/2000/svg"
// ValidateHProps validates the given [HProps].
// It returns an error if any of the keys have an invalid value.
// See the description of [HProps] for a table of the keys and their allowed value types.
func ValidateHProps(props HProps) error {
for key, value := range props {
if len(key) >= 2 && key[0] == 'o' && key[1] == 'n' {
if value == nil {
continue
}
if _, ok := value.(Dispatchable); !ok {
return fmt.Errorf("hypp: expected HProps key '%s' to have Dispatchable value. Got %#v", key, value)
}
} else if key == "class" {
switch value.(type) {
case bool, int, float64, string, []string, map[string]bool:
// Do nothing
default:
return fmt.Errorf("hypp: expected HProps key '%s' to have value of type bool, int, float64, string, []string or map[string]bool. Got %#v", key, value)
}
} else if key == "style" {
if _, ok := value.(map[string]string); !ok {
return fmt.Errorf("hypp: expected HProps key '%s' to have value of type map[string]string. Got %#v", key, value)
}
} else {
switch value.(type) {
case bool, int, float64, string:
// Do nothing
default:
return fmt.Errorf("hypp: expected HProps key '%s' to have value of type bool, int, float64 or string. Got %#v", key, value)
}
}
}
return nil
}
// MustValidateHProps calls [ValidateHProps] and panics if an error is returned.
func MustValidateHProps(props HProps) {
if err := ValidateHProps(props); err != nil {
panic(err)
}
}
func h(tag string, props HProps, children vKids) *VNode {
MustValidateHProps(props)
props = props.clone()
if classOption := props.get("class"); classOption.OK {
props.Set("class", createClass(classOption.V))
}
return &VNode{
tag: tag,
props: props,
children: children,
kind: ElementNode,
}
}
func memo(view func(data MemoData) *VNode, data MemoData) *VNode {
return &VNode{
memoView: view,
memoData: data,
}
}
func text(value string, node window.Element) *VNode {
return &VNode{
tag: value,
kind: TextNode,
node: node,
}
}
func dispatchWrapperID(dispatch Dispatch) Dispatch {
return dispatch
}
func recycleNode(node window.Element) *VNode {
if node.NodeType() == int(TextNode) {
return text(node.NodeValue(), node)
} else {
childNodes := node.ChildNodes()
children := make([]*VNode, len(childNodes))
for i, childNode := range childNodes {
children[i] = recycleNode(childNode)
}
return &VNode{
tag: strings.ToLower(node.NodeName()),
children: children,
kind: ElementNode,
node: node,
}
}
}
// shouldRestart returns true if a is not equal to b.
// If a and b are not comparable it always returns false.
func shouldRestart(a, b Payload) bool {
defer func() {
_ = recover()
}()
return a != b
}
type subscriptions []Subscription
func (s subscriptions) Index(i int) Subscription {
if i < len(s) {
return s[i]
}
return Subscription{Disabled: true}
}
// patchSubs loops over the oldSubs and newSubs and compares the subscriptions at the same index.
// It compares the Disabled field and Payload field. It does not compare the Subscriber field, because Go can't compare functions.
func patchSubs(oldSubs, newSubs subscriptions, dispatch Dispatch) []Subscription {
var subs []Subscription
for i := 0; i < len(oldSubs) || i < len(newSubs); i++ {
oldSub := oldSubs.Index(i)
newSub := newSubs.Index(i)
var sub Subscription
if !newSub.Disabled {
if oldSub.Disabled || shouldRestart(newSub.Payload, oldSub.Payload) {
if !oldSub.Disabled {
oldSub.unsubscribe()
}
sub = Subscription{
Subscriber: newSub.Subscriber,
Payload: newSub.Payload,
unsubscribe: newSub.Subscriber(dispatch, newSub.Payload),
}
} else {
sub = oldSub
}
} else {
if !oldSub.Disabled {
oldSub.unsubscribe()
}
sub = Subscription{
Disabled: true,
}
}
subs = append(subs, sub)
}
return subs
}
func mergeAndSortHPropsKeys(propsSlice ...HProps) []string {
seen := map[string]struct{}{}
var keys []string
for _, props := range propsSlice {
for key := range props {
if _, ok := seen[key]; !ok {
seen[key] = struct{}{}
keys = append(keys, key)
}
}
}
sort.Strings(keys)
return keys
}
func stylePairKeys(x, y any) []string {
seen := map[string]struct{}{}
var keys []string
if a, ok := x.(map[string]string); ok {
for key := range a {
seen[key] = struct{}{}
keys = append(keys, key)
}
}
if b, ok := y.(map[string]string); ok {
for key := range b {
if _, ok := seen[key]; !ok {
seen[key] = struct{}{}
keys = append(keys, key)
}
}
}
return keys
}
func createClass(obj any) string {
var parts []string
switch v := obj.(type) {
case string:
return v
case []string:
parts = v
case map[string]bool:
for k, ok := range v {
if ok {
parts = append(parts, k)
}
}
sort.Strings(parts)
default:
return fmt.Sprint(obj)
}
return strings.Join(parts, " ")
}
func isFalsy(v any) bool {
return v == false || v == 0 || v == 0.0 || v == "" || v == nil
}
// propertyInObject returns true if the specified property is in the specified object or its prototype chain.
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
func propertyInObject(property string, object js.Value) bool {
getPrototypeOf := js.Global().Get("Object").Get("getPrototypeOf")
for v := object; !v.IsNull(); v = getPrototypeOf.Invoke(v) {
if v.Call("hasOwnProperty", property).Bool() {
return true
}
}
return false
}
func patchProperty(node window.Element, key string, oldValue, newValue any, listener eventListenerGenerator, isSvg bool) {
if key == "key" {
// no-op
} else if key == "style" {
newStyle, isNewStyle := newValue.(map[string]string)
for _, k := range stylePairKeys(oldValue, newValue) {
oldValue := ""
if isNewStyle && newStyle != nil {
oldValue = newStyle[k]
}
if k[0] == '-' {
node.SetStyleProperty(k, oldValue)
} else {
node.SetStyle(k, oldValue)
}
}
} else if len(key) >= 2 && key[0] == 'o' && key[1] == 'n' {
key := key[2:]
if isFalsy(newValue) {
getEvents(node).del(key)
if id := getEventListeners(node).get(key); !id.IsUndefined() {
node.RemoveEventListener(key, id)
}
} else {
getEvents(node).set(key, newValue.(Dispatchable))
if isFalsy(oldValue) {
id := node.AddEventListener(key, listener(node))
getEventListeners(node).set(key, id)
}
}
} else if !isSvg && key != "list" && key != "form" && propertyInObject(key, node.Value) {
if isFalsy(newValue) {
node.Set(key, "")
} else {
node.Set(key, newValue)
}
} else {
if isFalsy(newValue) {
node.RemoveAttribute(key)
} else {
node.SetAttribute(key, newValue)
}
}
}
func createNode(vdom *VNode, listener eventListenerGenerator, isSvg bool) window.Element {
props := vdom.props
var node window.Element
if vdom.kind == TextNode {
node = window.Document().CreateTextNode(vdom.tag)
} else {
isSvg = isSvg || vdom.tag == "svg"
var options *window.ElementCreationOptions
if props.Has("is") {
options = &window.ElementCreationOptions{
Is: fmt.Sprint(props.get("is").V),
}
}
if isSvg {
node = window.Document().CreateElementNS(svgNS, vdom.tag, options)
} else {
node = window.Document().CreateElement(vdom.tag, options)
}
}
keys := mergeAndSortHPropsKeys(props)
for _, k := range keys {
patchProperty(node, k, nil, props[k], listener, isSvg)
}
for i := 0; i < len(vdom.children); i++ {
vdom.children[i] = maybeVNode(vdom.children[i], nil)
node.AppendChild(
createNode(
vdom.children[i],
listener,
isSvg,
),
)
}
vdom.node = node
return node
}
func equalProps(a, b option[any]) bool {
if a.OK != b.OK {
return false
}
switch v := a.V.(type) {
case int:
bInt, ok := b.V.(int)
return ok && v == bInt
case string:
bString, ok := b.V.(string)
return ok && v == bString
case bool:
bBool, ok := b.V.(bool)
return ok && v == bBool
default:
return false
}
}
func elementGet(e window.Element, name string) option[any] {
if !propertyInObject(name, e.Value) {
return option[any]{}
}
v := e.Value.Get(name)
kind := v.Type()
switch kind {
case js.TypeUndefined, js.TypeNull:
return option[any]{OK: true}
case js.TypeBoolean:
return option[any]{V: v.Bool(), OK: true}
case js.TypeNumber:
if js.Global().Get("Number").Call("isInteger", v).Bool() {
return option[any]{V: v.Int(), OK: true}
} else {
return option[any]{V: v.Float(), OK: true}
}
case js.TypeString:
return option[any]{V: v.String(), OK: true}
default:
panic(fmt.Errorf("hypp: cannot get property of window.Element with type '%s'", kind))
}
}
func patch(
parent window.Element,
node window.Element,
oldVNode *VNode,
newVNode *VNode,
listener eventListenerGenerator,
isSvg bool,
) window.Element {
if oldVNode == newVNode {
// Do nothing
} else if oldVNode != nil && oldVNode.kind == TextNode && newVNode.kind == TextNode {
if oldVNode.tag != newVNode.tag {
node.SetNodeValue(newVNode.tag)
}
} else if oldVNode == nil || oldVNode.tag != newVNode.tag {
newVNode = maybeVNode(newVNode, nil)
node = parent.InsertBefore(
createNode(newVNode, listener, isSvg),
node,
)
if oldVNode != nil {
removeChild(parent, oldVNode.node)
}
} else {
var tmpVKid *VNode
var oldVKid *VNode
var oldKey option[string]
var newKey option[string]
oldProps := oldVNode.props
newProps := newVNode.props
oldVKids := oldVNode.children
newVKids := newVNode.children
oldHead := 0
newHead := 0
oldTail := len(oldVKids) - 1
newTail := len(newVKids) - 1
isSvg := isSvg || newVNode.tag == "svg"
allKeys := mergeAndSortHPropsKeys(oldProps, newProps)
for _, i := range allKeys {
var cmpVal option[any]
if i == "value" || i == "selected" || i == "checked" {
cmpVal = elementGet(node, i)
} else {
cmpVal = oldProps.get(i)
}
if !equalProps(cmpVal, newProps.get(i)) {
patchProperty(node, i, oldProps.get(i).V, newProps.get(i).V, listener, isSvg)
}
}
for newHead <= newTail && oldHead <= oldTail {
oldKey = oldVKids.getKey(oldHead)
if !oldKey.OK || oldKey != newVKids.getKey(newHead) {
break
}
newVKids[newHead] = maybeVNode(
newVKids[newHead],
oldVKids[oldHead],
)
patch(
node,
oldVKids[oldHead].node,
oldVKids[oldHead],
newVKids[newHead],
listener,
isSvg,
)
newHead++
oldHead++
}
for newHead <= newTail && oldHead <= oldTail {
oldKey = oldVKids.getKey(oldTail)
if !oldKey.OK || oldKey != newVKids.getKey(newTail) {
break
}
newVKids[newTail] = maybeVNode(
newVKids[newTail],
oldVKids[oldTail],
)
patch(
node,
oldVKids[oldTail].node,
oldVKids[oldTail],
newVKids[newTail],
listener,
isSvg,
)
newTail--
oldTail--
}
if oldHead > oldTail {
for newHead <= newTail {
newVKids[newHead] = maybeVNode(newVKids[newHead], nil)
oldVKid = nil
var oldVKidNode window.Element
if oldHead < len(oldVKids) {
oldVKid = oldVKids[oldHead]
oldVKidNode = oldVKid.node
}
node.InsertBefore(
createNode(
newVKids[newHead],
listener,
isSvg,
),
oldVKidNode,
)
newHead++
}
} else if newHead > newTail {
for oldHead <= oldTail {
removeChild(node, oldVKids[oldHead].node)
oldHead++
}
} else {
keyed := map[string]*VNode{}
newKeyed := set[string]{}
for i := oldHead; i <= oldTail; i++ {
oldKey = oldVKids[i].key()
if oldKey.OK {
keyed[oldKey.V] = oldVKids[i]
}
}
for newHead <= newTail {
oldVKid = oldVKids.get(oldHead)
oldKey = oldVKids.getKey(oldHead)
newVKids[newHead] = maybeVNode(newVKids[newHead], oldVKid)
newKey = newVKids.getKey(newHead)
if (newKeyed.Has(oldKey.V)) || (newKey.OK && newKey == oldVKids.getKey(oldHead+1)) {
if !oldKey.OK {
removeChild(node, oldVKid.node)
}
oldHead++
continue
}
if !newKey.OK || oldVNode.kind == ElementNode {
if !oldKey.OK {
var oldVKidNode window.Element
if oldVKid != nil {
oldVKidNode = oldVKid.node
}
patch(
node,
oldVKidNode,
oldVKid,
newVKids[newHead],
listener,
isSvg,
)
newHead++
}
oldHead++
} else {
if oldKey == newKey {
patch(
node,
oldVKid.node,
oldVKid,
newVKids[newHead],
listener,
isSvg,
)
newKeyed.Add(newKey.V)
oldHead++
} else {
tmpVKid = keyed[newKey.V]
if tmpVKid != nil {
patch(
node,
node.InsertBefore(tmpVKid.node, oldVKid.node),
tmpVKid,
newVKids[newHead],
listener,
isSvg,
)
newKeyed.Add(newKey.V)
} else {
patch(
node,
oldVKid.node,
nil,
newVKids[newHead],
listener,
isSvg,
)
}
}
newHead++
}
}
for oldHead <= oldTail {
oldVKid = oldVKids.get(oldHead)
if !oldVKids.getKey(oldHead).OK {
removeChild(node, oldVKid.node)
}
oldHead++
}
for i := range keyed {
if !newKeyed.Has(i) {
removeChild(node, keyed[i].node)
}
}
}
}
newVNode.node = node
return node
}
func propsChanged(a, b MemoData) bool {
return a.Hash() != b.Hash()
}
func maybeVNode(newVNode, oldVNode *VNode) *VNode {
if newVNode != nil {
if newVNode.memoView != nil {
if oldVNode == nil || oldVNode.memoData == nil || propsChanged(oldVNode.memoData, newVNode.memoData) {
oldVNode = newVNode.memoView(newVNode.memoData)
oldVNode.memoData = newVNode.memoData
}
return oldVNode
} else {
return newVNode
}
} else {
return text("", window.Element{})
}
}
func update[S State](appProps *AppProps[S], newState S) {
if !appProps.hasRequestedRender || appProps.state != newState {
appProps.state = newState
if appProps.Subscriptions != nil {
appProps.subs = patchSubs(
appProps.subs,
appProps.Subscriptions(appProps.state),
appProps.dispatch,
)
}
if !appProps.busy {
appProps.busy = true
window.RequestAnimationFrame(appProps.render)
appProps.hasRequestedRender = true
}
}
}
type eventListenerGenerator func(this window.Element) window.EventListener
func app[S State](appProps AppProps[S]) Dispatch {
appProps.init()
appProps.vdom = recycleNode(appProps.Node)
listener := func(this window.Element) window.EventListener {
return func(event window.Event) {
appProps.dispatch(getEvents(this).get(event.Type()), event)
}
}
appProps.render = func() {
vdomOld := appProps.vdom
appProps.vdom = appProps.View(appProps.state)
appProps.busy = false
appProps.Node = patch(
appProps.Node.ParentNode(),
appProps.Node,
vdomOld,
appProps.vdom,
listener,
appProps.busy,
)
}
appProps.dispatch = func(dispatchable Dispatchable, props Payload) {
switch v := dispatchable.(type) {
case StateAndEffects[S]:
update(&appProps, v.State)
for _, effect := range v.Effects {
effect.Effecter(appProps.dispatch, effect.Payload)
}
case Action[S]:
appProps.dispatch(v(appProps.state, props), nil)
case func(S, Payload) Dispatchable:
appProps.dispatch(v(appProps.state, props), nil)
case ActionAndPayload[S]:
appProps.dispatch(v.Action, v.Payload)
case S: // State
update(&appProps, v)
default:
panic(fmt.Errorf("hypp: dispatchable has unexpected type '%[1]T'. Expected type 'StateAndEffects[%[2]T]', 'Action[%[2]T]', 'func(%[2]T, Payload) Dispatchable', 'ActionAndPayload[%[2]T]' or '%[2]T'", dispatchable, appProps.state))
}
}
appProps.dispatch = appProps.DispatchWrapper(appProps.dispatch)
appProps.dispatch(appProps.Init, nil)
return appProps.dispatch
}